From 7c35f28633978e486dd6a181d75ab3902ad4efcd Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 27 Apr 2019 15:24:37 +0200 Subject: [PATCH] do not synchronize twice when adding status and date bubbles --- .../ui/ConversationFragment.java | 186 +++++++++--------- 1 file changed, 89 insertions(+), 97 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 72bcb3052..2e91b750e 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -2279,110 +2279,102 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke this.binding.textSendButton.setImageResource(SendButtonTool.getSendButtonImageResource(getActivity(), action, status)); } - protected void updateDateSeparators() { - synchronized (this.messageList) { - DateSeparator.addAll(this.messageList); - } - } - protected void updateStatusMessages() { - updateDateSeparators(); - synchronized (this.messageList) { - if (showLoadMoreMessages(conversation)) { - this.messageList.add(0, Message.createLoadMoreMessage(conversation)); - } - if (conversation.getMode() == Conversation.MODE_SINGLE) { - ChatState state = conversation.getIncomingChatState(); - if (state == ChatState.COMPOSING) { - this.messageList.add(Message.createStatusMessage(conversation, getString(R.string.contact_is_typing, conversation.getName()))); - } else if (state == ChatState.PAUSED) { - this.messageList.add(Message.createStatusMessage(conversation, getString(R.string.contact_has_stopped_typing, conversation.getName()))); - } else { - for (int i = this.messageList.size() - 1; i >= 0; --i) { - final Message message = this.messageList.get(i); - if (message.getType() != Message.TYPE_STATUS) { - if (message.getStatus() == Message.STATUS_RECEIVED) { - return; - } else { - if (message.getStatus() == Message.STATUS_SEND_DISPLAYED) { - this.messageList.add(i + 1, - Message.createStatusMessage(conversation, getString(R.string.contact_has_read_up_to_this_point, conversation.getName()))); - return; - } - } - } - } - } + DateSeparator.addAll(this.messageList); + if (showLoadMoreMessages(conversation)) { + this.messageList.add(0, Message.createLoadMoreMessage(conversation)); + } + if (conversation.getMode() == Conversation.MODE_SINGLE) { + ChatState state = conversation.getIncomingChatState(); + if (state == ChatState.COMPOSING) { + this.messageList.add(Message.createStatusMessage(conversation, getString(R.string.contact_is_typing, conversation.getName()))); + } else if (state == ChatState.PAUSED) { + this.messageList.add(Message.createStatusMessage(conversation, getString(R.string.contact_has_stopped_typing, conversation.getName()))); } else { - final MucOptions mucOptions = conversation.getMucOptions(); - final List allUsers = mucOptions.getUsers(); - final Set addedMarkers = new HashSet<>(); - ChatState state = ChatState.COMPOSING; - List users = conversation.getMucOptions().getUsersWithChatState(state, 5); - if (users.size() == 0) { - state = ChatState.PAUSED; - users = conversation.getMucOptions().getUsersWithChatState(state, 5); - } - if (mucOptions.isPrivateAndNonAnonymous()) { - for (int i = this.messageList.size() - 1; i >= 0; --i) { - final Set markersForMessage = messageList.get(i).getReadByMarkers(); - final List shownMarkers = new ArrayList<>(); - for (ReadByMarker marker : markersForMessage) { - if (!ReadByMarker.contains(marker, addedMarkers)) { - addedMarkers.add(marker); //may be put outside this condition. set should do dedup anyway - MucOptions.User user = mucOptions.findUser(marker); - if (user != null && !users.contains(user)) { - shownMarkers.add(user); - } - } - } - final ReadByMarker markerForSender = ReadByMarker.from(messageList.get(i)); - final Message statusMessage; - final int size = shownMarkers.size(); - if (size > 1) { - final String body; - if (size <= 4) { - body = getString(R.string.contacts_have_read_up_to_this_point, UIHelper.concatNames(shownMarkers)); - } else if (ReadByMarker.allUsersRepresented(allUsers, markersForMessage, markerForSender)) { - body = getString(R.string.everyone_has_read_up_to_this_point); - } else { - body = getString(R.string.contacts_and_n_more_have_read_up_to_this_point, UIHelper.concatNames(shownMarkers, 3), size - 3); - } - statusMessage = Message.createStatusMessage(conversation, body); - statusMessage.setCounterparts(shownMarkers); - } else if (size == 1) { - statusMessage = Message.createStatusMessage(conversation, getString(R.string.contact_has_read_up_to_this_point, UIHelper.getDisplayName(shownMarkers.get(0)))); - statusMessage.setCounterpart(shownMarkers.get(0).getFullJid()); - statusMessage.setTrueCounterpart(shownMarkers.get(0).getRealJid()); + for (int i = this.messageList.size() - 1; i >= 0; --i) { + final Message message = this.messageList.get(i); + if (message.getType() != Message.TYPE_STATUS) { + if (message.getStatus() == Message.STATUS_RECEIVED) { + return; } else { - statusMessage = null; - } - if (statusMessage != null) { - this.messageList.add(i + 1, statusMessage); - } - addedMarkers.add(markerForSender); - if (ReadByMarker.allUsersRepresented(allUsers, addedMarkers)) { - break; + if (message.getStatus() == Message.STATUS_SEND_DISPLAYED) { + this.messageList.add(i + 1, + Message.createStatusMessage(conversation, getString(R.string.contact_has_read_up_to_this_point, conversation.getName()))); + return; + } } } } - if (users.size() > 0) { - Message statusMessage; - if (users.size() == 1) { - MucOptions.User user = users.get(0); - int id = state == ChatState.COMPOSING ? R.string.contact_is_typing : R.string.contact_has_stopped_typing; - statusMessage = Message.createStatusMessage(conversation, getString(id, UIHelper.getDisplayName(user))); - statusMessage.setTrueCounterpart(user.getRealJid()); - statusMessage.setCounterpart(user.getFullJid()); - } else { - int id = state == ChatState.COMPOSING ? R.string.contacts_are_typing : R.string.contacts_have_stopped_typing; - statusMessage = Message.createStatusMessage(conversation, getString(id, UIHelper.concatNames(users))); - statusMessage.setCounterparts(users); - } - this.messageList.add(statusMessage); - } - } + } else { + final MucOptions mucOptions = conversation.getMucOptions(); + final List allUsers = mucOptions.getUsers(); + final Set addedMarkers = new HashSet<>(); + ChatState state = ChatState.COMPOSING; + List users = conversation.getMucOptions().getUsersWithChatState(state, 5); + if (users.size() == 0) { + state = ChatState.PAUSED; + users = conversation.getMucOptions().getUsersWithChatState(state, 5); + } + if (mucOptions.isPrivateAndNonAnonymous()) { + for (int i = this.messageList.size() - 1; i >= 0; --i) { + final Set markersForMessage = messageList.get(i).getReadByMarkers(); + final List shownMarkers = new ArrayList<>(); + for (ReadByMarker marker : markersForMessage) { + if (!ReadByMarker.contains(marker, addedMarkers)) { + addedMarkers.add(marker); //may be put outside this condition. set should do dedup anyway + MucOptions.User user = mucOptions.findUser(marker); + if (user != null && !users.contains(user)) { + shownMarkers.add(user); + } + } + } + final ReadByMarker markerForSender = ReadByMarker.from(messageList.get(i)); + final Message statusMessage; + final int size = shownMarkers.size(); + if (size > 1) { + final String body; + if (size <= 4) { + body = getString(R.string.contacts_have_read_up_to_this_point, UIHelper.concatNames(shownMarkers)); + } else if (ReadByMarker.allUsersRepresented(allUsers, markersForMessage, markerForSender)) { + body = getString(R.string.everyone_has_read_up_to_this_point); + } else { + body = getString(R.string.contacts_and_n_more_have_read_up_to_this_point, UIHelper.concatNames(shownMarkers, 3), size - 3); + } + statusMessage = Message.createStatusMessage(conversation, body); + statusMessage.setCounterparts(shownMarkers); + } else if (size == 1) { + statusMessage = Message.createStatusMessage(conversation, getString(R.string.contact_has_read_up_to_this_point, UIHelper.getDisplayName(shownMarkers.get(0)))); + statusMessage.setCounterpart(shownMarkers.get(0).getFullJid()); + statusMessage.setTrueCounterpart(shownMarkers.get(0).getRealJid()); + } else { + statusMessage = null; + } + if (statusMessage != null) { + this.messageList.add(i + 1, statusMessage); + } + addedMarkers.add(markerForSender); + if (ReadByMarker.allUsersRepresented(allUsers, addedMarkers)) { + break; + } + } + } + if (users.size() > 0) { + Message statusMessage; + if (users.size() == 1) { + MucOptions.User user = users.get(0); + int id = state == ChatState.COMPOSING ? R.string.contact_is_typing : R.string.contact_has_stopped_typing; + statusMessage = Message.createStatusMessage(conversation, getString(id, UIHelper.getDisplayName(user))); + statusMessage.setTrueCounterpart(user.getRealJid()); + statusMessage.setCounterpart(user.getFullJid()); + } else { + int id = state == ChatState.COMPOSING ? R.string.contacts_are_typing : R.string.contacts_have_stopped_typing; + statusMessage = Message.createStatusMessage(conversation, getString(id, UIHelper.concatNames(users))); + statusMessage.setCounterparts(users); + } + this.messageList.add(statusMessage); + } + } }