mirror of
https://github.com/owncloud/android-library.git
synced 2025-06-07 16:06:08 +00:00
Handle remote operations, both sync and non-sync
This commit is contained in:
parent
9aff678fa4
commit
d73ee43e3d
@ -192,7 +192,7 @@ public abstract class RemoteOperation<T> implements Runnable {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Synchronously executes the remote operation
|
* Synchronously executes the remote operation
|
||||||
* <p>
|
*
|
||||||
* Do not call this method from the main thread.
|
* Do not call this method from the main thread.
|
||||||
*
|
*
|
||||||
* @param client Client object to reach an ownCloud server during the execution of
|
* @param client Client object to reach an ownCloud server during the execution of
|
||||||
|
@ -20,14 +20,22 @@
|
|||||||
package com.owncloud.android.lib.common.operations
|
package com.owncloud.android.lib.common.operations
|
||||||
|
|
||||||
suspend fun <T> awaitToRemoteOperationResult(remoteOperation: suspend () -> RemoteOperationResult<T>): T {
|
suspend fun <T> awaitToRemoteOperationResult(remoteOperation: suspend () -> RemoteOperationResult<T>): T {
|
||||||
|
remoteOperation.invoke().also {
|
||||||
|
return handleRemoteOperationResult(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val remoteOperationResult = remoteOperation.invoke()
|
fun <T> waitForRemoteOperationResult(remoteOperation: () -> RemoteOperationResult<T>): T {
|
||||||
|
remoteOperation.invoke().also {
|
||||||
|
return handleRemoteOperationResult(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun <T> handleRemoteOperationResult(remoteOperationResult: RemoteOperationResult<T>): T {
|
||||||
if (remoteOperationResult.isSuccess) {
|
if (remoteOperationResult.isSuccess) {
|
||||||
return remoteOperationResult.data
|
return remoteOperationResult.data
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errors
|
|
||||||
when (remoteOperationResult.code) {
|
when (remoteOperationResult.code) {
|
||||||
RemoteOperationResult.ResultCode.UNHANDLED_HTTP_CODE -> throw Exception()
|
RemoteOperationResult.ResultCode.UNHANDLED_HTTP_CODE -> throw Exception()
|
||||||
RemoteOperationResult.ResultCode.UNAUTHORIZED -> throw Exception()
|
RemoteOperationResult.ResultCode.UNAUTHORIZED -> throw Exception()
|
Loading…
x
Reference in New Issue
Block a user