wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*  Copyright (C) 2020-2024 Petr Vaněk
 
    This file is part of Gadgetbridge.
 
    Gadgetbridge is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    Gadgetbridge is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.adapter;
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
 
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
 
import nodomain.freeyourgadget.gadgetbridge.R;
 
/**
 * Adapter for displaying generic ItemWithDetails instances.
 */
public abstract class AbstractActivityListingAdapter<T> extends RecyclerView.Adapter<AbstractActivityListingAdapter.AbstractActivityListingViewHolder<T>> {
    private final Context context;
    private final List<T> items;
    private final BitSet selectedItems = new BitSet();
 
    private OnItemClickListener onItemSingleClickListener;
    private OnItemClickListener onItemLongClickListener;
 
    public AbstractActivityListingAdapter(Context context) {
        this.context = context;
        this.items = new ArrayList<T>();
    }
 
    public void setOnItemClickListener(final OnItemClickListener onItemSingleClickListener) {
        this.onItemSingleClickListener = onItemSingleClickListener;
    }
 
    public void setOnItemLongClickListener(final OnItemClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }
 
    public Context getContext() {
        return context;
    }
 
    public BitSet getSelectedItems() {
        return selectedItems;
    }
 
    @Override
    public int getItemCount() {
        return items.size();
    }
 
    public T getItem(final int position) {
        return items.get(position);
    }
 
    public int getPosition(final T item) {
        return items.indexOf(item);
    }
 
    public boolean isSelected(final int position) {
        return selectedItems.get(position);
    }
 
    @Override
    public int getItemViewType(final int position) {
        if (position == 0) {
            // First item is always the dashboard
            return 0;
        } else if (position == getItemCount() - 1) {
            // Last item is always an empty session (prevent overlap with fab)
            return 1;
        }
 
        return 2;
    }
 
    @NonNull
    @Override
    public AbstractActivityListingViewHolder<T> onCreateViewHolder(@NonNull final ViewGroup parent, final int viewType) {
        switch (viewType) {
            case 1: // empty
                return new EmptyViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.activity_list_item, parent, false));
        }
 
        throw new IllegalArgumentException("Unknown view type " + viewType);
    }
 
    @Override
    public void onBindViewHolder(@NonNull final AbstractActivityListingViewHolder<T> holder, int position) {
        holder.fill(position, getItem(position), isSelected(position));
        if (position > 0 && position < getItemCount() - 1) {
            if (onItemSingleClickListener != null) {
                holder.itemView.setOnClickListener(v -> onItemSingleClickListener.onClick(position));
            }
            if (onItemLongClickListener != null) {
                holder.itemView.setOnLongClickListener(v -> {
                    onItemLongClickListener.onClick(position);
                    return true;
                });
            }
        }
    }
 
    public List<T> getItems() {
        return items;
    }
 
    public void loadItems() {
    }
 
    public void setItems(List<T> items, boolean notify) {
        this.items.clear();
        this.items.addAll(items);
        this.selectedItems.clear();
        if (notify) {
            notifyDataSetChanged();
        }
    }
 
    public void setActivityKindFilter(int activityKind) {
    }
 
    public void setDateFromFilter(long date) {
    }
 
    public void setDateToFilter(long date) {
    }
 
    public void setNameContainsFilter(String name) {
    }
 
    public void setItemsFilter(List<Long> items) {
    }
 
    public void setDeviceFilter(long device) {
    }
 
    public abstract static class AbstractActivityListingViewHolder<T> extends RecyclerView.ViewHolder {
        public AbstractActivityListingViewHolder(@NonNull final View itemView) {
            super(itemView);
        }
 
        public abstract void fill(int position, T item, final boolean selected);
    }
 
    public interface OnItemClickListener {
        void onClick(int position);
    }
 
    public class EmptyViewHolder extends AbstractActivityListingViewHolder<T> {
        final View rootView;
 
        public EmptyViewHolder(@NonNull final View itemView) {
            super(itemView);
            this.rootView = itemView;
        }
 
        @Override
        public void fill(final int position, final T item, final boolean selected) {
            rootView.setVisibility(View.GONE);
        }
    }
}