From 70156381a7cd29f75fed664101af30e44deb7bb9 Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Wed, 8 Nov 2017 00:07:29 -0600 Subject: [PATCH] Bindings: Merge bugfixes and updates Signed-off-by: Jason A. Donenfeld --- .../wireguard/android/ConfigListFragment.java | 2 +- .../android/bindings/BindingAdapters.java | 57 ++++++++++--------- .../android/bindings/ItemChangeListener.java | 2 +- .../bindings/ObservableListAdapter.java | 15 ++++- .../bindings/ObservableMapAdapter.java | 19 ++++--- app/src/main/res/layout/config_list_item.xml | 6 +- 6 files changed, 62 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/com/wireguard/android/ConfigListFragment.java b/app/src/main/java/com/wireguard/android/ConfigListFragment.java index c89d2339..88d12745 100644 --- a/app/src/main/java/com/wireguard/android/ConfigListFragment.java +++ b/app/src/main/java/com/wireguard/android/ConfigListFragment.java @@ -89,7 +89,7 @@ public class ConfigListFragment extends BaseConfigFragment { @SuppressWarnings("unchecked") final ObservableMapAdapter adapter = (ObservableMapAdapter) listView.getAdapter(); - final int position = adapter.getItemPosition(config.getName()); + final int position = adapter.getPosition(config.getName()); if (position >= 0) listView.setItemChecked(position, true); } else { diff --git a/app/src/main/java/com/wireguard/android/bindings/BindingAdapters.java b/app/src/main/java/com/wireguard/android/bindings/BindingAdapters.java index 56540921..1542692a 100644 --- a/app/src/main/java/com/wireguard/android/bindings/BindingAdapters.java +++ b/app/src/main/java/com/wireguard/android/bindings/BindingAdapters.java @@ -28,6 +28,8 @@ public final class BindingAdapters { if (listener != null && oldList != null && oldLayoutId != newLayoutId) { listener.setList(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. if (newList == null || newLayoutId == 0) @@ -44,49 +46,52 @@ public final class BindingAdapters { public static void setItems(final ListView view, final ObservableList oldList, final int oldLayoutId, final ObservableList newList, final int newLayoutId) { - // Remove any existing binding when there is no new list or layout. - if (newList == null || newLayoutId == 0) { - view.setAdapter(null); + if (oldList == newList && oldLayoutId == newLayoutId) return; - } // The ListAdapter interface is not generic, so this cannot be checked. @SuppressWarnings("unchecked") ObservableListAdapter adapter = (ObservableListAdapter) view.getAdapter(); // 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; - // 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"}) - public static , V> void setItems( - final ListView view, - final ObservableSortedMap oldMap, final int oldLayoutId, - final ObservableSortedMap newMap, final int newLayoutId) { - // Remove any existing binding when there is no new map or layout. - if (newMap == null || newLayoutId == 0) { - view.setAdapter(null); + public static , V> void setItems(final ListView view, + final ObservableSortedMap oldMap, + final int oldLayoutId, + final ObservableSortedMap newMap, + final int newLayoutId) { + if (oldMap == newMap && oldLayoutId == newLayoutId) return; - } // The ListAdapter interface is not generic, so this cannot be checked. @SuppressWarnings("unchecked") ObservableMapAdapter adapter = (ObservableMapAdapter) view.getAdapter(); // 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; - // 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"}) diff --git a/app/src/main/java/com/wireguard/android/bindings/ItemChangeListener.java b/app/src/main/java/com/wireguard/android/bindings/ItemChangeListener.java index 39b3b654..1ee37a86 100644 --- a/app/src/main/java/com/wireguard/android/bindings/ItemChangeListener.java +++ b/app/src/main/java/com/wireguard/android/bindings/ItemChangeListener.java @@ -37,7 +37,7 @@ class ItemChangeListener { return binding.getRoot(); } - public void setList(final ObservableList newList) { + void setList(final ObservableList newList) { if (list != null) list.removeOnListChangedCallback(callback); list = newList; diff --git a/app/src/main/java/com/wireguard/android/bindings/ObservableListAdapter.java b/app/src/main/java/com/wireguard/android/bindings/ObservableListAdapter.java index 5b54ecaf..7938a2a6 100644 --- a/app/src/main/java/com/wireguard/android/bindings/ObservableListAdapter.java +++ b/app/src/main/java/com/wireguard/android/bindings/ObservableListAdapter.java @@ -37,12 +37,16 @@ class ObservableListAdapter extends BaseAdapter implements ListAdapter { @Override 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 public long getItemId(final int position) { - return position; + if (list == null || position < 0 || position >= list.size()) + return -1; + return list.get(position).hashCode(); } @Override @@ -55,7 +59,12 @@ class ObservableListAdapter extends BaseAdapter implements ListAdapter { return binding.getRoot(); } - public void setList(final ObservableList newList) { + @Override + public boolean hasStableIds() { + return true; + } + + void setList(final ObservableList newList) { if (list != null) list.removeOnListChangedCallback(callback); list = newList; diff --git a/app/src/main/java/com/wireguard/android/bindings/ObservableMapAdapter.java b/app/src/main/java/com/wireguard/android/bindings/ObservableMapAdapter.java index da0c36a7..8e474e47 100644 --- a/app/src/main/java/com/wireguard/android/bindings/ObservableMapAdapter.java +++ b/app/src/main/java/com/wireguard/android/bindings/ObservableMapAdapter.java @@ -44,20 +44,18 @@ public class ObservableMapAdapter, V> extends BaseAdapte public V getItem(final int position) { if (map == null || position < 0 || position >= map.size()) return null; - return map.get(getKeys().get(position)); + return map.get(getKey(position)); } @Override public long getItemId(final int position) { if (map == null || position < 0 || position >= map.size()) return -1; - return map.get(getKeys().get(position)).hashCode(); + return getItem(position).hashCode(); } - public int getItemPosition(final K key) { - if (map == null) - return -1; - return Collections.binarySearch(getKeys(), key); + private K getKey(final int position) { + return getKeys().get(position); } private ArrayList getKeys() { @@ -66,11 +64,18 @@ public class ObservableMapAdapter, V> extends BaseAdapte return keys; } + public int getPosition(final K key) { + if (map == null || key == null) + return -1; + return Collections.binarySearch(getKeys(), key); + } + @Override public View getView(final int position, final View convertView, final ViewGroup parent) { ViewDataBinding binding = DataBindingUtil.getBinding(convertView); if (binding == null) binding = DataBindingUtil.inflate(layoutInflater, layoutId, parent, false); + binding.setVariable(BR.key, getKey(position)); binding.setVariable(BR.item, getItem(position)); binding.executePendingBindings(); return binding.getRoot(); @@ -81,7 +86,7 @@ public class ObservableMapAdapter, V> extends BaseAdapte return true; } - public void setMap(final ObservableSortedMap newMap) { + void setMap(final ObservableSortedMap newMap) { if (map != null) map.removeOnMapChangedCallback(callback); keys = null; diff --git a/app/src/main/res/layout/config_list_item.xml b/app/src/main/res/layout/config_list_item.xml index d15d48d6..cdc0723a 100644 --- a/app/src/main/res/layout/config_list_item.xml +++ b/app/src/main/res/layout/config_list_item.xml @@ -7,6 +7,10 @@ + + @@ -27,7 +31,7 @@ android:layout_toStartOf="@+id/config_switch" android:ellipsize="end" android:maxLines="1" - android:text="@{item.name}" + android:text="@{key}" android:textStyle="@{item.primary ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT}" />