diff options
author | Samuel Holland <samuel@sholland.org> | 2018-01-01 02:06:37 -0600 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2018-01-06 04:09:29 -0600 |
commit | 609194fae2332e6f2ccd7a4464bfa492ad661a6f (patch) | |
tree | 96a7cd9846a093dfcdacfef285b0a4d77000edf0 /app/src/main/java/com/wireguard/android/databinding | |
parent | 4c0869393e2d8f52b9bcf312286dca8ced2e1900 (diff) |
Serviceless rewrite, part 1
Signed-off-by: Samuel Holland <samuel@sholland.org>
Diffstat (limited to 'app/src/main/java/com/wireguard/android/databinding')
-rw-r--r-- | app/src/main/java/com/wireguard/android/databinding/BindingAdapters.java | 34 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/android/databinding/ItemChangeListener.java | 3 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/android/databinding/ObservableListAdapter.java | 6 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/android/databinding/ObservableMapAdapter.java | 27 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/android/databinding/ObservableNavigableMap.java (renamed from app/src/main/java/com/wireguard/android/databinding/ObservableSortedMap.java) | 4 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/android/databinding/ObservableTreeMap.java | 17 |
6 files changed, 55 insertions, 36 deletions
diff --git a/app/src/main/java/com/wireguard/android/databinding/BindingAdapters.java b/app/src/main/java/com/wireguard/android/databinding/BindingAdapters.java index 5dfc2d19..072a9fdc 100644 --- a/app/src/main/java/com/wireguard/android/databinding/BindingAdapters.java +++ b/app/src/main/java/com/wireguard/android/databinding/BindingAdapters.java @@ -12,13 +12,22 @@ import android.widget.TextView; import com.wireguard.android.R; import com.wireguard.android.widget.ToggleSwitch; +import org.threeten.bp.Instant; +import org.threeten.bp.ZoneId; +import org.threeten.bp.ZonedDateTime; +import org.threeten.bp.format.DateTimeFormatter; + /** * Static methods for use by generated code in the Android data binding library. */ -@SuppressWarnings("unused") +@SuppressWarnings({"unused", "WeakerAccess"}) public final class BindingAdapters { - @BindingAdapter({"app:checked"}) + private BindingAdapters() { + // Prevent instantiation. + } + + @BindingAdapter({"checked"}) public static void setChecked(final ToggleSwitch view, final boolean checked) { view.setCheckedInternal(checked); } @@ -80,9 +89,9 @@ public final class BindingAdapters { @BindingAdapter({"items", "layout"}) public static <K extends Comparable<K>, V> void setItems(final ListView view, - final ObservableSortedMap<K, V> oldMap, + final ObservableNavigableMap<K, V> oldMap, final int oldLayoutId, - final ObservableSortedMap<K, V> newMap, + final ObservableNavigableMap<K, V> newMap, final int newLayoutId) { if (oldMap == newMap && oldLayoutId == newLayoutId) return; @@ -105,19 +114,26 @@ public final class BindingAdapters { adapter.setMap(newMap); } - @BindingAdapter({"app:onBeforeCheckedChanged"}) + @BindingAdapter({"onBeforeCheckedChanged"}) public static void setOnBeforeCheckedChanged(final ToggleSwitch view, final ToggleSwitch.OnBeforeCheckedChangeListener listener) { view.setOnBeforeCheckedChangeListener(listener); } + @BindingAdapter({"android:text"}) + public static void setText(final TextView view, final Instant instant) { + if (instant == null || Instant.EPOCH.equals(instant)) { + view.setText(R.string.never); + } else { + final ZoneId defaultZone = ZoneId.systemDefault(); + final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, defaultZone); + view.setText(zonedDateTime.format(DateTimeFormatter.RFC_1123_DATE_TIME)); + } + } + @BindingAdapter({"android:textStyle"}) public static void setTextStyle(final TextView view, final Typeface typeface) { view.setTypeface(typeface); } - - private BindingAdapters() { - // Prevent instantiation. - } } diff --git a/app/src/main/java/com/wireguard/android/databinding/ItemChangeListener.java b/app/src/main/java/com/wireguard/android/databinding/ItemChangeListener.java index 6d8debf2..2b693c9f 100644 --- a/app/src/main/java/com/wireguard/android/databinding/ItemChangeListener.java +++ b/app/src/main/java/com/wireguard/android/databinding/ItemChangeListener.java @@ -32,6 +32,7 @@ class ItemChangeListener<T> { ViewDataBinding binding = DataBindingUtil.getBinding(convertView); if (binding == null) binding = DataBindingUtil.inflate(layoutInflater, layoutId, container, false); + binding.setVariable(BR.collection, list); binding.setVariable(BR.item, list.get(position)); binding.executePendingBindings(); return binding.getRoot(); @@ -49,7 +50,7 @@ class ItemChangeListener<T> { } } - private static class OnListChangedCallback<T> + private static final class OnListChangedCallback<T> extends ObservableList.OnListChangedCallback<ObservableList<T>> { private final WeakReference<ItemChangeListener<T>> weakListener; diff --git a/app/src/main/java/com/wireguard/android/databinding/ObservableListAdapter.java b/app/src/main/java/com/wireguard/android/databinding/ObservableListAdapter.java index 5727900d..5c4e8c0a 100644 --- a/app/src/main/java/com/wireguard/android/databinding/ObservableListAdapter.java +++ b/app/src/main/java/com/wireguard/android/databinding/ObservableListAdapter.java @@ -8,7 +8,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; -import android.widget.ListAdapter; import com.wireguard.android.BR; @@ -18,7 +17,7 @@ import java.lang.ref.WeakReference; * A generic ListAdapter backed by an ObservableList. */ -class ObservableListAdapter<T> extends BaseAdapter implements ListAdapter { +class ObservableListAdapter<T> extends BaseAdapter { private final OnListChangedCallback<T> callback = new OnListChangedCallback<>(this); private final int layoutId; private final LayoutInflater layoutInflater; @@ -54,6 +53,7 @@ class ObservableListAdapter<T> extends BaseAdapter implements ListAdapter { ViewDataBinding binding = DataBindingUtil.getBinding(convertView); if (binding == null) binding = DataBindingUtil.inflate(layoutInflater, layoutId, parent, false); + binding.setVariable(BR.collection, list); binding.setVariable(BR.item, getItem(position)); binding.executePendingBindings(); return binding.getRoot(); @@ -74,7 +74,7 @@ class ObservableListAdapter<T> extends BaseAdapter implements ListAdapter { notifyDataSetChanged(); } - private static class OnListChangedCallback<U> + private static final class OnListChangedCallback<U> extends ObservableList.OnListChangedCallback<ObservableList<U>> { private final WeakReference<ObservableListAdapter<U>> weakAdapter; diff --git a/app/src/main/java/com/wireguard/android/databinding/ObservableMapAdapter.java b/app/src/main/java/com/wireguard/android/databinding/ObservableMapAdapter.java index 70180728..4f63d6dd 100644 --- a/app/src/main/java/com/wireguard/android/databinding/ObservableMapAdapter.java +++ b/app/src/main/java/com/wireguard/android/databinding/ObservableMapAdapter.java @@ -8,28 +8,27 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; -import android.widget.ListAdapter; import com.wireguard.android.BR; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collections; +import java.util.List; /** * A generic ListAdapter backed by a TreeMap that adds observability. */ -public class ObservableMapAdapter<K extends Comparable<K>, V> extends BaseAdapter - implements ListAdapter { +public class ObservableMapAdapter<K extends Comparable<K>, V> extends BaseAdapter { private final OnMapChangedCallback<K, V> callback = new OnMapChangedCallback<>(this); - private ArrayList<K> keys; private final int layoutId; private final LayoutInflater layoutInflater; - private ObservableSortedMap<K, V> map; + private List<K> keys; + private ObservableNavigableMap<K, V> map; ObservableMapAdapter(final Context context, final int layoutId, - final ObservableSortedMap<K, V> map) { + final ObservableNavigableMap<K, V> map) { this.layoutId = layoutId; layoutInflater = LayoutInflater.from(context); setMap(map); @@ -51,14 +50,17 @@ public class ObservableMapAdapter<K extends Comparable<K>, V> extends BaseAdapte public long getItemId(final int position) { if (map == null || position < 0 || position >= map.size()) return -1; - return getItem(position).hashCode(); + //final V item = getItem(position); + //return item != null ? item.hashCode() : -1; + final K key = getKey(position); + return key.hashCode(); } private K getKey(final int position) { return getKeys().get(position); } - private ArrayList<K> getKeys() { + private List<K> getKeys() { if (keys == null) keys = new ArrayList<>(map.keySet()); return keys; @@ -75,6 +77,7 @@ public class ObservableMapAdapter<K extends Comparable<K>, V> extends BaseAdapte ViewDataBinding binding = DataBindingUtil.getBinding(convertView); if (binding == null) binding = DataBindingUtil.inflate(layoutInflater, layoutId, parent, false); + binding.setVariable(BR.collection, map); binding.setVariable(BR.key, getKey(position)); binding.setVariable(BR.item, getItem(position)); binding.executePendingBindings(); @@ -86,7 +89,7 @@ public class ObservableMapAdapter<K extends Comparable<K>, V> extends BaseAdapte return true; } - void setMap(final ObservableSortedMap<K, V> newMap) { + void setMap(final ObservableNavigableMap<K, V> newMap) { if (map != null) map.removeOnMapChangedCallback(callback); keys = null; @@ -97,8 +100,8 @@ public class ObservableMapAdapter<K extends Comparable<K>, V> extends BaseAdapte notifyDataSetChanged(); } - private static class OnMapChangedCallback<K extends Comparable<K>, V> - extends ObservableMap.OnMapChangedCallback<ObservableSortedMap<K, V>, K, V> { + private static final class OnMapChangedCallback<K extends Comparable<K>, V> + extends ObservableMap.OnMapChangedCallback<ObservableNavigableMap<K, V>, K, V> { private final WeakReference<ObservableMapAdapter<K, V>> weakAdapter; @@ -107,7 +110,7 @@ public class ObservableMapAdapter<K extends Comparable<K>, V> extends BaseAdapte } @Override - public void onMapChanged(final ObservableSortedMap<K, V> sender, final K key) { + public void onMapChanged(final ObservableNavigableMap<K, V> sender, final K key) { final ObservableMapAdapter<K, V> adapter = weakAdapter.get(); if (adapter != null) { adapter.keys = null; diff --git a/app/src/main/java/com/wireguard/android/databinding/ObservableSortedMap.java b/app/src/main/java/com/wireguard/android/databinding/ObservableNavigableMap.java index 1cbfeb6b..ab8a4a1f 100644 --- a/app/src/main/java/com/wireguard/android/databinding/ObservableSortedMap.java +++ b/app/src/main/java/com/wireguard/android/databinding/ObservableNavigableMap.java @@ -2,12 +2,12 @@ package com.wireguard.android.databinding; import android.databinding.ObservableMap; -import java.util.SortedMap; +import java.util.NavigableMap; /** * Interface for maps that are both observable and sorted. */ -public interface ObservableSortedMap<K, V> extends ObservableMap<K, V>, SortedMap<K, V> { +public interface ObservableNavigableMap<K, V> extends NavigableMap<K, V>, ObservableMap<K, V> { // No additional methods. } diff --git a/app/src/main/java/com/wireguard/android/databinding/ObservableTreeMap.java b/app/src/main/java/com/wireguard/android/databinding/ObservableTreeMap.java index dc5f705b..074e122c 100644 --- a/app/src/main/java/com/wireguard/android/databinding/ObservableTreeMap.java +++ b/app/src/main/java/com/wireguard/android/databinding/ObservableTreeMap.java @@ -12,16 +12,10 @@ import java.util.TreeMap; * views. This behavior is in line with that of ObservableArrayMap. */ -public class ObservableTreeMap<K, V> extends TreeMap<K, V> implements ObservableSortedMap<K, V> { +public class ObservableTreeMap<K, V> extends TreeMap<K, V> implements ObservableNavigableMap<K, V> { private transient MapChangeRegistry listeners; @Override - public void clear() { - super.clear(); - notifyChange(null); - } - - @Override public void addOnMapChangedCallback( final OnMapChangedCallback<? extends ObservableMap<K, V>, K, V> listener) { if (listeners == null) @@ -29,6 +23,12 @@ public class ObservableTreeMap<K, V> extends TreeMap<K, V> implements Observable listeners.add(listener); } + @Override + public void clear() { + super.clear(); + notifyChange(null); + } + private void notifyChange(final K key) { if (listeners != null) listeners.notifyChange(this, key); @@ -51,8 +51,7 @@ public class ObservableTreeMap<K, V> extends TreeMap<K, V> implements Observable @Override public V remove(final Object key) { final V oldValue = super.remove(key); - @SuppressWarnings("unchecked") - final K k = (K) key; + @SuppressWarnings("unchecked") final K k = (K) key; notifyChange(k); return oldValue; } |