Skip to content

Commit

Permalink
Add TriggerViewModel and Trigger component
Browse files Browse the repository at this point in the history
  • Loading branch information
RyosukeCla committed Feb 14, 2024
1 parent f7eb0fd commit fd38b9e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.nativebrik.sdk.component

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
import androidx.lifecycle.ViewModel
import com.nativebrik.sdk.NativebrikEvent
import com.nativebrik.sdk.data.Container
import com.nativebrik.sdk.schema.TriggerEventNameDefs
import com.nativebrik.sdk.schema.UIBlock
import com.nativebrik.sdk.schema.UIRootBlock
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class TriggerViewModel(internal val container: Container) : ViewModel() {
internal val modalStacks = mutableStateListOf<UIRootBlock>()

fun dispatch(event: NativebrikEvent) {
val self = this
GlobalScope.launch(Dispatchers.IO) {
self.container.fetchInAppMessage(event.name).onSuccess {
GlobalScope.launch(Dispatchers.Main) {
if (it is UIBlock.UnionUIRootBlock) {
self.modalStacks.add(it.data)
}
}
}
}
}

fun handleDismiss(root: UIRootBlock) {
modalStacks.removeIf {
it.id == root.id
}
}

}

@Composable
fun Trigger(trigger: TriggerViewModel) {
LaunchedEffect("") {
trigger.dispatch(NativebrikEvent(TriggerEventNameDefs.USER_BOOT_APP.name))
}

if (trigger.modalStacks.isNotEmpty()) {
for (stack in trigger.modalStacks) {
Root(
container = trigger.container,
root = stack,
embeddingVisibility = false,
onDismiss = {
trigger.handleDismiss(it)
}
)
}
}
}
23 changes: 20 additions & 3 deletions android/nativebrik/src/main/java/com/nativebrik/sdk/sdk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,25 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.nativebrik.sdk.component.Embedding
import com.nativebrik.sdk.component.EmbeddingLoadingState
import com.nativebrik.sdk.component.Trigger
import com.nativebrik.sdk.component.TriggerViewModel
import com.nativebrik.sdk.data.Container
import com.nativebrik.sdk.data.ContainerImpl
import com.nativebrik.sdk.data.user.NativebrikUser

data class Endpoint(
val cdn: String = "https://cdn.nativebrik.com",
val track: String = "https://track.nativebrik.com/track/v1",
) {}
)

data class Config(
val projectId: String,
val endpoint: Endpoint = Endpoint()
) {}
)

data class NativebrikEvent(
val name: String
)

internal var LocalNativebrikClient = compositionLocalOf<NativebrikClient> {
error("NativebrikClient is not found")
Expand All @@ -54,6 +60,7 @@ public fun NativebrikProvider(
CompositionLocalProvider(
LocalNativebrikClient provides client
) {
client.experiment.Overlay()
content()
}
}
Expand All @@ -77,15 +84,25 @@ public class NativebrikClient {

public class NativebrikExperiment {
private val container: Container
private val trigger: TriggerViewModel

internal constructor(config: Config, user: NativebrikUser, context: Context) {
this.container = ContainerImpl(
config = config,
user = user,
context = context,
)
this.trigger = TriggerViewModel(this.container)
}

public fun dispatch(event: NativebrikEvent) {
this.trigger.dispatch(event)
}

public fun dispatch(name: String) {}
@Composable
public fun Overlay() {
Trigger(trigger = this.trigger)
}

@Composable
public fun Embedding(id: String, modifier: Modifier = Modifier) {
Expand Down

0 comments on commit fd38b9e

Please sign in to comment.