Skip to content

Commit

Permalink
Added Patient search in reference app. (#518)
Browse files Browse the repository at this point in the history
* Added Patient search in reference app.

* Updated the signature of OnQueryTextListener callbacks

* Review comment changes
  • Loading branch information
aditya-07 authored Jun 3, 2021
1 parent 57a1fe8 commit 1987bf0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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.
Expand All @@ -99,4 +115,8 @@ class PatientListActivity() : AppCompatActivity() {
}
return true
}

override fun onBackPressed() {
if (searchView.query.isNotEmpty()) searchView.setQuery("", true) else super.onBackPressed()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -36,17 +38,28 @@ class PatientListViewModel(application: Application, private val fhirEngine: Fhi
AndroidViewModel(application) {

private val samplePatients = SamplePatients()
val liveSearchedPatients = MutableLiveData<List<PatientItem>>()

val liveSearchedPatients = liveData { emit(getSearchResults()) }
init {
fetchAndPost { getSearchResults() }
}

fun searchPatientsByName(nameQuery: String) {
fetchAndPost { getSearchResults(nameQuery) }
}

private fun fetchAndPost(search: suspend () -> List<PatientItem>) {
viewModelScope.launch { liveSearchedPatients.value = search() }
}

private suspend fun getSearchResults(): List<PatientItem> {
private suspend fun getSearchResults(nameQuery: String = ""): List<PatientItem> {
val searchResults: List<Patient> =
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
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
13 changes: 10 additions & 3 deletions reference/src/main/res/layout/activity_patient_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,22 @@

</com.google.android.material.appbar.AppBarLayout>

<FrameLayout
<LinearLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:orientation="vertical"
>

<androidx.appcompat.widget.SearchView
android:id="@+id/search"
app:iconifiedByDefault="false"
app:queryHint="Find by Patient Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<include layout="@layout/patient_list" />
</FrameLayout>
</LinearLayout>

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
Expand Down

0 comments on commit 1987bf0

Please sign in to comment.