16
16
17
17
package org.smartregister.fhircore.quest.ui.patient.profile
18
18
19
+ import android.app.Activity
20
+ import androidx.activity.compose.rememberLauncherForActivityResult
21
+ import androidx.activity.result.contract.ActivityResultContracts
19
22
import androidx.compose.foundation.background
20
23
import androidx.compose.foundation.layout.Arrangement
21
24
import androidx.compose.foundation.layout.Box
@@ -32,6 +35,7 @@ import androidx.compose.material.DropdownMenu
32
35
import androidx.compose.material.DropdownMenuItem
33
36
import androidx.compose.material.Icon
34
37
import androidx.compose.material.IconButton
38
+ import androidx.compose.material.LinearProgressIndicator
35
39
import androidx.compose.material.Scaffold
36
40
import androidx.compose.material.Text
37
41
import androidx.compose.material.TopAppBar
@@ -60,6 +64,7 @@ import androidx.hilt.navigation.compose.hiltViewModel
60
64
import androidx.navigation.NavHostController
61
65
import java.util.Locale
62
66
import org.smartregister.fhircore.engine.R
67
+ import org.smartregister.fhircore.engine.domain.util.DataLoadState
63
68
import org.smartregister.fhircore.engine.ui.components.FormButton
64
69
import org.smartregister.fhircore.engine.ui.theme.PatientProfileSectionsBackgroundColor
65
70
import org.smartregister.fhircore.engine.ui.theme.WelcomeServiceBackToCareColor
@@ -88,81 +93,99 @@ fun PatientProfileScreen(
88
93
val taskId by appMainViewModel.completedTaskId.collectAsState()
89
94
val syncing by remember { patientProfileViewModel.isSyncing }
90
95
val tasksId = profileViewData.tasks.map { it.actionFormId }
96
+ val loadingState by patientProfileViewModel.loadingState.collectAsState()
91
97
92
- LaunchedEffect (taskId) {
93
- taskId?.let { patientProfileViewModel.fetchPatientProfileDataWithChildren() }
94
- }
98
+ val launchQuestionnaireActivityForResults =
99
+ rememberLauncherForActivityResult(
100
+ contract = ActivityResultContracts .StartActivityForResult (),
101
+ onResult = {
102
+ if (it.resultCode == Activity .RESULT_OK && it.data != null ) {
103
+ patientProfileViewModel.reFetch()
104
+ }
105
+ },
106
+ )
107
+
108
+ LaunchedEffect (taskId) { taskId?.let { patientProfileViewModel.reFetch() } }
95
109
96
110
Scaffold (
97
111
topBar = {
98
- TopAppBar (
99
- title = { Text (stringResource(R .string.profile)) },
100
- navigationIcon = {
101
- IconButton (onClick = { navController.popBackStack() }) {
102
- Icon (Icons .Filled .ArrowBack , null )
103
- }
104
- },
105
- actions = {
106
- IconButton (onClick = { patientProfileViewModel.reSync() }, enabled = ! syncing) {
107
- Icon (
108
- imageVector = Icons .Outlined .Refresh ,
109
- contentDescription = null ,
110
- tint = Color .White ,
111
- )
112
- }
113
- IconButton (onClick = { showOverflowMenu = ! showOverflowMenu }) {
114
- Icon (
115
- imageVector = Icons .Outlined .MoreVert ,
116
- contentDescription = null ,
117
- tint = Color .White ,
118
- )
119
- }
120
- DropdownMenu (
121
- expanded = showOverflowMenu,
122
- onDismissRequest = { showOverflowMenu = false },
123
- ) {
124
- viewState.visibleOverflowMenuItems().forEach {
125
- DropdownMenuItem (
126
- onClick = {
127
- showOverflowMenu = false
128
- patientProfileViewModel.onEvent(
129
- PatientProfileEvent .OverflowMenuClick (navController, context, it.id),
130
- )
131
- },
132
- contentPadding = PaddingValues (horizontal = 16 .dp, vertical = 8 .dp),
133
- modifier =
134
- modifier
135
- .fillMaxWidth()
136
- .background(
137
- color =
138
- if (it.confirmAction) {
139
- it.titleColor.copy(alpha = 0.1f )
140
- } else Color .Transparent ,
141
- ),
142
- ) {
143
- when (it.id) {
144
- R2 .id.view_children -> {
145
- Text (text = profileViewData.viewChildText, color = it.titleColor)
146
- }
147
- R2 .id.view_guardians -> {
148
- Text (
149
- text = stringResource(it.titleResource, profileViewData.guardians.size),
150
- color = it.titleColor,
112
+ Column (Modifier .fillMaxWidth()) {
113
+ TopAppBar (
114
+ title = { Text (stringResource(R .string.profile)) },
115
+ navigationIcon = {
116
+ IconButton (onClick = { navController.popBackStack() }) {
117
+ Icon (Icons .Filled .ArrowBack , null )
118
+ }
119
+ },
120
+ actions = {
121
+ IconButton (onClick = { patientProfileViewModel.reFetch() }, enabled = ! syncing) {
122
+ Icon (
123
+ imageVector = Icons .Outlined .Refresh ,
124
+ contentDescription = null ,
125
+ tint = Color .White ,
126
+ )
127
+ }
128
+ IconButton (onClick = { showOverflowMenu = ! showOverflowMenu }) {
129
+ Icon (
130
+ imageVector = Icons .Outlined .MoreVert ,
131
+ contentDescription = null ,
132
+ tint = Color .White ,
133
+ )
134
+ }
135
+ DropdownMenu (
136
+ expanded = showOverflowMenu,
137
+ onDismissRequest = { showOverflowMenu = false },
138
+ ) {
139
+ viewState.visibleOverflowMenuItems().forEach {
140
+ DropdownMenuItem (
141
+ onClick = {
142
+ showOverflowMenu = false
143
+ patientProfileViewModel.onEvent(
144
+ PatientProfileEvent .OverflowMenuClick (navController, context, it.id),
151
145
)
152
- }
153
- else -> {
154
- Text (text = stringResource(id = it.titleResource), color = it.titleColor)
146
+ },
147
+ contentPadding = PaddingValues (horizontal = 16 .dp, vertical = 8 .dp),
148
+ modifier =
149
+ modifier
150
+ .fillMaxWidth()
151
+ .background(
152
+ color =
153
+ if (it.confirmAction) {
154
+ it.titleColor.copy(alpha = 0.1f )
155
+ } else Color .Transparent ,
156
+ ),
157
+ ) {
158
+ when (it.id) {
159
+ R2 .id.view_children -> {
160
+ Text (text = profileViewData.viewChildText, color = it.titleColor)
161
+ }
162
+ R2 .id.view_guardians -> {
163
+ Text (
164
+ text = stringResource(it.titleResource, profileViewData.guardians.size),
165
+ color = it.titleColor,
166
+ )
167
+ }
168
+ else -> {
169
+ Text (text = stringResource(id = it.titleResource), color = it.titleColor)
170
+ }
155
171
}
156
172
}
157
173
}
158
174
}
159
- }
160
- },
161
- )
175
+ },
176
+ )
177
+ if (loadingState is DataLoadState .Loading ) {
178
+ LinearProgressIndicator (modifier = Modifier .fillMaxWidth())
179
+ }
180
+ }
162
181
},
163
182
) { innerPadding ->
164
- Column (modifier = modifier.fillMaxHeight().fillMaxWidth()) {
165
- Box (modifier = Modifier .padding(innerPadding).weight(2.0f )) {
183
+ Column (
184
+ modifier = modifier.fillMaxHeight().fillMaxWidth(),
185
+ ) {
186
+ Box (
187
+ modifier = Modifier .padding(innerPadding).weight(2.0f ),
188
+ ) {
166
189
Column (
167
190
modifier =
168
191
modifier
@@ -202,10 +225,10 @@ fun PatientProfileScreen(
202
225
) {
203
226
profileViewData.tasks.forEach {
204
227
ProfileActionableItem (
205
- it,
228
+ if (loadingState is DataLoadState . Loading ) it.copy(actionFormId = null ) else it,
206
229
onActionClick = { taskFormId, taskId ->
207
- patientProfileViewModel.onEvent (
208
- PatientProfileEvent . OpenTaskForm (
230
+ launchQuestionnaireActivityForResults.launch (
231
+ patientProfileViewModel.createLaunchTaskIntent (
209
232
context = context,
210
233
taskFormId = taskFormId,
211
234
taskId = taskId,
@@ -228,8 +251,11 @@ fun PatientProfileScreen(
228
251
FormButton (
229
252
formButtonData = it,
230
253
onFormClick = { questionnaireId, _ ->
231
- patientProfileViewModel.onEvent(
232
- PatientProfileEvent .LoadQuestionnaire (questionnaireId, context),
254
+ launchQuestionnaireActivityForResults.launch(
255
+ patientProfileViewModel.createLaunchQuestionnaireIntent(
256
+ context = context,
257
+ questionnaireId = questionnaireId,
258
+ ),
233
259
)
234
260
},
235
261
)
@@ -285,14 +311,14 @@ fun PatientProfileScreen(
285
311
modifier = Modifier .fillMaxWidth().padding(0 .dp),
286
312
shape = RectangleShape ,
287
313
onClick = {
288
- patientProfileViewModel.onEvent (
289
- PatientProfileEvent . FinishVisit (
314
+ launchQuestionnaireActivityForResults.launch (
315
+ patientProfileViewModel.createLaunchTaskIntent (
290
316
context = context,
291
- formId = PatientProfileViewModel .PATIENT_FINISH_VISIT ,
317
+ taskFormId = PatientProfileViewModel .PATIENT_FINISH_VISIT ,
292
318
),
293
319
)
294
320
},
295
- enabled = profileViewData.tasksCompleted,
321
+ enabled = profileViewData.tasksCompleted && loadingState !is DataLoadState . Loading ,
296
322
) {
297
323
Text (
298
324
modifier = Modifier .padding(10 .dp),
0 commit comments