mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Refactor existing adapters into a generic superclass
This commit also moves various blog classes into their own packages and makes the required visibility changes.
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package org.briarproject.android.blogs;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.util.SortedList;
|
||||
import android.content.Context;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
@@ -13,67 +11,18 @@ import android.widget.TextView;
|
||||
|
||||
import org.briarproject.R;
|
||||
import org.briarproject.android.util.AndroidUtils;
|
||||
import org.briarproject.android.util.BriarAdapter;
|
||||
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> {
|
||||
class RssFeedAdapter extends BriarAdapter<Feed, 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;
|
||||
RssFeedAdapter(Context ctx, RssFeedListener listener) {
|
||||
super(ctx, Feed.class);
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
@@ -86,7 +35,8 @@ class RssFeedAdapter extends
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(FeedViewHolder ui, int position) {
|
||||
final Feed item = getItem(position);
|
||||
final Feed item = getItemAt(position);
|
||||
if (item == null) return;
|
||||
|
||||
// Feed Title
|
||||
if (item.getTitle() != null) {
|
||||
@@ -128,39 +78,24 @@ class RssFeedAdapter extends
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return feeds.size();
|
||||
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;
|
||||
}
|
||||
|
||||
public Feed getItem(int position) {
|
||||
return feeds.get(position);
|
||||
@Override
|
||||
public boolean areContentsTheSame(Feed a, Feed b) {
|
||||
return a.getUpdated() == b.getUpdated();
|
||||
}
|
||||
|
||||
@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;
|
||||
@Override
|
||||
public boolean areItemsTheSame(Feed a, Feed b) {
|
||||
return a.getUrl().equals(b.getUrl()) &&
|
||||
a.getBlogId().equals(b.getBlogId()) &&
|
||||
a.getAdded() == b.getAdded();
|
||||
}
|
||||
|
||||
static class FeedViewHolder extends RecyclerView.ViewHolder {
|
||||
@@ -172,7 +107,7 @@ class RssFeedAdapter extends
|
||||
private final TextView authorLabel;
|
||||
private final TextView description;
|
||||
|
||||
FeedViewHolder(View v) {
|
||||
private FeedViewHolder(View v) {
|
||||
super(v);
|
||||
|
||||
title = (TextView) v.findViewById(R.id.titleView);
|
||||
|
||||
Reference in New Issue
Block a user