16
16
17
17
package org.matrix.android.sdk.internal.session.sync
18
18
19
- import androidx.lifecycle.MutableLiveData
19
+ import kotlinx.coroutines.CoroutineScope
20
+ import kotlinx.coroutines.flow.MutableSharedFlow
21
+ import kotlinx.coroutines.launch
20
22
import org.matrix.android.sdk.api.session.sync.InitialSyncStep
21
23
import org.matrix.android.sdk.api.session.sync.SyncRequestState
22
24
import org.matrix.android.sdk.internal.session.SessionScope
23
25
import javax.inject.Inject
24
26
25
27
@SessionScope
26
- internal class SyncRequestStateTracker @Inject constructor() :
27
- ProgressReporter {
28
+ internal class SyncRequestStateTracker @Inject constructor(
29
+ private val coroutineScope : CoroutineScope
30
+ ) : ProgressReporter {
28
31
29
- val syncRequestState = MutableLiveData <SyncRequestState >()
32
+ val syncRequestState = MutableSharedFlow <SyncRequestState >()
30
33
31
34
private var rootTask: TaskInfo ? = null
32
35
33
36
// Only to be used for incremental sync
34
37
fun setSyncRequestState (newSyncRequestState : SyncRequestState .IncrementalSyncRequestState ) {
35
- syncRequestState.postValue (newSyncRequestState)
38
+ emitSyncState (newSyncRequestState)
36
39
}
37
40
38
41
/* *
@@ -42,7 +45,9 @@ internal class SyncRequestStateTracker @Inject constructor() :
42
45
initialSyncStep : InitialSyncStep ,
43
46
totalProgress : Int
44
47
) {
45
- endAll()
48
+ if (rootTask != null ) {
49
+ endAll()
50
+ }
46
51
rootTask = TaskInfo (initialSyncStep, totalProgress, null , 1F )
47
52
reportProgress(0F )
48
53
}
@@ -71,7 +76,7 @@ internal class SyncRequestStateTracker @Inject constructor() :
71
76
// Update the progress of the leaf and all its parents
72
77
leaf.setProgress(progress)
73
78
// Then update the live data using leaf wording and root progress
74
- syncRequestState.postValue (SyncRequestState .InitialSyncProgressing (leaf.initialSyncStep, root.currentProgress.toInt()))
79
+ emitSyncState (SyncRequestState .InitialSyncProgressing (leaf.initialSyncStep, root.currentProgress.toInt()))
75
80
}
76
81
}
77
82
}
@@ -86,13 +91,19 @@ internal class SyncRequestStateTracker @Inject constructor() :
86
91
// And close it
87
92
endedTask.parent.child = null
88
93
} else {
89
- syncRequestState.postValue (SyncRequestState .Idle )
94
+ emitSyncState (SyncRequestState .Idle )
90
95
}
91
96
}
92
97
}
93
98
94
99
fun endAll () {
95
100
rootTask = null
96
- syncRequestState.postValue(SyncRequestState .Idle )
101
+ emitSyncState(SyncRequestState .Idle )
102
+ }
103
+
104
+ private fun emitSyncState (state : SyncRequestState ) {
105
+ coroutineScope.launch {
106
+ syncRequestState.emit(state)
107
+ }
97
108
}
98
109
}
0 commit comments