mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 03:39:05 +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:
@@ -11,7 +11,7 @@ import org.briarproject.api.sync.MessageId;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public interface BaseController {
|
||||
interface BaseController {
|
||||
|
||||
@UiThread
|
||||
void onStart();
|
||||
|
||||
@@ -20,7 +20,7 @@ import static android.view.View.INVISIBLE;
|
||||
import static android.view.View.VISIBLE;
|
||||
import static org.briarproject.android.util.AndroidUtils.MIN_RESOLUTION;
|
||||
|
||||
public abstract class BasePostFragment extends BaseFragment {
|
||||
abstract class BasePostFragment extends BaseFragment {
|
||||
|
||||
private final Logger LOG =
|
||||
Logger.getLogger(BasePostFragment.class.getName());
|
||||
|
||||
@@ -21,8 +21,6 @@ public class BlogActivity extends BriarActivity implements
|
||||
static final String BLOG_NAME = "briar.BLOG_NAME";
|
||||
static final String IS_NEW_BLOG = "briar.IS_NEW_BLOG";
|
||||
|
||||
private ProgressBar progressBar;
|
||||
|
||||
@Inject
|
||||
BlogController blogController;
|
||||
|
||||
@@ -45,7 +43,6 @@ public class BlogActivity extends BriarActivity implements
|
||||
boolean isNew = i.getBooleanExtra(IS_NEW_BLOG, false);
|
||||
|
||||
setContentView(R.layout.activity_fragment_container);
|
||||
progressBar = (ProgressBar) findViewById(R.id.progressBar);
|
||||
|
||||
if (state == null) {
|
||||
BlogFragment f = BlogFragment.newInstance(groupId, blogName, isNew);
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package org.briarproject.android.blogs;
|
||||
|
||||
import android.support.annotation.UiThread;
|
||||
|
||||
import org.briarproject.api.blogs.BlogCommentHeader;
|
||||
import org.briarproject.api.blogs.BlogPostHeader;
|
||||
|
||||
@@ -10,7 +8,7 @@ import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
@UiThread
|
||||
// This class is not thread-safe
|
||||
class BlogCommentItem extends BlogPostItem {
|
||||
|
||||
private static final BlogCommentComparator COMPARATOR =
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
package org.briarproject.android.blogs;
|
||||
|
||||
import android.support.annotation.UiThread;
|
||||
|
||||
import org.briarproject.android.controller.ActivityLifecycleController;
|
||||
import org.briarproject.android.controller.handler.ResultExceptionHandler;
|
||||
import org.briarproject.api.blogs.BlogPostHeader;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
|
||||
@@ -3,7 +3,6 @@ package org.briarproject.android.blogs;
|
||||
import org.briarproject.android.controller.ActivityLifecycleController;
|
||||
import org.briarproject.android.controller.handler.ResultExceptionHandler;
|
||||
import org.briarproject.api.blogs.Blog;
|
||||
import org.briarproject.api.blogs.BlogPostHeader;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.event.BlogPostAddedEvent;
|
||||
import org.briarproject.api.event.Event;
|
||||
@@ -14,7 +13,6 @@ import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@@ -108,9 +106,7 @@ public class BlogControllerImpl extends BaseControllerImpl
|
||||
Blog b = blogManager.getBlog(groupId);
|
||||
boolean ours = a.getId().equals(b.getAuthor().getId());
|
||||
boolean removable = blogManager.canBeRemoved(groupId);
|
||||
BlogItem blog = new BlogItem(b,
|
||||
Collections.<BlogPostHeader>emptyList(),
|
||||
ours, removable);
|
||||
BlogItem blog = new BlogItem(b, ours, removable);
|
||||
handler.onResult(blog);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING))
|
||||
@@ -119,7 +115,6 @@ public class BlogControllerImpl extends BaseControllerImpl
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -62,7 +62,7 @@ public class BlogFragment extends BaseFragment implements
|
||||
private MenuItem writeButton, deleteButton;
|
||||
private boolean isMyBlog = false, canDeleteBlog = false;
|
||||
|
||||
static BlogFragment newInstance(GroupId groupId, String name,
|
||||
static BlogFragment newInstance(GroupId groupId, @Nullable String name,
|
||||
boolean isNew) {
|
||||
|
||||
BlogFragment f = new BlogFragment();
|
||||
|
||||
@@ -1,40 +1,14 @@
|
||||
package org.briarproject.android.blogs;
|
||||
|
||||
import org.briarproject.api.blogs.Blog;
|
||||
import org.briarproject.api.blogs.BlogPostHeader;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
class BlogItem {
|
||||
|
||||
private final Blog blog;
|
||||
private final int postCount;
|
||||
private final long timestamp;
|
||||
private final int unread;
|
||||
private final boolean ours, removable;
|
||||
|
||||
BlogItem(Blog blog, Collection<BlogPostHeader> headers, boolean ours,
|
||||
boolean removable) {
|
||||
BlogItem(Blog blog, boolean ours, boolean removable) {
|
||||
this.blog = blog;
|
||||
if (headers.isEmpty()) {
|
||||
postCount = 0;
|
||||
timestamp = 0;
|
||||
unread = 0;
|
||||
} else {
|
||||
BlogPostHeader newest = null;
|
||||
long timestamp = -1;
|
||||
int unread = 0;
|
||||
for (BlogPostHeader h : headers) {
|
||||
if (h.getTimestamp() > timestamp) {
|
||||
timestamp = h.getTimestamp();
|
||||
newest = h;
|
||||
}
|
||||
if (!h.isRead()) unread++;
|
||||
}
|
||||
this.postCount = headers.size();
|
||||
this.timestamp = newest.getTimestamp();
|
||||
this.unread = unread;
|
||||
}
|
||||
this.ours = ours;
|
||||
this.removable = removable;
|
||||
}
|
||||
@@ -47,22 +21,6 @@ class BlogItem {
|
||||
return blog.getName();
|
||||
}
|
||||
|
||||
boolean isEmpty() {
|
||||
return postCount == 0;
|
||||
}
|
||||
|
||||
int getPostCount() {
|
||||
return postCount;
|
||||
}
|
||||
|
||||
long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
int getUnreadCount() {
|
||||
return unread;
|
||||
}
|
||||
|
||||
boolean isOurs() {
|
||||
return ours;
|
||||
}
|
||||
|
||||
@@ -1,60 +1,20 @@
|
||||
package org.briarproject.android.blogs;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.v7.util.SortedList;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import org.briarproject.R;
|
||||
import org.briarproject.android.util.BriarAdapter;
|
||||
|
||||
import java.util.Collection;
|
||||
class BlogPostAdapter
|
||||
extends BriarAdapter<BlogPostItem, BlogPostViewHolder> {
|
||||
|
||||
class BlogPostAdapter extends RecyclerView.Adapter<BlogPostViewHolder> {
|
||||
|
||||
private SortedList<BlogPostItem> posts = new SortedList<>(
|
||||
BlogPostItem.class, new SortedList.Callback<BlogPostItem>() {
|
||||
@Override
|
||||
public int compare(BlogPostItem a, BlogPostItem b) {
|
||||
return a.compareTo(b);
|
||||
}
|
||||
|
||||
@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(BlogPostItem a, BlogPostItem b) {
|
||||
return a.isRead() == b.isRead();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areItemsTheSame(BlogPostItem a, BlogPostItem b) {
|
||||
return a.getId().equals(b.getId());
|
||||
}
|
||||
});
|
||||
private final Context ctx;
|
||||
private final OnBlogPostClickListener listener;
|
||||
|
||||
BlogPostAdapter(Context ctx, OnBlogPostClickListener listener) {
|
||||
this.ctx = ctx;
|
||||
super(ctx, BlogPostItem.class);
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
@@ -70,36 +30,22 @@ class BlogPostAdapter extends RecyclerView.Adapter<BlogPostViewHolder> {
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(BlogPostViewHolder ui, int position) {
|
||||
ui.bindItem(getItem(position));
|
||||
ui.bindItem(getItemAt(position));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return posts.size();
|
||||
public int compare(BlogPostItem a, BlogPostItem b) {
|
||||
return a.compareTo(b);
|
||||
}
|
||||
|
||||
public BlogPostItem getItem(int position) {
|
||||
return posts.get(position);
|
||||
@Override
|
||||
public boolean areContentsTheSame(BlogPostItem a, BlogPostItem b) {
|
||||
return a.isRead() == b.isRead();
|
||||
}
|
||||
|
||||
public void add(BlogPostItem item) {
|
||||
posts.add(item);
|
||||
}
|
||||
|
||||
public void addAll(Collection<BlogPostItem> items) {
|
||||
posts.addAll(items);
|
||||
}
|
||||
|
||||
public void remove(BlogPostItem item) {
|
||||
posts.remove(item);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
posts.clear();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return posts.size() == 0;
|
||||
@Override
|
||||
public boolean areItemsTheSame(BlogPostItem a, BlogPostItem b) {
|
||||
return a.getId().equals(b.getId());
|
||||
}
|
||||
|
||||
interface OnBlogPostClickListener {
|
||||
|
||||
@@ -2,7 +2,6 @@ package org.briarproject.android.blogs;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.UiThread;
|
||||
|
||||
import org.briarproject.api.blogs.BlogPostHeader;
|
||||
import org.briarproject.api.identity.Author;
|
||||
@@ -10,8 +9,8 @@ import org.briarproject.api.identity.Author.Status;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
|
||||
@UiThread
|
||||
class BlogPostItem implements Comparable<BlogPostItem> {
|
||||
// This class is not thread-safe
|
||||
public class BlogPostItem implements Comparable<BlogPostItem> {
|
||||
|
||||
private final BlogPostHeader header;
|
||||
protected String body;
|
||||
|
||||
@@ -12,7 +12,6 @@ import java.util.Collection;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
|
||||
public class BlogPostPagerFragment extends BasePostPagerFragment {
|
||||
|
||||
public final static String TAG = BlogPostPagerFragment.class.getName();
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.briarproject.android.blogs;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.UiThread;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.app.ActivityOptionsCompat;
|
||||
@@ -85,7 +86,9 @@ class BlogPostViewHolder extends RecyclerView.ViewHolder {
|
||||
return "blogPost" + id.hashCode();
|
||||
}
|
||||
|
||||
void bindItem(final BlogPostItem item) {
|
||||
void bindItem(@Nullable final BlogPostItem item) {
|
||||
if (item == null) return;
|
||||
|
||||
setTransitionName(item.getId());
|
||||
if (listener != null) {
|
||||
layout.setClickable(true);
|
||||
|
||||
@@ -40,7 +40,7 @@ public class FeedPostPagerFragment extends BasePostPagerFragment {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
void loadBlogPosts(final MessageId select) {
|
||||
feedController.loadBlogPosts(
|
||||
new UiResultExceptionHandler<Collection<BlogPostItem>, DbException>(
|
||||
@@ -57,6 +57,7 @@ public class FeedPostPagerFragment extends BasePostPagerFragment {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
void loadBlogPost(BlogPostHeader header) {
|
||||
feedController.loadBlogPost(header,
|
||||
new UiResultExceptionHandler<BlogPostItem, DbException>(
|
||||
|
||||
@@ -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