diff options
author | Andy Scherzinger <info@andy-scherzinger.de> | 2022-04-07 20:21:09 +0300 |
---|---|---|
committer | Marcel Hibbe <dev@mhibbe.de> | 2022-04-08 10:21:14 +0300 |
commit | 660c3401f2030cf8680edf1156d28e6a3d00a5ee (patch) | |
tree | e38b9ead4a96670e0e2b71cfc39a4507eb3546a1 /app | |
parent | 2aa7a5eb67ea3fd86bdfcd135757f7f93ae17019 (diff) |
sort reaction lists
Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
Diffstat (limited to 'app')
-rw-r--r-- | app/src/main/java/com/nextcloud/talk/ui/dialog/ShowReactionsDialog.kt | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/app/src/main/java/com/nextcloud/talk/ui/dialog/ShowReactionsDialog.kt b/app/src/main/java/com/nextcloud/talk/ui/dialog/ShowReactionsDialog.kt index 137f0b3c8..74a08f353 100644 --- a/app/src/main/java/com/nextcloud/talk/ui/dialog/ShowReactionsDialog.kt +++ b/app/src/main/java/com/nextcloud/talk/ui/dialog/ShowReactionsDialog.kt @@ -54,6 +54,8 @@ import io.reactivex.Observer import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable import io.reactivex.schedulers.Schedulers +import java.util.Collections +import java.util.Comparator @AutoInjector(NextcloudTalkApplication::class) class ShowReactionsDialog( @@ -161,6 +163,8 @@ class ShowReactionsDialog( } } + Collections.sort(reactionVoters, ReactionComparator(userEntity?.userId)) + adapter?.list?.addAll(reactionVoters) adapter?.notifyDataSetChanged() } else { @@ -222,4 +226,122 @@ class ShowReactionsDialog( companion object { const val TAG = "ShowReactionsDialog" } + + class ReactionComparator(val activeUser: String?) : Comparator<ReactionItem> { + override fun compare(reactionItem1: ReactionItem?, reactionItem2: ReactionItem?): Int { + // sort by emoji, own account, display-name, timestamp, actor-id + + if (reactionItem1 == null && reactionItem2 == null) { + return 0 + } + if (reactionItem1 == null) { + return -1 + } + if (reactionItem2 == null) { + return 1 + } + + // emoji + val reaction = StringComparator().compare(reactionItem1.reaction, reactionItem2.reaction) + if (reaction != 0) { + return reaction + } + + // own account + val ownAccount = compareOwnAccount( + activeUser, + reactionItem1.reactionVoter.actorId, + reactionItem2.reactionVoter.actorId + ) + + if (ownAccount != 0) { + return ownAccount + } + + // display-name + val displayName = StringComparator() + .compare( + reactionItem1.reactionVoter.actorDisplayName, + reactionItem2.reactionVoter.actorDisplayName + ) + + if (displayName != 0) { + return displayName + } + + // timestamp + val timestamp = LongComparator() + .compare( + reactionItem1.reactionVoter.timestamp, + reactionItem2.reactionVoter.timestamp + ) + + if (timestamp != 0) { + return timestamp + } + + // actor-id + val actorId = StringComparator() + .compare( + reactionItem1.reactionVoter.actorId, + reactionItem2.reactionVoter.actorId + ) + + if (actorId != 0) { + return actorId + } + + return 0 + } + + fun compareOwnAccount(activeUser: String?, actorId1: String?, actorId2: String?): Int { + val reactionVote1Active = activeUser == actorId1 + val reactionVote2Active = activeUser == actorId2 + + if (!reactionVote1Active && !reactionVote2Active || reactionVote1Active && reactionVote2Active) { + return 0 + } + + if (activeUser == null) { + return 0 + } + + if (reactionVote1Active) { + return 1 + } + if (reactionVote2Active) { + return -1 + } + + return 0 + } + + internal class StringComparator : Comparator<String?> { + override fun compare(obj1: String?, obj2: String?): Int { + if (obj1 === obj2) { + return 0 + } + if (obj1 == null) { + return -1 + } + return if (obj2 == null) { + 1 + } else obj1.lowercase().compareTo(obj2.lowercase()) + } + } + + internal class LongComparator : Comparator<Long?> { + override fun compare(obj1: Long?, obj2: Long?): Int { + if (obj1 === obj2) { + return 0 + } + if (obj1 == null) { + return -1 + } + return if (obj2 == null) { + 1 + } else obj1.compareTo(obj2) + } + } + } } |