summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/BindingAdapters.java
blob: e3df7bb4a47be1a7352a45c0c9516fa856531634 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.wireguard.android;

import android.databinding.BindingAdapter;
import android.databinding.ObservableArrayMap;
import android.databinding.ObservableList;
import android.graphics.Typeface;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Static methods for use by generated code in the Android data binding library.
 */

@SuppressWarnings("unused")
public final class BindingAdapters {
    @BindingAdapter({"items", "layout"})
    public static <T> void listBinding(final ListView view,
                                       final ObservableList<T> oldList, final int oldLayoutId,
                                       final ObservableList<T> newList, final int newLayoutId) {
        // Remove any existing binding when there is no new list.
        if (newList == null) {
            view.setAdapter(null);
            return;
        }
        // The ListAdapter interface is not generic, so this cannot be checked.
        @SuppressWarnings("unchecked")
        ObservableListAdapter<T> adapter = (ObservableListAdapter<T>) view.getAdapter();
        // If the layout changes, any existing adapter must be replaced.
        if (newLayoutId != oldLayoutId)
            adapter = null;
        // Add a new binding if there was none, or if it must be replaced due to a layout change.
        if (adapter == null) {
            adapter = new ObservableListAdapter<>(view.getContext(), newLayoutId, newList);
            view.setAdapter(adapter);
        } else if (newList != oldList) {
            // Changing the list only requires modifying the existing adapter.
            adapter.setList(newList);
        }
    }

    @BindingAdapter({"items", "layout"})
    public static <K extends Comparable<K>, V> void sortedMapBinding(final ListView view,
                                         final ObservableSortedMap<K, V> oldMap,
                                         final int oldLayoutId,
                                         final ObservableSortedMap<K, V> newMap,
                                         final int newLayoutId) {
        // Remove any existing binding when there is no new map.
        if (newMap == null) {
            view.setAdapter(null);
            return;
        }
        // The ListAdapter interface is not generic, so this cannot be checked.
        @SuppressWarnings("unchecked")
        ObservableMapAdapter<K, V> adapter = (ObservableMapAdapter<K, V>) view.getAdapter();
        // If the layout changes, any existing adapter must be replaced.
        if (newLayoutId != oldLayoutId)
            adapter = null;
        // Add a new binding if there was none, or if it must be replaced due to a layout change.
        if (adapter == null) {
            adapter = new ObservableMapAdapter<>(view.getContext(), newLayoutId, newMap);
            view.setAdapter(adapter);
        } else if (newMap != oldMap) {
            // Changing the list only requires modifying the existing adapter.
            adapter.setMap(newMap);
        }
    }

    @BindingAdapter({"android:textStyle"})
    public static void textStyleBinding(final TextView view, final Typeface typeface) {
        view.setTypeface(typeface);
    }

    private BindingAdapters() {
        // Prevent instantiation.
    }
}