Skip to content

Commit 4b71ca8

Browse files
committed
Show refresh loader whenever counts are loading
1 parent ae72def commit 4b71ca8

File tree

3 files changed

+85
-28
lines changed

3 files changed

+85
-28
lines changed

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

+39-17
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ fun CountersScreen(
5959
Scaffold(
6060
topBar = {
6161
Column(
62-
modifier = modifier.fillMaxWidth().background(MaterialTheme.colors.primary),
62+
modifier = modifier
63+
.fillMaxWidth()
64+
.background(MaterialTheme.colors.primary),
6365
) {
6466
Row(
6567
verticalAlignment = Alignment.CenterVertically,
@@ -84,10 +86,18 @@ fun CountersScreen(
8486
) { innerPadding ->
8587
Box(modifier = modifier.padding(innerPadding)) {
8688
val isRefreshing by countersViewModel.isRefreshing.collectAsState()
89+
8790
val patientsCount by countersViewModel.patientsCountStateFlow.collectAsState()
91+
val isRefreshingPatientsCount by countersViewModel.isRefreshingPatientsCountStateFlow.collectAsState()
92+
8893
val homeTracingCount by countersViewModel.homeTracingCountStateFlow.collectAsState()
94+
val isRefreshingHomeTracingCount by countersViewModel.isRefreshingHomeTracingCountStateFlow.collectAsState()
95+
8996
val phoneTracingCount by countersViewModel.phoneTracingCountStateFlow.collectAsState()
97+
val isRefreshingPhoneTracingCount by countersViewModel.isRefreshingPhoneTracingCountStateFlow.collectAsState()
98+
9099
val appointmentsCount by countersViewModel.appointmentsCountStateFlow.collectAsState()
100+
val isRefreshingAppointmentsCount by countersViewModel.isRefreshingAppointmentsCountStateFlow.collectAsState()
91101

92102
SwipeRefresh(
93103
state = rememberSwipeRefreshState(isRefreshing),
@@ -97,20 +107,23 @@ fun CountersScreen(
97107
LazyColumn {
98108
item {
99109
Card(
100-
modifier = Modifier.padding(8.dp).fillMaxWidth().height(IntrinsicSize.Min),
110+
modifier = Modifier
111+
.padding(8.dp)
112+
.fillMaxWidth()
113+
.height(IntrinsicSize.Min),
101114
) {
102115
Box(
103116
modifier = Modifier.padding(8.dp),
104117
) {
105118
Column {
106119
Text(
107-
text = "Patients",
108-
style = MaterialTheme.typography.h4,
120+
text = stringResource(R.string.patients_counter_label),
121+
style = MaterialTheme.typography.h4.copy(color = Color.Gray),
109122
)
110123
Spacer(modifier = Modifier.height(8.dp))
111124
Text(
112125
text = "$patientsCount",
113-
style = MaterialTheme.typography.h2.copy(color = Color.Gray),
126+
style = if (isRefreshingPatientsCount) MaterialTheme.typography.h2.copy(color = Color.Gray.copy(alpha = 0.5F)) else MaterialTheme.typography.h2,
114127
)
115128
}
116129
}
@@ -119,20 +132,23 @@ fun CountersScreen(
119132

120133
item {
121134
Card(
122-
modifier = Modifier.padding(8.dp).fillMaxWidth().height(IntrinsicSize.Min),
135+
modifier = Modifier
136+
.padding(8.dp)
137+
.fillMaxWidth()
138+
.height(IntrinsicSize.Min),
123139
) {
124140
Box(
125141
modifier = Modifier.padding(8.dp),
126142
) {
127143
Column {
128144
Text(
129-
text = "Home Tracing",
130-
style = MaterialTheme.typography.h4,
145+
text = stringResource(R.string.home_tracing_conter_label),
146+
style = MaterialTheme.typography.h4.copy(color = Color.Gray),
131147
)
132148
Spacer(modifier = Modifier.height(8.dp))
133149
Text(
134150
text = "$homeTracingCount",
135-
style = MaterialTheme.typography.h2.copy(color = Color.Gray),
151+
style = if (isRefreshingHomeTracingCount) MaterialTheme.typography.h2.copy(color = Color.Gray.copy(alpha = 0.5F)) else MaterialTheme.typography.h2,
136152
)
137153
}
138154
}
@@ -141,20 +157,23 @@ fun CountersScreen(
141157

142158
item {
143159
Card(
144-
modifier = Modifier.padding(8.dp).fillMaxWidth().height(IntrinsicSize.Min),
160+
modifier = Modifier
161+
.padding(8.dp)
162+
.fillMaxWidth()
163+
.height(IntrinsicSize.Min),
145164
) {
146165
Box(
147166
modifier = Modifier.padding(8.dp),
148167
) {
149168
Column {
150169
Text(
151-
text = "Phone Tracing",
152-
style = MaterialTheme.typography.h4,
170+
text = stringResource(R.string.phone_tracing_counter_label),
171+
style = MaterialTheme.typography.h4.copy(color = Color.Gray),
153172
)
154173
Spacer(modifier = Modifier.height(8.dp))
155174
Text(
156175
text = "$phoneTracingCount",
157-
style = MaterialTheme.typography.h2.copy(color = Color.Gray),
176+
style = if (isRefreshingPhoneTracingCount) MaterialTheme.typography.h2.copy(color = Color.Gray.copy(alpha = 0.5F)) else MaterialTheme.typography.h2,
158177
)
159178
}
160179
}
@@ -163,20 +182,23 @@ fun CountersScreen(
163182

164183
item {
165184
Card(
166-
modifier = Modifier.padding(8.dp).fillMaxWidth().height(IntrinsicSize.Min),
185+
modifier = Modifier
186+
.padding(8.dp)
187+
.fillMaxWidth()
188+
.height(IntrinsicSize.Min),
167189
) {
168190
Box(
169191
modifier = Modifier.padding(8.dp),
170192
) {
171193
Column {
172194
Text(
173-
text = "Appointments",
174-
style = MaterialTheme.typography.h4,
195+
text = stringResource(R.string.appointments_counter_label),
196+
style = MaterialTheme.typography.h4.copy(color = Color.Gray),
175197
)
176198
Spacer(modifier = Modifier.height(8.dp))
177199
Text(
178200
text = "$appointmentsCount",
179-
style = MaterialTheme.typography.h2.copy(color = Color.Gray),
201+
style = if (isRefreshingAppointmentsCount) MaterialTheme.typography.h2.copy(color = Color.Gray.copy(alpha = 0.5F)) else MaterialTheme.typography.h2,
180202
)
181203
}
182204
}

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

+42-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
2525
import kotlinx.coroutines.flow.MutableStateFlow
2626
import kotlinx.coroutines.flow.SharingStarted
2727
import kotlinx.coroutines.flow.asStateFlow
28+
import kotlinx.coroutines.flow.combine
2829
import kotlinx.coroutines.flow.flatMapLatest
30+
import kotlinx.coroutines.flow.onCompletion
2931
import kotlinx.coroutines.flow.onEach
3032
import kotlinx.coroutines.flow.stateIn
3133
import kotlinx.coroutines.flow.update
@@ -44,39 +46,69 @@ constructor(
4446
val registerRepository: AppRegisterRepository,
4547
) : ViewModel() {
4648

47-
private val _isRefreshing = MutableStateFlow(false)
48-
val isRefreshing = _isRefreshing.asStateFlow()
49+
private val _isRefreshingPatientsCount = MutableStateFlow(false)
50+
val isRefreshingPatientsCountStateFlow = _isRefreshingPatientsCount.asStateFlow()
51+
52+
private val _isRefreshingHomeTracingCount = MutableStateFlow(false)
53+
val isRefreshingHomeTracingCountStateFlow = _isRefreshingHomeTracingCount.asStateFlow()
54+
55+
private val _isRefreshingPhoneTracingCount = MutableStateFlow(false)
56+
val isRefreshingPhoneTracingCountStateFlow = _isRefreshingPhoneTracingCount.asStateFlow()
57+
58+
private val _isRefreshingAppointmentsCount = MutableStateFlow(false)
59+
val isRefreshingAppointmentsCountStateFlow = _isRefreshingAppointmentsCount.asStateFlow()
60+
61+
val isRefreshing =
62+
combine(
63+
_isRefreshingPatientsCount,
64+
_isRefreshingHomeTracingCount,
65+
_isRefreshingPhoneTracingCount,
66+
_isRefreshingAppointmentsCount,
67+
) { p, ht, pt, ap ->
68+
p || ht || pt || ap
69+
}
70+
.stateIn(viewModelScope, SharingStarted.Lazily, initialValue = false)
4971

5072
private val _refreshCounter = MutableStateFlow(0)
5173

5274
val patientsCountStateFlow =
5375
_refreshCounter
54-
.flatMapLatest { registerRepository.countRegisterData(healthModule = HealthModule.HIV) }
55-
.onEach { _isRefreshing.update { false } }
76+
.onEach { _isRefreshingPatientsCount.update { true } }
77+
.flatMapLatest {
78+
registerRepository.countRegisterData(healthModule = HealthModule.HIV).onCompletion {
79+
_isRefreshingPatientsCount.update { false }
80+
}
81+
}
5682
.stateIn(viewModelScope, SharingStarted.Lazily, initialValue = 0L)
5783

5884
val homeTracingCountStateFlow =
5985
_refreshCounter
86+
.onEach { _isRefreshingHomeTracingCount.update { true } }
6087
.flatMapLatest {
61-
registerRepository.countRegisterData(healthModule = HealthModule.HOME_TRACING)
88+
registerRepository
89+
.countRegisterData(healthModule = HealthModule.HOME_TRACING)
90+
.onCompletion { _isRefreshingHomeTracingCount.update { false } }
6291
}
63-
.onEach { _isRefreshing.update { false } }
6492
.stateIn(viewModelScope, SharingStarted.Lazily, initialValue = 0L)
6593

6694
val phoneTracingCountStateFlow =
6795
_refreshCounter
96+
.onEach { _isRefreshingPhoneTracingCount.update { true } }
6897
.flatMapLatest {
69-
registerRepository.countRegisterData(healthModule = HealthModule.PHONE_TRACING)
98+
registerRepository
99+
.countRegisterData(healthModule = HealthModule.PHONE_TRACING)
100+
.onCompletion { _isRefreshingPhoneTracingCount.update { false } }
70101
}
71-
.onEach { _isRefreshing.update { false } }
72102
.stateIn(viewModelScope, SharingStarted.Lazily, initialValue = 0L)
73103

74104
val appointmentsCountStateFlow =
75105
_refreshCounter
106+
.onEach { _isRefreshingAppointmentsCount.update { true } }
76107
.flatMapLatest {
77-
registerRepository.countRegisterData(healthModule = HealthModule.APPOINTMENT)
108+
registerRepository.countRegisterData(healthModule = HealthModule.APPOINTMENT).onCompletion {
109+
_isRefreshingAppointmentsCount.update { false }
110+
}
78111
}
79-
.onEach { _isRefreshing.update { false } }
80112
.stateIn(viewModelScope, SharingStarted.Lazily, initialValue = 0L)
81113

82114
init {
@@ -92,7 +124,6 @@ constructor(
92124
}
93125

94126
fun refresh() {
95-
_isRefreshing.update { true }
96127
_refreshCounter.update { it + 1 }
97128
}
98129
}

android/quest/src/main/res/values/strings.xml

+4
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,8 @@
140140
<string name="filter_appointment_reason">Appointment Reason</string>
141141
<string name="clear_filter">Clear Filter</string>
142142
<string name="counters" translatable="false">Counters</string>
143+
<string name="patients_counter_label">Patients</string>
144+
<string name="home_tracing_conter_label">Home Tracing</string>
145+
<string name="phone_tracing_counter_label">Phone Tracing</string>
146+
<string name="appointments_counter_label">Appointments</string>
143147
</resources>

0 commit comments

Comments
 (0)