Skip to content

Commit

Permalink
MBX-3483: added cancelable synchronous operation call
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Sozinov committed Jul 1, 2024
1 parent 330cc9a commit 8d8c71b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
47 changes: 47 additions & 0 deletions sdk/src/main/java/cloud/mindbox/mobile_sdk/Mindbox.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import cloud.mindbox.mobile_sdk.inapp.presentation.InAppMessageManager
import cloud.mindbox.mobile_sdk.logger.*
import cloud.mindbox.mobile_sdk.managers.*
import cloud.mindbox.mobile_sdk.models.*
import cloud.mindbox.mobile_sdk.models.operation.Cancelable
import cloud.mindbox.mobile_sdk.models.operation.OperationBody
import cloud.mindbox.mobile_sdk.models.operation.request.OperationBodyRequestBase
import cloud.mindbox.mobile_sdk.models.operation.response.OperationResponse
Expand Down Expand Up @@ -924,6 +925,52 @@ object Mindbox : MindboxLog {
}
}

/**
* Executes a synchronous operation with the specified name and body.
*
* @param context The current context used.
* @param operationSystemName The name of the synchronous operation.
* @param operationBodyJson JSON body of the operation.
* @param onSuccess Callback that will be invoked for a successful response to the given request.
* @param onError Callback for response typed [MindboxError] that will be invoked for an error response to the given request.
* @param onCancel Callback that will be invoked if the operation is cancelled.
* @return [Cancelable] object that can be used to cancel the ongoing operation.
*/
fun executeSyncOperation(
context: Context,
operationSystemName: String,
operationBodyJson: String,
onSuccess: (String) -> Unit,
onError: (MindboxError) -> Unit,
onCancel: () -> Unit
): Cancelable {
initComponents(context)
MindboxLoggerImpl.d(
this, "executeSyncOperation Cancelable (with operationBodyJson). " +
"operationSystemName: $operationSystemName, operationBodyJson: $operationBodyJson"
)
val syncOperationJob = if (validateOperation(operationSystemName)) {
mindboxScope.launch {
try {
InitializeLock.await(InitializeLock.State.APP_STARTED)
MindboxEventManager.syncOperation(
name = operationSystemName,
bodyJson = operationBodyJson,
onSuccess = onSuccess,
onError = onError,
)
} catch (e: CancellationException) {
onCancel()
Mindbox.logD("Operation cancelled")
}
}
} else {
Job().apply { complete() }
}

return Cancelable { syncOperationJob.cancel() }
}

/**
* Handles only Mindbox notification message from [HmsMessageService] or [FirebaseMessageServise].
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cloud.mindbox.mobile_sdk.models.operation

fun interface Cancelable {
fun cancel()
}

0 comments on commit 8d8c71b

Please sign in to comment.