Improve DefaultInitialSyncProgressService

This commit is contained in:
Benoit Marty 2021-02-08 19:44:02 +01:00 committed by Benoit Marty
parent a2225b3f76
commit 4e1fcf87ae

View File

@ -19,11 +19,14 @@ import androidx.annotation.StringRes
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import org.matrix.android.sdk.api.session.InitialSyncProgressService import org.matrix.android.sdk.api.session.InitialSyncProgressService
import org.matrix.android.sdk.internal.util.StringProvider
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
@SessionScope @SessionScope
class DefaultInitialSyncProgressService @Inject constructor() : InitialSyncProgressService { internal class DefaultInitialSyncProgressService @Inject constructor(
private val stringProvider: StringProvider
) : InitialSyncProgressService {
private val status = MutableLiveData<InitialSyncProgressService.Status>() private val status = MutableLiveData<InitialSyncProgressService.Status>()
@ -40,10 +43,12 @@ class DefaultInitialSyncProgressService @Inject constructor() : InitialSyncProgr
} else { } else {
val currentLeaf = rootTask!!.leaf() val currentLeaf = rootTask!!.leaf()
val newTask = TaskInfo(nameRes, val newTask = TaskInfo(
totalProgress, nameRes = nameRes,
currentLeaf, totalProgress = totalProgress,
parentWeight) parent = currentLeaf,
parentWeight = parentWeight
)
currentLeaf.child = newTask currentLeaf.child = newTask
} }
@ -72,11 +77,11 @@ class DefaultInitialSyncProgressService @Inject constructor() : InitialSyncProgr
status.postValue(InitialSyncProgressService.Status.Idle) status.postValue(InitialSyncProgressService.Status.Idle)
} }
private inner class TaskInfo(@StringRes var nameRes: Int, private inner class TaskInfo(@StringRes val nameRes: Int,
var totalProgress: Int, val totalProgress: Int,
var parent: TaskInfo? = null, val parent: TaskInfo? = null,
var parentWeight: Float = 1f, val parentWeight: Float = 1f,
var offset: Int = parent?.currentProgress ?: 0) { val offset: Int = parent?.currentProgress ?: 0) {
var child: TaskInfo? = null var child: TaskInfo? = null
var currentProgress: Int = 0 var currentProgress: Int = 0
@ -97,18 +102,18 @@ class DefaultInitialSyncProgressService @Inject constructor() : InitialSyncProgr
fun setProgress(progress: Int) { fun setProgress(progress: Int) {
currentProgress = progress currentProgress = progress
// val newProgress = Math.min(currentProgress + progress, totalProgress) // val newProgress = Math.min(currentProgress + progress, totalProgress)
parent?.let { if (parent != null) {
val parentProgress = (currentProgress * parentWeight).toInt() val parentProgress = (currentProgress * parentWeight).toInt()
it.setProgress(offset + parentProgress) parent.setProgress(offset + parentProgress)
} ?: run { } else {
Timber.v("--- ${leaf().nameRes}: $currentProgress") Timber.v("--- ${stringProvider.getString(leaf().nameRes)}: $currentProgress")
status.postValue(InitialSyncProgressService.Status.Progressing(leaf().nameRes, currentProgress)) status.postValue(InitialSyncProgressService.Status.Progressing(leaf().nameRes, currentProgress))
} }
} }
} }
} }
inline fun <T> reportSubtask(reporter: DefaultInitialSyncProgressService?, internal inline fun <T> reportSubtask(reporter: DefaultInitialSyncProgressService?,
@StringRes nameRes: Int, @StringRes nameRes: Int,
totalProgress: Int, totalProgress: Int,
parentWeight: Float = 1f, parentWeight: Float = 1f,
@ -119,7 +124,7 @@ inline fun <T> reportSubtask(reporter: DefaultInitialSyncProgressService?,
} }
} }
inline fun <K, V, R> Map<out K, V>.mapWithProgress(reporter: DefaultInitialSyncProgressService?, internal inline fun <K, V, R> Map<out K, V>.mapWithProgress(reporter: DefaultInitialSyncProgressService?,
taskId: Int, taskId: Int,
weight: Float, weight: Float,
transform: (Map.Entry<K, V>) -> R): List<R> { transform: (Map.Entry<K, V>) -> R): List<R> {