-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TriggerViewModel and Trigger component
- Loading branch information
1 parent
f7eb0fd
commit fd38b9e
Showing
2 changed files
with
78 additions
and
3 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
android/nativebrik/src/main/java/com/nativebrik/sdk/component/trigger.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,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) | ||
} | ||
) | ||
} | ||
} | ||
} |
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