Do not show messages as unread when the conversation is open

* Only show them as unread when they arrive out of order
* Mark all messages as read when sending a message
This commit is contained in:
Torsten Grote
2016-01-13 19:01:41 -02:00
parent 36a850d83e
commit 9aa1bbd1ed
4 changed files with 70 additions and 15 deletions

View File

@@ -331,11 +331,21 @@ public class ConversationActivity extends BriarActivity
finishOnUiThread();
}
} else if (e instanceof MessageAddedEvent) {
GroupId g = ((MessageAddedEvent) e).getGroupId();
MessageAddedEvent mEvent = (MessageAddedEvent) e;
GroupId g = mEvent.getGroupId();
if (g.equals(groupId)) {
// mark new incoming messages as read directly
if (mEvent.getContactId() != null) {
ConversationItem item = adapter.getLastItem();
if (item != null) {
markIncomingMessageRead(mEvent.getMessage(),
item.getHeader().getTimestamp());
}
}
LOG.info("Message added, reloading");
// TODO: find a way of not needing to reload the entire
// conversation just because one message was added
// TODO: get and add the ConversationItem here to prevent
// reloading the entire conversation
loadHeaders();
}
} else if (e instanceof MessagesSentEvent) {
@@ -384,7 +394,41 @@ public class ConversationActivity extends BriarActivity
});
}
private void markIncomingMessageRead(final Message m,
final long lastMsgTime) {
// stop here if message is older than latest message we have
long newMsgTime = m.getTimestamp();
if (newMsgTime < lastMsgTime) return;
runOnDbThread(new Runnable() {
public void run() {
try {
// mark messages as read, because is latest
messagingManager.setReadFlag(m.getId(), true);
showIncomingMessageRead();
} catch (DbException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
}
// TODO else: smooth-scroll up to unread messages if out of view
});
}
private void showIncomingMessageRead() {
runOnUiThread(new Runnable() {
public void run() {
// this is only called from markIncomingMessageRead()
// so we can assume that it was the last message that changed
adapter.notifyItemChanged(adapter.getItemCount() - 1);
}
});
}
public void onClick(View view) {
markMessagesRead();
String message = content.getText().toString();
if (message.equals("")) return;
long timestamp = System.currentTimeMillis();
@@ -397,10 +441,9 @@ public class ConversationActivity extends BriarActivity
private long getMinTimestampForNewMessage() {
// Don't use an earlier timestamp than the newest message
long timestamp = 0;
int count = adapter.getItemCount();
for (int i = 0; i < count; i++) {
long t = adapter.getItem(i).getHeader().getTimestamp();
if (t > timestamp) timestamp = t;
ConversationItem item = adapter.getLastItem();
if (item != null) {
timestamp = item.getHeader().getTimestamp();
}
return timestamp + 1;
}

View File

@@ -159,6 +159,14 @@ class ConversationAdapter extends
return messages.get(position);
}
public ConversationItem getLastItem() {
if (messages.size() > 0) {
return messages.get(messages.size() - 1);
} else {
return null;
}
}
public void add(final ConversationItem message) {
this.messages.add(message);
}

View File

@@ -2,21 +2,27 @@ package org.briarproject.api.event;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
/** An event that is broadcast when a message is added to the database. */
public class MessageAddedEvent extends Event {
private final GroupId groupId;
private final Message message;
private final ContactId contactId;
public MessageAddedEvent(GroupId groupId, ContactId contactId) {
this.groupId = groupId;
public MessageAddedEvent(Message message, ContactId contactId) {
this.message = message;
this.contactId = contactId;
}
/** Returns the message that was added. */
public Message getMessage() {
return message;
}
/** Returns the ID of the group to which the message belongs. */
public GroupId getGroupId() {
return groupId;
return message.getGroup().getId();
}
/**

View File

@@ -223,8 +223,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
lock.writeLock().unlock();
}
if (!duplicate && subscribed) {
GroupId g = m.getGroup().getId();
eventBus.broadcast(new MessageAddedEvent(g, null));
eventBus.broadcast(new MessageAddedEvent(m, null));
}
}
@@ -1053,8 +1052,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
if (visible) {
if (!duplicate) {
GroupId g = m.getGroup().getId();
eventBus.broadcast(new MessageAddedEvent(g, c));
eventBus.broadcast(new MessageAddedEvent(m, c));
}
eventBus.broadcast(new MessageToAckEvent(c));
}