From 1691537a1e3b56e98d1be2440724e5f870bf34d3 Mon Sep 17 00:00:00 2001 From: ganfra Date: Mon, 20 May 2019 14:46:14 +0200 Subject: [PATCH] Room list : add chronological and alphabetical comparators --- build.gradle | 2 +- .../vector/riotredesign/core/di/AppModule.kt | 9 +++- .../room/list/AlphabeticalRoomComparator.kt | 32 +++++++++++++ .../room/list/ChronologicalRoomComparator.kt | 47 +++++++++++++++++++ .../home/room/list/RoomListViewModel.kt | 26 ++++++---- 5 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 vector/src/main/java/im/vector/riotredesign/features/home/room/list/AlphabeticalRoomComparator.kt create mode 100644 vector/src/main/java/im/vector/riotredesign/features/home/room/list/ChronologicalRoomComparator.kt diff --git a/build.gradle b/build.gradle index c54c0f59..ca7654c4 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ buildscript { } } dependencies { - classpath 'com.android.tools.build:gradle:3.3.2' + classpath 'com.android.tools.build:gradle:3.4.1' classpath 'com.google.gms:google-services:4.2.0' classpath "com.airbnb.okreplay:gradle-plugin:1.4.0" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" diff --git a/vector/src/main/java/im/vector/riotredesign/core/di/AppModule.kt b/vector/src/main/java/im/vector/riotredesign/core/di/AppModule.kt index 8b957d00..c0a4f9cf 100644 --- a/vector/src/main/java/im/vector/riotredesign/core/di/AppModule.kt +++ b/vector/src/main/java/im/vector/riotredesign/core/di/AppModule.kt @@ -25,7 +25,8 @@ import im.vector.riotredesign.core.resources.StringArrayProvider import im.vector.riotredesign.core.resources.StringProvider import im.vector.riotredesign.features.home.HomeRoomListObservableStore import im.vector.riotredesign.features.home.group.SelectedGroupStore -import im.vector.riotredesign.features.home.room.list.RoomSummaryComparator +import im.vector.riotredesign.features.home.room.list.AlphabeticalRoomComparator +import im.vector.riotredesign.features.home.room.list.ChronologicalRoomComparator import im.vector.riotredesign.features.notifications.NotificationDrawerManager import org.koin.dsl.module.module @@ -58,7 +59,11 @@ class AppModule(private val context: Context) { } single { - RoomSummaryComparator() + ChronologicalRoomComparator() + } + + single { + AlphabeticalRoomComparator() } single { diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/room/list/AlphabeticalRoomComparator.kt b/vector/src/main/java/im/vector/riotredesign/features/home/room/list/AlphabeticalRoomComparator.kt new file mode 100644 index 00000000..c1031c45 --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/home/room/list/AlphabeticalRoomComparator.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotredesign.features.home.room.list + +import im.vector.matrix.android.api.session.room.model.RoomSummary + +class AlphabeticalRoomComparator + : Comparator { + + override fun compare(leftRoomSummary: RoomSummary?, rightRoomSummary: RoomSummary?): Int { + return when { + rightRoomSummary?.displayName == null -> -1 + leftRoomSummary?.displayName == null -> 1 + else -> leftRoomSummary.displayName.compareTo(rightRoomSummary.displayName) + } + + } +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/room/list/ChronologicalRoomComparator.kt b/vector/src/main/java/im/vector/riotredesign/features/home/room/list/ChronologicalRoomComparator.kt new file mode 100644 index 00000000..2ffe559d --- /dev/null +++ b/vector/src/main/java/im/vector/riotredesign/features/home/room/list/ChronologicalRoomComparator.kt @@ -0,0 +1,47 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotredesign.features.home.room.list + +import im.vector.matrix.android.api.session.room.model.RoomSummary + +class ChronologicalRoomComparator : Comparator { + + override fun compare(leftRoomSummary: RoomSummary?, rightRoomSummary: RoomSummary?): Int { + var rightTimestamp = 0L + var leftTimestamp = 0L + if (null != leftRoomSummary) { + leftTimestamp = leftRoomSummary.lastMessage?.originServerTs ?: 0 + } + if (null != rightRoomSummary) { + rightTimestamp = rightRoomSummary.lastMessage?.originServerTs ?: 0 + } + return if (rightRoomSummary?.lastMessage == null) { + -1 + } else if (leftRoomSummary?.lastMessage == null) { + 1 + } else { + val deltaTimestamp = rightTimestamp - leftTimestamp + if (deltaTimestamp > 0) { + 1 + } else if (deltaTimestamp < 0) { + -1 + } else { + 0 + } + } + } +} \ No newline at end of file diff --git a/vector/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListViewModel.kt b/vector/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListViewModel.kt index 585d8572..6c640d30 100644 --- a/vector/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListViewModel.kt +++ b/vector/src/main/java/im/vector/riotredesign/features/home/room/list/RoomListViewModel.kt @@ -37,7 +37,8 @@ typealias RoomListFilterName = CharSequence class RoomListViewModel(initialState: RoomListViewState, private val session: Session, private val homeRoomListObservableSource: HomeRoomListObservableStore, - private val roomSummaryComparator: RoomSummaryComparator) + private val alphabeticalRoomComparator: AlphabeticalRoomComparator, + private val chronologicalRoomComparator: ChronologicalRoomComparator) : VectorViewModel(initialState) { companion object : MvRxViewModelFactory { @@ -46,8 +47,9 @@ class RoomListViewModel(initialState: RoomListViewState, override fun create(viewModelContext: ViewModelContext, state: RoomListViewState): RoomListViewModel? { val currentSession = viewModelContext.activity.get() val homeRoomListObservableSource = viewModelContext.activity.get() - val roomSummaryComparator = viewModelContext.activity.get() - return RoomListViewModel(state, currentSession, homeRoomListObservableSource, roomSummaryComparator) + val chronologicalRoomComparator = viewModelContext.activity.get() + val alphabeticalRoomComparator = viewModelContext.activity.get() + return RoomListViewModel(state, currentSession, homeRoomListObservableSource, alphabeticalRoomComparator, chronologicalRoomComparator) } } @@ -135,13 +137,19 @@ class RoomListViewModel(initialState: RoomListViewState, } } + val roomComparator = when (displayMode) { + RoomListFragment.DisplayMode.HOME -> chronologicalRoomComparator + RoomListFragment.DisplayMode.PEOPLE -> chronologicalRoomComparator + RoomListFragment.DisplayMode.ROOMS -> alphabeticalRoomComparator + } + return RoomSummaries().apply { - put(RoomCategory.INVITE, invites.sortedWith(roomSummaryComparator)) - put(RoomCategory.FAVOURITE, favourites.sortedWith(roomSummaryComparator)) - put(RoomCategory.DIRECT, directChats.sortedWith(roomSummaryComparator)) - put(RoomCategory.GROUP, groupRooms.sortedWith(roomSummaryComparator)) - put(RoomCategory.LOW_PRIORITY, lowPriorities.sortedWith(roomSummaryComparator)) - put(RoomCategory.SERVER_NOTICE, serverNotices.sortedWith(roomSummaryComparator)) + put(RoomCategory.INVITE, invites.sortedWith(roomComparator)) + put(RoomCategory.FAVOURITE, favourites.sortedWith(roomComparator)) + put(RoomCategory.DIRECT, directChats.sortedWith(roomComparator)) + put(RoomCategory.GROUP, groupRooms.sortedWith(roomComparator)) + put(RoomCategory.LOW_PRIORITY, lowPriorities.sortedWith(roomComparator)) + put(RoomCategory.SERVER_NOTICE, serverNotices.sortedWith(roomComparator)) } }