diff options
author | Samuel Holland <samuel@sholland.org> | 2017-08-17 02:43:15 -0500 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2017-08-17 02:43:15 -0500 |
commit | 25412e05377dabf5aa02bf5adb894fff3d9b7cb1 (patch) | |
tree | a9f2cd663f5abf308d85af40fa24dfd7b845dd11 /app | |
parent | 97149fff3f867e25103c632138468010cc804811 (diff) |
Convert to using the ObservableTreeMap and its adapter
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app')
4 files changed, 34 insertions, 4 deletions
diff --git a/app/src/main/java/com/wireguard/android/BindingAdapters.java b/app/src/main/java/com/wireguard/android/BindingAdapters.java index 756b38a3..7e93c228 100644 --- a/app/src/main/java/com/wireguard/android/BindingAdapters.java +++ b/app/src/main/java/com/wireguard/android/BindingAdapters.java @@ -67,6 +67,33 @@ public final class BindingAdapters { } } + @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); diff --git a/app/src/main/java/com/wireguard/android/ConfigListFragment.java b/app/src/main/java/com/wireguard/android/ConfigListFragment.java index c5c9dbf7..ed21ac00 100644 --- a/app/src/main/java/com/wireguard/android/ConfigListFragment.java +++ b/app/src/main/java/com/wireguard/android/ConfigListFragment.java @@ -78,7 +78,10 @@ public class ConfigListFragment extends BaseConfigFragment { private void setConfigChecked(final Config config) { if (config != null) { - final int position = VpnService.getInstance().getConfigs().indexOfKey(config.getName()); + @SuppressWarnings("unchecked") + final ObservableMapAdapter<String, Config> adapter = + (ObservableMapAdapter<String, Config>) listView.getAdapter(); + final int position = adapter.getItemPosition(config.getName()); if (position >= 0) listView.setItemChecked(position, true); } else { diff --git a/app/src/main/java/com/wireguard/android/VpnService.java b/app/src/main/java/com/wireguard/android/VpnService.java index fd2aadc3..35d54255 100644 --- a/app/src/main/java/com/wireguard/android/VpnService.java +++ b/app/src/main/java/com/wireguard/android/VpnService.java @@ -41,7 +41,7 @@ public class VpnService extends Service } private final IBinder binder = new Binder(); - private final ObservableArrayMap<String, Config> configurations = new ObservableArrayMap<>(); + private final ObservableTreeMap<String, Config> configurations = new ObservableTreeMap<>(); private final Set<String> enabledConfigs = new HashSet<>(); private SharedPreferences preferences; private Config primaryConfig; @@ -103,7 +103,7 @@ public class VpnService extends Service * * @return The set of known configurations. */ - public ObservableArrayMap<String, Config> getConfigs() { + public ObservableSortedMap<String, Config> getConfigs() { return configurations; } diff --git a/app/src/main/res/layout/config_list_fragment.xml b/app/src/main/res/layout/config_list_fragment.xml index 155e708a..e2a80419 100644 --- a/app/src/main/res/layout/config_list_fragment.xml +++ b/app/src/main/res/layout/config_list_fragment.xml @@ -7,7 +7,7 @@ <!--suppress AndroidDomInspection --> <variable name="configs" - type="android.databinding.ObservableArrayMap<String, com.wireguard.config.Config>" /> + type="com.wireguard.android.ObservableSortedMap<String, com.wireguard.config.Config>" /> </data> <ListView |