From 1987bf08176c38173a861ba4bbeda1ba060043f0 Mon Sep 17 00:00:00 2001 From: aditya-07 Date: Thu, 3 Jun 2021 15:08:15 +0530 Subject: [PATCH] Added Patient search in reference app. (#518) * Added Patient search in reference app. * Updated the signature of OnQueryTextListener callbacks * Review comment changes --- .../PatientItemRecyclerViewAdapter.kt | 2 +- .../fhir/reference/PatientListActivity.kt | 22 ++++++++++++- .../fhir/reference/PatientListViewModel.kt | 32 +++++++++++++------ .../fhir/reference/data/SamplePatients.kt | 10 +++++- .../main/res/layout/activity_patient_list.xml | 13 ++++++-- 5 files changed, 64 insertions(+), 15 deletions(-) diff --git a/reference/src/main/java/com/google/android/fhir/reference/PatientItemRecyclerViewAdapter.kt b/reference/src/main/java/com/google/android/fhir/reference/PatientItemRecyclerViewAdapter.kt index 0bf66d47f1..36bee70584 100644 --- a/reference/src/main/java/com/google/android/fhir/reference/PatientItemRecyclerViewAdapter.kt +++ b/reference/src/main/java/com/google/android/fhir/reference/PatientItemRecyclerViewAdapter.kt @@ -31,7 +31,7 @@ class PatientItemRecyclerViewAdapter( override fun areItemsTheSame( oldItem: PatientListViewModel.PatientItem, newItem: PatientListViewModel.PatientItem - ): Boolean = oldItem.id == newItem.id + ): Boolean = oldItem.resourceId == newItem.resourceId override fun areContentsTheSame( oldItem: PatientListViewModel.PatientItem, diff --git a/reference/src/main/java/com/google/android/fhir/reference/PatientListActivity.kt b/reference/src/main/java/com/google/android/fhir/reference/PatientListActivity.kt index 1712cd1624..243b29aaa5 100644 --- a/reference/src/main/java/com/google/android/fhir/reference/PatientListActivity.kt +++ b/reference/src/main/java/com/google/android/fhir/reference/PatientListActivity.kt @@ -24,6 +24,7 @@ import android.view.Menu import android.view.MenuInflater import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.view.menu.MenuBuilder +import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.Toolbar import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.RecyclerView @@ -40,7 +41,7 @@ import java.util.concurrent.TimeUnit class PatientListActivity() : AppCompatActivity() { private lateinit var fhirEngine: FhirEngine private lateinit var patientListViewModel: PatientListViewModel - + private lateinit var searchView: SearchView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Log.d("PatientListActivity", "onCreate() called") @@ -75,6 +76,21 @@ class PatientListActivity() : AppCompatActivity() { adapter.submitList(it) } ) + + searchView = findViewById(R.id.search) + searchView.setOnQueryTextListener( + object : SearchView.OnQueryTextListener { + override fun onQueryTextChange(newText: String): Boolean { + patientListViewModel.searchPatientsByName(newText) + return true + } + + override fun onQueryTextSubmit(query: String): Boolean { + patientListViewModel.searchPatientsByName(query) + return true + } + } + ) } // Click handler to help display the details about the patients from the list. @@ -99,4 +115,8 @@ class PatientListActivity() : AppCompatActivity() { } return true } + + override fun onBackPressed() { + if (searchView.query.isNotEmpty()) searchView.setQuery("", true) else super.onBackPressed() + } } diff --git a/reference/src/main/java/com/google/android/fhir/reference/PatientListViewModel.kt b/reference/src/main/java/com/google/android/fhir/reference/PatientListViewModel.kt index e5c975e66e..209128f64d 100644 --- a/reference/src/main/java/com/google/android/fhir/reference/PatientListViewModel.kt +++ b/reference/src/main/java/com/google/android/fhir/reference/PatientListViewModel.kt @@ -18,14 +18,16 @@ package com.google.android.fhir.reference import android.app.Application import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider -import androidx.lifecycle.liveData +import androidx.lifecycle.viewModelScope import com.google.android.fhir.FhirEngine import com.google.android.fhir.reference.data.SamplePatients import com.google.android.fhir.search.Order import com.google.android.fhir.search.StringFilterModifier import com.google.android.fhir.search.search +import kotlinx.coroutines.launch import org.hl7.fhir.r4.model.Patient /** @@ -36,17 +38,28 @@ class PatientListViewModel(application: Application, private val fhirEngine: Fhi AndroidViewModel(application) { private val samplePatients = SamplePatients() + val liveSearchedPatients = MutableLiveData>() - val liveSearchedPatients = liveData { emit(getSearchResults()) } + init { + fetchAndPost { getSearchResults() } + } + + fun searchPatientsByName(nameQuery: String) { + fetchAndPost { getSearchResults(nameQuery) } + } + + private fun fetchAndPost(search: suspend () -> List) { + viewModelScope.launch { liveSearchedPatients.value = search() } + } - private suspend fun getSearchResults(): List { + private suspend fun getSearchResults(nameQuery: String = ""): List { val searchResults: List = fhirEngine.search { - filter(Patient.ADDRESS_CITY) { - modifier = StringFilterModifier.MATCHES_EXACTLY - value = "NAIROBI" - } - filter(Patient.ACTIVE, true) + if (nameQuery.isNotEmpty()) + filter(Patient.NAME) { + modifier = StringFilterModifier.CONTAINS + value = nameQuery + } sort(Patient.GIVEN, Order.ASCENDING) count = 100 from = 0 @@ -61,7 +74,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 resourceId: String ) { override fun toString(): String = name } diff --git a/reference/src/main/java/com/google/android/fhir/reference/data/SamplePatients.kt b/reference/src/main/java/com/google/android/fhir/reference/data/SamplePatients.kt index 8576568e3e..ffe43c2c88 100644 --- a/reference/src/main/java/com/google/android/fhir/reference/data/SamplePatients.kt +++ b/reference/src/main/java/com/google/android/fhir/reference/data/SamplePatients.kt @@ -79,7 +79,15 @@ class SamplePatients { val html: String = if (patient.hasText()) patient.text.div.valueAsString else "" val phone: String = if (patient.hasTelecom()) patient.telecom[0].value else "" - return PatientListViewModel.PatientItem(position.toString(), name, gender, dob, html, phone) + return PatientListViewModel.PatientItem( + position.toString(), + name, + gender, + dob, + html, + phone, + patient.idElement.idPart + ) } /** Returns list of ObservationItem objects based on observations from the json string. */ diff --git a/reference/src/main/res/layout/activity_patient_list.xml b/reference/src/main/res/layout/activity_patient_list.xml index 3dcd66c1b6..f157a17730 100644 --- a/reference/src/main/res/layout/activity_patient_list.xml +++ b/reference/src/main/res/layout/activity_patient_list.xml @@ -40,15 +40,22 @@ - - + - +