@momo-kits/native-kits 0.164.1-beta.13 → 0.164.1-beta.14-debug

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/build.gradle.kts +11 -0
  2. package/compose/build.gradle.kts +190 -0
  3. package/compose/build.gradle.kts.backup +190 -0
  4. package/compose/compose.podspec +47 -0
  5. package/compose/src/androidMain/kotlin/vn/momo/kits/const/HostFont.android.kt +19 -0
  6. package/compose/src/androidMain/kotlin/vn/momo/kits/navigation/ScrollToTop.android.kt +6 -0
  7. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/BackGesture.android.kt +12 -0
  8. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/OsFontScale.android.kt +7 -0
  9. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/Platform.android.kt +150 -0
  10. package/compose/src/commonMain/kotlin/vn/momo/kits/application/AnimationSearchInput.kt +52 -0
  11. package/compose/src/commonMain/kotlin/vn/momo/kits/application/Context.kt +106 -0
  12. package/compose/src/commonMain/kotlin/vn/momo/kits/application/FloatingButton.kt +183 -0
  13. package/compose/src/commonMain/kotlin/vn/momo/kits/application/Header.kt +207 -0
  14. package/compose/src/commonMain/kotlin/vn/momo/kits/application/HeaderAnimated.kt +39 -0
  15. package/compose/src/commonMain/kotlin/vn/momo/kits/application/HeaderBackground.kt +86 -0
  16. package/compose/src/commonMain/kotlin/vn/momo/kits/application/HeaderDefault.kt +76 -0
  17. package/compose/src/commonMain/kotlin/vn/momo/kits/application/HeaderExtended.kt +76 -0
  18. package/compose/src/commonMain/kotlin/vn/momo/kits/application/HeaderRight.kt +293 -0
  19. package/compose/src/commonMain/kotlin/vn/momo/kits/application/HeaderTitle.kt +33 -0
  20. package/compose/src/commonMain/kotlin/vn/momo/kits/application/LiteScreen.kt +826 -0
  21. package/compose/src/commonMain/kotlin/vn/momo/kits/application/Localize.kt +269 -0
  22. package/compose/src/commonMain/kotlin/vn/momo/kits/application/NavigationContainer.kt +110 -0
  23. package/compose/src/commonMain/kotlin/vn/momo/kits/application/Screen.kt +378 -0
  24. package/compose/src/commonMain/kotlin/vn/momo/kits/application/WidgetContainer.kt +56 -0
  25. package/compose/src/commonMain/kotlin/vn/momo/kits/application/useHeaderSearchAnimation.kt +69 -0
  26. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Avatar.kt +157 -0
  27. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Badge.kt +82 -0
  28. package/compose/src/commonMain/kotlin/vn/momo/kits/components/BadgeDot.kt +32 -0
  29. package/compose/src/commonMain/kotlin/vn/momo/kits/components/BadgeRibbon.kt +338 -0
  30. package/compose/src/commonMain/kotlin/vn/momo/kits/components/BaselineView.kt +175 -0
  31. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Button.kt +356 -0
  32. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Carousel.kt +113 -0
  33. package/compose/src/commonMain/kotlin/vn/momo/kits/components/CheckBox.kt +89 -0
  34. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Chip.kt +128 -0
  35. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Collapse.kt +209 -0
  36. package/compose/src/commonMain/kotlin/vn/momo/kits/components/CupertinoOverscroll.kt +542 -0
  37. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Divider.kt +23 -0
  38. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Icon.kt +76 -0
  39. package/compose/src/commonMain/kotlin/vn/momo/kits/components/IconButton.kt +125 -0
  40. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Image.kt +231 -0
  41. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Information.kt +134 -0
  42. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Input.kt +475 -0
  43. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputDropDown.kt +114 -0
  44. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputMoney.kt +214 -0
  45. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputOTP.kt +240 -0
  46. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputPhoneNumber.kt +216 -0
  47. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputSearch.kt +259 -0
  48. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputTextArea.kt +182 -0
  49. package/compose/src/commonMain/kotlin/vn/momo/kits/components/LazyColumnWithBouncing.kt +343 -0
  50. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Loader.kt +108 -0
  51. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationDot.kt +56 -0
  52. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationNumber.kt +41 -0
  53. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationScroll.kt +84 -0
  54. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationWhiteDot.kt +40 -0
  55. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PopupNotify.kt +328 -0
  56. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PopupPromotion.kt +97 -0
  57. package/compose/src/commonMain/kotlin/vn/momo/kits/components/ProgressInfo.kt +310 -0
  58. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Radio.kt +59 -0
  59. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Rating.kt +87 -0
  60. package/compose/src/commonMain/kotlin/vn/momo/kits/components/ScaleSizeScope.kt +21 -0
  61. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Skeleton.kt +90 -0
  62. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Slider.kt +323 -0
  63. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Stepper.kt +217 -0
  64. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Steps.kt +473 -0
  65. package/compose/src/commonMain/kotlin/vn/momo/kits/components/SuggestAction.kt +122 -0
  66. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Swipe.kt +199 -0
  67. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Switch.kt +91 -0
  68. package/compose/src/commonMain/kotlin/vn/momo/kits/components/TabView.kt +470 -0
  69. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Tag.kt +88 -0
  70. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Text.kt +131 -0
  71. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Title.kt +203 -0
  72. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Tooltip.kt +498 -0
  73. package/compose/src/commonMain/kotlin/vn/momo/kits/components/TrustBanner.kt +175 -0
  74. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Uploader.kt +182 -0
  75. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePicker.kt +193 -0
  76. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePickerTypes.kt +29 -0
  77. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePickerUtils.kt +234 -0
  78. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/WheelPicker.kt +188 -0
  79. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Colors.kt +325 -0
  80. package/compose/src/commonMain/kotlin/vn/momo/kits/const/HostFont.kt +16 -0
  81. package/compose/src/commonMain/kotlin/vn/momo/kits/const/KitFontScale.kt +22 -0
  82. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Radius.kt +12 -0
  83. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Spacing.kt +16 -0
  84. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Theme.kt +188 -0
  85. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Typography.kt +263 -0
  86. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Card.kt +52 -0
  87. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Item.kt +38 -0
  88. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Section.kt +39 -0
  89. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/AutomationId.kt +52 -0
  90. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Clickable.kt +66 -0
  91. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Conditional.kt +11 -0
  92. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/DeprecatedModifier.kt +104 -0
  93. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Shadow.kt +48 -0
  94. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Size.kt +51 -0
  95. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/BottomSheet.kt +299 -0
  96. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/ModalScreen.kt +129 -0
  97. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/Navigation.kt +103 -0
  98. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/NavigationContainer.kt +191 -0
  99. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/Navigator.kt +386 -0
  100. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/ScrollToTop.kt +8 -0
  101. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/StackScreen.kt +605 -0
  102. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/StackTransition.kt +157 -0
  103. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/SwipeBackHaptic.kt +39 -0
  104. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/BottomTab.kt +156 -0
  105. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/BottomTabBar.kt +225 -0
  106. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/CurvedContainer.kt +86 -0
  107. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/FloatingButton.kt +169 -0
  108. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/Header.kt +320 -0
  109. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderBackground.kt +81 -0
  110. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderRight.kt +309 -0
  111. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderTitle.kt +34 -0
  112. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderUser.kt +360 -0
  113. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/SnackBar.kt +132 -0
  114. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/tracking/ScreenTracker.kt +127 -0
  115. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/BackGesture.kt +15 -0
  116. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ComposeLottieAnimation.kt +91 -0
  117. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/OsFontScale.kt +9 -0
  118. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/Platform.kt +69 -0
  119. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Icons.kt +1329 -0
  120. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Resources.kt +55 -0
  121. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Tracking.kt +15 -0
  122. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Utils.kt +105 -0
  123. package/compose/src/iosMain/kotlin/vn/momo/kits/const/HostFont.ios.kt +40 -0
  124. package/compose/src/iosMain/kotlin/vn/momo/kits/navigation/HostBackGesture.ios.kt +125 -0
  125. package/compose/src/iosMain/kotlin/vn/momo/kits/navigation/ScrollToTop.ios.kt +32 -0
  126. package/compose/src/iosMain/kotlin/vn/momo/kits/platform/BackGesture.ios.kt +35 -0
  127. package/compose/src/iosMain/kotlin/vn/momo/kits/platform/OsFontScale.ios.kt +66 -0
  128. package/compose/src/iosMain/kotlin/vn/momo/kits/platform/Platform.ios.kt +220 -0
  129. package/gradle/libs.versions.toml +69 -0
  130. package/gradle/wrapper/gradle-wrapper.jar +0 -0
  131. package/gradle/wrapper/gradle-wrapper.properties +8 -0
  132. package/gradle.properties +26 -0
  133. package/gradlew +252 -0
  134. package/gradlew.bat +94 -0
  135. package/ios/native-kits.podspec +1 -1
  136. package/ios-demo/MoMoUIKitsDemo.podspec +1 -1
  137. package/ios-demo/README.md +120 -0
  138. package/local.properties +8 -0
  139. package/package.json +1 -1
  140. package/settings.gradle.kts +64 -0
@@ -0,0 +1,191 @@
1
+ package vn.momo.kits.navigation
2
+
3
+ import androidx.compose.animation.core.tween
4
+ import androidx.compose.animation.fadeIn
5
+ import androidx.compose.animation.slideInVertically
6
+ import androidx.compose.animation.slideOutVertically
7
+ import androidx.compose.runtime.Composable
8
+ import androidx.compose.runtime.CompositionLocalProvider
9
+ import androidx.compose.runtime.DisposableEffect
10
+ import androidx.compose.runtime.LaunchedEffect
11
+ import androidx.compose.runtime.mutableStateOf
12
+ import androidx.compose.runtime.remember
13
+ import androidx.compose.runtime.saveable.rememberSaveable
14
+ import androidx.compose.runtime.staticCompositionLocalOf
15
+ import androidx.compose.ui.unit.Dp
16
+ import androidx.navigation.compose.NavHost
17
+ import androidx.navigation.compose.composable
18
+ import androidx.navigation.compose.rememberNavController
19
+ import androidx.navigation.toRoute
20
+ import kotlinx.coroutines.delay
21
+ import kotlinx.coroutines.flow.collectLatest
22
+ import vn.momo.kits.application.LocalContext
23
+ import vn.momo.kits.application.LocalLocalize
24
+ import vn.momo.kits.application.Localize
25
+ import vn.momo.kits.application.MiniAppContext
26
+ import vn.momo.kits.const.AppNavigationBar
27
+ import vn.momo.kits.const.AppStatusBar
28
+ import vn.momo.kits.const.AppTheme
29
+ import vn.momo.kits.const.Theme
30
+ import vn.momo.kits.const.defaultTheme
31
+ import vn.momo.kits.platform.ProvideNavigationEventDispatcherOwner
32
+ import vn.momo.kits.platform.ProvideOsFontScale
33
+ import vn.momo.kits.utils.getAppStatusBarHeight
34
+ import vn.momo.kits.utils.getNavigationBarHeight
35
+ import vn.momo.maxapi.IMaxApi
36
+ import kotlin.time.Duration.Companion.milliseconds
37
+
38
+ @Composable
39
+ fun NavigationContainer(
40
+ initialScreenName: String,
41
+ initialScreen: @Composable () -> Unit,
42
+ options: NavigationOptions? = null,
43
+ initialTheme: Theme = defaultTheme,
44
+ miniAppContext: MiniAppContext? = null,
45
+ maxApi: IMaxApi? = null,
46
+ setNavigator: ((Navigator) -> Unit)? = null,
47
+ statusBarHeight: Dp? = null,
48
+ localize: Localize? = null,
49
+ ) {
50
+ val navController = rememberNavController()
51
+ val registry = remember { DynamicScreenRegistry() }
52
+ val navigator = remember { Navigator(navController = navController, maxApi = maxApi, registry = registry) }
53
+ val statusBarHeight = statusBarHeight ?: getAppStatusBarHeight()
54
+ val navigationBarHeight = getNavigationBarHeight()
55
+
56
+ val parentContext = LocalContext.current
57
+ val mergedContext = MiniAppContext.merge(parentContext, miniAppContext)
58
+
59
+ val stackTransition = remember { StackTransitionCoordinator() }
60
+ val theme = remember { mutableStateOf(initialTheme) }
61
+ val fallbackLocalize = remember { Localize() }
62
+
63
+ val resolvedLocalize = localize ?: fallbackLocalize
64
+
65
+ LaunchedEffect(maxApi, resolvedLocalize) {
66
+ val api = maxApi ?: return@LaunchedEffect
67
+ runCatching {
68
+ api.getLanguage(callback = { data ->
69
+ if (parseLanguage(data) == Localize.EN) {
70
+ resolvedLocalize.changeLanguage(Localize.EN)
71
+ }
72
+ })
73
+ }
74
+ }
75
+
76
+ val screenId = rememberSaveable { registry.nextId() }
77
+ registry.bind(screenId, initialScreenName, initialScreen, options)
78
+ val startDestination = remember(screenId) { DynamicScreenRoute(screenId) }
79
+
80
+ ProvideOsFontScale {
81
+ ProvideNavigationEventDispatcherOwner {
82
+ CompositionLocalProvider(
83
+ LocalNavigator provides navigator,
84
+ LocalDynamicScreenRegistry provides registry,
85
+ LocalMaxApi provides maxApi,
86
+ AppTheme provides theme.value,
87
+ AppStatusBar provides statusBarHeight,
88
+ AppNavigationBar provides navigationBarHeight,
89
+ LocalContext provides mergedContext,
90
+ LocalLocalize provides resolvedLocalize,
91
+ LocalStackTransition provides stackTransition,
92
+ ) {
93
+ LaunchedEffect(Unit) {
94
+ setNavigator?.invoke(navigator)
95
+ }
96
+
97
+ SwipeBackHaptic(maxApi)
98
+
99
+ // Gesture pops never reach Navigator.dismissScreen(), so prune the registry from
100
+ // the back stack itself. collectLatest lets a rapid re-push cancel a pending prune.
101
+ LaunchedEffect(navController, registry) {
102
+ navController.currentBackStack.collectLatest { entries ->
103
+ delay(STACK_TRANSITION_DURATION.milliseconds)
104
+ val aliveIds = entries.mapNotNullTo(mutableSetOf()) { entry ->
105
+ runCatching { entry.toRoute<DynamicScreenRoute>().id }.getOrNull()
106
+ ?: runCatching { entry.toRoute<DynamicDialogRoute>().id }.getOrNull()
107
+ }
108
+ aliveIds.add(screenId)
109
+ registry.retainOnly(aliveIds)
110
+ }
111
+ }
112
+
113
+ // The transitions must live on the NavHost call site, not on the destinations:
114
+ // NavHost.uikit computes isDefaultTransition from these arguments alone, and while
115
+ // it is true the library paints its own hard-coded black iOS blackout that would
116
+ // sit over the pink scrim.
117
+ NavHost(
118
+ navController,
119
+ startDestination = startDestination,
120
+ enterTransition = { stackEnter() },
121
+ exitTransition = { stackExit() },
122
+ popEnterTransition = { stackPopEnter() },
123
+ popExitTransition = { stackPopExit() },
124
+ ) {
125
+ composable<DynamicScreenRoute> { backStackEntry ->
126
+ val route = backStackEntry.toRoute<DynamicScreenRoute>()
127
+ val screen = registry.getScreen(route.id)
128
+
129
+ if (screen != null) {
130
+ StackTransitionDecor(id = route.id, isStackRoute = true) {
131
+ StackScreen(
132
+ id = route.id,
133
+ name = screen.name,
134
+ content = screen.content,
135
+ navigationOptions = screen.options
136
+ )
137
+ }
138
+ }
139
+ }
140
+
141
+ composable<DynamicDialogRoute>(
142
+ enterTransition = {
143
+ slideInVertically(
144
+ animationSpec = tween(300),
145
+ initialOffsetY = { it }
146
+ )
147
+ },
148
+ exitTransition = null,
149
+ popEnterTransition = { fadeIn(animationSpec = tween(0)) },
150
+ popExitTransition = {
151
+ slideOutVertically(
152
+ animationSpec = tween(300),
153
+ targetOffsetY = { it }
154
+ )
155
+ }
156
+ ) { backStackEntry ->
157
+ val route = backStackEntry.toRoute<DynamicDialogRoute>()
158
+ val screen = registry.getScreen(route.id)
159
+
160
+ if (screen != null) {
161
+ StackTransitionDecor(id = route.id, isStackRoute = false) {
162
+ StackScreen(
163
+ id = route.id,
164
+ name = screen.name,
165
+ content = screen.content,
166
+ navigationOptions = screen.options
167
+ )
168
+ }
169
+ }
170
+ }
171
+ }
172
+ }
173
+ }
174
+ }
175
+
176
+ DisposableEffect(Unit) {
177
+ onDispose {
178
+ navigator.dispose()
179
+ registry.unregisterScreen(screenId)
180
+ }
181
+ }
182
+ }
183
+
184
+ val LocalMaxApi = staticCompositionLocalOf<IMaxApi?> { null }
185
+
186
+ private fun parseLanguage(raw: Map<String, Any?>?): String? {
187
+ val code = (raw?.get("response") as? String)?.trim()?.lowercase()
188
+ if (code.isNullOrEmpty()) return null
189
+ return if (code.startsWith(Localize.EN)) Localize.EN else Localize.VI
190
+ }
191
+
@@ -0,0 +1,386 @@
1
+ package vn.momo.kits.navigation
2
+
3
+ import androidx.compose.foundation.layout.Box
4
+ import androidx.compose.foundation.layout.fillMaxSize
5
+ import androidx.compose.runtime.Composable
6
+ import androidx.compose.runtime.MutableState
7
+ import androidx.compose.runtime.mutableStateOf
8
+ import androidx.compose.runtime.staticCompositionLocalOf
9
+ import androidx.compose.ui.Alignment
10
+ import androidx.compose.ui.Modifier
11
+ import androidx.navigation.NavController
12
+ import androidx.navigation.toRoute
13
+ import kotlinx.coroutines.CoroutineScope
14
+ import kotlinx.coroutines.Dispatchers
15
+ import kotlinx.coroutines.SupervisorJob
16
+ import kotlinx.coroutines.cancel
17
+ import kotlinx.coroutines.delay
18
+ import kotlinx.coroutines.launch
19
+ import kotlinx.serialization.Serializable
20
+ import vn.momo.kits.components.PopupAction
21
+ import vn.momo.kits.components.PopupNotify
22
+ import vn.momo.kits.navigation.BottomHeader.*
23
+ import vn.momo.kits.navigation.component.SnackBar
24
+ import vn.momo.maxapi.IMaxApi
25
+ import kotlin.time.Duration.Companion.milliseconds
26
+
27
+ class Navigator(
28
+ private val navController: NavController,
29
+ private val maxApi: IMaxApi?,
30
+ private val registry: DynamicScreenRegistry
31
+ ) {
32
+ private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
33
+
34
+ private fun currentScreen(): DynamicScreen? {
35
+ val entry = navController.currentBackStackEntry ?: return null
36
+ val id = runCatching { entry.toRoute<DynamicScreenRoute>().id }.getOrNull()
37
+ ?: runCatching { entry.toRoute<DynamicDialogRoute>().id }.getOrNull()
38
+ ?: return null
39
+ return registry.getScreen(id)
40
+ }
41
+
42
+ fun push(screenName: String, content: @Composable () -> Unit, options: NavigationOptions? = null) {
43
+ val route = registry.register(screenName, content, options)
44
+ navController.navigate(DynamicScreenRoute(route.id))
45
+ }
46
+
47
+ fun replace(screenName: String, content: @Composable () -> Unit, options: NavigationOptions? = null) {
48
+ val replacedScreen = currentScreen()
49
+ replacedScreen?.let { registry.unregisterScreen(it.id) }
50
+ navController.popBackStack()
51
+ push(screenName, content, options)
52
+ }
53
+
54
+ fun onBackSafe(callBack: (() -> Unit)? = null) {
55
+ val currentScreen = currentScreen()
56
+ val options = currentScreen?.options
57
+ if (options?.onBackHandler != null) {
58
+ options.onBackHandler.invoke()
59
+ return
60
+ }
61
+ val preventBack = options?.headerBackProps?.preventBack
62
+ if (preventBack != null) {
63
+ showModal(
64
+ content = {
65
+ PopupNotify(preventBack.copy(
66
+ primary = PopupAction(
67
+ title = preventBack.primary?.title ?: "",
68
+ onPress = {
69
+ preventBack.primary?.onPress?.invoke()
70
+ pop(2, callBack) // close popup notify, then close the screen
71
+ }
72
+ )
73
+ ))
74
+ }
75
+ )
76
+ } else {
77
+ pop(1, callBack)
78
+ }
79
+ }
80
+
81
+ // The swipe-back opt-out predicate in StackScreen needs this: the root screen must keep custom
82
+ // back handling so it still reaches maxApi.dismiss.
83
+ // Public: the iOS host asks this to decide whether the kit owns the swipe on this screen.
84
+ fun canPop(): Boolean = navController.previousBackStackEntry != null
85
+
86
+ fun pop(count: Int = 1, callBack: (() -> Unit)? = null) {
87
+ scope.launch {
88
+ repeat(count) {
89
+ if (OverplayComponentRegistry.getOverplayType() == OverplayComponentType.SNACK_BAR){
90
+ dismissScreen()
91
+ OverplayComponentRegistry.hardClearAfterDismiss()
92
+ }
93
+ else if (OverplayComponentRegistry.isOverplayShowing()){
94
+ dismissOverplay()
95
+ } else {
96
+ dismissScreen()
97
+ }
98
+ }
99
+ callBack?.invoke()
100
+ }
101
+ }
102
+ private suspend fun dismissOverplay(isDelay: Boolean = true) {
103
+ OverplayComponentRegistry.clear()
104
+ if (isDelay) delay(300L.milliseconds)
105
+ OverplayComponentRegistry.hardClearAfterDismiss()
106
+ }
107
+
108
+ private suspend fun dismissScreen() {
109
+ if (navController.previousBackStackEntry != null){
110
+ val dismissing = currentScreen()
111
+ navController.popBackStack()
112
+ delay(STACK_TRANSITION_DURATION.milliseconds)
113
+ dismissing?.let { registry.unregisterScreen(it.id) }
114
+ } else {
115
+ maxApi?.dismiss { }
116
+ }
117
+ }
118
+
119
+ fun present(screenName: String, content: @Composable () -> Unit, options: NavigationOptions? = null) {
120
+ val route = registry.register(screenName, content, options)
121
+ navController.navigate(DynamicDialogRoute(route.id))
122
+ }
123
+
124
+ fun reset(screenName: String, content: @Composable () -> Unit, options: NavigationOptions? = null) {
125
+ registry.unregisterAll()
126
+
127
+ val route = registry.register(screenName, content, options)
128
+ navController.navigate(DynamicScreenRoute(route.id)) {
129
+ popUpTo(0) { inclusive = true }
130
+ }
131
+ }
132
+
133
+ fun showModal(
134
+ content: @Composable () -> Unit,
135
+ barrierDismissible: Boolean = true,
136
+ onDismiss: (() -> Unit)? = null
137
+ ){
138
+ val id = currentScreen()?.id ?: -1
139
+ OverplayComponentRegistry.registerOverplay(id, content, OverplayComponentType.MODAL, false, barrierDismissible, onDismiss)
140
+ }
141
+
142
+ fun showBottomSheet(
143
+ content: @Composable () -> Unit,
144
+ isSurface: Boolean = false,
145
+ barrierDismissible: Boolean = true,
146
+ onDismiss: (() -> Unit)? = null,
147
+ bottomSheetHeader: BottomHeader? = null,
148
+ draggable: Boolean = true,
149
+ useBottomInset: Boolean = true,
150
+ footerComponent: (@Composable () -> Unit)? = null
151
+ ){
152
+ val id = currentScreen()?.id ?: -1
153
+ OverplayComponentRegistry.registerOverplay(id, content, OverplayComponentType.BOTTOM_SHEET, isSurface, barrierDismissible, onDismiss, bottomSheetHeader, draggable, useBottomInset, footerComponent)
154
+ }
155
+
156
+ fun showSnackBar(snackBar: SnackBar, onDismiss: (() -> Unit)? = null) {
157
+ val id = currentScreen()?.id ?: -1
158
+ scope.launch {
159
+ OverplayComponentRegistry.registerOverplay(
160
+ id = id,
161
+ content = {
162
+ SnackBar(snackBar, onDismiss)
163
+ },
164
+ type = OverplayComponentType.SNACK_BAR,
165
+ onDismiss = onDismiss
166
+ )
167
+ }
168
+ }
169
+
170
+ fun hideSnackBar() {
171
+ scope.launch {
172
+ OverplayComponentRegistry.clear()
173
+ delay(250L.milliseconds)
174
+ OverplayComponentRegistry.hardClearAfterDismiss()
175
+ }
176
+ }
177
+
178
+ fun dispose(){
179
+ scope.cancel()
180
+ }
181
+ }
182
+
183
+ val LocalNavigator = staticCompositionLocalOf<Navigator> {
184
+ error("No NavigationStack provided")
185
+ }
186
+
187
+ @Serializable
188
+ data class DynamicScreenRoute(val id: Int)
189
+
190
+ @Serializable
191
+ data class DynamicDialogRoute(val id: Int)
192
+
193
+ data class DynamicScreen(
194
+ val id: Int,
195
+ val name: String,
196
+ val content: @Composable () -> Unit,
197
+ var options: NavigationOptions? = null
198
+ )
199
+
200
+ class DynamicScreenRegistry {
201
+ private val screens = mutableMapOf<Int, DynamicScreen>()
202
+
203
+ fun register(screenName: String, content: @Composable () -> Unit, options: NavigationOptions?): DynamicScreenRoute {
204
+ val id = nextGlobalId()
205
+ screens[id] = DynamicScreen(
206
+ id = id,
207
+ name = screenName,
208
+ content = content,
209
+ options = options
210
+ )
211
+ return DynamicScreenRoute(id)
212
+ }
213
+
214
+ fun nextId(): Int = nextGlobalId()
215
+
216
+ fun bind(id: Int, screenName: String, content: @Composable () -> Unit, options: NavigationOptions?) {
217
+ screens[id] = DynamicScreen(
218
+ id = id,
219
+ name = screenName,
220
+ content = content,
221
+ options = options
222
+ )
223
+ advanceGlobalIdPast(id) // keep counter ahead after process-death restore
224
+ }
225
+
226
+ fun unregisterScreen(id: Int) {
227
+ screens.remove(id)
228
+ }
229
+
230
+ // Gesture pops go through composeNavigator.popBackStack() inside NavHost and never reach
231
+ // Navigator.dismissScreen(), so screens would leak without a stack-driven prune.
232
+ fun retainOnly(ids: Set<Int>) {
233
+ val stale = screens.keys.filter { it !in ids }
234
+ for (id in stale) {
235
+ screens.remove(id)
236
+ }
237
+ }
238
+
239
+ fun unregisterAll(){
240
+ val ids = screens.keys.toList()
241
+ for (id in ids) {
242
+ unregisterScreen(id)
243
+ }
244
+ }
245
+
246
+ fun getScreen(id: Int): DynamicScreen? = screens[id]
247
+
248
+ fun setOptions(id: Int, options: NavigationOptions){
249
+ screens[id]?.options = options
250
+ }
251
+
252
+ companion object {
253
+ private var globalIdCounter = 1
254
+
255
+ private fun nextGlobalId(): Int = globalIdCounter++
256
+
257
+ private fun advanceGlobalIdPast(id: Int) {
258
+ if (id >= globalIdCounter) globalIdCounter = id + 1
259
+ }
260
+ }
261
+ }
262
+
263
+ val LocalDynamicScreenRegistry = staticCompositionLocalOf<DynamicScreenRegistry?> { null }
264
+
265
+
266
+ sealed class OverplayComponentParams {
267
+ class Modal(
268
+ val onDismiss: (() -> Unit)? = null,
269
+ val barrierDismissible: Boolean = true
270
+ ) : OverplayComponentParams()
271
+
272
+ class BottomSheet(
273
+ val isSurface: Boolean = false,
274
+ val onDismiss: (() -> Unit)? = null,
275
+ val barrierDismissible: Boolean = true,
276
+ val bottomSheetHeader: BottomHeader? = null,
277
+ val draggable: Boolean = true,
278
+ val useBottomInset: Boolean = true,
279
+ val footerComponent: (@Composable () -> Unit)? = null,
280
+ ) : OverplayComponentParams()
281
+
282
+ class SnackBar : OverplayComponentParams()
283
+ }
284
+
285
+ data class OverplayComponent(
286
+ val id: Int,
287
+ val type: OverplayComponentType? = null,
288
+ val content: @Composable () -> Unit,
289
+ val params: OverplayComponentParams? = null
290
+ )
291
+
292
+ enum class OverplayComponentType {
293
+ MODAL, BOTTOM_SHEET, SNACK_BAR
294
+ }
295
+
296
+ object OverplayComponentRegistry {
297
+ private var currentOverlayComponent : MutableState<OverplayComponent?> = mutableStateOf(null)
298
+ private var requestClose: (() -> Unit)? = null
299
+ internal fun bindClose(handler: (() -> Unit)?) {
300
+ requestClose = handler
301
+ }
302
+
303
+ fun registerOverplay(
304
+ id: Int,
305
+ content: @Composable () -> Unit,
306
+ type: OverplayComponentType,
307
+ isSurface: Boolean = false,
308
+ barrierDismissible: Boolean = true,
309
+ onDismiss: (() -> Unit)?,
310
+ bottomSheetHeader: BottomHeader? = null,
311
+ draggable: Boolean = true,
312
+ useBottomInset: Boolean = true,
313
+ footerComponent: (@Composable () -> Unit)? = null,
314
+ ){
315
+ val params = when(type){
316
+ OverplayComponentType.MODAL -> OverplayComponentParams.Modal(onDismiss, barrierDismissible)
317
+ OverplayComponentType.BOTTOM_SHEET -> OverplayComponentParams.BottomSheet(isSurface, onDismiss, barrierDismissible, bottomSheetHeader, draggable, useBottomInset, footerComponent)
318
+ OverplayComponentType.SNACK_BAR -> OverplayComponentParams.SnackBar()
319
+ }
320
+
321
+ currentOverlayComponent.value = OverplayComponent(
322
+ id = id,
323
+ type = type,
324
+ content = content,
325
+ params = params
326
+ )
327
+ }
328
+
329
+ fun isOverplayShowing(): Boolean = currentOverlayComponent.value != null
330
+
331
+ fun getOverplayType(): OverplayComponentType? = currentOverlayComponent.value?.type
332
+
333
+ fun currentRootId(): Int? = currentOverlayComponent.value?.id
334
+
335
+ fun clear(){
336
+ if (requestClose != null) {
337
+ requestClose?.invoke()
338
+ } else {
339
+ currentOverlayComponent.value = null
340
+ }
341
+ }
342
+
343
+ internal fun hardClearAfterDismiss() {
344
+ currentOverlayComponent.value = null
345
+ requestClose = null
346
+ }
347
+
348
+ @Composable
349
+ internal fun OverlayComponent(){
350
+ val overplay = currentOverlayComponent.value ?: return
351
+
352
+ when (val params = overplay.params) {
353
+ is OverplayComponentParams.BottomSheet -> {
354
+ BottomSheet(
355
+ content = overplay.content,
356
+ header = params.bottomSheetHeader ?: Title(),
357
+ isSurface = params.isSurface,
358
+ barrierDismissible = params.barrierDismissible,
359
+ draggable = params.draggable,
360
+ useBottomInset = params.useBottomInset,
361
+ footerComponent = params.footerComponent,
362
+ onDismiss = params.onDismiss
363
+ )
364
+ }
365
+
366
+ is OverplayComponentParams.Modal -> {
367
+ ModalScreen(
368
+ content = overplay.content,
369
+ barrierDismissible = params.barrierDismissible,
370
+ onDismiss = params.onDismiss
371
+ )
372
+ }
373
+
374
+ is OverplayComponentParams.SnackBar -> {
375
+ Box(
376
+ modifier = Modifier.fillMaxSize(),
377
+ contentAlignment = Alignment.BottomCenter
378
+ ) {
379
+ overplay.content()
380
+ }
381
+ }
382
+
383
+ null -> {}
384
+ }
385
+ }
386
+ }
@@ -0,0 +1,8 @@
1
+ package vn.momo.kits.navigation
2
+
3
+ import androidx.compose.runtime.Composable
4
+
5
+ @Composable
6
+ internal expect fun RegisterScrollToTop(callback: (() -> Unit)?)
7
+
8
+