@@ -24,8 +24,9 @@ import javax.inject.Inject
24
24
import kotlinx.coroutines.ExperimentalCoroutinesApi
25
25
import kotlinx.coroutines.flow.MutableStateFlow
26
26
import kotlinx.coroutines.flow.SharingStarted
27
- import kotlinx.coroutines.flow.asStateFlow
27
+ import kotlinx.coroutines.flow.combine
28
28
import kotlinx.coroutines.flow.flatMapLatest
29
+ import kotlinx.coroutines.flow.onCompletion
29
30
import kotlinx.coroutines.flow.onEach
30
31
import kotlinx.coroutines.flow.stateIn
31
32
import kotlinx.coroutines.flow.update
@@ -44,39 +45,62 @@ constructor(
44
45
val registerRepository: AppRegisterRepository ,
45
46
) : ViewModel () {
46
47
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 )
49
63
50
64
private val _refreshCounter = MutableStateFlow (0 )
51
65
52
66
val patientsCountStateFlow =
53
67
_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
+ }
56
74
.stateIn(viewModelScope, SharingStarted .Lazily , initialValue = 0L )
57
75
58
76
val homeTracingCountStateFlow =
59
77
_refreshCounter
78
+ .onEach { _isRefreshingHomeTracingCount .update { true } }
60
79
.flatMapLatest {
61
- registerRepository.countRegisterData(healthModule = HealthModule .HOME_TRACING )
80
+ registerRepository
81
+ .countRegisterData(healthModule = HealthModule .HOME_TRACING )
82
+ .onCompletion { _isRefreshingHomeTracingCount .update { false } }
62
83
}
63
- .onEach { _isRefreshing .update { false } }
64
84
.stateIn(viewModelScope, SharingStarted .Lazily , initialValue = 0L )
65
85
66
86
val phoneTracingCountStateFlow =
67
87
_refreshCounter
88
+ .onEach { _isRefreshingPhoneTracingCount .update { true } }
68
89
.flatMapLatest {
69
- registerRepository.countRegisterData(healthModule = HealthModule .PHONE_TRACING )
90
+ registerRepository
91
+ .countRegisterData(healthModule = HealthModule .PHONE_TRACING )
92
+ .onCompletion { _isRefreshingPhoneTracingCount .update { false } }
70
93
}
71
- .onEach { _isRefreshing .update { false } }
72
94
.stateIn(viewModelScope, SharingStarted .Lazily , initialValue = 0L )
73
95
74
96
val appointmentsCountStateFlow =
75
97
_refreshCounter
98
+ .onEach { _isRefreshingAppointmentsCount .update { true } }
76
99
.flatMapLatest {
77
- registerRepository.countRegisterData(healthModule = HealthModule .APPOINTMENT )
100
+ registerRepository.countRegisterData(healthModule = HealthModule .APPOINTMENT ).onCompletion {
101
+ _isRefreshingAppointmentsCount .update { false }
102
+ }
78
103
}
79
- .onEach { _isRefreshing .update { false } }
80
104
.stateIn(viewModelScope, SharingStarted .Lazily , initialValue = 0L )
81
105
82
106
init {
@@ -92,7 +116,6 @@ constructor(
92
116
}
93
117
94
118
fun refresh () {
95
- _isRefreshing .update { true }
96
119
_refreshCounter .update { it + 1 }
97
120
}
98
121
}
0 commit comments