2017-07-29 13:09:55 +02:00
|
|
|
package com.wireguard.android;
|
|
|
|
|
|
|
|
import android.databinding.BindingAdapter;
|
2017-08-08 02:46:19 +02:00
|
|
|
import android.databinding.ObservableArrayMap;
|
2017-07-29 13:09:55 +02:00
|
|
|
import android.databinding.ObservableList;
|
2017-08-16 11:30:42 +02:00
|
|
|
import android.graphics.Typeface;
|
|
|
|
import android.widget.EditText;
|
2017-07-29 13:09:55 +02:00
|
|
|
import android.widget.ListView;
|
2017-08-16 11:30:42 +02:00
|
|
|
import android.widget.TextView;
|
2017-07-29 13:09:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Static methods for use by generated code in the Android data binding library.
|
|
|
|
*/
|
|
|
|
|
2017-08-13 14:24:03 +02:00
|
|
|
@SuppressWarnings("unused")
|
2017-07-29 13:09:55 +02:00
|
|
|
public final class BindingAdapters {
|
|
|
|
@BindingAdapter({"items", "layout"})
|
2017-08-13 14:24:03 +02:00
|
|
|
public static <T> void listBinding(final ListView view,
|
|
|
|
final ObservableList<T> oldList, final int oldLayoutId,
|
|
|
|
final ObservableList<T> newList, final int newLayoutId) {
|
2017-07-29 13:09:55 +02:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 09:43:15 +02:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-16 11:30:42 +02:00
|
|
|
@BindingAdapter({"android:textStyle"})
|
|
|
|
public static void textStyleBinding(final TextView view, final Typeface typeface) {
|
|
|
|
view.setTypeface(typeface);
|
|
|
|
}
|
|
|
|
|
2017-07-29 13:09:55 +02:00
|
|
|
private BindingAdapters() {
|
2017-08-13 14:24:03 +02:00
|
|
|
// Prevent instantiation.
|
2017-07-29 13:09:55 +02:00
|
|
|
}
|
|
|
|
}
|