From 44561a9cb6a44eecb0305c3295993cb430d881d5 Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Thu, 17 Aug 2017 02:44:18 -0500 Subject: [PATCH] ObservableArrayMapAdapter: Remove Signed-off-by: Jason A. Donenfeld --- .../wireguard/android/BindingAdapters.java | 28 ------ .../android/ObservableArrayMapAdapter.java | 92 ------------------- 2 files changed, 120 deletions(-) delete mode 100644 app/src/main/java/com/wireguard/android/ObservableArrayMapAdapter.java diff --git a/app/src/main/java/com/wireguard/android/BindingAdapters.java b/app/src/main/java/com/wireguard/android/BindingAdapters.java index 7e93c228..e3df7bb4 100644 --- a/app/src/main/java/com/wireguard/android/BindingAdapters.java +++ b/app/src/main/java/com/wireguard/android/BindingAdapters.java @@ -14,34 +14,6 @@ import android.widget.TextView; @SuppressWarnings("unused") public final class BindingAdapters { - @BindingAdapter({"items", "layout"}) - public static void arrayMapBinding(final ListView view, - final ObservableArrayMap oldMap, - final int oldLayoutId, - final ObservableArrayMap 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") - ObservableArrayMapAdapter adapter = - (ObservableArrayMapAdapter) 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 ObservableArrayMapAdapter<>(view.getContext(), newLayoutId, newMap); - view.setAdapter(adapter); - } else if (newMap != oldMap) { - // Changing the list only requires modifying the existing adapter. - adapter.setMap(newMap); - } - } - @BindingAdapter({"items", "layout"}) public static void listBinding(final ListView view, final ObservableList oldList, final int oldLayoutId, diff --git a/app/src/main/java/com/wireguard/android/ObservableArrayMapAdapter.java b/app/src/main/java/com/wireguard/android/ObservableArrayMapAdapter.java deleted file mode 100644 index dd3a380f..00000000 --- a/app/src/main/java/com/wireguard/android/ObservableArrayMapAdapter.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.wireguard.android; - -import android.content.Context; -import android.databinding.DataBindingUtil; -import android.databinding.ObservableArrayMap; -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 android.widget.ListAdapter; - -import java.lang.ref.WeakReference; - -/** - * A generic ListAdapter backed by an ObservableArrayMap. - */ - -class ObservableArrayMapAdapter extends BaseAdapter implements ListAdapter { - private final int layoutId; - private final LayoutInflater layoutInflater; - private ObservableArrayMap map; - private final OnMapChangedCallback callback = new OnMapChangedCallback<>(this); - - ObservableArrayMapAdapter(final Context context, final int layoutId, - final ObservableArrayMap map) { - super(); - layoutInflater = LayoutInflater.from(context); - this.layoutId = layoutId; - setMap(map); - } - - @Override - public int getCount() { - return map != null ? map.size() : 0; - } - - @Override - public V getItem(final int position) { - return map != null ? map.valueAt(position) : null; - } - - @Override - public long getItemId(final int position) { - return getItem(position) != null ? getItem(position).hashCode() : -1; - } - - @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.item, getItem(position)); - binding.executePendingBindings(); - return binding.getRoot(); - } - - @Override - public boolean hasStableIds() { - return true; - } - - public void setMap(final ObservableArrayMap newMap) { - if (map != null) - map.removeOnMapChangedCallback(callback); - map = newMap; - if (map != null) { - map.addOnMapChangedCallback(callback); - } - } - - private static class OnMapChangedCallback - extends ObservableMap.OnMapChangedCallback, K, V> { - - private final WeakReference> weakAdapter; - - private OnMapChangedCallback(final ObservableArrayMapAdapter adapter) { - super(); - weakAdapter = new WeakReference<>(adapter); - } - - @Override - public void onMapChanged(final ObservableMap sender, final K key) { - final ObservableArrayMapAdapter adapter = weakAdapter.get(); - if (adapter != null) - adapter.notifyDataSetChanged(); - else - sender.removeOnMapChangedCallback(this); - } - } -}