From 93ceb9db7871aedb125e2fe376f01bb62f56108c Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 31 Mar 2018 10:57:59 +0200 Subject: [PATCH] fixed read event when using fab scroll down --- .../services/XmppConnectionService.java | 8 +- .../ui/ConversationFragment.java | 104 +++++++++--------- 2 files changed, 58 insertions(+), 54 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index fbad5a797..efdf6a0d7 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -607,7 +607,7 @@ public class XmppConnectionService extends Service { } try { restoredFromDatabaseLatch.await(); - sendReadMarker(c,null); + sendReadMarker(c, null); } catch (InterruptedException e) { Log.d(Config.LOGTAG, "unable to process notification read marker for conversation " + c.getName()); } @@ -1617,7 +1617,7 @@ public class XmppConnectionService extends Service { public boolean isConversationStillOpen(final Conversation conversation) { synchronized (this.conversations) { - for(Conversation current : this.conversations) { + for (Conversation current : this.conversations) { if (current == conversation) { return true; } @@ -3265,7 +3265,7 @@ public class XmppConnectionService extends Service { } public boolean markRead(final Conversation conversation, boolean dismiss) { - return markRead(conversation,null,dismiss).size() > 0; + return markRead(conversation, null, dismiss).size() > 0; } public boolean markRead(final Conversation conversation) { @@ -3306,7 +3306,7 @@ public class XmppConnectionService extends Service { public void sendReadMarker(final Conversation conversation, String upToUuid) { final boolean isPrivateAndNonAnonymousMuc = conversation.getMode() == Conversation.MODE_MULTI && conversation.isPrivateAndNonAnonymous(); - final List readMessages = this.markRead(conversation,upToUuid,true); + final List readMessages = this.markRead(conversation, upToUuid, true); if (readMessages.size() > 0) { updateConversationUi(); } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index fecd0715a..8af307667 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -131,7 +131,6 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke private static final String STATE_LAST_MESSAGE_UUID = "state_last_message_uuid"; final protected List messageList = new ArrayList<>(); - private String lastMessageUuid = null; private final PendingItem postponedActivityResult = new PendingItem<>(); private final PendingItem pendingConversationsUuid = new PendingItem<>(); private final PendingItem pendingExtras = new PendingItem<>(); @@ -141,6 +140,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke private final PendingItem pendingMessage = new PendingItem<>(); public Uri mPendingEditorContent = null; protected MessageAdapter messageListAdapter; + private String lastMessageUuid = null; private Conversation conversation; private FragmentConversationBinding binding; private Toast messageLoaderToast; @@ -185,30 +185,6 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke }); } }; - - private void toggleScrollDownButton() { - toggleScrollDownButton(binding.messagesView); - } - - private void toggleScrollDownButton(AbsListView listView) { - if (conversation == null) { - return; - } - if (scrolledToBottom(listView)) { - lastMessageUuid = null; - hideUnreadMessagesCount(); - } else { - binding.scrollToBottomButton.setEnabled(true); - binding.scrollToBottomButton.setVisibility(View.VISIBLE); - if (lastMessageUuid == null) { - lastMessageUuid = conversation.getLatestMessage().getUuid(); - } - if (conversation.getReceivedMessagesCountSinceUuid(lastMessageUuid) > 0) { - binding.unreadCountCustomView.setVisibility(View.VISIBLE); - } - } - } - private OnScrollListener mOnScrollListener = new OnScrollListener() { @Override @@ -391,12 +367,12 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke }; private OnClickListener mScrollButtonListener = new OnClickListener() { - @Override - public void onClick(View v) { - stopScrolling(); - setSelection(binding.messagesView.getCount() - 1); + @Override + public void onClick(View v) { + stopScrolling(); + setSelection(binding.messagesView.getCount() - 1); } - }; + }; private OnClickListener mSendButtonListener = new OnClickListener() { @Override @@ -523,6 +499,49 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke return null; } + private static void hideSoftKeyboard(final Activity activity) { + InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); + View view = activity.getCurrentFocus(); + if (view != null && imm != null) { + imm.hideSoftInputFromWindow(view.getWindowToken(), 0); + } + } + + private static boolean scrolledToBottom(AbsListView listView) { + final int count = listView.getCount(); + if (count == 0) { + return true; + } else if (listView.getLastVisiblePosition() == count - 1) { + final View lastChild = listView.getChildAt(listView.getChildCount() - 1); + return lastChild != null && lastChild.getBottom() <= listView.getHeight(); + } else { + return false; + } + } + + private void toggleScrollDownButton() { + toggleScrollDownButton(binding.messagesView); + } + + private void toggleScrollDownButton(AbsListView listView) { + if (conversation == null) { + return; + } + if (scrolledToBottom(listView)) { + lastMessageUuid = null; + hideUnreadMessagesCount(); + } else { + binding.scrollToBottomButton.setEnabled(true); + binding.scrollToBottomButton.setVisibility(View.VISIBLE); + if (lastMessageUuid == null) { + lastMessageUuid = conversation.getLatestMessage().getUuid(); + } + if (conversation.getReceivedMessagesCountSinceUuid(lastMessageUuid) > 0) { + binding.unreadCountCustomView.setVisibility(View.VISIBLE); + } + } + } + private int getIndexOf(String uuid, List messages) { if (uuid == null) { return messages.size() - 1; @@ -1011,14 +1030,6 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } } - private static void hideSoftKeyboard(final Activity activity) { - InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); - View view = activity.getCurrentFocus(); - if (view != null && imm != null) { - imm.hideSoftInputFromWindow(view.getWindowToken(), 0); - } - } - private void quoteMessage(Message message) { quoteText(MessageUtils.prepareQuote(message)); } @@ -1569,7 +1580,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke if (pos >= 0) { Message message = (Message) binding.messagesView.getItemAtPosition(pos); if (message != null) { - while(message.next() != null && message.next().wasMergedIntoPrevious()) { + while (message.next() != null && message.next().wasMergedIntoPrevious()) { message = message.next(); } return message.getUuid(); @@ -1850,7 +1861,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke this.reInitRequiredOnStart = true; pendingExtras.push(extras); } - updateUnreadMessagesCount(); + resetUnreadMessagesCount(); } private void reInit(Conversation conversation) { @@ -1895,6 +1906,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke Log.d(Config.LOGTAG, "scrolledToBottomAndNoPending=" + Boolean.toString(scrolledToBottomAndNoPending)); if (hasExtras || scrolledToBottomAndNoPending) { + resetUnreadMessagesCount(); synchronized (this.messageList) { Log.d(Config.LOGTAG, "jump to first unread message"); final Message first = conversation.getFirstUnreadMessage(); @@ -1917,7 +1929,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke return true; } - private void updateUnreadMessagesCount() { + private void resetUnreadMessagesCount() { lastMessageUuid = null; hideUnreadMessagesCount(); } @@ -1934,6 +1946,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke private void setSelection(int pos) { this.binding.messagesView.setSelection(pos); this.binding.messagesView.post(() -> this.binding.messagesView.setSelection(pos)); + this.binding.messagesView.post(this::fireReadEvent); } private boolean scrolledToBottom() { @@ -1943,15 +1956,6 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke return scrolledToBottom(this.binding.messagesView); } - private static boolean scrolledToBottom(AbsListView listView) { - if (listView.getLastVisiblePosition() == listView.getCount() - 1) { - final View lastChild = listView.getChildAt(listView.getChildCount() - 1); - return lastChild != null && lastChild.getBottom() <= listView.getHeight(); - } else { - return false; - } - } - private void processExtras(Bundle extras) { final String downloadUuid = extras.getString(ConversationsActivity.EXTRA_DOWNLOAD_UUID); final String text = extras.getString(ConversationsActivity.EXTRA_TEXT);