-
Notifications
You must be signed in to change notification settings - Fork 188
/
SingleSourceOfTruthStrategy.kt
33 lines (30 loc) · 1.28 KB
/
SingleSourceOfTruthStrategy.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.elifox.legocatalog.data
import androidx.lifecycle.LiveData
import androidx.lifecycle.liveData
import androidx.lifecycle.map
import com.elifox.legocatalog.data.Result.Status.ERROR
import com.elifox.legocatalog.data.Result.Status.SUCCESS
import kotlinx.coroutines.Dispatchers
/**
* The database serves as the single source of truth.
* Therefore UI can receive data updates from database only.
* Function notify UI about:
* [Result.Status.SUCCESS] - with data from database
* [Result.Status.ERROR] - if error has occurred from any source
* [Result.Status.LOADING]
*/
fun <T, A> resultLiveData(databaseQuery: () -> LiveData<T>,
networkCall: suspend () -> Result<A>,
saveCallResult: suspend (A) -> Unit): LiveData<Result<T>> =
liveData(Dispatchers.IO) {
emit(Result.loading<T>())
val source = databaseQuery.invoke().map { Result.success(it) }
emitSource(source)
val responseStatus = networkCall.invoke()
if (responseStatus.status == SUCCESS) {
saveCallResult(responseStatus.data!!)
} else if (responseStatus.status == ERROR) {
emit(Result.error<T>(responseStatus.message!!))
emitSource(source)
}
}