Create ProgressReporter interface

This commit is contained in:
Benoit Marty 2021-02-09 10:30:11 +01:00 committed by Benoit Marty
parent 1c83ee086a
commit b870a8b791
5 changed files with 41 additions and 31 deletions

View File

@ -22,8 +22,20 @@ import org.matrix.android.sdk.api.session.InitialSyncProgressService
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
internal interface ProgressReporter {
fun startTask(@StringRes nameRes: Int,
totalProgress: Int,
parentWeight: Float)
fun reportProgress(progress: Float)
fun endTask()
}
@SessionScope @SessionScope
internal class DefaultInitialSyncProgressService @Inject constructor() : InitialSyncProgressService { internal class DefaultInitialSyncProgressService @Inject constructor()
: InitialSyncProgressService,
ProgressReporter {
private val status = MutableLiveData<InitialSyncProgressService.Status>() private val status = MutableLiveData<InitialSyncProgressService.Status>()
@ -33,7 +45,7 @@ internal class DefaultInitialSyncProgressService @Inject constructor() : Initial
return status return status
} }
fun startTask(@StringRes nameRes: Int, override fun startTask(@StringRes nameRes: Int,
totalProgress: Int, totalProgress: Int,
parentWeight: Float) { parentWeight: Float) {
// Create a rootTask, or add a child to the leaf // Create a rootTask, or add a child to the leaf
@ -51,7 +63,7 @@ internal class DefaultInitialSyncProgressService @Inject constructor() : Initial
reportProgress(0F) reportProgress(0F)
} }
fun reportProgress(progress: Float) { override fun reportProgress(progress: Float) {
rootTask?.let { root -> rootTask?.let { root ->
root.leaf().let { leaf -> root.leaf().let { leaf ->
// Update the progress of the leaf and all its parents // Update the progress of the leaf and all its parents
@ -62,7 +74,7 @@ internal class DefaultInitialSyncProgressService @Inject constructor() : Initial
} }
} }
fun endTask() { override fun endTask() {
rootTask?.leaf()?.let { endedTask -> rootTask?.leaf()?.let { endedTask ->
// Ensure the task progress is complete // Ensure the task progress is complete
reportProgress(endedTask.totalProgress.toFloat()) reportProgress(endedTask.totalProgress.toFloat())
@ -117,7 +129,7 @@ private class TaskInfo(@StringRes val nameRes: Int,
} }
} }
internal inline fun <T> reportSubtask(reporter: DefaultInitialSyncProgressService?, internal inline fun <T> reportSubtask(reporter: ProgressReporter?,
@StringRes nameRes: Int, @StringRes nameRes: Int,
totalProgress: Int, totalProgress: Int,
parentWeight: Float, parentWeight: Float,
@ -128,7 +140,7 @@ internal inline fun <T> reportSubtask(reporter: DefaultInitialSyncProgressServic
} }
} }
internal inline fun <K, V, R> Map<out K, V>.mapWithProgress(reporter: DefaultInitialSyncProgressService?, internal inline fun <K, V, R> Map<out K, V>.mapWithProgress(reporter: ProgressReporter?,
@StringRes nameRes: Int, @StringRes nameRes: Int,
parentWeight: Float, parentWeight: Float,
transform: (Map.Entry<K, V>) -> R): List<R> { transform: (Map.Entry<K, V>) -> R): List<R> {

View File

@ -26,7 +26,7 @@ import org.matrix.android.sdk.internal.crypto.MXEventDecryptionResult
import org.matrix.android.sdk.internal.crypto.algorithms.olm.OlmDecryptionResult import org.matrix.android.sdk.internal.crypto.algorithms.olm.OlmDecryptionResult
import org.matrix.android.sdk.internal.crypto.model.event.OlmEventContent import org.matrix.android.sdk.internal.crypto.model.event.OlmEventContent
import org.matrix.android.sdk.internal.crypto.verification.DefaultVerificationService import org.matrix.android.sdk.internal.crypto.verification.DefaultVerificationService
import org.matrix.android.sdk.internal.session.DefaultInitialSyncProgressService import org.matrix.android.sdk.internal.session.ProgressReporter
import org.matrix.android.sdk.internal.session.sync.model.SyncResponse import org.matrix.android.sdk.internal.session.sync.model.SyncResponse
import org.matrix.android.sdk.internal.session.sync.model.ToDeviceSyncResponse import org.matrix.android.sdk.internal.session.sync.model.ToDeviceSyncResponse
import timber.log.Timber import timber.log.Timber
@ -35,10 +35,10 @@ import javax.inject.Inject
internal class CryptoSyncHandler @Inject constructor(private val cryptoService: DefaultCryptoService, internal class CryptoSyncHandler @Inject constructor(private val cryptoService: DefaultCryptoService,
private val verificationService: DefaultVerificationService) { private val verificationService: DefaultVerificationService) {
fun handleToDevice(toDevice: ToDeviceSyncResponse, initialSyncProgressService: DefaultInitialSyncProgressService? = null) { fun handleToDevice(toDevice: ToDeviceSyncResponse, progressReporter: ProgressReporter? = null) {
val total = toDevice.events?.size ?: 0 val total = toDevice.events?.size ?: 0
toDevice.events?.forEachIndexed { index, event -> toDevice.events?.forEachIndexed { index, event ->
initialSyncProgressService?.reportProgress(index * 100F / total) progressReporter?.reportProgress(index * 100F / total)
// Decrypt event if necessary // Decrypt event if necessary
Timber.i("## CRYPTO | To device event from ${event.senderId} of type:${event.type}") Timber.i("## CRYPTO | To device event from ${event.senderId} of type:${event.type}")
decryptToDeviceEvent(event, null) decryptToDeviceEvent(event, null)

View File

@ -16,17 +16,17 @@
package org.matrix.android.sdk.internal.session.sync package org.matrix.android.sdk.internal.session.sync
import io.realm.Realm
import org.matrix.android.sdk.R import org.matrix.android.sdk.R
import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.internal.database.model.GroupEntity import org.matrix.android.sdk.internal.database.model.GroupEntity
import org.matrix.android.sdk.internal.database.model.GroupSummaryEntity import org.matrix.android.sdk.internal.database.model.GroupSummaryEntity
import org.matrix.android.sdk.internal.database.query.getOrCreate import org.matrix.android.sdk.internal.database.query.getOrCreate
import org.matrix.android.sdk.internal.database.query.where import org.matrix.android.sdk.internal.database.query.where
import org.matrix.android.sdk.internal.session.DefaultInitialSyncProgressService import org.matrix.android.sdk.internal.session.ProgressReporter
import org.matrix.android.sdk.internal.session.mapWithProgress import org.matrix.android.sdk.internal.session.mapWithProgress
import org.matrix.android.sdk.internal.session.sync.model.GroupsSyncResponse import org.matrix.android.sdk.internal.session.sync.model.GroupsSyncResponse
import org.matrix.android.sdk.internal.session.sync.model.InvitedGroupSync import org.matrix.android.sdk.internal.session.sync.model.InvitedGroupSync
import io.realm.Realm
import javax.inject.Inject import javax.inject.Inject
internal class GroupSyncHandler @Inject constructor() { internal class GroupSyncHandler @Inject constructor() {
@ -37,11 +37,9 @@ internal class GroupSyncHandler @Inject constructor() {
data class LEFT(val data: Map<String, Any>) : HandlingStrategy() data class LEFT(val data: Map<String, Any>) : HandlingStrategy()
} }
fun handle( fun handle(realm: Realm,
realm: Realm,
roomsSyncResponse: GroupsSyncResponse, roomsSyncResponse: GroupsSyncResponse,
reporter: DefaultInitialSyncProgressService? = null reporter: ProgressReporter? = null) {
) {
handleGroupSync(realm, HandlingStrategy.JOINED(roomsSyncResponse.join), reporter) handleGroupSync(realm, HandlingStrategy.JOINED(roomsSyncResponse.join), reporter)
handleGroupSync(realm, HandlingStrategy.INVITED(roomsSyncResponse.invite), reporter) handleGroupSync(realm, HandlingStrategy.INVITED(roomsSyncResponse.invite), reporter)
handleGroupSync(realm, HandlingStrategy.LEFT(roomsSyncResponse.leave), reporter) handleGroupSync(realm, HandlingStrategy.LEFT(roomsSyncResponse.leave), reporter)
@ -49,7 +47,7 @@ internal class GroupSyncHandler @Inject constructor() {
// PRIVATE METHODS ***************************************************************************** // PRIVATE METHODS *****************************************************************************
private fun handleGroupSync(realm: Realm, handlingStrategy: HandlingStrategy, reporter: DefaultInitialSyncProgressService?) { private fun handleGroupSync(realm: Realm, handlingStrategy: HandlingStrategy, reporter: ProgressReporter?) {
val groups = when (handlingStrategy) { val groups = when (handlingStrategy) {
is HandlingStrategy.JOINED -> is HandlingStrategy.JOINED ->
handlingStrategy.data.mapWithProgress(reporter, R.string.initial_sync_start_importing_account_groups, 0.6f) { handlingStrategy.data.mapWithProgress(reporter, R.string.initial_sync_start_importing_account_groups, 0.6f) {

View File

@ -49,7 +49,7 @@ import org.matrix.android.sdk.internal.database.query.where
import org.matrix.android.sdk.internal.di.MoshiProvider import org.matrix.android.sdk.internal.di.MoshiProvider
import org.matrix.android.sdk.internal.di.UserId import org.matrix.android.sdk.internal.di.UserId
import org.matrix.android.sdk.internal.extensions.clearWith import org.matrix.android.sdk.internal.extensions.clearWith
import org.matrix.android.sdk.internal.session.DefaultInitialSyncProgressService import org.matrix.android.sdk.internal.session.ProgressReporter
import org.matrix.android.sdk.internal.session.mapWithProgress import org.matrix.android.sdk.internal.session.mapWithProgress
import org.matrix.android.sdk.internal.session.reportSubtask import org.matrix.android.sdk.internal.session.reportSubtask
import org.matrix.android.sdk.internal.session.room.membership.RoomChangeMembershipStateDataSource import org.matrix.android.sdk.internal.session.room.membership.RoomChangeMembershipStateDataSource
@ -86,12 +86,10 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle
data class LEFT(val data: Map<String, RoomSync>) : HandlingStrategy() data class LEFT(val data: Map<String, RoomSync>) : HandlingStrategy()
} }
fun handle( fun handle(realm: Realm,
realm: Realm,
roomsSyncResponse: RoomsSyncResponse, roomsSyncResponse: RoomsSyncResponse,
isInitialSync: Boolean, isInitialSync: Boolean,
reporter: DefaultInitialSyncProgressService? = null reporter: ProgressReporter? = null) {
) {
Timber.v("Execute transaction from $this") Timber.v("Execute transaction from $this")
handleRoomSync(realm, HandlingStrategy.JOINED(roomsSyncResponse.join), isInitialSync, reporter) handleRoomSync(realm, HandlingStrategy.JOINED(roomsSyncResponse.join), isInitialSync, reporter)
handleRoomSync(realm, HandlingStrategy.INVITED(roomsSyncResponse.invite), isInitialSync, reporter) handleRoomSync(realm, HandlingStrategy.INVITED(roomsSyncResponse.invite), isInitialSync, reporter)
@ -100,7 +98,7 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle
// PRIVATE METHODS ***************************************************************************** // PRIVATE METHODS *****************************************************************************
private fun handleRoomSync(realm: Realm, handlingStrategy: HandlingStrategy, isInitialSync: Boolean, reporter: DefaultInitialSyncProgressService?) { private fun handleRoomSync(realm: Realm, handlingStrategy: HandlingStrategy, isInitialSync: Boolean, reporter: ProgressReporter?) {
val insertType = if (isInitialSync) { val insertType = if (isInitialSync) {
EventInsertType.INITIAL_SYNC EventInsertType.INITIAL_SYNC
} else { } else {
@ -137,7 +135,7 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle
handlingStrategy: HandlingStrategy.JOINED, handlingStrategy: HandlingStrategy.JOINED,
insertType: EventInsertType, insertType: EventInsertType,
syncLocalTimeStampMillis: Long, syncLocalTimeStampMillis: Long,
reporter: DefaultInitialSyncProgressService?) { reporter: ProgressReporter?) {
val maxSize = (initialSyncStrategy as? InitialSyncStrategy.Optimized)?.maxRoomsToInsert ?: Int.MAX_VALUE val maxSize = (initialSyncStrategy as? InitialSyncStrategy.Optimized)?.maxRoomsToInsert ?: Int.MAX_VALUE
val listSize = handlingStrategy.data.keys.size val listSize = handlingStrategy.data.keys.size
val numberOfChunks = ceil(listSize / maxSize.toDouble()).toInt() val numberOfChunks = ceil(listSize / maxSize.toDouble()).toInt()

View File

@ -25,7 +25,7 @@ import org.matrix.android.sdk.internal.crypto.DefaultCryptoService
import org.matrix.android.sdk.internal.di.SessionDatabase import org.matrix.android.sdk.internal.di.SessionDatabase
import org.matrix.android.sdk.internal.di.SessionId import org.matrix.android.sdk.internal.di.SessionId
import org.matrix.android.sdk.internal.di.WorkManagerProvider import org.matrix.android.sdk.internal.di.WorkManagerProvider
import org.matrix.android.sdk.internal.session.DefaultInitialSyncProgressService import org.matrix.android.sdk.internal.session.ProgressReporter
import org.matrix.android.sdk.internal.session.group.GetGroupDataWorker import org.matrix.android.sdk.internal.session.group.GetGroupDataWorker
import org.matrix.android.sdk.internal.session.notification.ProcessEventForPushTask import org.matrix.android.sdk.internal.session.notification.ProcessEventForPushTask
import org.matrix.android.sdk.internal.session.reportSubtask import org.matrix.android.sdk.internal.session.reportSubtask
@ -53,7 +53,9 @@ internal class SyncResponseHandler @Inject constructor(@SessionDatabase private
private val processEventForPushTask: ProcessEventForPushTask, private val processEventForPushTask: ProcessEventForPushTask,
private val pushRuleService: PushRuleService) { private val pushRuleService: PushRuleService) {
suspend fun handleResponse(syncResponse: SyncResponse, fromToken: String?, reporter: DefaultInitialSyncProgressService?) { suspend fun handleResponse(syncResponse: SyncResponse,
fromToken: String?,
reporter: ProgressReporter?) {
val isInitialSync = fromToken == null val isInitialSync = fromToken == null
Timber.v("Start handling sync, is InitialSync: $isInitialSync") Timber.v("Start handling sync, is InitialSync: $isInitialSync")