summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/ObservableArrayMapAdapter.java
blob: d2a5a4ccc8d5a5cdc0b0375d0d4a934605a59743 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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 ObservableMap.
 */

class ObservableArrayMapAdapter<K, V> extends BaseAdapter implements ListAdapter {
    private final int layoutId;
    private final LayoutInflater layoutInflater;
    private ObservableArrayMap<K, V> map;
    private final OnMapChangedCallback<K, V> callback = new OnMapChangedCallback<>(this);

    ObservableArrayMapAdapter(Context context, int layoutId, ObservableArrayMap<K, V> map) {
        this.layoutInflater = LayoutInflater.from(context);
        this.layoutId = layoutId;
        setMap(map);
    }

    @Override
    public int getCount() {
        return map != null ? map.size() : 0;
    }

    @Override
    public V getItem(int position) {
        return map != null ? map.get(map.keyAt(position)) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, 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();
    }

    public void setMap(ObservableArrayMap<K, V> newMap) {
        if (map != null)
            map.removeOnMapChangedCallback(callback);
        map = newMap;
        if (map != null) {
            map.addOnMapChangedCallback(callback);
        }
    }

    private static class OnMapChangedCallback<K, V>
            extends ObservableMap.OnMapChangedCallback<ObservableMap<K, V>, K, V> {

        private final WeakReference<ObservableArrayMapAdapter<K, V>> weakAdapter;

        private OnMapChangedCallback(ObservableArrayMapAdapter<K, V> adapter) {
            weakAdapter = new WeakReference<>(adapter);
        }

        @Override
        public void onMapChanged(ObservableMap<K, V> sender, K key) {
            final ObservableArrayMapAdapter<K, V> adapter = weakAdapter.get();
            if (adapter != null)
                adapter.notifyDataSetChanged();
            else
                sender.removeOnMapChangedCallback(this);
        }
    }
}