Use new GroupCount to display Forum List

Fixes #531, #532
This commit is contained in:
Torsten Grote
2016-10-05 16:41:46 -03:00
parent 70d39d03bc
commit 784561144a
6 changed files with 60 additions and 123 deletions

View File

@@ -1,27 +0,0 @@
package org.briarproject.android.forum;
import org.briarproject.api.forum.ForumPostHeader;
// This class is not thread-safe
class ForumItem {
private final ForumPostHeader header;
private byte[] body;
ForumItem(ForumPostHeader header) {
this.header = header;
body = null;
}
ForumPostHeader getHeader() {
return header;
}
byte[] getBody() {
return body;
}
void setBody(byte[] body) {
this.body = body;
}
}

View File

@@ -1,17 +0,0 @@
package org.briarproject.android.forum;
import java.util.Comparator;
class ForumItemComparator implements Comparator<ForumItem> {
static final ForumItemComparator INSTANCE = new ForumItemComparator();
public int compare(ForumItem a, ForumItem b) {
// The oldest message comes first
long aTime = a.getHeader().getTimestamp();
long bTime = b.getHeader().getTimestamp();
if (aTime < bTime) return -1;
if (aTime > bTime) return 1;
return 0;
}
}

View File

@@ -17,6 +17,7 @@ import org.briarproject.android.view.TextAvatarView;
import org.briarproject.api.forum.Forum; import org.briarproject.api.forum.Forum;
import org.briarproject.api.sync.GroupId; import org.briarproject.api.sync.GroupId;
import static android.support.v7.util.SortedList.INVALID_POSITION;
import static android.view.View.GONE; import static android.view.View.GONE;
import static android.view.View.VISIBLE; import static android.view.View.VISIBLE;
import static org.briarproject.android.BriarActivity.GROUP_ID; import static org.briarproject.android.BriarActivity.GROUP_ID;
@@ -50,7 +51,7 @@ class ForumListAdapter
ui.name.setText(item.getForum().getName()); ui.name.setText(item.getForum().getName());
// Post Count // Post Count
int postCount = item.getPostCount(); int postCount = (int) item.getPostCount();
if (postCount > 0) { if (postCount > 0) {
ui.avatar.setProblem(false); ui.avatar.setProblem(false);
ui.postCount.setText(ctx.getResources() ui.postCount.setText(ctx.getResources()
@@ -104,7 +105,7 @@ class ForumListAdapter
@Override @Override
public boolean areContentsTheSame(ForumListItem a, ForumListItem b) { public boolean areContentsTheSame(ForumListItem a, ForumListItem b) {
return a.getForum().equals(b.getForum()) && return a.isEmpty() == b.isEmpty() &&
a.getTimestamp() == b.getTimestamp() && a.getTimestamp() == b.getTimestamp() &&
a.getUnreadCount() == b.getUnreadCount(); a.getUnreadCount() == b.getUnreadCount();
} }
@@ -125,10 +126,14 @@ class ForumListAdapter
return null; return null;
} }
void updateItem(ForumListItem item) { int findItemPosition(GroupId g) {
ForumListItem oldItem = findItem(item.getForum().getGroup().getId()); int count = getItemCount();
int position = items.indexOf(oldItem); for (int i = 0; i < count; i++) {
items.updateItemAt(position, item); ForumListItem item = getItemAt(i);
if (item != null && item.getForum().getGroup().getId().equals(g))
return i;
}
return INVALID_POSITION; // Not found
} }
static class ForumViewHolder extends RecyclerView.ViewHolder { static class ForumViewHolder extends RecyclerView.ViewHolder {

View File

@@ -20,6 +20,7 @@ import org.briarproject.android.api.AndroidNotificationManager;
import org.briarproject.android.fragment.BaseEventFragment; import org.briarproject.android.fragment.BaseEventFragment;
import org.briarproject.android.sharing.InvitationsForumActivity; import org.briarproject.android.sharing.InvitationsForumActivity;
import org.briarproject.android.view.BriarRecyclerView; import org.briarproject.android.view.BriarRecyclerView;
import org.briarproject.api.clients.MessageTracker.GroupCount;
import org.briarproject.api.db.DbException; import org.briarproject.api.db.DbException;
import org.briarproject.api.db.NoSuchGroupException; import org.briarproject.api.db.NoSuchGroupException;
import org.briarproject.api.event.ContactRemovedEvent; import org.briarproject.api.event.ContactRemovedEvent;
@@ -47,11 +48,8 @@ import static java.util.logging.Level.WARNING;
public class ForumListFragment extends BaseEventFragment implements public class ForumListFragment extends BaseEventFragment implements
OnClickListener { OnClickListener {
public final static String TAG = "ForumListFragment"; public final static String TAG = ForumListFragment.class.getName();
private final static Logger LOG = Logger.getLogger(TAG);
private static final Logger LOG =
Logger.getLogger(ForumListFragment.class.getName());
private BriarRecyclerView list; private BriarRecyclerView list;
private ForumListAdapter adapter; private ForumListAdapter adapter;
@@ -118,7 +116,7 @@ public class ForumListFragment extends BaseEventFragment implements
notificationManager.blockAllForumPostNotifications(); notificationManager.blockAllForumPostNotifications();
notificationManager.clearAllForumPostNotifications(); notificationManager.clearAllForumPostNotifications();
loadForumHeaders(); loadForums();
loadAvailableForums(); loadAvailableForums();
list.startPeriodicUpdate(); list.startPeriodicUpdate();
} }
@@ -153,7 +151,7 @@ public class ForumListFragment extends BaseEventFragment implements
} }
} }
private void loadForumHeaders() { private void loadForums() {
listener.runOnDbThread(new Runnable() { listener.runOnDbThread(new Runnable() {
@Override @Override
public void run() { public void run() {
@@ -163,14 +161,14 @@ public class ForumListFragment extends BaseEventFragment implements
Collection<ForumListItem> forums = new ArrayList<>(); Collection<ForumListItem> forums = new ArrayList<>();
for (Forum f : forumManager.getForums()) { for (Forum f : forumManager.getForums()) {
try { try {
Collection<ForumPostHeader> headers = GroupCount count =
forumManager.getPostHeaders(f.getId()); forumManager.getGroupCount(f.getId());
forums.add(new ForumListItem(f, headers)); forums.add(new ForumListItem(f, count));
} catch (NoSuchGroupException e) { } catch (NoSuchGroupException e) {
// Continue // Continue
} }
} }
displayForumHeaders(forums); displayForums(forums);
long duration = System.currentTimeMillis() - now; long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO)) if (LOG.isLoggable(INFO))
LOG.info("Full load took " + duration + " ms"); LOG.info("Full load took " + duration + " ms");
@@ -182,7 +180,7 @@ public class ForumListFragment extends BaseEventFragment implements
}); });
} }
private void displayForumHeaders(final Collection<ForumListItem> forums) { private void displayForums(final Collection<ForumListItem> forums) {
listener.runOnUiThread(new Runnable() { listener.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
@@ -238,7 +236,7 @@ public class ForumListFragment extends BaseEventFragment implements
GroupAddedEvent g = (GroupAddedEvent) e; GroupAddedEvent g = (GroupAddedEvent) e;
if (g.getGroup().getClientId().equals(forumManager.getClientId())) { if (g.getGroup().getClientId().equals(forumManager.getClientId())) {
LOG.info("Forum added, reloading forums"); LOG.info("Forum added, reloading forums");
loadForumHeaders(); loadForums();
} }
} else if (e instanceof GroupRemovedEvent) { } else if (e instanceof GroupRemovedEvent) {
GroupRemovedEvent g = (GroupRemovedEvent) e; GroupRemovedEvent g = (GroupRemovedEvent) e;
@@ -248,39 +246,23 @@ public class ForumListFragment extends BaseEventFragment implements
} }
} else if (e instanceof ForumPostReceivedEvent) { } else if (e instanceof ForumPostReceivedEvent) {
ForumPostReceivedEvent m = (ForumPostReceivedEvent) e; ForumPostReceivedEvent m = (ForumPostReceivedEvent) e;
LOG.info("Forum post added, reloading"); LOG.info("Forum post added, updating...");
loadForumHeaders(m.getGroupId()); updateItem(m.getGroupId(), m.getForumPostHeader());
} else if (e instanceof ForumInvitationReceivedEvent) { } else if (e instanceof ForumInvitationReceivedEvent) {
loadAvailableForums(); loadAvailableForums();
} }
} }
private void loadForumHeaders(final GroupId g) { private void updateItem(final GroupId g, final ForumPostHeader m) {
listener.runOnDbThread(new Runnable() {
@Override
public void run() {
try {
long now = System.currentTimeMillis();
Forum f = forumManager.getForum(g);
Collection<ForumPostHeader> headers =
forumManager.getPostHeaders(g);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Partial load took " + duration + " ms");
updateForum(new ForumListItem(f, headers));
} catch (DbException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
}
});
}
private void updateForum(final ForumListItem item) {
listener.runOnUiThread(new Runnable() { listener.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
adapter.updateItem(item); int position = adapter.findItemPosition(g);
ForumListItem item = adapter.getItemAt(position);
if (item != null) {
item.addHeader(m);
adapter.updateItemAt(position, item);
}
} }
}); });
} }
@@ -289,7 +271,8 @@ public class ForumListFragment extends BaseEventFragment implements
listener.runOnUiThread(new Runnable() { listener.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
ForumListItem item = adapter.findItem(g); int position = adapter.findItemPosition(g);
ForumListItem item = adapter.getItemAt(position);
if (item != null) adapter.remove(item); if (item != null) adapter.remove(item);
} }
}); });

View File

@@ -1,41 +1,26 @@
package org.briarproject.android.forum; package org.briarproject.android.forum;
import org.briarproject.api.clients.MessageTracker.GroupCount;
import org.briarproject.api.forum.Forum; import org.briarproject.api.forum.Forum;
import org.briarproject.api.forum.ForumPostHeader; import org.briarproject.api.forum.ForumPostHeader;
import java.util.Collection;
// This class is NOT thread-safe // This class is NOT thread-safe
class ForumListItem { class ForumListItem {
private final Forum forum; private final Forum forum;
private final boolean empty; private long postCount, unread, timestamp;
private final int postCount;
private final long timestamp;
private final int unread;
ForumListItem(Forum forum, Collection<ForumPostHeader> headers) { ForumListItem(Forum forum, GroupCount count) {
this.forum = forum; this.forum = forum;
empty = headers.isEmpty(); this.postCount = count.getMsgCount();
if (empty) { this.unread = count.getUnreadCount();
postCount = 0; this.timestamp = count.getLatestMsgTime();
timestamp = 0; }
unread = 0;
} else { void addHeader(ForumPostHeader h) {
ForumPostHeader newest = null; postCount++;
long timestamp = -1; if (!h.isRead()) unread++;
int unread = 0; if (h.getTimestamp() > timestamp) timestamp = h.getTimestamp();
for (ForumPostHeader 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;
}
} }
Forum getForum() { Forum getForum() {
@@ -43,10 +28,10 @@ class ForumListItem {
} }
boolean isEmpty() { boolean isEmpty() {
return empty; return postCount == 0;
} }
int getPostCount() { long getPostCount() {
return postCount; return postCount;
} }
@@ -54,7 +39,7 @@ class ForumListItem {
return timestamp; return timestamp;
} }
int getUnreadCount() { long getUnreadCount() {
return unread; return unread;
} }
} }

View File

@@ -24,7 +24,7 @@ public class TextAvatarView extends FrameLayout {
final private AppCompatTextView character; final private AppCompatTextView character;
final private CircleImageView background; final private CircleImageView background;
final private TextView badge; final private TextView badge;
private int unreadCount; private long unreadCount;
public TextAvatarView(Context context, @Nullable AttributeSet attrs) { public TextAvatarView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs); super(context, attrs);
@@ -48,13 +48,19 @@ public class TextAvatarView extends FrameLayout {
} }
public void setUnreadCount(int count) { public void setUnreadCount(int count) {
setUnreadCount((long) count);
}
public void setUnreadCount(long count) {
this.unreadCount = count;
if (count > 0) { if (count > 0) {
this.unreadCount = count;
badge.setBackgroundResource(R.drawable.bubble); badge.setBackgroundResource(R.drawable.bubble);
badge.setText(String.valueOf(count)); badge.setText(String.valueOf(count));
badge.setTextColor(ContextCompat.getColor(getContext(), R.color.briar_text_primary_inverse)); badge.setTextColor(ContextCompat.getColor(getContext(),
R.color.briar_text_primary_inverse));
badge.setVisibility(VISIBLE); badge.setVisibility(VISIBLE);
} else { } else {
badge.setText("");
badge.setVisibility(INVISIBLE); badge.setVisibility(INVISIBLE);
} }
} }
@@ -63,11 +69,13 @@ public class TextAvatarView extends FrameLayout {
if (problem) { if (problem) {
badge.setBackgroundResource(R.drawable.bubble_problem); badge.setBackgroundResource(R.drawable.bubble_problem);
badge.setText("!"); badge.setText("!");
badge.setTextColor(ContextCompat.getColor(getContext(), R.color.briar_primary)); badge.setTextColor(ContextCompat
.getColor(getContext(), R.color.briar_primary));
badge.setVisibility(VISIBLE); badge.setVisibility(VISIBLE);
} else if (unreadCount > 0) { } else if (unreadCount > 0) {
setUnreadCount(unreadCount); setUnreadCount(unreadCount);
} else { } else {
badge.setText("");
badge.setVisibility(INVISIBLE); badge.setVisibility(INVISIBLE);
} }
} }