Bindings: Merge bugfixes and updates

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Samuel Holland 2017-11-08 00:07:29 -06:00
parent 6231bb18ac
commit 70156381a7
6 changed files with 62 additions and 39 deletions

View File

@ -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 {

View File

@ -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.
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 <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.
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"})

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -7,6 +7,10 @@
<import type="com.wireguard.android.VpnService" />
<variable
name="key"
type="String" />
<variable
name="item"
type="com.wireguard.config.Config" />
@ -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