databinding: Remove unused classes and methods

Signed-off-by: Samuel Holland <samuel@sholland.org>
This commit is contained in:
Samuel Holland 2018-01-06 04:05:43 -06:00
parent ff0bb081a0
commit 6eef4093d8
5 changed files with 0 additions and 347 deletions

View File

@ -90,33 +90,6 @@ public final class BindingAdapters {
adapter.setList(newList); adapter.setList(newList);
} }
@BindingAdapter({"items", "layout"})
public static <K extends Comparable<K>, V> void setItems(final ListView view,
final ObservableNavigableMap<K, V> oldMap,
final int oldLayoutId,
final ObservableNavigableMap<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 (adapter != null && oldMap != null && oldLayoutId != newLayoutId) {
adapter.setMap(null);
adapter = null;
}
// 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({"onBeforeCheckedChanged"}) @BindingAdapter({"onBeforeCheckedChanged"})
public static void setOnBeforeCheckedChanged(final ToggleSwitch view, public static void setOnBeforeCheckedChanged(final ToggleSwitch view,
final ToggleSwitch.OnBeforeCheckedChangeListener final ToggleSwitch.OnBeforeCheckedChangeListener

View File

@ -1,119 +0,0 @@
package com.wireguard.android.databinding;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableList;
import android.databinding.ViewDataBinding;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.wireguard.android.BR;
import java.lang.ref.WeakReference;
/**
* A generic ListAdapter backed by an ObservableList.
*/
class ObservableListAdapter<T> extends BaseAdapter {
private final OnListChangedCallback<T> callback = new OnListChangedCallback<>(this);
private final int layoutId;
private final LayoutInflater layoutInflater;
private ObservableList<T> list;
ObservableListAdapter(final Context context, final int layoutId, final ObservableList<T> list) {
this.layoutId = layoutId;
layoutInflater = LayoutInflater.from(context);
setList(list);
}
@Override
public int getCount() {
return list != null ? list.size() : 0;
}
@Override
public T getItem(final int position) {
if (list == null || position < 0 || position >= list.size())
return null;
return list.get(position);
}
@Override
public long getItemId(final int position) {
if (list == null || position < 0 || position >= list.size())
return -1;
return list.get(position).hashCode();
}
@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.collection, list);
binding.setVariable(BR.item, getItem(position));
binding.executePendingBindings();
return binding.getRoot();
}
@Override
public boolean hasStableIds() {
return true;
}
void setList(final ObservableList<T> newList) {
if (list != null)
list.removeOnListChangedCallback(callback);
list = newList;
if (list != null) {
list.addOnListChangedCallback(callback);
}
notifyDataSetChanged();
}
private static final class OnListChangedCallback<U>
extends ObservableList.OnListChangedCallback<ObservableList<U>> {
private final WeakReference<ObservableListAdapter<U>> weakAdapter;
private OnListChangedCallback(final ObservableListAdapter<U> adapter) {
weakAdapter = new WeakReference<>(adapter);
}
@Override
public void onChanged(final ObservableList<U> sender) {
final ObservableListAdapter<U> adapter = weakAdapter.get();
if (adapter != null)
adapter.notifyDataSetChanged();
else
sender.removeOnListChangedCallback(this);
}
@Override
public void onItemRangeChanged(final ObservableList<U> sender, final int positionStart,
final int itemCount) {
onChanged(sender);
}
@Override
public void onItemRangeInserted(final ObservableList<U> sender, final int positionStart,
final int itemCount) {
onChanged(sender);
}
@Override
public void onItemRangeMoved(final ObservableList<U> sender, final int fromPosition,
final int toPosition, final int itemCount) {
onChanged(sender);
}
@Override
public void onItemRangeRemoved(final ObservableList<U> sender, final int positionStart,
final int itemCount) {
onChanged(sender);
}
}
}

View File

@ -1,123 +0,0 @@
package com.wireguard.android.databinding;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableMap;
import android.databinding.ViewDataBinding;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
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 {
private final OnMapChangedCallback<K, V> callback = new OnMapChangedCallback<>(this);
private final int layoutId;
private final LayoutInflater layoutInflater;
private List<K> keys;
private ObservableNavigableMap<K, V> map;
ObservableMapAdapter(final Context context, final int layoutId,
final ObservableNavigableMap<K, V> map) {
this.layoutId = layoutId;
layoutInflater = LayoutInflater.from(context);
setMap(map);
}
@Override
public int getCount() {
return map != null ? map.size() : 0;
}
@Override
public V getItem(final int position) {
if (map == null || position < 0 || position >= map.size())
return null;
return map.get(getKey(position));
}
@Override
public long getItemId(final int position) {
if (map == null || position < 0 || position >= map.size())
return -1;
//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 List<K> getKeys() {
if (keys == null)
keys = new ArrayList<>(map.keySet());
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.collection, map);
binding.setVariable(BR.key, getKey(position));
binding.setVariable(BR.item, getItem(position));
binding.executePendingBindings();
return binding.getRoot();
}
@Override
public boolean hasStableIds() {
return true;
}
void setMap(final ObservableNavigableMap<K, V> newMap) {
if (map != null)
map.removeOnMapChangedCallback(callback);
keys = null;
map = newMap;
if (map != null) {
map.addOnMapChangedCallback(callback);
}
notifyDataSetChanged();
}
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;
private OnMapChangedCallback(final ObservableMapAdapter<K, V> adapter) {
weakAdapter = new WeakReference<>(adapter);
}
@Override
public void onMapChanged(final ObservableNavigableMap<K, V> sender, final K key) {
final ObservableMapAdapter<K, V> adapter = weakAdapter.get();
if (adapter != null) {
adapter.keys = null;
adapter.notifyDataSetChanged();
} else {
sender.removeOnMapChangedCallback(this);
}
}
}
}

View File

@ -1,13 +0,0 @@
package com.wireguard.android.databinding;
import android.databinding.ObservableMap;
import java.util.NavigableMap;
/**
* Interface for maps that are both observable and sorted.
*/
public interface ObservableNavigableMap<K, V> extends NavigableMap<K, V>, ObservableMap<K, V> {
// No additional methods.
}

View File

@ -1,65 +0,0 @@
package com.wireguard.android.databinding;
import android.databinding.MapChangeRegistry;
import android.databinding.ObservableMap;
import android.support.annotation.NonNull;
import java.util.Map;
import java.util.TreeMap;
/**
* Observable version of a TreeMap. Only notifies for changes made through methods, not iterators or
* views. This behavior is in line with that of ObservableArrayMap.
*/
public class ObservableTreeMap<K, V> extends TreeMap<K, V> implements ObservableNavigableMap<K, V> {
private transient MapChangeRegistry listeners;
@Override
public void addOnMapChangedCallback(
final OnMapChangedCallback<? extends ObservableMap<K, V>, K, V> listener) {
if (listeners == null)
listeners = new MapChangeRegistry();
listeners.add(listener);
}
@Override
public void clear() {
super.clear();
notifyChange(null);
}
private void notifyChange(final K key) {
if (listeners != null)
listeners.notifyChange(this, key);
}
@Override
public V put(final K key, final V value) {
final V oldValue = super.put(key, value);
notifyChange(key);
return oldValue;
}
@Override
public void putAll(@NonNull final Map<? extends K, ? extends V> map) {
super.putAll(map);
for (final K key : map.keySet())
notifyChange(key);
}
@Override
public V remove(final Object key) {
final V oldValue = super.remove(key);
@SuppressWarnings("unchecked") final K k = (K) key;
notifyChange(k);
return oldValue;
}
@Override
public void removeOnMapChangedCallback(
final OnMapChangedCallback<? extends ObservableMap<K, V>, K, V> listener) {
if (listeners != null)
listeners.remove(listener);
}
}