diff options
6 files changed, 60 insertions, 37 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<String, Config> adapter = (ObservableMapAdapter<String, Config>) 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 <T> void setItems(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 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<T> adapter = (ObservableListAdapter<T>) 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. + } + // Avoid setting an adapter when there is no new list or layout. + if (newList == null || newLayoutId == 0) + return; 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); + 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 <K extends Comparable<K>, V> void setItems( - 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 or layout. - if (newMap == null || newLayoutId == 0) { - view.setAdapter(null); + public static <K extends Comparable<K>, V> void setItems(final ListView view, + final ObservableSortedMap<K, V> oldMap, + final int oldLayoutId, + final ObservableSortedMap<K, V> newMap, + final int newLayoutId) { + if (oldMap == newMap && oldLayoutId == newLayoutId) 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) + 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. + } + // Avoid setting an adapter when there is no new list or layout. + if (newMap == null || newLayoutId == 0) + return; 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); + 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<T> { return binding.getRoot(); } - public void setList(final ObservableList<T> newList) { + void setList(final ObservableList<T> 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<T> 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<T> extends BaseAdapter implements ListAdapter { 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) 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<K extends Comparable<K>, 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<K> getKeys() { @@ -66,11 +64,18 @@ public class ObservableMapAdapter<K extends Comparable<K>, 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<K extends Comparable<K>, V> extends BaseAdapte return true; } - public void setMap(final ObservableSortedMap<K, V> newMap) { + void setMap(final ObservableSortedMap<K, V> 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 @@ -8,6 +8,10 @@ <import type="com.wireguard.android.VpnService" /> <variable + name="key" + type="String" /> + + <variable name="item" type="com.wireguard.config.Config" /> </data> @@ -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}" /> <TextView |