Skip to content

Instantly share code, notes, and snippets.

@shubhamsgit
Created June 5, 2023 15:04
Show Gist options
  • Select an option

  • Save shubhamsgit/7bb5b6abd593aba58b6135130d714040 to your computer and use it in GitHub Desktop.

Select an option

Save shubhamsgit/7bb5b6abd593aba58b6135130d714040 to your computer and use it in GitHub Desktop.
SDK Function for fetching encounters from server and storing them locally
fun getAllEncountersByPatientUuidAndSaveLocally(uuid: String): Observable<List<Encounter>> {
val encounterList: MutableList<Encounter> = mutableListOf()
if (!NetworkUtils.isOnline()) throw Exception("Must be online to fetch encounters")
restApi.getAllEncountersForPatientByPatientUuid(uuid).execute().run {
if (isSuccessful && this.body() != null) {
val encounterResources: List<Resource> = this.body()!!.results
for (encounterResource in encounterResources) {
getEncounterByUuid(encounterResource.uuid!!).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ encounter ->
encounterList.add(encounter)
},
{ error ->
Log.e("Encounter Repository", "Error: ${error.message}")
},
{
Log.d("Encounter Repository", "Observable completed")
}
)
}
encounterDAO.deleteAllStandaloneEncounters(uuid) //delete previous list
encounterDAO.saveStandaloneEncounters(encounterList) //save latest list
return Observable.just(encounterList.toList())
} else {
throw Exception("Get Encounters error: ${message()}")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment