Skip to content

Commit

Permalink
Show on UI if task is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenreup committed Jul 31, 2024
1 parent cb14a25 commit 9e2d5ee
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import org.smartregister.fhircore.engine.configuration.ConfigurationRegistry
import org.smartregister.fhircore.engine.data.domain.Guardian
import org.smartregister.fhircore.engine.data.domain.PregnancyStatus
import org.smartregister.fhircore.engine.data.local.DefaultRepository
import org.smartregister.fhircore.engine.domain.model.CarePlanTask
import org.smartregister.fhircore.engine.domain.model.HealthStatus
import org.smartregister.fhircore.engine.domain.model.ProfileData
import org.smartregister.fhircore.engine.domain.model.RegisterData
Expand Down Expand Up @@ -245,7 +246,7 @@ constructor(
fhirEngine.withTransaction {
val patient = defaultRepository.loadResource<Patient>(resourceId)!!
val carePlan = patient.activeCarePlans(fhirEngine).firstOrNull()

val (exists, activities) = fetchCarePlanActivities(carePlan)
profileData =
ProfileData.HivProfileData(
logicalId = patient.logicalId,
Expand All @@ -265,7 +266,8 @@ constructor(
showIdentifierInProfile = true,
currentCarePlan = carePlan,
healthStatus = patient.extractHealthStatusFromMeta(patientTypeMetaTagCodingSystem),
tasks = fetchCarePlanActivities(carePlan),
tasks = activities,
hasMissingTasks = exists,
conditions = defaultRepository.activePatientConditions(patient.logicalId),
otherPatients = patient.otherChildren(),
guardians = patient.guardians(),
Expand Down Expand Up @@ -512,33 +514,48 @@ constructor(

private suspend fun fetchCarePlanActivities(
carePlan: CarePlan?,
): List<CarePlan.CarePlanActivityComponent> {
if (carePlan == null) return emptyList()
val activityOnList = mutableMapOf<String, CarePlan.CarePlanActivityComponent>()
val tasksToFetch = mutableListOf<String>()
for (planActivity in carePlan.activity) {
if (!planActivity.shouldShowOnProfile()) {
continue
}
val taskId = planActivity.outcomeReference.firstOrNull()?.extractId()
if (taskId != null) {
tasksToFetch.add(taskId)
activityOnList[taskId] = planActivity
): Pair<Boolean, List<CarePlanTask>> {
if (carePlan == null) return Pair(false, emptyList())

val activityOnList = mutableMapOf<String, CarePlanTask>()
val tasksToFetch =
carePlan.activity.mapNotNull { planActivity ->
if (planActivity.shouldShowOnProfile()) {
planActivity.outcomeReference.firstOrNull()?.extractId()?.also { taskId ->
activityOnList[taskId] = CarePlanTask(planActivity, false)
}
} else {
null
}
}
}
if (tasksToFetch.isNotEmpty()) {
val tasks = fhirEngine.getResourcesByIds<Task>(tasksToFetch)
tasks.forEach { task ->
val planActivity: CarePlan.CarePlanActivityComponent? = activityOnList[task.logicalId]
if (planActivity != null) {
planActivity.detail?.status = task.taskStatusToCarePlanActivityStatus()
activityOnList[task.logicalId] = planActivity

var hasMissingTask = false
val items: List<CarePlanTask> =
if (tasksToFetch.isNotEmpty()) {
val tasks = fhirEngine.getResourcesByIds<Task>(tasksToFetch).associateBy { it.logicalId }
activityOnList.map { (taskId, carePlanTask) ->
tasks[taskId]?.let { task ->
val updatedTask =
carePlanTask.task.apply { detail?.status = task.taskStatusToCarePlanActivityStatus() }
carePlanTask.copy(task = updatedTask, taskExists = true)
}
?: run {
if (carePlanTask.task.detail?.status != CarePlan.CarePlanActivityStatus.SCHEDULED) {
hasMissingTask = true
}
carePlanTask.copy(taskExists = false)
}
}
} else {
activityOnList.values.toList()
}
}
return activityOnList.values.sortedWith(
compareBy(nullsLast()) { it.detail?.code?.text?.toBigIntegerOrNull() },
)

val sortedItems =
items.sortedWith(
compareBy(nullsLast()) { it.task.detail?.code?.text?.toBigIntegerOrNull() },
)

return Pair(hasMissingTask, sortedItems)
}

object ResourceValue {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2021 Ona Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.smartregister.fhircore.engine.domain.model

import org.hl7.fhir.r4.model.CarePlan

data class CarePlanTask(
val task: CarePlan.CarePlanActivityComponent,
val taskExists: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ sealed class ProfileData(open val logicalId: String, open val name: String) {
val addressDistrict: String = "",
val addressTracingCatchment: String = "",
val addressPhysicalLocator: String = "",
val tasks: List<CarePlan.CarePlanActivityComponent> = listOf(),
val hasMissingTasks: Boolean = false,
val tasks: List<CarePlanTask> = listOf(),
val chwAssigned: Reference,
val healthStatus: HealthStatus,
val phoneContacts: List<String> = listOf(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ constructor(
hivPatientProfileData.copy(
tasks =
hivPatientProfileData.tasks.filter {
it.isGuardianVisit(applicationConfiguration.taskFilterTagViaMetaCodingSystem)
it.task.isGuardianVisit(applicationConfiguration.taskFilterTagViaMetaCodingSystem)
},
)
_patientProfileViewDataFlow.value =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Divider
import androidx.compose.material.Icon
import androidx.compose.material.MaterialTheme
import androidx.compose.material.OutlinedButton
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Error
import androidx.compose.material.icons.outlined.ChevronRight
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
Expand Down Expand Up @@ -69,7 +71,26 @@ fun ProfileActionableItem(
verticalAlignment = Alignment.CenterVertically,
) {
if (patientProfileRowItem.title.isEmpty() && patientProfileRowItem.subtitle.isEmpty()) {
ActionButton(patientProfileRowItem, modifier = modifier.fillMaxWidth(1f), onActionClick)
Row(verticalAlignment = Alignment.CenterVertically) {
if (!patientProfileRowItem.taskExists) {
Box(
modifier
.padding(end = 8.dp)
.clip(RoundedCornerShape(6.dp))
.background(MaterialTheme.colors.error)
.padding(8.dp),
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(2.dp)
) {
Icon(Icons.Filled.Error, contentDescription = "", tint = MaterialTheme.colors.onError)
Text(text = "Missing Task", color = MaterialTheme.colors.onError)
}
}
}
ActionButton(patientProfileRowItem, modifier = modifier.fillMaxWidth(1f), onActionClick)
}
} else {
Row(verticalAlignment = Alignment.CenterVertically) {
if (
Expand Down Expand Up @@ -258,6 +279,20 @@ fun ProfileActionableItemForVisitPreview() {
),
onActionClick = { _, _ -> },
)
Divider()
ProfileActionableItem(
PatientProfileRowItem(
id = "3",
title = "",
titleIcon = R.drawable.ic_pregnant,
subtitle = "",
profileViewSection = PatientProfileViewSection.TASKS,
actionButtonColor = OverdueColor,
actionButtonText = "Malaria medicine",
taskExists = false,
),
onActionClick = { _, _ -> },
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ data class PatientProfileRowItem(
val actionButtonText: String? = null,
val showAngleRightIcon: Boolean = false,
val showDot: Boolean = false,
val taskExists: Boolean = true,
)
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,24 @@ class ProfileViewDataMapper @Inject constructor(@ApplicationContext val context:
tasks =
inputModel.tasks.map {
PatientProfileRowItem(
id = it.outcomeReference.first().extractId(),
actionFormId = if (it.canBeCompleted()) it.getQuestionnaire() else null,
id = it.task.outcomeReference.first().extractId(),
actionFormId = if (it.task.canBeCompleted()) it.task.getQuestionnaire() else null,
title = "", // it.description,
subtitle = "", // context.getString(R.string.due_on,
// it.executionPeriod.start.makeItReadable()),
taskExists = it.taskExists,
profileViewSection = PatientProfileViewSection.TASKS,
actionButtonIcon =
if (it.detail.status == CarePlan.CarePlanActivityStatus.COMPLETED) {
if (it.task.detail.status == CarePlan.CarePlanActivityStatus.COMPLETED) {
Icons.Filled.Check
} else Icons.Filled.Add,
actionIconColor =
if (it.detail.status == CarePlan.CarePlanActivityStatus.COMPLETED) {
if (it.task.detail.status == CarePlan.CarePlanActivityStatus.COMPLETED) {
SuccessColor
} else it.detail.status.retrieveColorCode(),
actionButtonColor = it.detail.status.retrieveColorCode(),
actionButtonText = it.getQuestionnaireName(),
subtitleStatus = it.detail.status.name,
} else it.task.detail.status.retrieveColorCode(),
actionButtonColor = it.task.detail.status.retrieveColorCode(),
actionButtonText = it.task.getQuestionnaireName(),
subtitleStatus = it.task.detail.status.name,
)
},
practitioners = inputModel.practitioners,
Expand Down

0 comments on commit 9e2d5ee

Please sign in to comment.