mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 21:59:54 +01:00
Use start/stop lifecycle callbacks rather than pause/resume.
Also fixed a couple of bugs.
This commit is contained in:
@@ -27,8 +27,8 @@ import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class GroupControllerImpl
|
||||
extends ThreadListControllerImpl<PrivateGroup, GroupMessageItem, GroupMessageHeader, GroupMessage>
|
||||
public class GroupControllerImpl extends
|
||||
ThreadListControllerImpl<PrivateGroup, GroupMessageItem, GroupMessageHeader, GroupMessage>
|
||||
implements GroupController {
|
||||
|
||||
private static final Logger LOG =
|
||||
@@ -48,8 +48,8 @@ public class GroupControllerImpl
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResume() {
|
||||
super.onActivityResume();
|
||||
public void onActivityStart() {
|
||||
super.onActivityStart();
|
||||
// TODO: Add new notification manager methods for private groups
|
||||
}
|
||||
|
||||
@@ -101,9 +101,8 @@ public class GroupControllerImpl
|
||||
@Override
|
||||
protected GroupMessage createLocalMessage(String body, long timestamp,
|
||||
@Nullable MessageId parentId, LocalAuthor author) {
|
||||
return privateGroupManager
|
||||
.createLocalMessage(getGroupId(), body, timestamp, parentId,
|
||||
author);
|
||||
return privateGroupManager.createLocalMessage(getGroupId(), body,
|
||||
timestamp, parentId, author);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,56 +1,51 @@
|
||||
package org.briarproject.android.privategroup.list;
|
||||
|
||||
import org.briarproject.api.clients.MessageTracker.GroupCount;
|
||||
import org.briarproject.api.identity.Author;
|
||||
import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.api.privategroup.GroupMessageHeader;
|
||||
import org.briarproject.api.privategroup.PrivateGroup;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
// This class is not thread-safe
|
||||
@NotNullByDefault
|
||||
class GroupItem {
|
||||
|
||||
private final PrivateGroup privateGroup;
|
||||
private int messageCount;
|
||||
private long lastUpdate;
|
||||
private int unreadCount;
|
||||
private int messageCount, unreadCount;
|
||||
private long timestamp;
|
||||
private boolean dissolved;
|
||||
|
||||
GroupItem(@NotNull PrivateGroup privateGroup, int messageCount,
|
||||
long lastUpdate, int unreadCount, boolean dissolved) {
|
||||
|
||||
GroupItem(PrivateGroup privateGroup, GroupCount count, boolean dissolved) {
|
||||
this.privateGroup = privateGroup;
|
||||
this.messageCount = messageCount;
|
||||
this.lastUpdate = lastUpdate;
|
||||
this.unreadCount = unreadCount;
|
||||
this.messageCount = count.getMsgCount();
|
||||
this.unreadCount = count.getUnreadCount();
|
||||
this.timestamp = count.getLatestMsgTime();
|
||||
this.dissolved = dissolved;
|
||||
}
|
||||
|
||||
void addMessageHeader(GroupMessageHeader header) {
|
||||
messageCount++;
|
||||
if (header.getTimestamp() > lastUpdate) {
|
||||
lastUpdate = header.getTimestamp();
|
||||
if (header.getTimestamp() > timestamp) {
|
||||
timestamp = header.getTimestamp();
|
||||
}
|
||||
if (!header.isRead()) {
|
||||
unreadCount++;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
PrivateGroup getPrivateGroup() {
|
||||
return privateGroup;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
GroupId getId() {
|
||||
return privateGroup.getId();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
Author getCreator() {
|
||||
return privateGroup.getAuthor();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
String getName() {
|
||||
return privateGroup.getName();
|
||||
}
|
||||
@@ -63,8 +58,8 @@ class GroupItem {
|
||||
return messageCount;
|
||||
}
|
||||
|
||||
long getLastUpdate() {
|
||||
return lastUpdate;
|
||||
long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
int getUnreadCount() {
|
||||
|
||||
@@ -38,7 +38,7 @@ class GroupListAdapter extends BriarAdapter<GroupItem, GroupViewHolder> {
|
||||
public int compare(GroupItem a, GroupItem b) {
|
||||
if (a == b) return 0;
|
||||
// The group with the latest message comes first
|
||||
long aTime = a.getLastUpdate(), bTime = b.getLastUpdate();
|
||||
long aTime = a.getTimestamp(), bTime = b.getTimestamp();
|
||||
if (aTime > bTime) return -1;
|
||||
if (aTime < bTime) return 1;
|
||||
// Break ties by group name
|
||||
@@ -50,7 +50,7 @@ class GroupListAdapter extends BriarAdapter<GroupItem, GroupViewHolder> {
|
||||
@Override
|
||||
public boolean areContentsTheSame(GroupItem a, GroupItem b) {
|
||||
return a.getMessageCount() == b.getMessageCount() &&
|
||||
a.getLastUpdate() == b.getLastUpdate() &&
|
||||
a.getTimestamp() == b.getTimestamp() &&
|
||||
a.getUnreadCount() == b.getUnreadCount() &&
|
||||
a.isDissolved() == b.isDissolved();
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ public interface GroupListController extends DbController {
|
||||
ResultExceptionHandler<Void, DbException> result);
|
||||
|
||||
interface GroupListListener extends DestroyableContext {
|
||||
|
||||
@UiThread
|
||||
void onGroupMessageAdded(GroupMessageHeader header);
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import org.briarproject.android.controller.handler.ResultExceptionHandler;
|
||||
import org.briarproject.api.clients.MessageTracker.GroupCount;
|
||||
import org.briarproject.api.db.DatabaseExecutor;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.db.NoSuchGroupException;
|
||||
import org.briarproject.api.event.Event;
|
||||
import org.briarproject.api.event.EventBus;
|
||||
import org.briarproject.api.event.EventListener;
|
||||
@@ -16,6 +17,7 @@ import org.briarproject.api.event.GroupMessageAddedEvent;
|
||||
import org.briarproject.api.event.GroupRemovedEvent;
|
||||
import org.briarproject.api.identity.IdentityManager;
|
||||
import org.briarproject.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.api.privategroup.GroupMessageHeader;
|
||||
import org.briarproject.api.privategroup.PrivateGroup;
|
||||
import org.briarproject.api.privategroup.PrivateGroupManager;
|
||||
import org.briarproject.api.sync.ClientId;
|
||||
@@ -29,6 +31,7 @@ import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
public class GroupListControllerImpl extends DbControllerImpl
|
||||
@@ -81,59 +84,77 @@ public class GroupListControllerImpl extends DbControllerImpl
|
||||
@CallSuper
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof GroupMessageAddedEvent) {
|
||||
final GroupMessageAddedEvent m = (GroupMessageAddedEvent) e;
|
||||
LOG.info("New group message added");
|
||||
listener.runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
listener.onGroupMessageAdded(m.getHeader());
|
||||
}
|
||||
});
|
||||
GroupMessageAddedEvent g = (GroupMessageAddedEvent) e;
|
||||
LOG.info("Private group message added");
|
||||
onGroupMessageAdded(g.getHeader());
|
||||
} else if (e instanceof GroupAddedEvent) {
|
||||
final GroupAddedEvent gae = (GroupAddedEvent) e;
|
||||
ClientId id = gae.getGroup().getClientId();
|
||||
GroupAddedEvent g = (GroupAddedEvent) e;
|
||||
ClientId id = g.getGroup().getClientId();
|
||||
if (id.equals(groupManager.getClientId())) {
|
||||
LOG.info("Private group added");
|
||||
listener.runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
listener.onGroupAdded(gae.getGroup().getId());
|
||||
}
|
||||
});
|
||||
onGroupAdded(g.getGroup().getId());
|
||||
}
|
||||
} else if (e instanceof GroupRemovedEvent) {
|
||||
final GroupRemovedEvent gre = (GroupRemovedEvent) e;
|
||||
ClientId id = gre.getGroup().getClientId();
|
||||
GroupRemovedEvent g = (GroupRemovedEvent) e;
|
||||
ClientId id = g.getGroup().getClientId();
|
||||
if (id.equals(groupManager.getClientId())) {
|
||||
LOG.info("Private group removed");
|
||||
listener.runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
listener.onGroupRemoved(gre.getGroup().getId());
|
||||
}
|
||||
});
|
||||
onGroupRemoved(g.getGroup().getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void onGroupMessageAdded(final GroupMessageHeader h) {
|
||||
listener.runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
listener.onGroupMessageAdded(h);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void onGroupAdded(final GroupId g) {
|
||||
listener.runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
listener.onGroupAdded(g);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void onGroupRemoved(final GroupId g) {
|
||||
listener.runOnUiThreadUnlessDestroyed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
listener.onGroupRemoved(g);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadGroups(
|
||||
final ResultExceptionHandler<Collection<GroupItem>, DbException> handler) {
|
||||
runOnDbThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
LOG.info("Loading groups from database...");
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
Collection<PrivateGroup> groups =
|
||||
groupManager.getPrivateGroups();
|
||||
List<GroupItem> items = new ArrayList<>(groups.size());
|
||||
for (PrivateGroup g : groups) {
|
||||
GroupCount c = groupManager.getGroupCount(g.getId());
|
||||
boolean dissolved = groupManager.isDissolved(g.getId());
|
||||
items.add(new GroupItem(g, c.getMsgCount(),
|
||||
c.getLatestMsgTime(), c.getUnreadCount(),
|
||||
dissolved));
|
||||
try {
|
||||
GroupId id = g.getId();
|
||||
GroupCount count = groupManager.getGroupCount(id);
|
||||
boolean dissolved = groupManager.isDissolved(id);
|
||||
items.add(new GroupItem(g, count, dissolved));
|
||||
} catch (NoSuchGroupException e) {
|
||||
// Continue
|
||||
}
|
||||
}
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Loading groups took " + duration + " ms");
|
||||
handler.onResult(items);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING))
|
||||
@@ -150,9 +171,13 @@ public class GroupListControllerImpl extends DbControllerImpl
|
||||
runOnDbThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
LOG.info("Removing group from database...");
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
groupManager.removePrivateGroup(g);
|
||||
long duration = System.currentTimeMillis() - now;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Removing group took " + duration + " ms");
|
||||
handler.onResult(null);
|
||||
} catch (DbException e) {
|
||||
if (LOG.isLoggable(WARNING))
|
||||
LOG.log(WARNING, e.toString(), e);
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.briarproject.android.privategroup.list;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.UiThread;
|
||||
@@ -16,7 +15,6 @@ import org.briarproject.R;
|
||||
import org.briarproject.android.ActivityComponent;
|
||||
import org.briarproject.android.controller.handler.UiResultExceptionHandler;
|
||||
import org.briarproject.android.fragment.BaseFragment;
|
||||
import org.briarproject.android.invitation.AddContactActivity;
|
||||
import org.briarproject.android.privategroup.list.GroupListController.GroupListListener;
|
||||
import org.briarproject.android.privategroup.list.GroupViewHolder.OnGroupRemoveClickListener;
|
||||
import org.briarproject.android.view.BriarRecyclerView;
|
||||
@@ -152,12 +150,9 @@ public class GroupListFragment extends BaseFragment implements
|
||||
new UiResultExceptionHandler<Collection<GroupItem>, DbException>(
|
||||
listener) {
|
||||
@Override
|
||||
public void onResultUi(Collection<GroupItem> result) {
|
||||
if (result.isEmpty()) {
|
||||
list.showData();
|
||||
} else {
|
||||
adapter.addAll(result);
|
||||
}
|
||||
public void onResultUi(Collection<GroupItem> groups) {
|
||||
if (groups.isEmpty()) list.showData();
|
||||
else adapter.addAll(groups);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -88,7 +88,7 @@ class GroupViewHolder extends RecyclerView.ViewHolder {
|
||||
postCount.setTextColor(
|
||||
getColor(ctx, R.color.briar_text_secondary));
|
||||
|
||||
long lastUpdate = group.getLastUpdate();
|
||||
long lastUpdate = group.getTimestamp();
|
||||
date.setText(AndroidUtils.formatDate(ctx, lastUpdate));
|
||||
date.setVisibility(VISIBLE);
|
||||
avatar.setProblem(false);
|
||||
|
||||
Reference in New Issue
Block a user