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(); finishOnUiThread();
} }
} else if (e instanceof MessageAddedEvent) { } else if (e instanceof MessageAddedEvent) {
GroupId g = ((MessageAddedEvent) e).getGroupId(); MessageAddedEvent mEvent = (MessageAddedEvent) e;
GroupId g = mEvent.getGroupId();
if (g.equals(groupId)) { 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"); LOG.info("Message added, reloading");
// TODO: find a way of not needing to reload the entire // TODO: get and add the ConversationItem here to prevent
// conversation just because one message was added // reloading the entire conversation
loadHeaders(); loadHeaders();
} }
} else if (e instanceof MessagesSentEvent) { } 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) { public void onClick(View view) {
markMessagesRead();
String message = content.getText().toString(); String message = content.getText().toString();
if (message.equals("")) return; if (message.equals("")) return;
long timestamp = System.currentTimeMillis(); long timestamp = System.currentTimeMillis();
@@ -397,10 +441,9 @@ public class ConversationActivity extends BriarActivity
private long getMinTimestampForNewMessage() { private long getMinTimestampForNewMessage() {
// Don't use an earlier timestamp than the newest message // Don't use an earlier timestamp than the newest message
long timestamp = 0; long timestamp = 0;
int count = adapter.getItemCount(); ConversationItem item = adapter.getLastItem();
for (int i = 0; i < count; i++) { if (item != null) {
long t = adapter.getItem(i).getHeader().getTimestamp(); timestamp = item.getHeader().getTimestamp();
if (t > timestamp) timestamp = t;
} }
return timestamp + 1; return timestamp + 1;
} }

View File

@@ -159,6 +159,14 @@ class ConversationAdapter extends
return messages.get(position); 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) { public void add(final ConversationItem message) {
this.messages.add(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.contact.ContactId;
import org.briarproject.api.sync.GroupId; 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. */ /** An event that is broadcast when a message is added to the database. */
public class MessageAddedEvent extends Event { public class MessageAddedEvent extends Event {
private final GroupId groupId; private final Message message;
private final ContactId contactId; private final ContactId contactId;
public MessageAddedEvent(GroupId groupId, ContactId contactId) { public MessageAddedEvent(Message message, ContactId contactId) {
this.groupId = groupId; this.message = message;
this.contactId = contactId; 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. */ /** Returns the ID of the group to which the message belongs. */
public GroupId getGroupId() { public GroupId getGroupId() {
return groupId; return message.getGroup().getId();
} }
/** /**

View File

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