This commit is contained in:
Valere 2019-07-08 16:31:21 +02:00
parent 95d83db90c
commit 8159a52bd7
3 changed files with 38 additions and 50 deletions

View File

@ -94,4 +94,19 @@ public inline fun <T> reportSubtask(reporter: DefaultInitialSyncProgressService?
return block().also {
reporter?.endTask(nameRes)
}
}
}
public inline fun <K, V, R> Map<out K, V>.mapWithProgress(reporter: DefaultInitialSyncProgressService?, taskId: Int, weight: Float, transform: (Map.Entry<K, V>) -> R): List<R> {
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)
}
}

View File

@ -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)
}

View File

@ -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) }
}
}
}