Skip to content

Commit

Permalink
Merge pull request #4 from omarismail94/engine-codelab
Browse files Browse the repository at this point in the history
add update function to code
  • Loading branch information
jingtang10 authored Jun 30, 2023
2 parents b22af7a + eb8ee4f commit cd5020a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 17 deletions.
4 changes: 2 additions & 2 deletions codelabs/engine/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.fhir.codelabs.engine"
>

<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Codelab"
android:networkSecurityConfig="@xml/network_security_config"
android:name=".FhirApplication"
>
<activity android:name=".MainActivity" android:exported="true">
Expand All @@ -21,6 +22,5 @@
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package com.google.android.fhir.codelabs.engine

import android.app.Application
import android.content.Context
import android.util.Log
import com.google.android.fhir.DatabaseErrorStrategy.RECREATE_AT_OPEN
import com.google.android.fhir.FhirEngine
import com.google.android.fhir.FhirEngineConfiguration
import com.google.android.fhir.FhirEngineProvider
import com.google.android.fhir.ServerConfiguration
import com.google.android.fhir.sync.remote.HttpLogger

class FhirApplication : Application() {
private val fhirEngine: FhirEngine by lazy { FhirEngineProvider.getInstance(this) }
Expand All @@ -36,6 +38,12 @@ class FhirApplication : Application() {
RECREATE_AT_OPEN,
ServerConfiguration(
baseUrl = "http://10.0.2.2:8080/fhir/",
httpLogger =
HttpLogger(
HttpLogger.Configuration(
if (BuildConfig.DEBUG) HttpLogger.Level.BODY else HttpLogger.Level.BASIC
)
) { Log.d("App-HttpLog", it) },
),
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.google.android.fhir.codelabs.engine

import android.content.Context
import androidx.work.WorkerParameters
import com.google.android.fhir.sync.AcceptLocalConflictResolver
import com.google.android.fhir.sync.AcceptRemoteConflictResolver
import com.google.android.fhir.sync.FhirSyncWorker

Expand All @@ -26,7 +27,7 @@ class FhirSyncWorker(appContext: Context, workerParams: WorkerParameters) :

override fun getDownloadWorkManager() = PatientDownloadWorkManagerImpl()

override fun getConflictResolver() = AcceptRemoteConflictResolver
override fun getConflictResolver() = AcceptLocalConflictResolver

override fun getFhirEngine() = FhirApplication.fhirEngine(applicationContext)
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,51 @@ class MainActivityViewModel(application: Application) : AndroidViewModel(applica
}
}

/*
Fetches patients stored locally based on the city they are in, and then updates the city field for
each patient. Once that is complete, trigger a new sync so the changes can be uploaded.
*/
fun triggerUpdate() {
viewModelScope.launch {
val fhirEngine = FhirApplication.fhirEngine(getApplication())
val patientsFromWakefield =
fhirEngine
.search<Patient> {
filter(
Patient.ADDRESS_CITY,
{
modifier = StringFilterModifier.CONTAINS
value = "WAKEFIELD"
}
)
}

val patientsFromTaunton =
fhirEngine
.search<Patient> {
filter(
Patient.ADDRESS_CITY,
{
modifier = StringFilterModifier.CONTAINS
value = "TAUNTON"
}
)
}

patientsFromWakefield.forEach {
it.address.first().city = "TAUNTON"
fhirEngine.update(it)
}

patientsFromTaunton.forEach {
it.address.first().city = "WAKEFIELD"
fhirEngine.update(it)
}

triggerOneTimeSync()
}
}

fun searchPatientsByName(nameQuery: String) {
updatePatientListAndPatientCount { getSearchResults(nameQuery) }
}
Expand Down Expand Up @@ -82,9 +127,16 @@ class MainActivityViewModel(application: Application) : AndroidViewModel(applica
}
)
}
// Always only show patients from one city to see how triggering an update changes the
// patients listed
filter(
Patient.ADDRESS_CITY,
{
modifier = StringFilterModifier.CONTAINS
value = "WAKEFIELD"
}
)
sort(Patient.GIVEN, Order.ASCENDING)
count = 100
from = 0
}
.mapIndexed { index, fhirPatient -> fhirPatient.toPatientItem(index + 1) }
.let { patients.addAll(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,29 @@ class PatientListFragment : Fragment() {
setDrawable(ColorDrawable(Color.LTGRAY))
})
}

initSearchView()
initMenu()

viewModel.liveSearchedPatients.observe(viewLifecycleOwner) {
adapter.submitList(it)
}
initMenu()

viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
// Show the user a message when the sync is finished and then refresh the list of patients
// on the UI by sending a search patient request
viewModel.pollState.collect {
when (it) {
is SyncJobStatus.Started -> Toast.makeText(
requireContext(),
"Sync Started",
Toast.LENGTH_SHORT
).show()

is SyncJobStatus.InProgress -> {}
else -> {
Toast.makeText(requireContext(), "Sync Finished", Toast.LENGTH_SHORT).show()
is SyncJobStatus.Finished -> {
Toast.makeText(
requireContext(),
"Sync Finished",
Toast.LENGTH_SHORT
).show()
viewModel.searchPatientsByName("")
}

else -> {}
}

}
Expand All @@ -119,7 +120,7 @@ class PatientListFragment : Fragment() {
}

R.id.update -> {
//TODO: add code when triggered
viewModel.triggerUpdate()
true
}

Expand Down

0 comments on commit cd5020a

Please sign in to comment.