Skip to content

Commit

Permalink
Update the count field in the Patient list page based on screen data. (
Browse files Browse the repository at this point in the history
…#896)

* Update the count field in the Patient list page based on the data that is showing.

* Review comment changes
  • Loading branch information
aditya-07 authored Oct 29, 2021
1 parent 1c4d27b commit 0d15fe4
Showing 1 changed file with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
package com.google.android.fhir.reference

import android.app.Application
import android.util.Log
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.search.Order
Expand All @@ -42,36 +40,63 @@ class PatientListViewModel(application: Application, private val fhirEngine: Fhi
AndroidViewModel(application) {

val liveSearchedPatients = MutableLiveData<List<PatientItem>>()
val patientCount = liveData { emit(count()) }
val patientCount = MutableLiveData<Long>()

init {
fetchAndPost { getSearchResults() }
updatePatientListAndPatientCount({ getSearchResults() }, { count() })
}

fun searchPatientsByName(nameQuery: String) {
fetchAndPost { getSearchResults(nameQuery) }
updatePatientListAndPatientCount({ getSearchResults(nameQuery) }, { count(nameQuery) })
}

private fun fetchAndPost(search: suspend () -> List<PatientItem>) {
viewModelScope.launch { liveSearchedPatients.value = search() }
/**
* [updatePatientListAndPatientCount] calls the search and count lambda and updates the live data
* values accordingly. It is initially called when this [ViewModel] is created. Later its called
* by the client every time search query changes or data-sync is completed.
*/
private fun updatePatientListAndPatientCount(
search: suspend () -> List<PatientItem>,
count: suspend () -> Long
) {
viewModelScope.launch {
liveSearchedPatients.value = search()
patientCount.value = count()
}
}

private suspend fun count(): Long {
return fhirEngine.count<Patient> { filterCity(this) }
/**
* Returns count of all the [Patient] who match the filter criteria unlike [getSearchResults]
* which only returns a fixed range.
*/
private suspend fun count(nameQuery: String = ""): Long {
return fhirEngine.count<Patient> {
if (nameQuery.isNotEmpty()) {
filter(
Patient.NAME,
{
modifier = StringFilterModifier.CONTAINS
value = nameQuery
}
)
}
filterCity(this)
}
}

private suspend fun getSearchResults(nameQuery: String = ""): List<PatientItem> {
val patients: MutableList<PatientItem> = mutableListOf()
fhirEngine
.search<Patient> {
if (nameQuery.isNotEmpty())
if (nameQuery.isNotEmpty()) {
filter(
Patient.NAME,
{
modifier = StringFilterModifier.CONTAINS
value = nameQuery
}
)
}
filterCity(this)
sort(Patient.GIVEN, Order.ASCENDING)
count = 100
Expand All @@ -83,8 +108,7 @@ class PatientListViewModel(application: Application, private val fhirEngine: Fhi
val risks = getRiskAssessments()
patients.forEach { patient ->
risks["Patient/${patient.resourceId}"]?.let {
patient.risk = it?.prediction?.first()?.qualitativeRisk?.coding?.first()?.code
Log.d(TAG, "getSearchResults: ${patient.name} : ${patient.risk}")
patient.risk = it.prediction?.first()?.qualitativeRisk?.coding?.first()?.code
}
}
return patients
Expand Down

0 comments on commit 0d15fe4

Please sign in to comment.