Skip to content

Commit 5cf7907

Browse files
siddh1004sagarwal
andauthored
Migrate MedicalHistoryDiagnosisWithTreatment to jetpack compose (#5593)
https://app.shortcut.com/simpledotorg/story/16595/migrate-hypertension-and-diabetes-diagnosis-to-ui-compose <img width="1198" height="2531" alt="Screenshot_20250908_140009" src="https://github.com/user-attachments/assets/cd2044cd-411e-40b8-bd2d-e335caa10456" /> <img width="1198" height="2531" alt="Screenshot_20250908_141940" src="https://github.com/user-attachments/assets/f46bfff0-3e7f-4b33-8ada-4ac69329a720" /> --------- Co-authored-by: sagarwal <[email protected]>
1 parent f19e001 commit 5cf7907

File tree

10 files changed

+273
-444
lines changed

10 files changed

+273
-444
lines changed

app/src/main/java/org/simple/clinic/medicalhistory/MedicalHistoryDiagnosisWithTreatment.kt

Lines changed: 0 additions & 123 deletions
This file was deleted.

app/src/main/java/org/simple/clinic/medicalhistory/newentry/NewMedicalHistoryScreen.kt

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import android.content.Context
44
import android.os.Bundle
55
import android.view.LayoutInflater
66
import android.view.View
7-
import android.view.View.GONE
8-
import android.view.View.VISIBLE
97
import android.view.ViewGroup
108
import androidx.appcompat.app.AppCompatActivity
119
import androidx.compose.foundation.layout.Arrangement
@@ -18,6 +16,7 @@ import androidx.compose.runtime.setValue
1816
import androidx.compose.ui.Modifier
1917
import androidx.compose.ui.platform.ViewCompositionStrategy
2018
import androidx.compose.ui.res.dimensionResource
19+
import androidx.compose.ui.res.stringResource
2120
import com.google.android.material.dialog.MaterialAlertDialogBuilder
2221
import com.jakewharton.rxbinding3.view.clicks
2322
import com.spotify.mobius.functions.Consumer
@@ -35,7 +34,6 @@ import org.simple.clinic.di.injector
3534
import org.simple.clinic.feature.Feature
3635
import org.simple.clinic.feature.Features
3736
import org.simple.clinic.medicalhistory.Answer
38-
import org.simple.clinic.medicalhistory.MedicalHistoryQuestion
3937
import org.simple.clinic.medicalhistory.MedicalHistoryQuestion.DiagnosedWithDiabetes
4038
import org.simple.clinic.medicalhistory.MedicalHistoryQuestion.DiagnosedWithHypertension
4139
import org.simple.clinic.medicalhistory.MedicalHistoryQuestion.IsOnDiabetesTreatment
@@ -45,6 +43,7 @@ import org.simple.clinic.medicalhistory.SelectDiagnosisErrorDialog
4543
import org.simple.clinic.medicalhistory.SelectOngoingDiabetesTreatmentErrorDialog
4644
import org.simple.clinic.medicalhistory.SelectOngoingHypertensionTreatmentErrorDialog
4745
import org.simple.clinic.medicalhistory.ui.HistoryContainer
46+
import org.simple.clinic.medicalhistory.ui.MedicalHistoryDiagnosisWithTreatment
4847
import org.simple.clinic.medicalhistory.ui.TobaccoQuestion
4948
import org.simple.clinic.navigation.v2.Router
5049
import org.simple.clinic.navigation.v2.ScreenKey
@@ -98,15 +97,12 @@ class NewMedicalHistoryScreen : BaseScreen<
9897
private val nextButton
9998
get() = binding.nextButton
10099

101-
private val hypertensionDiagnosis
102-
get() = binding.hypertensionDiagnosis
103-
104-
private val diabetesDiagnosis
105-
get() = binding.diabetesDiagnosis
106-
107100
private var showSmokerQuestion by mutableStateOf(false)
108101
private var showSmokelessTobaccoQuestion by mutableStateOf(false)
109102
private var showDiabetesQuestion by mutableStateOf(false)
103+
private var showHypertensionTreatmentQuestion by mutableStateOf(false)
104+
private var showDiabetesTreatmentQuestion by mutableStateOf(false)
105+
private var showDiabetesDiagnosis by mutableStateOf(false)
110106
private var ongoingMedicalHistoryEntry by mutableStateOf<OngoingMedicalHistoryEntry?>(null)
111107

112108
private val composeView
@@ -175,6 +171,28 @@ class NewMedicalHistoryScreen : BaseScreen<
175171
.padding(dimensionResource(R.dimen.spacing_8)),
176172
verticalArrangement = Arrangement.spacedBy(dimensionResource(R.dimen.spacing_8))
177173
) {
174+
MedicalHistoryDiagnosisWithTreatment(
175+
diagnosisLabel = stringResource(R.string.medicalhistory_diagnosis_hypertension_required),
176+
diagnosisQuestion = DiagnosedWithHypertension,
177+
diagnosisAnswer = ongoingMedicalHistoryEntry?.diagnosedWithHypertension,
178+
treatmentQuestion = IsOnHypertensionTreatment(country.isoCountryCode),
179+
treatmentAnswer = ongoingMedicalHistoryEntry?.isOnHypertensionTreatment,
180+
showTreatmentQuestion = showHypertensionTreatmentQuestion
181+
) { question, answer ->
182+
hotEvents.onNext(NewMedicalHistoryAnswerToggled(question, answer))
183+
}
184+
if (showDiabetesDiagnosis) {
185+
MedicalHistoryDiagnosisWithTreatment(
186+
diagnosisLabel = stringResource(R.string.medicalhistory_diagnosis_diabetes_required),
187+
diagnosisQuestion = DiagnosedWithDiabetes,
188+
diagnosisAnswer = ongoingMedicalHistoryEntry?.hasDiabetes,
189+
treatmentQuestion = IsOnDiabetesTreatment,
190+
treatmentAnswer = ongoingMedicalHistoryEntry?.isOnDiabetesTreatment,
191+
showTreatmentQuestion = showDiabetesTreatmentQuestion
192+
) { question, answer ->
193+
hotEvents.onNext(NewMedicalHistoryAnswerToggled(question, answer))
194+
}
195+
}
178196
HistoryContainer(
179197
heartAttackAnswer = ongoingMedicalHistoryEntry?.hasHadHeartAttack,
180198
strokeAnswer = ongoingMedicalHistoryEntry?.hasHadStroke,
@@ -216,11 +234,11 @@ class NewMedicalHistoryScreen : BaseScreen<
216234
}
217235

218236
override fun showDiabetesDiagnosisView() {
219-
diabetesDiagnosis.visibility = VISIBLE
237+
showDiabetesDiagnosis = true
220238
}
221239

222240
override fun hideDiabetesDiagnosisView() {
223-
diabetesDiagnosis.visibility = GONE
241+
showDiabetesDiagnosis = false
224242
}
225243

226244
override fun hideDiabetesHistorySection() {
@@ -231,24 +249,6 @@ class NewMedicalHistoryScreen : BaseScreen<
231249
showDiabetesQuestion = true
232250
}
233251

234-
override fun renderDiagnosisAnswer(question: MedicalHistoryQuestion, answer: Answer) {
235-
val view = when (question) {
236-
DiagnosedWithHypertension -> hypertensionDiagnosis
237-
DiagnosedWithDiabetes -> diabetesDiagnosis
238-
else -> null
239-
}
240-
241-
val label = when (question) {
242-
DiagnosedWithHypertension -> R.string.medicalhistory_diagnosis_hypertension_required
243-
DiagnosedWithDiabetes -> R.string.medicalhistory_diagnosis_diabetes_required
244-
else -> question.questionRes
245-
}
246-
247-
view?.renderDiagnosis(label, question, answer) { questionForView, newAnswer ->
248-
hotEvents.onNext(NewMedicalHistoryAnswerToggled(questionForView, newAnswer))
249-
}
250-
}
251-
252252
override fun showNextButtonProgress() {
253253
nextButton.setButtonState(InProgress)
254254
}
@@ -258,32 +258,19 @@ class NewMedicalHistoryScreen : BaseScreen<
258258
}
259259

260260
override fun showHypertensionTreatmentQuestion(answer: Answer) {
261-
hypertensionDiagnosis.renderTreatmentQuestion(
262-
question = IsOnHypertensionTreatment(country.isoCountryCode),
263-
answer = answer
264-
) { questionForView, newAnswer ->
265-
hotEvents.onNext(NewMedicalHistoryAnswerToggled(questionForView, newAnswer))
266-
}
267-
268-
hypertensionDiagnosis.showTreatmentQuestion()
261+
showHypertensionTreatmentQuestion = true
269262
}
270263

271264
override fun hideHypertensionTreatmentQuestion() {
272-
hypertensionDiagnosis.hideTreatmentQuestion()
273-
hypertensionDiagnosis.clearTreatmentChipGroup()
265+
showHypertensionTreatmentQuestion = false
274266
}
275267

276268
override fun showDiabetesTreatmentQuestion(answer: Answer) {
277-
diabetesDiagnosis.renderTreatmentQuestion(IsOnDiabetesTreatment, answer) { questionForView, newAnswer ->
278-
hotEvents.onNext(NewMedicalHistoryAnswerToggled(questionForView, newAnswer))
279-
}
280-
281-
diabetesDiagnosis.showTreatmentQuestion()
269+
showDiabetesTreatmentQuestion = true
282270
}
283271

284272
override fun hideDiabetesTreatmentQuestion() {
285-
diabetesDiagnosis.hideTreatmentQuestion()
286-
diabetesDiagnosis.clearTreatmentChipGroup()
273+
showDiabetesTreatmentQuestion = false
287274
}
288275

289276
override fun showCurrentSmokerQuestion() {

app/src/main/java/org/simple/clinic/medicalhistory/newentry/NewMedicalHistoryUi.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import org.simple.clinic.medicalhistory.OngoingMedicalHistoryEntry
77
interface NewMedicalHistoryUi {
88
fun setPatientName(patientName: String)
99
fun populateOngoingMedicalHistoryEntry(ongoingMedicalHistoryEntry: OngoingMedicalHistoryEntry)
10-
fun renderDiagnosisAnswer(question: MedicalHistoryQuestion, answer: Answer)
1110
fun showDiabetesDiagnosisView()
1211
fun hideDiabetesDiagnosisView()
1312
fun showDiabetesHistorySection()

app/src/main/java/org/simple/clinic/medicalhistory/newentry/NewMedicalHistoryUiRenderer.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.simple.clinic.medicalhistory.newentry
22

3-
import org.simple.clinic.medicalhistory.MedicalHistoryQuestion.DiagnosedWithDiabetes
4-
import org.simple.clinic.medicalhistory.MedicalHistoryQuestion.DiagnosedWithHypertension
53
import org.simple.clinic.mobius.ViewRenderer
64

75
class NewMedicalHistoryUiRenderer(
@@ -33,7 +31,6 @@ class NewMedicalHistoryUiRenderer(
3331

3432
private fun renderMedicalHistoryQuestions(model: NewMedicalHistoryModel) {
3533
with(model.ongoingMedicalHistoryEntry) {
36-
ui.renderDiagnosisAnswer(DiagnosedWithHypertension, diagnosedWithHypertension)
3734
renderHypertensionTreatmentQuestion(model)
3835
}
3936
ui.populateOngoingMedicalHistoryEntry(model.ongoingMedicalHistoryEntry)
@@ -50,7 +47,6 @@ class NewMedicalHistoryUiRenderer(
5047
private fun renderDiabetesManagementEnabled(model: NewMedicalHistoryModel) {
5148
ui.showDiabetesDiagnosisView()
5249
ui.hideDiabetesHistorySection()
53-
ui.renderDiagnosisAnswer(DiagnosedWithDiabetes, model.ongoingMedicalHistoryEntry.hasDiabetes)
5450
renderDiabetesTreatmentQuestion(model)
5551
}
5652

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.simple.clinic.medicalhistory.ui
2+
3+
import androidx.compose.animation.animateColorAsState
4+
import androidx.compose.foundation.layout.Arrangement
5+
import androidx.compose.foundation.layout.Row
6+
import androidx.compose.foundation.layout.padding
7+
import androidx.compose.material.ChipDefaults
8+
import androidx.compose.material.ExperimentalMaterialApi
9+
import androidx.compose.material.FilterChip
10+
import androidx.compose.material.MaterialTheme
11+
import androidx.compose.material.Text
12+
import androidx.compose.runtime.Composable
13+
import androidx.compose.runtime.getValue
14+
import androidx.compose.ui.Alignment
15+
import androidx.compose.ui.Modifier
16+
import androidx.compose.ui.res.dimensionResource
17+
import androidx.compose.ui.res.stringResource
18+
import androidx.compose.ui.text.style.TextAlign
19+
import org.simple.clinic.R
20+
import org.simple.clinic.medicalhistory.Answer
21+
22+
@Composable
23+
fun AnswerChipsGroup(
24+
modifier: Modifier = Modifier,
25+
selectedAnswer: Answer?,
26+
onSelectionChange: (Answer) -> Unit
27+
) {
28+
Row(
29+
modifier = modifier,
30+
horizontalArrangement = Arrangement.spacedBy(dimensionResource(R.dimen.spacing_8)),
31+
verticalAlignment = Alignment.CenterVertically,
32+
) {
33+
MedicalHistoryAnswerChip(
34+
label = stringResource(R.string.newmedicalhistory_yes),
35+
selected = selectedAnswer == Answer.Yes,
36+
onSelectionChange = {
37+
onSelectionChange(Answer.Yes)
38+
}
39+
)
40+
41+
MedicalHistoryAnswerChip(
42+
label = stringResource(R.string.newmedicalhistory_no),
43+
selected = selectedAnswer == Answer.No,
44+
onSelectionChange = {
45+
onSelectionChange(Answer.No)
46+
}
47+
)
48+
}
49+
}
50+
51+
@OptIn(ExperimentalMaterialApi::class)
52+
@Composable
53+
fun MedicalHistoryAnswerChip(
54+
label: String,
55+
selected: Boolean,
56+
onSelectionChange: () -> Unit
57+
) {
58+
val backgroundColor by animateColorAsState(
59+
if (selected) MaterialTheme.colors.primary else MaterialTheme.colors.primaryVariant
60+
)
61+
val textColor by animateColorAsState(
62+
if (selected) MaterialTheme.colors.onPrimary else MaterialTheme.colors.primary
63+
)
64+
65+
FilterChip(
66+
selected = selected,
67+
colors = ChipDefaults.filterChipColors(
68+
backgroundColor = backgroundColor,
69+
),
70+
onClick = onSelectionChange,
71+
) {
72+
Text(
73+
modifier = Modifier.padding(horizontal = dimensionResource(R.dimen.spacing_12)),
74+
text = label,
75+
style = MaterialTheme.typography.body2,
76+
color = textColor,
77+
textAlign = TextAlign.Center,
78+
)
79+
}
80+
}

0 commit comments

Comments
 (0)