Skip to content

Commit

Permalink
Wrap FhirEngine apis within IO Dispatcher (google#2557)
Browse files Browse the repository at this point in the history
* Wrap fhirengine apis within io dispatcher

* Make functions look more kotlin like
  • Loading branch information
MJ1998 authored Jul 18, 2024
1 parent 7b24b62 commit 1391a04
Showing 1 changed file with 25 additions and 34 deletions.
59 changes: 25 additions & 34 deletions engine/src/main/java/com/google/android/fhir/impl/FhirEngineImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,60 +33,51 @@ import com.google.android.fhir.sync.upload.ResourceConsolidatorFactory
import com.google.android.fhir.sync.upload.SyncUploadProgress
import com.google.android.fhir.sync.upload.UploadRequestResult
import com.google.android.fhir.sync.upload.UploadStrategy
import java.time.OffsetDateTime
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.withContext
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.ResourceType

/** Implementation of [FhirEngine]. */
internal class FhirEngineImpl(private val database: Database, private val context: Context) :
FhirEngine {
override suspend fun create(vararg resource: Resource): List<String> {
return database.insert(*resource)
}
override suspend fun create(vararg resource: Resource) =
withContext(Dispatchers.IO) { database.insert(*resource) }

override suspend fun get(type: ResourceType, id: String): Resource {
return database.select(type, id)
}
override suspend fun get(type: ResourceType, id: String) =
withContext(Dispatchers.IO) { database.select(type, id) }

override suspend fun update(vararg resource: Resource) {
database.update(*resource)
}
override suspend fun update(vararg resource: Resource) =
withContext(Dispatchers.IO) { database.update(*resource) }

override suspend fun delete(type: ResourceType, id: String) {
database.delete(type, id)
}
override suspend fun delete(type: ResourceType, id: String) =
withContext(Dispatchers.IO) { database.delete(type, id) }

override suspend fun <R : Resource> search(search: Search): List<SearchResult<R>> {
return search.execute(database)
}
override suspend fun <R : Resource> search(search: Search): List<SearchResult<R>> =
withContext(Dispatchers.IO) { search.execute(database) }

override suspend fun count(search: Search): Long {
return search.count(database)
}
override suspend fun count(search: Search) =
withContext(Dispatchers.IO) { search.count(database) }

override suspend fun getLastSyncTimeStamp(): OffsetDateTime? {
return FhirEngineProvider.getFhirDataStore(context).readLastSyncTimestamp()
}
override suspend fun getLastSyncTimeStamp() =
withContext(Dispatchers.IO) {
FhirEngineProvider.getFhirDataStore(context).readLastSyncTimestamp()
}

override suspend fun clearDatabase() {
database.clearDatabase()
}
override suspend fun clearDatabase() = withContext(Dispatchers.IO) { database.clearDatabase() }

override suspend fun getLocalChanges(type: ResourceType, id: String): List<LocalChange> {
return database.getLocalChanges(type, id)
}
override suspend fun getLocalChanges(type: ResourceType, id: String) =
withContext(Dispatchers.IO) { database.getLocalChanges(type, id) }

override suspend fun purge(type: ResourceType, id: String, forcePurge: Boolean) {
database.purge(type, setOf(id), forcePurge)
}
override suspend fun purge(type: ResourceType, id: String, forcePurge: Boolean) =
withContext(Dispatchers.IO) { database.purge(type, setOf(id), forcePurge) }

override suspend fun purge(type: ResourceType, ids: Set<String>, forcePurge: Boolean) {
database.purge(type, ids, forcePurge)
}
override suspend fun purge(type: ResourceType, ids: Set<String>, forcePurge: Boolean) =
withContext(Dispatchers.IO) { database.purge(type, ids, forcePurge) }

override suspend fun syncDownload(
conflictResolver: ConflictResolver,
Expand Down

0 comments on commit 1391a04

Please sign in to comment.