⌘K

Kotlin action integration

In this doc we will learn how we can integrate our code with Nativeblocks

Get started

Make sure that your project meets these requirements:

  • Uses Android API level 26 or higher
  • Uses Jetpack (AndroidX)

Initialization

Root gradle

settings.gradle
repositories {
    mavenCentral()
}

Module gradle

build.gradle
dependencies {
    implementation ("io.nativeblocks:nativeblocks-android:1.1.0")
}

After initialization we need to write our business with help of jetpack compose, and then import it into Nativeblocks

In this doc we will cover basic SDK api

Business logic

Let's start to build our custom logic,

We have one function and we need to define all properties and actions of the logic,

AuthenticationCheck.kt
fun authCheck(
    onSuccess: () -> Unit,
    onFailure: () -> Unit
) {
    val authToken = authRepository.getToken()
    if (authToken.isNullOrEmpty()) {
        onFailure.invoke()
    } else {
        onSuccess.invoke()
    }
}

Business logic into action

After providing all properties and actions we have to wrap it into Nativeblocks SDK, and connect action data, properties and events with Nativeblocks,

AuthCheckerAction.kt
class AuthCheckerAction : INativeAction {

    override fun handle(actionProps: ActionProps) {
        val data = actionProps.nativeTrigger.data ?: mapOf()
        // change the frame's variable to update the login state
        val result = actionProps.variables?.get(data["isLogin"]?.value)

        authCheck(
            onSuccess = {
                actionProps.onVariableChange?.invoke(result?.copy(value = "true"))
                actionProps.nativeTrigger?.let { trigger ->
                    actionProps.onHandleSuccessNextTrigger?.invoke(trigger)
                }
            },
            onFailure = {
                actionProps.onVariableChange?.invoke(result?.copy(value = "false"))
                actionProps.nativeTrigger?.let { trigger ->
                    actionProps.onHandleFailureNextTrigger?.invoke(trigger)
                }
            }
        )
    }
}

Register action

At the end the AuthCheckerAction must be registered into Nativeblocks,

NativeblocksManager.getInstance().provideAction(
    actionType = "MY_CUSTOM_AUTH_CHECKER",
    action = AuthCheckerAction()
)

From now on, the MY_CUSTOM_AUTH_CHECKER is accessable for every frame in Nativeblocks