Created
June 5, 2023 15:04
-
-
Save shubhamsgit/7bb5b6abd593aba58b6135130d714040 to your computer and use it in GitHub Desktop.
SDK Function for fetching encounters from server and storing them locally
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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