mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 05:09:53 +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:
@@ -4,7 +4,6 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v7.util.SortedList;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
@@ -13,73 +12,21 @@ import android.widget.TextView;
|
||||
|
||||
import org.briarproject.R;
|
||||
import org.briarproject.android.util.AndroidUtils;
|
||||
import org.briarproject.android.util.BriarAdapter;
|
||||
import org.briarproject.android.view.TextAvatarView;
|
||||
import org.briarproject.api.forum.Forum;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import static android.view.View.GONE;
|
||||
import static android.view.View.VISIBLE;
|
||||
import static org.briarproject.android.BriarActivity.GROUP_ID;
|
||||
import static org.briarproject.android.forum.ForumActivity.FORUM_NAME;
|
||||
|
||||
class ForumListAdapter extends
|
||||
RecyclerView.Adapter<ForumListAdapter.ForumViewHolder> {
|
||||
|
||||
private SortedList<ForumListItem> forums = new SortedList<>(
|
||||
ForumListItem.class, new SortedList.Callback<ForumListItem>() {
|
||||
|
||||
@Override
|
||||
public int compare(ForumListItem a, ForumListItem b) {
|
||||
if (a == b) return 0;
|
||||
// The forum with the newest message comes first
|
||||
long aTime = a.getTimestamp(), bTime = b.getTimestamp();
|
||||
if (aTime > bTime) return -1;
|
||||
if (aTime < bTime) return 1;
|
||||
// Break ties by forum name
|
||||
String aName = a.getForum().getName();
|
||||
String bName = b.getForum().getName();
|
||||
return String.CASE_INSENSITIVE_ORDER.compare(aName, bName);
|
||||
}
|
||||
|
||||
@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(ForumListItem a, ForumListItem b) {
|
||||
return a.getForum().equals(b.getForum()) &&
|
||||
a.getTimestamp() == b.getTimestamp() &&
|
||||
a.getUnreadCount() == b.getUnreadCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areItemsTheSame(ForumListItem a, ForumListItem b) {
|
||||
return a.getForum().equals(b.getForum());
|
||||
}
|
||||
});
|
||||
|
||||
private final Context ctx;
|
||||
class ForumListAdapter
|
||||
extends BriarAdapter<ForumListItem, ForumListAdapter.ForumViewHolder> {
|
||||
|
||||
ForumListAdapter(Context ctx) {
|
||||
this.ctx = ctx;
|
||||
super(ctx, ForumListItem.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -91,7 +38,8 @@ class ForumListAdapter extends
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(ForumViewHolder ui, int position) {
|
||||
final ForumListItem item = getItem(position);
|
||||
final ForumListItem item = getItemAt(position);
|
||||
if (item == null) return;
|
||||
|
||||
// Avatar
|
||||
ui.avatar.setText(item.getForum().getName().substring(0, 1));
|
||||
@@ -142,18 +90,34 @@ class ForumListAdapter extends
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return forums.size();
|
||||
public int compare(ForumListItem a, ForumListItem b) {
|
||||
if (a == b) return 0;
|
||||
// The forum with the newest message comes first
|
||||
long aTime = a.getTimestamp(), bTime = b.getTimestamp();
|
||||
if (aTime > bTime) return -1;
|
||||
if (aTime < bTime) return 1;
|
||||
// Break ties by forum name
|
||||
String aName = a.getForum().getName();
|
||||
String bName = b.getForum().getName();
|
||||
return String.CASE_INSENSITIVE_ORDER.compare(aName, bName);
|
||||
}
|
||||
|
||||
public ForumListItem getItem(int position) {
|
||||
return forums.get(position);
|
||||
@Override
|
||||
public boolean areContentsTheSame(ForumListItem a, ForumListItem b) {
|
||||
return a.getForum().equals(b.getForum()) &&
|
||||
a.getTimestamp() == b.getTimestamp() &&
|
||||
a.getUnreadCount() == b.getUnreadCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areItemsTheSame(ForumListItem a, ForumListItem b) {
|
||||
return a.getForum().equals(b.getForum());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public ForumListItem getItem(GroupId g) {
|
||||
for (int i = 0; i < forums.size(); i++) {
|
||||
ForumListItem item = forums.get(i);
|
||||
public ForumListItem findItem(GroupId g) {
|
||||
for (int i = 0; i < items.size(); i++) {
|
||||
ForumListItem item = items.get(i);
|
||||
if (item.getForum().getGroup().getId().equals(g)) {
|
||||
return item;
|
||||
}
|
||||
@@ -161,26 +125,10 @@ class ForumListAdapter extends
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addAll(Collection<ForumListItem> items) {
|
||||
forums.addAll(items);
|
||||
}
|
||||
|
||||
void updateItem(ForumListItem item) {
|
||||
ForumListItem oldItem = getItem(item.getForum().getGroup().getId());
|
||||
int position = forums.indexOf(oldItem);
|
||||
forums.updateItemAt(position, item);
|
||||
}
|
||||
|
||||
public void remove(ForumListItem item) {
|
||||
forums.remove(item);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
forums.clear();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return forums.size() == 0;
|
||||
ForumListItem oldItem = findItem(item.getForum().getGroup().getId());
|
||||
int position = items.indexOf(oldItem);
|
||||
items.updateItemAt(position, item);
|
||||
}
|
||||
|
||||
static class ForumViewHolder extends RecyclerView.ViewHolder {
|
||||
@@ -191,7 +139,7 @@ class ForumListAdapter extends
|
||||
private final TextView postCount;
|
||||
private final TextView date;
|
||||
|
||||
ForumViewHolder(View v) {
|
||||
private ForumViewHolder(View v) {
|
||||
super(v);
|
||||
|
||||
layout = (ViewGroup) v;
|
||||
|
||||
@@ -11,6 +11,7 @@ import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import org.briarproject.R;
|
||||
@@ -44,7 +45,7 @@ import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
public class ForumListFragment extends BaseEventFragment implements
|
||||
View.OnClickListener {
|
||||
OnClickListener {
|
||||
|
||||
public final static String TAG = "ForumListFragment";
|
||||
|
||||
@@ -288,7 +289,7 @@ public class ForumListFragment extends BaseEventFragment implements
|
||||
listener.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ForumListItem item = adapter.getItem(g);
|
||||
ForumListItem item = adapter.findItem(g);
|
||||
if (item != null) adapter.remove(item);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user