From 8159a52bd7530406c7294f4b56de3c966fb041c6 Mon Sep 17 00:00:00 2001 From: Valere Date: Mon, 8 Jul 2019 16:31:21 +0200 Subject: [PATCH] cleaning --- .../DefaultInitialSyncProgressService.kt | 17 ++++++++- .../internal/session/sync/GroupSyncHandler.kt | 34 +++++------------ .../internal/session/sync/RoomSyncHandler.kt | 37 ++++++------------- 3 files changed, 38 insertions(+), 50 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultInitialSyncProgressService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultInitialSyncProgressService.kt index 3bf5f7ae..b3eac5d4 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultInitialSyncProgressService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultInitialSyncProgressService.kt @@ -94,4 +94,19 @@ public inline fun reportSubtask(reporter: DefaultInitialSyncProgressService? return block().also { reporter?.endTask(nameRes) } -} \ No newline at end of file +} + + +public inline fun Map.mapWithProgress(reporter: DefaultInitialSyncProgressService?, taskId: Int, weight: Float, transform: (Map.Entry) -> R): List { + val total = this.count() + var current = 0 + reporter?.startTask(taskId, 100, weight) + return this.map { + reporter?.reportProgress((current / total.toFloat() * 100).toInt()) + current++ + transform.invoke(it) + }.also { + reporter?.endTask(taskId) + } +} + diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/GroupSyncHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/GroupSyncHandler.kt index 4d19bcf5..29b119dd 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/GroupSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/GroupSyncHandler.kt @@ -22,6 +22,7 @@ import im.vector.matrix.android.api.session.room.model.Membership import im.vector.matrix.android.internal.database.model.GroupEntity import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.session.DefaultInitialSyncProgressService +import im.vector.matrix.android.internal.session.mapWithProgress import im.vector.matrix.android.internal.session.sync.model.GroupsSyncResponse import im.vector.matrix.android.internal.session.sync.model.InvitedGroupSync import io.realm.Realm @@ -47,36 +48,21 @@ internal class GroupSyncHandler @Inject constructor(private val monarchy: Monarc private fun handleGroupSync(realm: Realm, handlingStrategy: HandlingStrategy, reporter: DefaultInitialSyncProgressService?) { val groups = when (handlingStrategy) { - is HandlingStrategy.JOINED -> { - val total = handlingStrategy.data.size - reporter?.startTask(R.string.initial_sync_start_importing_account_groups, total, 0.6f) - var current = 0 - handlingStrategy.data.map { - reporter?.reportProgress((current / total.toFloat() * 100).toInt()) - current++ + is HandlingStrategy.JOINED -> + handlingStrategy.data.mapWithProgress(reporter, R.string.initial_sync_start_importing_account_groups, 0.6f) { handleJoinedGroup(realm, it.key) } - } - is HandlingStrategy.INVITED -> { - val total = handlingStrategy.data.size - reporter?.startTask(R.string.initial_sync_start_importing_account_groups, total, 0.3f) - var current = 0 - handlingStrategy.data.map { - reporter?.reportProgress((current / total.toFloat() * 100).toInt()) - current++ + + is HandlingStrategy.INVITED -> + handlingStrategy.data.mapWithProgress(reporter, R.string.initial_sync_start_importing_account_groups, 0.3f) { handleInvitedGroup(realm, it.key) } - } - is HandlingStrategy.LEFT -> { - val total = handlingStrategy.data.size - reporter?.startTask(R.string.initial_sync_start_importing_account_groups, total, 0.1f) - var current = 0 - handlingStrategy.data.map { - reporter?.reportProgress((current / total.toFloat() * 100).toInt()) - current++ + + is HandlingStrategy.LEFT -> + handlingStrategy.data.mapWithProgress(reporter, R.string.initial_sync_start_importing_account_groups, 0.1f) { handleLeftGroup(realm, it.key) } - } + } realm.insertOrUpdate(groups) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt index ae59bad8..bdf81064 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/RoomSyncHandler.kt @@ -34,6 +34,7 @@ import im.vector.matrix.android.internal.database.query.find import im.vector.matrix.android.internal.database.query.findLastLiveChunkFromRoom import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.session.DefaultInitialSyncProgressService +import im.vector.matrix.android.internal.session.mapWithProgress import im.vector.matrix.android.internal.session.notification.DefaultPushRuleService import im.vector.matrix.android.internal.session.notification.ProcessEventForPushTask import im.vector.matrix.android.internal.session.room.RoomSummaryUpdater @@ -92,33 +93,19 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch private fun handleRoomSync(realm: Realm, handlingStrategy: HandlingStrategy, reporter: DefaultInitialSyncProgressService?) { val rooms = when (handlingStrategy) { - is HandlingStrategy.JOINED -> { - val total = handlingStrategy.data.size - reporter?.startTask(R.string.initial_sync_start_importing_account_joined_rooms, total, 0.6f) - var current = 0 - handlingStrategy.data.map { - reporter?.reportProgress((current / total.toFloat() * 100).toInt()) - current++ - handleJoinedRoom(realm, it.key, it.value).also { - reporter?.endTask(R.string.initial_sync_start_importing_account_joined_rooms) - } + is HandlingStrategy.JOINED -> + handlingStrategy.data.mapWithProgress(reporter, R.string.initial_sync_start_importing_account_joined_rooms, 0.6f) { + handleJoinedRoom(realm, it.key, it.value) + } + is HandlingStrategy.INVITED -> + handlingStrategy.data.mapWithProgress(reporter, R.string.initial_sync_start_importing_account_invited_rooms, 0.4f) { + handleInvitedRoom(realm, it.key, it.value) } - } - is HandlingStrategy.INVITED -> { - val total = handlingStrategy.data.size - reporter?.startTask(R.string.initial_sync_start_importing_account_invited_rooms, total, 0.4f) - var current = 0 - handlingStrategy.data.map { - reporter?.reportProgress((current / total.toFloat() * 100).toInt()) - current++ - handleInvitedRoom(realm, it.key, it.value) - }.also { - reporter?.endTask(R.string.initial_sync_start_importing_account_invited_rooms) - } - } is HandlingStrategy.LEFT -> { - handlingStrategy.data.map { handleLeftRoom(realm, it.key, it.value) } + handlingStrategy.data.mapWithProgress(reporter, R.string.initial_sync_start_importing_account_left_rooms, 0.2f) { + handleLeftRoom(realm, it.key, it.value) + } } } realm.insertOrUpdate(rooms) @@ -262,4 +249,4 @@ internal class RoomSyncHandler @Inject constructor(private val monarchy: Monarch .forEach { roomTagHandler.handle(realm, roomId, it) } } -} \ No newline at end of file +}