From 8124b2447907327e3d0eef1baada4b2a8401ea22 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 11 Jan 2019 17:19:54 +0100 Subject: [PATCH] maintain order of conversations in share with dialog --- .../services/XmppConnectionService.java | 31 +++++++++++++++++-- .../conversations/ui/ShareWithActivity.java | 5 +-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 91ccadd61..9773d3c8e 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -55,6 +55,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Hashtable; @@ -1650,10 +1651,23 @@ public class XmppConnectionService extends Service { } public void populateWithOrderedConversations(final List list) { - populateWithOrderedConversations(list, true); + populateWithOrderedConversations(list, true, true); } - public void populateWithOrderedConversations(final List list, boolean includeNoFileUpload) { + public void populateWithOrderedConversations(final List list, final boolean includeNoFileUpload) { + populateWithOrderedConversations(list, includeNoFileUpload, true); + } + + public void populateWithOrderedConversations(final List list, final boolean includeNoFileUpload, final boolean sort) { + final List orderedUuids; + if (sort) { + orderedUuids = null; + } else { + orderedUuids = new ArrayList<>(); + for(Conversation conversation : list) { + orderedUuids.add(conversation.getUuid()); + } + } list.clear(); if (includeNoFileUpload) { list.addAll(getConversations()); @@ -1666,7 +1680,18 @@ public class XmppConnectionService extends Service { } } try { - Collections.sort(list); + if (orderedUuids != null) { + Collections.sort(list, (a, b) -> { + final int indexA = orderedUuids.indexOf(a.getUuid()); + final int indexB = orderedUuids.indexOf(b.getUuid()); + if (indexA == -1 || indexB == -1 || indexA == indexB) { + return a.compareTo(b); + } + return indexA - indexB; + }); + } else { + Collections.sort(list); + } } catch (IllegalArgumentException e) { //ignore } diff --git a/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java b/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java index ef9289b83..5daf9933c 100644 --- a/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java @@ -141,7 +141,7 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer this.share.uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); } if (xmppConnectionServiceBound) { - xmppConnectionService.populateWithOrderedConversations(mConversations, this.share.uris.size() == 0); + xmppConnectionService.populateWithOrderedConversations(mConversations, this.share.uris.size() == 0, false); } } @@ -195,7 +195,8 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer } public void refreshUiReal() { - xmppConnectionService.populateWithOrderedConversations(mConversations, this.share != null && this.share.uris.size() == 0); + //TODO inject desired order to not resort on refresh + xmppConnectionService.populateWithOrderedConversations(mConversations, this.share != null && this.share.uris.size() == 0, false); mAdapter.notifyDataSetChanged(); } }