From 95089b91b810f91803343d7a9692f54148b5a98f Mon Sep 17 00:00:00 2001 From: ganfra Date: Tue, 30 Jul 2019 21:41:29 +0200 Subject: [PATCH] UserAccountData: optimize helper and clean code. --- .../sync/UserAccountDataSyncHandler.kt | 26 ++++++++----------- .../user/accountdata/DirectChatsHelper.kt | 2 +- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/UserAccountDataSyncHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/UserAccountDataSyncHandler.kt index 73e2f266..6ea46931 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/UserAccountDataSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/UserAccountDataSyncHandler.kt @@ -78,13 +78,7 @@ internal class UserAccountDataSyncHandler @Inject constructor(private val monarc // If we get some direct chat invites, we synchronize the user account data including those. private fun synchronizeWithServerIfNeeded(realm: Realm, invites: Map?) { if (invites.isNullOrEmpty()) return - val directChats = directChatsHelper.getLocalUserAccount() - val directChatInvites = HashMap>().apply { - directChats.forEach { (inviterId, roomIds) -> - put(inviterId, ArrayList(roomIds)) - } - } var hasUpdate = false invites.forEach { (roomId, _) -> val myUserStateEvent = RoomMembers(realm, roomId).getStateEvent(credentials.userId) @@ -92,19 +86,21 @@ internal class UserAccountDataSyncHandler @Inject constructor(private val monarc val myUserRoomMember: RoomMember? = myUserStateEvent?.let { it.asDomain().content?.toModel() } val isDirect = myUserRoomMember?.isDirect if (inviterId != null && inviterId != credentials.userId && isDirect == true) { - directChatInvites.getOrPut(inviterId, { arrayListOf() }).apply { - if (contains(roomId)) { - Timber.v("Direct chats already include room $roomId with user $inviterId") - } else { - this.add(roomId) - hasUpdate = true - } - } + directChats + .getOrPut(inviterId, { arrayListOf() }) + .apply { + if (contains(roomId)) { + Timber.v("Direct chats already include room $roomId with user $inviterId") + } else { + add(roomId) + hasUpdate = true + } + } } } if (hasUpdate) { val updateUserAccountParams = UpdateUserAccountDataTask.DirectChatParams( - directMessages = directChatInvites + directMessages = directChats ) updateUserAccountDataTask.configureWith(updateUserAccountParams).executeBy(taskExecutor) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/DirectChatsHelper.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/DirectChatsHelper.kt index 0f26f789..8d23a988 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/DirectChatsHelper.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/user/accountdata/DirectChatsHelper.kt @@ -30,7 +30,7 @@ internal class DirectChatsHelper @Inject constructor(@SessionDatabase /** * @return a map of userId <-> list of roomId */ - fun getLocalUserAccount(filterRoomId: String? = null): Map> { + fun getLocalUserAccount(filterRoomId: String? = null): MutableMap> { return Realm.getInstance(realmConfiguration).use { realm -> val currentDirectRooms = RoomSummaryEntity.getDirectRooms(realm) val directChatsMap = mutableMapOf>()