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 3bf5f7ae3a..b3eac5d488 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 4d19bcf57c..29b119dd15 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 ae59bad852..bdf8106461 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 +}