Bindings: Merge bugfixes and updates
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
6231bb18ac
commit
70156381a7
@ -89,7 +89,7 @@ public class ConfigListFragment extends BaseConfigFragment {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
final ObservableMapAdapter<String, Config> adapter =
|
final ObservableMapAdapter<String, Config> adapter =
|
||||||
(ObservableMapAdapter<String, Config>) listView.getAdapter();
|
(ObservableMapAdapter<String, Config>) listView.getAdapter();
|
||||||
final int position = adapter.getItemPosition(config.getName());
|
final int position = adapter.getPosition(config.getName());
|
||||||
if (position >= 0)
|
if (position >= 0)
|
||||||
listView.setItemChecked(position, true);
|
listView.setItemChecked(position, true);
|
||||||
} else {
|
} else {
|
||||||
|
@ -28,6 +28,8 @@ public final class BindingAdapters {
|
|||||||
if (listener != null && oldList != null && oldLayoutId != newLayoutId) {
|
if (listener != null && oldList != null && oldLayoutId != newLayoutId) {
|
||||||
listener.setList(null);
|
listener.setList(null);
|
||||||
listener = null;
|
listener = null;
|
||||||
|
// Stop tracking the old listener.
|
||||||
|
ListenerUtil.trackListener(view, null, R.id.item_change_listener);
|
||||||
}
|
}
|
||||||
// Avoid adding a listener when there is no new list or layout.
|
// Avoid adding a listener when there is no new list or layout.
|
||||||
if (newList == null || newLayoutId == 0)
|
if (newList == null || newLayoutId == 0)
|
||||||
@ -44,49 +46,52 @@ public final class BindingAdapters {
|
|||||||
public static <T> void setItems(final ListView view,
|
public static <T> void setItems(final ListView view,
|
||||||
final ObservableList<T> oldList, final int oldLayoutId,
|
final ObservableList<T> oldList, final int oldLayoutId,
|
||||||
final ObservableList<T> newList, final int newLayoutId) {
|
final ObservableList<T> newList, final int newLayoutId) {
|
||||||
// Remove any existing binding when there is no new list or layout.
|
if (oldList == newList && oldLayoutId == newLayoutId)
|
||||||
if (newList == null || newLayoutId == 0) {
|
|
||||||
view.setAdapter(null);
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
// The ListAdapter interface is not generic, so this cannot be checked.
|
// The ListAdapter interface is not generic, so this cannot be checked.
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
ObservableListAdapter<T> adapter = (ObservableListAdapter<T>) view.getAdapter();
|
ObservableListAdapter<T> adapter = (ObservableListAdapter<T>) view.getAdapter();
|
||||||
// If the layout changes, any existing adapter must be replaced.
|
// If the layout changes, any existing adapter must be replaced.
|
||||||
if (newLayoutId != oldLayoutId)
|
if (adapter != null && oldList != null && oldLayoutId != newLayoutId) {
|
||||||
|
adapter.setList(null);
|
||||||
adapter = null;
|
adapter = null;
|
||||||
// Add a new binding if there was none, or if it must be replaced due to a layout change.
|
|
||||||
if (adapter == null) {
|
|
||||||
view.setAdapter(new ObservableListAdapter<>(view.getContext(), newLayoutId, newList));
|
|
||||||
} else if (newList != oldList) {
|
|
||||||
// Changing the list only requires modifying the existing adapter.
|
|
||||||
adapter.setList(newList);
|
|
||||||
}
|
}
|
||||||
|
// Avoid setting an adapter when there is no new list or layout.
|
||||||
|
if (newList == null || newLayoutId == 0)
|
||||||
|
return;
|
||||||
|
if (adapter == null) {
|
||||||
|
adapter = new ObservableListAdapter<>(view.getContext(), newLayoutId, newList);
|
||||||
|
view.setAdapter(adapter);
|
||||||
|
}
|
||||||
|
// Either the list changed, or this is an entirely new listener because the layout changed.
|
||||||
|
adapter.setList(newList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@BindingAdapter({"items", "layout"})
|
@BindingAdapter({"items", "layout"})
|
||||||
public static <K extends Comparable<K>, V> void setItems(
|
public static <K extends Comparable<K>, V> void setItems(final ListView view,
|
||||||
final ListView view,
|
final ObservableSortedMap<K, V> oldMap,
|
||||||
final ObservableSortedMap<K, V> oldMap, final int oldLayoutId,
|
final int oldLayoutId,
|
||||||
final ObservableSortedMap<K, V> newMap, final int newLayoutId) {
|
final ObservableSortedMap<K, V> newMap,
|
||||||
// Remove any existing binding when there is no new map or layout.
|
final int newLayoutId) {
|
||||||
if (newMap == null || newLayoutId == 0) {
|
if (oldMap == newMap && oldLayoutId == newLayoutId)
|
||||||
view.setAdapter(null);
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
// The ListAdapter interface is not generic, so this cannot be checked.
|
// The ListAdapter interface is not generic, so this cannot be checked.
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
ObservableMapAdapter<K, V> adapter = (ObservableMapAdapter<K, V>) view.getAdapter();
|
ObservableMapAdapter<K, V> adapter = (ObservableMapAdapter<K, V>) view.getAdapter();
|
||||||
// If the layout changes, any existing adapter must be replaced.
|
// If the layout changes, any existing adapter must be replaced.
|
||||||
if (newLayoutId != oldLayoutId)
|
if (adapter != null && oldMap != null && oldLayoutId != newLayoutId) {
|
||||||
|
adapter.setMap(null);
|
||||||
adapter = null;
|
adapter = null;
|
||||||
// Add a new binding if there was none, or if it must be replaced due to a layout change.
|
|
||||||
if (adapter == null) {
|
|
||||||
view.setAdapter(new ObservableMapAdapter<>(view.getContext(), newLayoutId, newMap));
|
|
||||||
} else if (newMap != oldMap) {
|
|
||||||
// Changing the list only requires modifying the existing adapter.
|
|
||||||
adapter.setMap(newMap);
|
|
||||||
}
|
}
|
||||||
|
// Avoid setting an adapter when there is no new list or layout.
|
||||||
|
if (newMap == null || newLayoutId == 0)
|
||||||
|
return;
|
||||||
|
if (adapter == null) {
|
||||||
|
adapter = new ObservableMapAdapter<>(view.getContext(), newLayoutId, newMap);
|
||||||
|
view.setAdapter(adapter);
|
||||||
|
}
|
||||||
|
// Either the list changed, or this is an entirely new listener because the layout changed.
|
||||||
|
adapter.setMap(newMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
@BindingAdapter({"filter"})
|
@BindingAdapter({"filter"})
|
||||||
|
@ -37,7 +37,7 @@ class ItemChangeListener<T> {
|
|||||||
return binding.getRoot();
|
return binding.getRoot();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setList(final ObservableList<T> newList) {
|
void setList(final ObservableList<T> newList) {
|
||||||
if (list != null)
|
if (list != null)
|
||||||
list.removeOnListChangedCallback(callback);
|
list.removeOnListChangedCallback(callback);
|
||||||
list = newList;
|
list = newList;
|
||||||
|
@ -37,12 +37,16 @@ class ObservableListAdapter<T> extends BaseAdapter implements ListAdapter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T getItem(final int position) {
|
public T getItem(final int position) {
|
||||||
return list != null ? list.get(position) : null;
|
if (list == null || position < 0 || position >= list.size())
|
||||||
|
return null;
|
||||||
|
return list.get(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getItemId(final int position) {
|
public long getItemId(final int position) {
|
||||||
return position;
|
if (list == null || position < 0 || position >= list.size())
|
||||||
|
return -1;
|
||||||
|
return list.get(position).hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -55,7 +59,12 @@ class ObservableListAdapter<T> extends BaseAdapter implements ListAdapter {
|
|||||||
return binding.getRoot();
|
return binding.getRoot();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setList(final ObservableList<T> newList) {
|
@Override
|
||||||
|
public boolean hasStableIds() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setList(final ObservableList<T> newList) {
|
||||||
if (list != null)
|
if (list != null)
|
||||||
list.removeOnListChangedCallback(callback);
|
list.removeOnListChangedCallback(callback);
|
||||||
list = newList;
|
list = newList;
|
||||||
|
@ -44,20 +44,18 @@ public class ObservableMapAdapter<K extends Comparable<K>, V> extends BaseAdapte
|
|||||||
public V getItem(final int position) {
|
public V getItem(final int position) {
|
||||||
if (map == null || position < 0 || position >= map.size())
|
if (map == null || position < 0 || position >= map.size())
|
||||||
return null;
|
return null;
|
||||||
return map.get(getKeys().get(position));
|
return map.get(getKey(position));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getItemId(final int position) {
|
public long getItemId(final int position) {
|
||||||
if (map == null || position < 0 || position >= map.size())
|
if (map == null || position < 0 || position >= map.size())
|
||||||
return -1;
|
return -1;
|
||||||
return map.get(getKeys().get(position)).hashCode();
|
return getItem(position).hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getItemPosition(final K key) {
|
private K getKey(final int position) {
|
||||||
if (map == null)
|
return getKeys().get(position);
|
||||||
return -1;
|
|
||||||
return Collections.binarySearch(getKeys(), key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArrayList<K> getKeys() {
|
private ArrayList<K> getKeys() {
|
||||||
@ -66,11 +64,18 @@ public class ObservableMapAdapter<K extends Comparable<K>, V> extends BaseAdapte
|
|||||||
return keys;
|
return keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getPosition(final K key) {
|
||||||
|
if (map == null || key == null)
|
||||||
|
return -1;
|
||||||
|
return Collections.binarySearch(getKeys(), key);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View getView(final int position, final View convertView, final ViewGroup parent) {
|
public View getView(final int position, final View convertView, final ViewGroup parent) {
|
||||||
ViewDataBinding binding = DataBindingUtil.getBinding(convertView);
|
ViewDataBinding binding = DataBindingUtil.getBinding(convertView);
|
||||||
if (binding == null)
|
if (binding == null)
|
||||||
binding = DataBindingUtil.inflate(layoutInflater, layoutId, parent, false);
|
binding = DataBindingUtil.inflate(layoutInflater, layoutId, parent, false);
|
||||||
|
binding.setVariable(BR.key, getKey(position));
|
||||||
binding.setVariable(BR.item, getItem(position));
|
binding.setVariable(BR.item, getItem(position));
|
||||||
binding.executePendingBindings();
|
binding.executePendingBindings();
|
||||||
return binding.getRoot();
|
return binding.getRoot();
|
||||||
@ -81,7 +86,7 @@ public class ObservableMapAdapter<K extends Comparable<K>, V> extends BaseAdapte
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMap(final ObservableSortedMap<K, V> newMap) {
|
void setMap(final ObservableSortedMap<K, V> newMap) {
|
||||||
if (map != null)
|
if (map != null)
|
||||||
map.removeOnMapChangedCallback(callback);
|
map.removeOnMapChangedCallback(callback);
|
||||||
keys = null;
|
keys = null;
|
||||||
|
@ -7,6 +7,10 @@
|
|||||||
|
|
||||||
<import type="com.wireguard.android.VpnService" />
|
<import type="com.wireguard.android.VpnService" />
|
||||||
|
|
||||||
|
<variable
|
||||||
|
name="key"
|
||||||
|
type="String" />
|
||||||
|
|
||||||
<variable
|
<variable
|
||||||
name="item"
|
name="item"
|
||||||
type="com.wireguard.config.Config" />
|
type="com.wireguard.config.Config" />
|
||||||
@ -27,7 +31,7 @@
|
|||||||
android:layout_toStartOf="@+id/config_switch"
|
android:layout_toStartOf="@+id/config_switch"
|
||||||
android:ellipsize="end"
|
android:ellipsize="end"
|
||||||
android:maxLines="1"
|
android:maxLines="1"
|
||||||
android:text="@{item.name}"
|
android:text="@{key}"
|
||||||
android:textStyle="@{item.primary ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT}" />
|
android:textStyle="@{item.primary ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT}" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
|
Loading…
Reference in New Issue
Block a user