mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 05:39:53 +01:00
Make GroupItem immutable and introduce copy constructors
This commit is contained in:
@@ -8,17 +8,19 @@ import org.briarproject.briar.api.client.MessageTracker.GroupCount;
|
|||||||
import org.briarproject.briar.api.privategroup.GroupMessageHeader;
|
import org.briarproject.briar.api.privategroup.GroupMessageHeader;
|
||||||
import org.briarproject.briar.api.privategroup.PrivateGroup;
|
import org.briarproject.briar.api.privategroup.PrivateGroup;
|
||||||
|
|
||||||
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
// This class is not thread-safe
|
@Immutable
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
class GroupItem implements Comparable<GroupItem> {
|
class GroupItem implements Comparable<GroupItem> {
|
||||||
|
|
||||||
private final PrivateGroup privateGroup;
|
private final PrivateGroup privateGroup;
|
||||||
private final AuthorInfo authorInfo;
|
private final AuthorInfo authorInfo;
|
||||||
private int messageCount, unreadCount;
|
private final int messageCount, unreadCount;
|
||||||
private long timestamp;
|
private final long timestamp;
|
||||||
private boolean dissolved;
|
private final boolean dissolved;
|
||||||
|
|
||||||
GroupItem(PrivateGroup privateGroup, AuthorInfo authorInfo,
|
GroupItem(PrivateGroup privateGroup, AuthorInfo authorInfo,
|
||||||
GroupCount count, boolean dissolved) {
|
GroupCount count, boolean dissolved) {
|
||||||
@@ -30,23 +32,22 @@ class GroupItem implements Comparable<GroupItem> {
|
|||||||
this.dissolved = dissolved;
|
this.dissolved = dissolved;
|
||||||
}
|
}
|
||||||
|
|
||||||
GroupItem(GroupItem item) {
|
GroupItem(GroupItem item, GroupMessageHeader header) {
|
||||||
|
this.privateGroup = item.privateGroup;
|
||||||
|
this.authorInfo = item.authorInfo;
|
||||||
|
this.messageCount = item.messageCount + 1;
|
||||||
|
this.unreadCount = item.unreadCount + (header.isRead() ? 0 : 1);
|
||||||
|
this.timestamp = Math.max(header.getTimestamp(), item.timestamp);
|
||||||
|
this.dissolved = item.dissolved;
|
||||||
|
}
|
||||||
|
|
||||||
|
GroupItem(GroupItem item, boolean isDissolved) {
|
||||||
this.privateGroup = item.privateGroup;
|
this.privateGroup = item.privateGroup;
|
||||||
this.authorInfo = item.authorInfo;
|
this.authorInfo = item.authorInfo;
|
||||||
this.messageCount = item.messageCount;
|
this.messageCount = item.messageCount;
|
||||||
this.unreadCount = item.unreadCount;
|
this.unreadCount = item.unreadCount;
|
||||||
this.timestamp = item.timestamp;
|
this.timestamp = item.timestamp;
|
||||||
this.dissolved = item.dissolved;
|
this.dissolved = isDissolved;
|
||||||
}
|
|
||||||
|
|
||||||
void addMessageHeader(GroupMessageHeader header) {
|
|
||||||
messageCount++;
|
|
||||||
if (header.getTimestamp() > timestamp) {
|
|
||||||
timestamp = header.getTimestamp();
|
|
||||||
}
|
|
||||||
if (!header.isRead()) {
|
|
||||||
unreadCount++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GroupId getId() {
|
GroupId getId() {
|
||||||
@@ -85,8 +86,9 @@ class GroupItem implements Comparable<GroupItem> {
|
|||||||
return dissolved;
|
return dissolved;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setDissolved() {
|
@Override
|
||||||
dissolved = true;
|
public int hashCode() {
|
||||||
|
return getId().hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -171,11 +171,7 @@ class GroupListViewModel extends DbViewModel implements EventListener {
|
|||||||
GroupId g = header.getGroupId();
|
GroupId g = header.getGroupId();
|
||||||
List<GroupItem> list = updateListItem(groupItems,
|
List<GroupItem> list = updateListItem(groupItems,
|
||||||
itemToTest -> itemToTest.getId().equals(g),
|
itemToTest -> itemToTest.getId().equals(g),
|
||||||
itemToUpdate -> {
|
itemToUpdate -> new GroupItem(itemToUpdate, header));
|
||||||
GroupItem newItem = new GroupItem(itemToUpdate);
|
|
||||||
newItem.addMessageHeader(header);
|
|
||||||
return newItem;
|
|
||||||
});
|
|
||||||
if (list == null) return;
|
if (list == null) return;
|
||||||
// re-sort as the order of items may have changed
|
// re-sort as the order of items may have changed
|
||||||
Collections.sort(list);
|
Collections.sort(list);
|
||||||
@@ -186,11 +182,7 @@ class GroupListViewModel extends DbViewModel implements EventListener {
|
|||||||
private void onGroupDissolved(GroupId groupId) {
|
private void onGroupDissolved(GroupId groupId) {
|
||||||
List<GroupItem> list = updateListItem(groupItems,
|
List<GroupItem> list = updateListItem(groupItems,
|
||||||
itemToTest -> itemToTest.getId().equals(groupId),
|
itemToTest -> itemToTest.getId().equals(groupId),
|
||||||
itemToUpdate -> {
|
itemToUpdate -> new GroupItem(itemToUpdate, true));
|
||||||
GroupItem newItem = new GroupItem(itemToUpdate);
|
|
||||||
newItem.setDissolved();
|
|
||||||
return newItem;
|
|
||||||
});
|
|
||||||
if (list == null) return;
|
if (list == null) return;
|
||||||
groupItems.setValue(new LiveResult<>(list));
|
groupItems.setValue(new LiveResult<>(list));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user