Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(chore) patient file enhancements #85

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions core/src/main/java/org/openmrs/android/fhir/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,10 @@ class MainActivity : AppCompatActivity() {

private fun setLocationInDrawer() {
lifecycleScope.launch {
val selectedLocationName = applicationContext?.dataStore?.data?.first()?.get(
PreferenceKeys.LOCATION_NAME)
if (selectedLocationName != null) {
binding.navigationView.menu.findItem(R.id.menu_current_location).title = selectedLocationName
}
binding.navigationView.menu.findItem(R.id.menu_current_location).title =
applicationContext?.dataStore?.data
?.first()
?.get(PreferenceKeys.LOCATION_NAME) ?: getString(R.string.no_location_selected)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ class PatientDetailsEncounterItemViewHolder(
val encounter = it.encounter;
binding.name.text = encounter.type
binding.fieldName.text = encounter.dateTime
binding.syncItemStatus.visibility = if (encounter.isSynced!! && encounter.isSynced) View.GONE else View.VISIBLE
binding.name.setOnClickListener {
onEditEncounterClick(
encounter.encounterId ?: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ class OpenMRSHelper {
): Map<Encounter, List<Encounter>> {
val visits: MutableMap<Encounter, List<Encounter>> = HashMap()

val patientResult = fhirEngine.search<Patient> {
filter(Resource.RES_ID, { value = of(patientId) })
revInclude<Encounter>(Encounter.SUBJECT)
}

val allEncounters = LinkedList<Encounter>();

fhirEngine.search<Encounter> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,19 @@ import kotlinx.coroutines.launch
import org.openmrs.android.fhir.viewmodel.EditPatientViewModel
import org.openmrs.android.fhir.MainActivity
import org.openmrs.android.fhir.R
import org.openmrs.android.fhir.auth.dataStore
import org.openmrs.android.fhir.data.PreferenceKeys
import org.openmrs.android.fhir.databinding.AddPatientFragmentBinding
import kotlinx.coroutines.flow.first


/** A fragment representing Edit Patient screen. This fragment is contained in a [MainActivity]. */
class EditPatientFragment : Fragment(R.layout.add_patient_fragment) {
private val viewModel: EditPatientViewModel by viewModels()
private var _binding: AddPatientFragmentBinding? = null

private val binding
get() = _binding!!

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -42,9 +51,18 @@ class EditPatientFragment : Fragment(R.layout.add_patient_fragment) {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
_binding = AddPatientFragmentBinding.bind(view)
(requireActivity() as AppCompatActivity).supportActionBar?.apply {
title = requireContext().getString(R.string.edit_patient)
}

lifecycleScope.launch {
val selectedLocation = context?.applicationContext?.dataStore?.data
?.first()
?.get(PreferenceKeys.LOCATION_NAME)

binding.currentLocationLabel.text = selectedLocation ?: getString(R.string.no_location_selected)
}
requireArguments()
.putString(QUESTIONNAIRE_FILE_PATH_KEY, "new-patient-registration-paginated.json")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.hl7.fhir.r4.model.*
import org.openmrs.android.fhir.FhirApplication
import org.openmrs.android.fhir.extensions.readFileFromAssets
import java.util.UUID
import java.util.Date

class EditEncounterViewModel(application: Application, private val state: SavedStateHandle) :
AndroidViewModel(application) {
Expand Down Expand Up @@ -101,18 +102,30 @@ class EditEncounterViewModel(application: Application, private val state: SavedS
val existingObservation = observations.find { obs ->
obs.code.coding.any { coding -> coding.code == resource.code.codingFirstRep.code }
}
if (existingObservation != null) {
resource.id = existingObservation.id
} else {
resource.id = UUID.randomUUID().toString()

if(existingObservation != null && existingObservation.value.equalsDeep(resource.value)) {
return;
}
resource.subject = subjectReference
resource.encounter = encounterReference

if (existingObservation != null) {
updateResourceToDatabase(resource)
existingObservation?.apply {
id = existingObservation.id
status = Observation.ObservationStatus.AMENDED
value = resource.value
}

if (existingObservation != null && existingObservation.hasValue()) {
updateResourceToDatabase(existingObservation)
} else {
createResourceToDatabase(resource)
resource.apply {
id = UUID.randomUUID().toString()
subject = subjectReference
encounter = encounterReference
status = Observation.ObservationStatus.FINAL
effective = DateTimeType(Date())
}
if(resource.hasValue()){
createResourceToDatabase(resource)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ class EditPatientViewModel(application: Application, private val state: SavedSta

private val patientId: String = requireNotNull(state["patient_id"])
val livePatientData = liveData { emit(prepareEditPatient()) }
lateinit var originalPatient: Patient

private suspend fun prepareEditPatient(): Pair<String, String> {
val patient = fhirEngine.get<Patient>(patientId)
originalPatient = patient
val launchContexts = mapOf<String, Resource>("client" to patient)
val question =
getApplication<Application>()
Expand Down Expand Up @@ -94,6 +96,7 @@ class EditPatientViewModel(application: Application, private val state: SavedSta
patient.hasTelecom() &&
patient.telecom[0].value != null
) {
patient.name[0].id = originalPatient.name[0].id
patient.id = patientId
fhirEngine.update(patient)
isPatientSaved.value = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,15 @@ class GenericFormEntryViewModel(application: Application, private val state: Sav
when (val resource = it.resource) {
is Observation -> {
if (resource.hasCode() && resource.hasValue()) {
resource.id = generateUuid()
resource.subject = patientReference
resource.encounter = encounterReference
resource.effective = DateTimeType(Date())
resource.status = Observation.ObservationStatus.FINAL

resource.apply {
id = generateUuid()
subject = patientReference
encounter = encounterReference
status = Observation.ObservationStatus.FINAL
effective = DateTimeType(Date())
value = resource.value
}
saveResourceToDatabase(resource)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,19 @@ class PatientDetailsViewModel(
}

encounters.forEach { encounter ->
data.addEncounterData(encounter)
val isSynced = fhirEngine.getLocalChanges(ResourceType.Encounter, encounter.logicalId).isEmpty()
data.addEncounterData(encounter, isSynced)
}
}

return data
return data.sortedBy {
when (it) {
is PatientDetailOverview -> false
is PatientDetailHeader -> false
is PatientDetailEncounter -> it.encounter.isSynced
else -> true
}
}
}

private fun MutableList<PatientDetailData>.addPatientDetailData(
Expand Down Expand Up @@ -118,8 +126,8 @@ class PatientDetailsViewModel(
add(PatientDetailVisit(visitItem))
}

private fun MutableList<PatientDetailData>.addEncounterData(encounter: Encounter) {
add(PatientDetailEncounter(createEncounterItem(encounter)))
private fun MutableList<PatientDetailData>.addEncounterData(encounter: Encounter, isSynced: Boolean) {
add(PatientDetailEncounter(createEncounterItem(encounter, isSynced)))
}

private fun createVisitItem(visit: Encounter): PatientListViewModel.VisitItem {
Expand All @@ -140,7 +148,7 @@ class PatientDetailsViewModel(
)
}

private fun createEncounterItem(encounter: Encounter): PatientListViewModel.EncounterItem {
private fun createEncounterItem(encounter: Encounter, isSynced: Boolean): PatientListViewModel.EncounterItem {
val visitType = encounter.type.firstOrNull()?.coding?.firstOrNull()?.display ?: "Type"
val visitDate = encounter.period?.start?.let {
SimpleDateFormat("dd MMM yyyy", Locale.getDefault()).format(it)
Expand All @@ -152,7 +160,8 @@ class PatientDetailsViewModel(
visitDate,
encounter.type.getOrNull(0)?.id,
encounter.type.getOrNull(0)?.text,
"assessment.json"
"assessment.json",
isSynced
)
}

Expand Down Expand Up @@ -215,7 +224,7 @@ data class PatientDetailVisit(
override val lastInGroup: Boolean = false,
) : PatientDetailData

data class PatientProperty(val header: String, val value: String)
data class PatientProperty(val header: String, val value: String, val isSynced: Boolean)

class PatientDetailsViewModelFactory(
private val application: Application,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ class PatientListViewModel(application: Application, private val fhirEngine: Fhi
val dateTime: String,
val formCode: String?,
val formDisplay: String?,
val formResource: String?
val formResource: String?,
val isSynced: Boolean?,
) {
override fun toString(): String = encounterId ?: type
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/res/drawable/baseline_edit_24.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="@color/edit_menu_color"
android:tint="@color/black"
>
<path
android:fillColor="@android:color/white"
Expand Down
15 changes: 7 additions & 8 deletions core/src/main/res/layout/item_encounter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
android:orientation="vertical"
android:padding="8dp">

<TextView
android:id="@+id/encounterDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Encounter Date"
android:textSize="14sp"
android:textColor="@android:color/darker_gray"/>
<TextView
android:id="@+id/encounterType"
android:layout_width="wrap_content"
Expand All @@ -13,12 +20,4 @@
android:textSize="16sp"
android:layout_marginBottom="4dp"/>

<TextView
android:id="@+id/encounterDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Encounter Date"
android:textSize="14sp"
android:textColor="@android:color/darker_gray"/>

</LinearLayout>
2 changes: 1 addition & 1 deletion core/src/main/res/layout/patient_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
android:layout_marginBottom="24dp"
android:clickable="true"
android:backgroundTint="@color/surfaceVariant_neutral_variant_30"
android:text="@string/edit_encounter"
android:text="@string/create_encounter"
android:textColor="@color/white"
app:iconTint="@color/white"
app:icon="@drawable/ic_baseline_add_24"
Expand Down
60 changes: 38 additions & 22 deletions core/src/main/res/layout/patient_property_item_view.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginStart="@dimen/text_margin"
android:padding="8dp">
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_marginStart="@dimen/text_margin"
android:padding="8dp">

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/inter"
android:text="Navya Patel"
android:textSize="16sp"
android:layout_marginBottom="4dp"/>
<ImageView
android:id="@+id/sync_item_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_sync_24"
app:tint="@color/moderate_risk"
android:layout_gravity="center"
android:layout_marginEnd="8dp"/>

<TextView
android:id="@+id/field_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/inter"
android:text="50 years old"
android:textSize="14sp"
android:textColor="@android:color/darker_gray"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/field_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/inter"
android:text="50 years old"
android:textSize="14sp"
android:textColor="@android:color/darker_gray"/>

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/inter"
android:text="Navya Patel"
android:textSize="16sp"
android:layout_marginTop="4dp"/>

</LinearLayout>

</LinearLayout>
1 change: 1 addition & 0 deletions core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<string name="patient_property_gender">Gender</string>
<string name="register_new_patient">Register new patient</string>
<string name="edit_patient">Edit Patient</string>
<string name="create_encounter">Create Encounter</string>
<string name="edit_encounter">Edit Encounter</string>
<string name="location">Location</string>
<string name="identifier">Identifier</string>
Expand Down