Skip to content

Commit

Permalink
refactor: name mix ups
Browse files Browse the repository at this point in the history
  • Loading branch information
CraZyLegenD committed Nov 13, 2020
1 parent 908ee92 commit 105bc19
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ inline fun <T : DBResult<T>> CoroutineScope.makeDBCallListStateFlow(stateFlow: M
}


fun <T> CoroutineScope.apiCallStateFlow(sharing: SharingStarted = SharingStarted.WhileSubscribed(),
initialValue: DBResult<T> = databaseQuerying,
apiCall: suspend () -> T?): StateFlow<DBResult<T>> =
fun <T> CoroutineScope.dbCallStateFlow(sharing: SharingStarted = SharingStarted.WhileSubscribed(),
initialValue: DBResult<T> = databaseQuerying,
dbCall: suspend () -> T?): StateFlow<DBResult<T>> =
flow {
try {
emit(databaseSubscribe(apiCall.invoke()))
emit(databaseSubscribe(dbCall.invoke()))
} catch (t: Throwable) {
emit(databaseError(t))
}
Expand All @@ -98,10 +98,10 @@ fun <T> CoroutineScope.apiCallStateFlow(sharing: SharingStarted = SharingStarted
}.stateIn(this, sharing, initialValue)


suspend fun <T> CoroutineScope.apiCallStateFlowInScope(apiCall: suspend () -> T?): StateFlow<DBResult<T>> =
suspend fun <T> CoroutineScope.dbCallStateFlowInScope(dbCall: suspend () -> T?): StateFlow<DBResult<T>> =
flow {
try {
emit(databaseSubscribe(apiCall.invoke()))
emit(databaseSubscribe(dbCall.invoke()))
} catch (t: Throwable) {
emit(databaseError(t))
}
Expand All @@ -110,10 +110,10 @@ suspend fun <T> CoroutineScope.apiCallStateFlowInScope(apiCall: suspend () -> T?
}.stateIn(this)


suspend fun <T> apiCallStateFlowWithinScope(coroutineScope: CoroutineScope, apiCall: suspend () -> T?): StateFlow<DBResult<T>> =
suspend fun <T> dbCallStateFlowWithinScope(coroutineScope: CoroutineScope, dbCall: suspend () -> T?): StateFlow<DBResult<T>> =
flow {
try {
emit(databaseSubscribe(apiCall.invoke()))
emit(databaseSubscribe(dbCall.invoke()))
} catch (t: Throwable) {
emit(databaseError(t))
}
Expand All @@ -122,37 +122,37 @@ suspend fun <T> apiCallStateFlowWithinScope(coroutineScope: CoroutineScope, apiC
}.stateIn(coroutineScope)


fun <T> CoroutineScope.makeApiCallList(
fun <T> CoroutineScope.makeDBCallList(
dbResult: MutableStateFlow<DBResult<T>>,
includeEmptyData: Boolean = true,
apiCall: suspend () -> T?): Job {
dbCall: suspend () -> T?): Job {

dbResult.querying()

return launch(ioDispatcher) {
try {
dbResult.subscribeList(apiCall(), includeEmptyData)
dbResult.subscribeList(dbCall(), includeEmptyData)
} catch (t: Throwable) {
dbResult.callError(t)
}
}
}

fun <T> CoroutineScope.makeApiCall(
fun <T> CoroutineScope.makeDBCall(
dbResult: MutableStateFlow<DBResult<T>>,
apiCall: suspend () -> T?): Job {
dbCall: suspend () -> T?): Job {
dbResult.querying()
return launch(ioDispatcher) {
try {
dbResult.subscribe(apiCall())
dbResult.subscribe(dbCall())
} catch (t: Throwable) {
dbResult.callError(t)
}
}
}


fun <T> CoroutineScope.makeApiCallList(
fun <T> CoroutineScope.makeDBCallList(
response: T?,
dbResult: MutableStateFlow<DBResult<T>>,
includeEmptyData: Boolean = true
Expand All @@ -168,21 +168,21 @@ fun <T> CoroutineScope.makeApiCallList(

}

fun <T> ViewModel.makeApiCallList(
fun <T> ViewModel.makeDBCallList(
dbResult: MutableStateFlow<DBResult<T>>,
includeEmptyData: Boolean = true,
apiCall: suspend () -> T?): Job {
dbCall: suspend () -> T?): Job {
dbResult.querying()
return viewModelIOCoroutine {
try {
dbResult.subscribeList(apiCall(), includeEmptyData)
dbResult.subscribeList(dbCall(), includeEmptyData)
} catch (t: Throwable) {
dbResult.callError(t)
}
}
}

fun <T> CoroutineScope.makeApiCall(
fun <T> CoroutineScope.makeDBCall(
response: T?,
dbResult: MutableStateFlow<DBResult<T>>
): Job {
Expand All @@ -197,30 +197,30 @@ fun <T> CoroutineScope.makeApiCall(

}

fun <T> ViewModel.makeApiCall(
fun <T> ViewModel.makeDBCall(
dbResult: MutableStateFlow<DBResult<T>>,
apiCall: suspend () -> T?): Job {
dbCall: suspend () -> T?): Job {
dbResult.querying()
return viewModelIOCoroutine {
try {
dbResult.subscribe(apiCall())
dbResult.subscribe(dbCall())
} catch (t: Throwable) {
dbResult.callError(t)
}
}
}


fun <T> CoroutineScope.makeApiCallAsync(
fun <T> CoroutineScope.makeDBCallAsync(
dbResult: MutableStateFlow<DBResult<T>>,
apiCall: suspend () -> T?): Job {
dbCall: suspend () -> T?): Job {

return launch(mainDispatcher) {
supervisorScope {
dbResult.querying()
try {
val task = async(ioDispatcher) {
apiCall()
dbCall()
}
dbResult.subscribe(task.await())
} catch (t: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ fun <T> CoroutineScope.makeApiCallAsync(
}
}

inline fun <T : RetrofitResult<T>> ViewModel.makeDBCallStateFlow(stateFlow: MutableStateFlow<RetrofitResult<T>>, crossinline dbCall: suspend () -> Response<T>?) {
inline fun <T : RetrofitResult<T>> ViewModel.makeApiCallStateFlow(stateFlow: MutableStateFlow<RetrofitResult<T>>, crossinline apiCall: suspend () -> Response<T>?) {
viewModelScope.launch {
supervisorScope {
stateFlow.value = retrofitLoading
try {
val task = async(ioDispatcher) {
dbCall()
apiCall()
}
stateFlow.value = retrofitSubscribe(task.await())
} catch (t: Throwable) {
Expand All @@ -177,15 +177,15 @@ inline fun <T : RetrofitResult<T>> ViewModel.makeDBCallStateFlow(stateFlow: Muta
}
}

inline fun <T : RetrofitResult<T>> ViewModel.makeDBCallListStateFlow(stateFlow: MutableStateFlow<RetrofitResult<T>>,
includeEmptyData: Boolean = false,
crossinline dbCall: suspend () -> Response<T>?) {
inline fun <T : RetrofitResult<T>> ViewModel.makeApiCallListStateFlow(stateFlow: MutableStateFlow<RetrofitResult<T>>,
includeEmptyData: Boolean = false,
crossinline apiCall: suspend () -> Response<T>?) {
viewModelScope.launch {
supervisorScope {
stateFlow.value = retrofitLoading
try {
val task = async(ioDispatcher) {
dbCall()
apiCall()
}
stateFlow.value = retrofitSubscribeList(task.await(), includeEmptyData)
} catch (t: Throwable) {
Expand All @@ -195,15 +195,15 @@ inline fun <T : RetrofitResult<T>> ViewModel.makeDBCallListStateFlow(stateFlow:
}
}

inline fun <T : RetrofitResult<T>> CoroutineScope.makeDBCallStateFlow(stateFlow: MutableStateFlow<RetrofitResult<T>>, crossinline dbCall: suspend () -> Response<T>?) {
inline fun <T : RetrofitResult<T>> CoroutineScope.makeApiCallStateFlow(stateFlow: MutableStateFlow<RetrofitResult<T>>, crossinline apiCall: suspend () -> Response<T>?) {

launch {
supervisorScope {
stateFlow.value = retrofitLoading

try {
val task = async(ioDispatcher) {
dbCall()
apiCall()
}
stateFlow.value = retrofitSubscribe(task.await())
} catch (t: Throwable) {
Expand All @@ -213,15 +213,15 @@ inline fun <T : RetrofitResult<T>> CoroutineScope.makeDBCallStateFlow(stateFlow:
}
}

inline fun <T : RetrofitResult<T>> CoroutineScope.makeDBCallListStateFlow(stateFlow: MutableStateFlow<RetrofitResult<T>>,
includeEmptyData: Boolean = false,
crossinline dbCall: suspend () -> Response<T>?) {
inline fun <T : RetrofitResult<T>> CoroutineScope.makeApiCallListStateFlow(stateFlow: MutableStateFlow<RetrofitResult<T>>,
includeEmptyData: Boolean = false,
crossinline apiCall: suspend () -> Response<T>?) {
launch {
supervisorScope {
stateFlow.value = retrofitLoading
try {
val task = async(ioDispatcher) {
dbCall()
apiCall()
}
stateFlow.value = retrofitSubscribeList(task.await(), includeEmptyData)
} catch (t: Throwable) {
Expand Down

0 comments on commit 105bc19

Please sign in to comment.