-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrated client functionalities to use the OMH Task abstraction. (#44)
- Loading branch information
Showing
4 changed files
with
114 additions
and
59 deletions.
There are no files selected for viewing
This file contains 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
42 changes: 42 additions & 0 deletions
42
storage-api/src/main/java/com/omh/android/storage/api/async/OmhStorageTaskImpl.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.omh.android.storage.api.async | ||
|
||
import com.omh.android.auth.api.async.OmhCancellable | ||
import com.omh.android.auth.api.async.OmhTask | ||
import com.omh.android.storage.api.domain.usecase.OmhResult | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.cancelChildren | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
|
||
internal class OmhStorageTaskImpl<T>(private val task: suspend () -> OmhResult<T>) : OmhTask<T>() { | ||
|
||
private val coroutineContext = Dispatchers.Main + SupervisorJob() | ||
private val customScope: CoroutineScope = CoroutineScope(context = coroutineContext) | ||
|
||
override fun execute(): OmhCancellable { | ||
customScope.launch { | ||
executeScopedTask() | ||
} | ||
return OmhCancellable { coroutineContext.cancelChildren() } | ||
} | ||
|
||
@SuppressWarnings("TooGenericExceptionCaught") | ||
private suspend fun executeScopedTask() { | ||
when (val result: OmhResult<T> = task()) { | ||
is OmhResult.OmhSuccess -> executeSuccess(result.data) | ||
is OmhResult.OmhError -> executeFailure(result.exception) | ||
} | ||
} | ||
|
||
private suspend fun executeFailure(e: Exception) = withContext(Dispatchers.Main) { | ||
onFailure?.invoke(e) | ||
} | ||
|
||
private suspend fun executeSuccess(data: T) { | ||
withContext(Dispatchers.Main) { | ||
onSuccess?.invoke(data) | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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