show first unread message on top after reinit

This commit is contained in:
Daniel Gultsch 2016-05-22 17:52:27 +02:00
parent 33218ec32a
commit e5b8302fd9
2 changed files with 46 additions and 24 deletions

View file

@ -96,6 +96,21 @@ public class Conversation extends AbstractEntity implements Blockable {
this.messagesLeftOnServer = value; this.messagesLeftOnServer = value;
} }
public Message getFirstUnreadMessage() {
Message first = null;
synchronized (this.messages) {
for (int i = messages.size() - 1; i >= 0; --i) {
if (messages.get(i).isRead()) {
return first;
} else {
first = messages.get(i);
}
}
}
return first;
}
public Message findUnsentMessageWithUuid(String uuid) { public Message findUnsentMessageWithUuid(String uuid) {
synchronized(this.messages) { synchronized(this.messages) {
for (final Message message : this.messages) { for (final Message message : this.messages) {

View file

@ -123,27 +123,6 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
} }
private int getIndexOf(String uuid, List<Message> messages) {
if (uuid == null) {
return messages.size() - 1;
}
for(int i = 0; i < messages.size(); ++i) {
if (uuid.equals(messages.get(i).getUuid())) {
return i;
} else {
Message next = messages.get(i);
while(next != null && next.wasMergedIntoPrevious()) {
if (uuid.equals(next.getUuid())) {
return i;
}
next = next.next();
}
}
}
return 0;
}
@Override @Override
public void onScroll(AbsListView view, int firstVisibleItem, public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) { int visibleItemCount, int totalItemCount) {
@ -212,6 +191,28 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
} }
} }
}; };
private int getIndexOf(String uuid, List<Message> messages) {
if (uuid == null) {
return messages.size() - 1;
}
for(int i = 0; i < messages.size(); ++i) {
if (uuid.equals(messages.get(i).getUuid())) {
return i;
} else {
Message next = messages.get(i);
while(next != null && next.wasMergedIntoPrevious()) {
if (uuid.equals(next.getUuid())) {
return i;
}
next = next.next();
}
}
}
return 0;
}
private final int KEYCHAIN_UNLOCK_NOT_REQUIRED = 0; private final int KEYCHAIN_UNLOCK_NOT_REQUIRED = 0;
private final int KEYCHAIN_UNLOCK_REQUIRED = 1; private final int KEYCHAIN_UNLOCK_REQUIRED = 1;
private final int KEYCHAIN_UNLOCK_PENDING = 2; private final int KEYCHAIN_UNLOCK_PENDING = 2;
@ -796,9 +797,15 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
this.messagesView.setAdapter(messageListAdapter); this.messagesView.setAdapter(messageListAdapter);
updateMessages(); updateMessages();
this.messagesLoaded = true; this.messagesLoaded = true;
int size = this.messageList.size(); synchronized (this.messageList) {
if (size > 0) { final Message first = conversation.getFirstUnreadMessage();
messagesView.setSelection(size - 1); final int pos;
if (first == null) {
pos = Math.max(0,this.messageList.size() - 1);
} else {
pos = getIndexOf(first.getUuid(), this.messageList);
}
messagesView.setSelection(pos);
} }
} }