diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultInitialSyncProgressService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultInitialSyncProgressService.kt index 9918e83fbc..9c816964db 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultInitialSyncProgressService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultInitialSyncProgressService.kt @@ -19,11 +19,14 @@ import androidx.annotation.StringRes import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import org.matrix.android.sdk.api.session.InitialSyncProgressService +import org.matrix.android.sdk.internal.util.StringProvider import timber.log.Timber import javax.inject.Inject @SessionScope -class DefaultInitialSyncProgressService @Inject constructor() : InitialSyncProgressService { +internal class DefaultInitialSyncProgressService @Inject constructor( + private val stringProvider: StringProvider +) : InitialSyncProgressService { private val status = MutableLiveData() @@ -40,10 +43,12 @@ class DefaultInitialSyncProgressService @Inject constructor() : InitialSyncProgr } else { val currentLeaf = rootTask!!.leaf() - val newTask = TaskInfo(nameRes, - totalProgress, - currentLeaf, - parentWeight) + val newTask = TaskInfo( + nameRes = nameRes, + totalProgress = totalProgress, + parent = currentLeaf, + parentWeight = parentWeight + ) currentLeaf.child = newTask } @@ -72,11 +77,11 @@ class DefaultInitialSyncProgressService @Inject constructor() : InitialSyncProgr status.postValue(InitialSyncProgressService.Status.Idle) } - private inner class TaskInfo(@StringRes var nameRes: Int, - var totalProgress: Int, - var parent: TaskInfo? = null, - var parentWeight: Float = 1f, - var offset: Int = parent?.currentProgress ?: 0) { + private inner class TaskInfo(@StringRes val nameRes: Int, + val totalProgress: Int, + val parent: TaskInfo? = null, + val parentWeight: Float = 1f, + val offset: Int = parent?.currentProgress ?: 0) { var child: TaskInfo? = null var currentProgress: Int = 0 @@ -97,32 +102,32 @@ class DefaultInitialSyncProgressService @Inject constructor() : InitialSyncProgr fun setProgress(progress: Int) { currentProgress = progress // val newProgress = Math.min(currentProgress + progress, totalProgress) - parent?.let { + if (parent != null) { val parentProgress = (currentProgress * parentWeight).toInt() - it.setProgress(offset + parentProgress) - } ?: run { - Timber.v("--- ${leaf().nameRes}: $currentProgress") + parent.setProgress(offset + parentProgress) + } else { + Timber.v("--- ${stringProvider.getString(leaf().nameRes)}: $currentProgress") status.postValue(InitialSyncProgressService.Status.Progressing(leaf().nameRes, currentProgress)) } } } } -inline fun reportSubtask(reporter: DefaultInitialSyncProgressService?, - @StringRes nameRes: Int, - totalProgress: Int, - parentWeight: Float = 1f, - block: () -> T): T { +internal inline fun reportSubtask(reporter: DefaultInitialSyncProgressService?, + @StringRes nameRes: Int, + totalProgress: Int, + parentWeight: Float = 1f, + block: () -> T): T { reporter?.startTask(nameRes, totalProgress, parentWeight) return block().also { reporter?.endTask(nameRes) } } -inline fun Map.mapWithProgress(reporter: DefaultInitialSyncProgressService?, - taskId: Int, - weight: Float, - transform: (Map.Entry) -> R): List { +internal inline fun Map.mapWithProgress(reporter: DefaultInitialSyncProgressService?, + taskId: Int, + weight: Float, + transform: (Map.Entry) -> R): List { val total = count().toFloat() var current = 0 reporter?.startTask(taskId, 100, weight)