Repository: opensrp/fhircore
Issue: Patient bio data info not loading on profile view

After updates made to the [match fhir sdk update + uploading fhir resources PR](https://github.com/OpenSRP/fhircore/pull/83) , patient bio data is not being displayed on the profile view.

Changed files:
android/app/src/main/java/org/smartregister/fhircore/fragment/PatientDetailFragment.kt
android/app/src/main/java/org/smartregister/fhircore/data/SamplePatients.kt
android/app/src/main/java/org/smartregister/fhircore/viewmodel/PatientListViewModel.kt
android/app/src/main/java/org/smartregister/fhircore/activity/PatientListActivity.kt

diff --git a/android/app/src/main/java/org/smartregister/fhircore/activity/PatientListActivity.kt b/android/app/src/main/java/org/smartregister/fhircore/activity/PatientListActivity.kt
index afc329f9e..2fb2d08e5 100644
--- a/android/app/src/main/java/org/smartregister/fhircore/activity/PatientListActivity.kt
+++ b/android/app/src/main/java/org/smartregister/fhircore/activity/PatientListActivity.kt
@@ -114,7 +114,7 @@ class PatientListActivity : AppCompatActivity() {
   private fun onPatientItemClicked(patientItem: PatientListViewModel.PatientItem) {
     val intent =
       Intent(this.applicationContext, PatientDetailActivity::class.java).apply {
-        putExtra(PatientDetailFragment.ARG_ITEM_ID, patientItem.id)
+        putExtra(PatientDetailFragment.ARG_ITEM_ID, patientItem.logicalId)
       }
     this.startActivity(intent)
   }
diff --git a/android/app/src/main/java/org/smartregister/fhircore/data/SamplePatients.kt b/android/app/src/main/java/org/smartregister/fhircore/data/SamplePatients.kt
index cbcaac794..e52e5dfaa 100644
--- a/android/app/src/main/java/org/smartregister/fhircore/data/SamplePatients.kt
+++ b/android/app/src/main/java/org/smartregister/fhircore/data/SamplePatients.kt
@@ -18,6 +18,7 @@ package org.smartregister.fhircore.data
 
 import ca.uhn.fhir.context.FhirContext
 import ca.uhn.fhir.parser.IParser
+import com.google.android.fhir.logicalId
 import org.hl7.fhir.r4.model.Bundle
 import org.hl7.fhir.r4.model.Observation
 import org.hl7.fhir.r4.model.Patient
@@ -78,8 +79,17 @@ class SamplePatients {
     val dob = if (patient.hasBirthDateElement()) patient.birthDateElement.valueAsString else ""
     val html: String = if (patient.hasText()) patient.text.div.valueAsString else ""
     val phone: String = if (patient.hasTelecom()) patient.telecom[0].value else ""
+    val logicalId: String = patient.logicalId
 
-    return PatientListViewModel.PatientItem(position.toString(), name, gender, dob, html, phone)
+    return PatientListViewModel.PatientItem(
+      position.toString(),
+      name,
+      gender,
+      dob,
+      html,
+      phone,
+      logicalId
+    )
   }
 
   /** Returns list of ObservationItem objects based on observations from the json string. */
diff --git a/android/app/src/main/java/org/smartregister/fhircore/fragment/PatientDetailFragment.kt b/android/app/src/main/java/org/smartregister/fhircore/fragment/PatientDetailFragment.kt
index f6249111c..d2802e089 100644
--- a/android/app/src/main/java/org/smartregister/fhircore/fragment/PatientDetailFragment.kt
+++ b/android/app/src/main/java/org/smartregister/fhircore/fragment/PatientDetailFragment.kt
@@ -51,7 +51,6 @@ class PatientDetailFragment : Fragment() {
     recyclerView.adapter = adapter
 
     val fhirEngine: FhirEngine = FhirApplication.fhirEngine(requireContext())
-    var patient: PatientListViewModel.PatientItem? = null
 
     val viewModel: PatientListViewModel =
       ViewModelProvider(
@@ -67,27 +66,21 @@ class PatientDetailFragment : Fragment() {
         Observer<List<PatientListViewModel.ObservationItem>> { adapter.submitList(it) }
       )
 
+    viewModel.liveSearchPatient.observe(
+      viewLifecycleOwner,
+      Observer<PatientListViewModel.PatientItem> { setupPatientData(it) }
+    )
+
     arguments?.let {
       if (it.containsKey(ARG_ITEM_ID)) {
-        it.getString(ARG_ITEM_ID)?.let { it1 -> observePatientList(viewModel, it1) }
+        it.getString(ARG_ITEM_ID)?.let { it1 -> viewModel.getPatientItem(it1) }
       }
     }
-
     viewModel.getSearchResults()
 
     return rootView
   }
 
-  // Workaround till search by id is implemented
-  private fun observePatientList(viewModel: PatientListViewModel, itemId: String) {
-    viewModel.liveSearchedPatients.observe(
-      viewLifecycleOwner,
-      Observer<List<PatientListViewModel.PatientItem>> {
-        setupPatientData(it.associateBy { it.id }[itemId])
-      }
-    )
-  }
-
   private fun setupPatientData(patient: PatientListViewModel.PatientItem?) {
     val gender = if (patient?.gender == "male") 'M' else 'F'
     if (patient != null) {
@@ -98,7 +91,7 @@ class PatientDetailFragment : Fragment() {
           ", " +
           patient?.dob?.let { it1 -> Utils.getAgeFromDate(it1) }
       activity?.findViewById<TextView>(R.id.patient_bio_data)?.text = patientDetailLabel
-      activity?.findViewById<TextView>(R.id.id_patient_number)?.text = "ID: " + patient.id
+      activity?.findViewById<TextView>(R.id.id_patient_number)?.text = "ID: " + patient.logicalId
     }
   }
 
diff --git a/android/app/src/main/java/org/smartregister/fhircore/viewmodel/PatientListViewModel.kt b/android/app/src/main/java/org/smartregister/fhircore/viewmodel/PatientListViewModel.kt
index 7a21913ee..5c13c777c 100644
--- a/android/app/src/main/java/org/smartregister/fhircore/viewmodel/PatientListViewModel.kt
+++ b/android/app/src/main/java/org/smartregister/fhircore/viewmodel/PatientListViewModel.kt
@@ -57,6 +57,8 @@ class PatientListViewModel(application: Application, private val fhirEngine: Fhi
     return liveObservations
   }
 
+  val liveSearchPatient: MutableLiveData<PatientItem> by lazy { MutableLiveData<PatientItem>() }
+
   fun getSearchResults(query: String? = null) {
     viewModelScope.launch {
       val searchResults: List<Patient> =
@@ -81,6 +83,16 @@ class PatientListViewModel(application: Application, private val fhirEngine: Fhi
     }
   }
 
+  fun getPatientItem(id: String) {
+    var patientItems: List<PatientItem>? = null
+    viewModelScope.launch {
+      var patient = fhirEngine.load(Patient::class.java, id)
+      patientItems = samplePatients.getPatientItems(listOf(patient))
+    }
+
+    liveSearchPatient.value = patientItems?.get(0)
+  }
+
   fun syncUpload() {
     viewModelScope.launch { fhirEngine.syncUpload() }
   }
@@ -101,7 +113,8 @@ class PatientListViewModel(application: Application, private val fhirEngine: Fhi
     val gender: String,
     val dob: String,
     val html: String,
-    val phone: String
+    val phone: String,
+    val logicalId: String
   ) {
     override fun toString(): String = name
   }