Skip to content

Commit 77ab651

Browse files
committed
Show refresh loader whenever counts are loading
1 parent ae72def commit 77ab651

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/counters/CountersViewModel.kt

+35-12
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ import javax.inject.Inject
2424
import kotlinx.coroutines.ExperimentalCoroutinesApi
2525
import kotlinx.coroutines.flow.MutableStateFlow
2626
import kotlinx.coroutines.flow.SharingStarted
27-
import kotlinx.coroutines.flow.asStateFlow
27+
import kotlinx.coroutines.flow.combine
2828
import kotlinx.coroutines.flow.flatMapLatest
29+
import kotlinx.coroutines.flow.onCompletion
2930
import kotlinx.coroutines.flow.onEach
3031
import kotlinx.coroutines.flow.stateIn
3132
import kotlinx.coroutines.flow.update
@@ -44,39 +45,62 @@ constructor(
4445
val registerRepository: AppRegisterRepository,
4546
) : ViewModel() {
4647

47-
private val _isRefreshing = MutableStateFlow(false)
48-
val isRefreshing = _isRefreshing.asStateFlow()
48+
private val _isRefreshingPatientsCount = MutableStateFlow(false)
49+
private val _isRefreshingHomeTracingCount = MutableStateFlow(false)
50+
private val _isRefreshingPhoneTracingCount = MutableStateFlow(false)
51+
private val _isRefreshingAppointmentsCount = MutableStateFlow(false)
52+
53+
val isRefreshing =
54+
combine(
55+
_isRefreshingPatientsCount,
56+
_isRefreshingHomeTracingCount,
57+
_isRefreshingPhoneTracingCount,
58+
_isRefreshingAppointmentsCount,
59+
) { p, ht, pt, ap ->
60+
p || ht || pt || ap
61+
}
62+
.stateIn(viewModelScope, SharingStarted.Lazily, initialValue = false)
4963

5064
private val _refreshCounter = MutableStateFlow(0)
5165

5266
val patientsCountStateFlow =
5367
_refreshCounter
54-
.flatMapLatest { registerRepository.countRegisterData(healthModule = HealthModule.HIV) }
55-
.onEach { _isRefreshing.update { false } }
68+
.onEach { _isRefreshingPatientsCount.update { true } }
69+
.flatMapLatest {
70+
registerRepository.countRegisterData(healthModule = HealthModule.HIV).onCompletion {
71+
_isRefreshingPatientsCount.update { false }
72+
}
73+
}
5674
.stateIn(viewModelScope, SharingStarted.Lazily, initialValue = 0L)
5775

5876
val homeTracingCountStateFlow =
5977
_refreshCounter
78+
.onEach { _isRefreshingHomeTracingCount.update { true } }
6079
.flatMapLatest {
61-
registerRepository.countRegisterData(healthModule = HealthModule.HOME_TRACING)
80+
registerRepository
81+
.countRegisterData(healthModule = HealthModule.HOME_TRACING)
82+
.onCompletion { _isRefreshingHomeTracingCount.update { false } }
6283
}
63-
.onEach { _isRefreshing.update { false } }
6484
.stateIn(viewModelScope, SharingStarted.Lazily, initialValue = 0L)
6585

6686
val phoneTracingCountStateFlow =
6787
_refreshCounter
88+
.onEach { _isRefreshingPhoneTracingCount.update { true } }
6889
.flatMapLatest {
69-
registerRepository.countRegisterData(healthModule = HealthModule.PHONE_TRACING)
90+
registerRepository
91+
.countRegisterData(healthModule = HealthModule.PHONE_TRACING)
92+
.onCompletion { _isRefreshingPhoneTracingCount.update { false } }
7093
}
71-
.onEach { _isRefreshing.update { false } }
7294
.stateIn(viewModelScope, SharingStarted.Lazily, initialValue = 0L)
7395

7496
val appointmentsCountStateFlow =
7597
_refreshCounter
98+
.onEach { _isRefreshingAppointmentsCount.update { true } }
7699
.flatMapLatest {
77-
registerRepository.countRegisterData(healthModule = HealthModule.APPOINTMENT)
100+
registerRepository.countRegisterData(healthModule = HealthModule.APPOINTMENT).onCompletion {
101+
_isRefreshingAppointmentsCount.update { false }
102+
}
78103
}
79-
.onEach { _isRefreshing.update { false } }
80104
.stateIn(viewModelScope, SharingStarted.Lazily, initialValue = 0L)
81105

82106
init {
@@ -92,7 +116,6 @@ constructor(
92116
}
93117

94118
fun refresh() {
95-
_isRefreshing.update { true }
96119
_refreshCounter.update { it + 1 }
97120
}
98121
}

0 commit comments

Comments
 (0)