Newer
Older
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
182
183
184
185
186
187
188
189
190
191
192
package org.briarproject.android.blogs;
import android.app.Activity;
import android.support.annotation.Nullable;
import android.support.v7.util.SortedList;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import org.briarproject.R;
import org.briarproject.android.util.AndroidUtils;
import org.briarproject.api.feed.Feed;
import org.briarproject.api.sync.GroupId;
import java.util.Collection;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
class RssFeedAdapter extends
RecyclerView.Adapter<RssFeedAdapter.FeedViewHolder> {
private SortedList<Feed> feeds = new SortedList<>(
Feed.class, new SortedList.Callback<Feed>() {
@Override
public int compare(Feed a, Feed b) {
if (a == b) return 0;
long aTime = a.getAdded(), bTime = b.getAdded();
if (aTime > bTime) return -1;
if (aTime < bTime) return 1;
return 0;
}
@Override
public void onInserted(int position, int count) {
notifyItemRangeInserted(position, count);
}
@Override
public void onRemoved(int position, int count) {
notifyItemRangeRemoved(position, count);
}
@Override
public void onMoved(int fromPosition, int toPosition) {
notifyItemMoved(fromPosition, toPosition);
}
@Override
public void onChanged(int position, int count) {
notifyItemRangeChanged(position, count);
}
@Override
public boolean areContentsTheSame(Feed a, Feed b) {
return a.getUpdated() == b.getUpdated();
}
@Override
public boolean areItemsTheSame(Feed a, Feed b) {
return a.getUrl().equals(b.getUrl()) &&
a.getBlogId().equals(b.getBlogId()) &&
a.getAdded() == b.getAdded();
}
});
private final Activity ctx;
private final RssFeedListener listener;
RssFeedAdapter(Activity ctx, RssFeedListener listener) {
this.ctx = ctx;
this.listener = listener;
}
@Override
public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(ctx).inflate(
R.layout.list_item_rss_feed, parent, false);
return new FeedViewHolder(v);
}
@Override
public void onBindViewHolder(FeedViewHolder ui, int position) {
final Feed item = getItem(position);
// Feed Title
if (item.getTitle() != null) {
ui.title.setText(item.getTitle());
ui.title.setVisibility(VISIBLE);
} else {
ui.title.setVisibility(GONE);
}
// Delete Button
ui.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.onDeleteClick(item);
}
});
// Author
if (item.getAuthor() != null) {
ui.author.setText(item.getAuthor());
ui.author.setVisibility(VISIBLE);
ui.authorLabel.setVisibility(VISIBLE);
} else {
ui.author.setVisibility(GONE);
ui.authorLabel.setVisibility(GONE);
}
// Imported and Last Updated
ui.imported.setText(AndroidUtils.formatDate(ctx, item.getAdded()));
ui.updated.setText(AndroidUtils.formatDate(ctx, item.getUpdated()));
// Description
if (item.getDescription() != null) {
ui.description.setText(item.getDescription());
ui.description.setVisibility(VISIBLE);
} else {
ui.description.setVisibility(GONE);
}
}
@Override
public int getItemCount() {
return feeds.size();
}
public Feed getItem(int position) {
return feeds.get(position);
}
@Nullable
public Feed getItem(GroupId g) {
for (int i = 0; i < feeds.size(); i++) {
Feed item = feeds.get(i);
if (item.getBlogId().equals(g)) {
return item;
}
}
return null;
}
public void addAll(Collection<Feed> items) {
feeds.addAll(items);
}
public void remove(Feed item) {
feeds.remove(item);
}
public void clear() {
feeds.clear();
}
public boolean isEmpty() {
return feeds.size() == 0;
}
static class FeedViewHolder extends RecyclerView.ViewHolder {
private final TextView title;
private final ImageView delete;
private final TextView imported;
private final TextView updated;
private final TextView author;
private final TextView authorLabel;
private final TextView description;
FeedViewHolder(View v) {
super(v);
title = (TextView) v.findViewById(R.id.titleView);
delete = (ImageView) v.findViewById(R.id.deleteButton);
imported = (TextView) v.findViewById(R.id.importedView);
updated = (TextView) v.findViewById(R.id.updatedView);
author = (TextView) v.findViewById(R.id.authorView);
authorLabel = (TextView) v.findViewById(R.id.author);
description = (TextView) v.findViewById(R.id.descriptionView);
}
}
interface RssFeedListener {
void onDeleteClick(Feed feed);
}
}