@momo-kits/native-kits 0.164.1-beta.4 → 0.164.1-beta.5-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/.claude/settings.local.json +185 -0
  2. package/build.gradle.kts +11 -0
  3. package/compose/build.gradle.kts +190 -0
  4. package/compose/build.gradle.kts.backup +190 -0
  5. package/compose/compose.podspec +47 -0
  6. package/compose/src/androidMain/kotlin/vn/momo/kits/const/HostFont.android.kt +19 -0
  7. package/compose/src/androidMain/kotlin/vn/momo/kits/navigation/ScrollToTop.android.kt +6 -0
  8. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/BackGesture.android.kt +12 -0
  9. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/OsFontScale.android.kt +7 -0
  10. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/Platform.android.kt +150 -0
  11. package/compose/src/commonMain/kotlin/vn/momo/kits/application/AnimationSearchInput.kt +52 -0
  12. package/compose/src/commonMain/kotlin/vn/momo/kits/application/Context.kt +106 -0
  13. package/compose/src/commonMain/kotlin/vn/momo/kits/application/FloatingButton.kt +183 -0
  14. package/compose/src/commonMain/kotlin/vn/momo/kits/application/Header.kt +207 -0
  15. package/compose/src/commonMain/kotlin/vn/momo/kits/application/HeaderAnimated.kt +39 -0
  16. package/compose/src/commonMain/kotlin/vn/momo/kits/application/HeaderBackground.kt +86 -0
  17. package/compose/src/commonMain/kotlin/vn/momo/kits/application/HeaderDefault.kt +76 -0
  18. package/compose/src/commonMain/kotlin/vn/momo/kits/application/HeaderExtended.kt +76 -0
  19. package/compose/src/commonMain/kotlin/vn/momo/kits/application/HeaderRight.kt +293 -0
  20. package/compose/src/commonMain/kotlin/vn/momo/kits/application/HeaderTitle.kt +33 -0
  21. package/compose/src/commonMain/kotlin/vn/momo/kits/application/LiteScreen.kt +826 -0
  22. package/compose/src/commonMain/kotlin/vn/momo/kits/application/Localize.kt +269 -0
  23. package/compose/src/commonMain/kotlin/vn/momo/kits/application/NavigationContainer.kt +110 -0
  24. package/compose/src/commonMain/kotlin/vn/momo/kits/application/Screen.kt +378 -0
  25. package/compose/src/commonMain/kotlin/vn/momo/kits/application/WidgetContainer.kt +56 -0
  26. package/compose/src/commonMain/kotlin/vn/momo/kits/application/useHeaderSearchAnimation.kt +69 -0
  27. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Avatar.kt +157 -0
  28. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Badge.kt +82 -0
  29. package/compose/src/commonMain/kotlin/vn/momo/kits/components/BadgeDot.kt +32 -0
  30. package/compose/src/commonMain/kotlin/vn/momo/kits/components/BadgeRibbon.kt +338 -0
  31. package/compose/src/commonMain/kotlin/vn/momo/kits/components/BaselineView.kt +175 -0
  32. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Button.kt +356 -0
  33. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Carousel.kt +113 -0
  34. package/compose/src/commonMain/kotlin/vn/momo/kits/components/CheckBox.kt +89 -0
  35. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Chip.kt +128 -0
  36. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Collapse.kt +209 -0
  37. package/compose/src/commonMain/kotlin/vn/momo/kits/components/CupertinoOverscroll.kt +542 -0
  38. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Divider.kt +23 -0
  39. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Icon.kt +76 -0
  40. package/compose/src/commonMain/kotlin/vn/momo/kits/components/IconButton.kt +125 -0
  41. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Image.kt +231 -0
  42. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Information.kt +134 -0
  43. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Input.kt +475 -0
  44. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputDropDown.kt +114 -0
  45. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputMoney.kt +214 -0
  46. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputOTP.kt +240 -0
  47. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputPhoneNumber.kt +216 -0
  48. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputSearch.kt +259 -0
  49. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputTextArea.kt +182 -0
  50. package/compose/src/commonMain/kotlin/vn/momo/kits/components/LazyColumnWithBouncing.kt +343 -0
  51. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Loader.kt +108 -0
  52. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationDot.kt +56 -0
  53. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationNumber.kt +41 -0
  54. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationScroll.kt +84 -0
  55. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationWhiteDot.kt +40 -0
  56. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PopupNotify.kt +328 -0
  57. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PopupPromotion.kt +97 -0
  58. package/compose/src/commonMain/kotlin/vn/momo/kits/components/ProgressInfo.kt +310 -0
  59. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Radio.kt +59 -0
  60. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Rating.kt +87 -0
  61. package/compose/src/commonMain/kotlin/vn/momo/kits/components/ScaleSizeScope.kt +21 -0
  62. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Skeleton.kt +90 -0
  63. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Slider.kt +323 -0
  64. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Stepper.kt +217 -0
  65. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Steps.kt +473 -0
  66. package/compose/src/commonMain/kotlin/vn/momo/kits/components/SuggestAction.kt +122 -0
  67. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Swipe.kt +199 -0
  68. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Switch.kt +91 -0
  69. package/compose/src/commonMain/kotlin/vn/momo/kits/components/TabView.kt +470 -0
  70. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Tag.kt +88 -0
  71. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Text.kt +131 -0
  72. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Title.kt +203 -0
  73. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Tooltip.kt +498 -0
  74. package/compose/src/commonMain/kotlin/vn/momo/kits/components/TrustBanner.kt +175 -0
  75. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Uploader.kt +182 -0
  76. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePicker.kt +193 -0
  77. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePickerTypes.kt +29 -0
  78. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePickerUtils.kt +234 -0
  79. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/WheelPicker.kt +188 -0
  80. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Colors.kt +325 -0
  81. package/compose/src/commonMain/kotlin/vn/momo/kits/const/HostFont.kt +16 -0
  82. package/compose/src/commonMain/kotlin/vn/momo/kits/const/KitFontScale.kt +22 -0
  83. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Radius.kt +12 -0
  84. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Spacing.kt +16 -0
  85. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Theme.kt +188 -0
  86. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Typography.kt +263 -0
  87. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Card.kt +52 -0
  88. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Item.kt +38 -0
  89. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Section.kt +39 -0
  90. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/AutomationId.kt +52 -0
  91. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Clickable.kt +66 -0
  92. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Conditional.kt +11 -0
  93. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/DeprecatedModifier.kt +104 -0
  94. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Shadow.kt +48 -0
  95. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Size.kt +51 -0
  96. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/BottomSheet.kt +299 -0
  97. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/ModalScreen.kt +129 -0
  98. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/Navigation.kt +103 -0
  99. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/NavigationContainer.kt +191 -0
  100. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/Navigator.kt +385 -0
  101. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/ScrollToTop.kt +8 -0
  102. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/StackScreen.kt +599 -0
  103. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/StackTransition.kt +157 -0
  104. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/SwipeBackHaptic.kt +39 -0
  105. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/BottomTab.kt +156 -0
  106. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/BottomTabBar.kt +225 -0
  107. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/CurvedContainer.kt +86 -0
  108. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/FloatingButton.kt +169 -0
  109. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/Header.kt +320 -0
  110. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderBackground.kt +81 -0
  111. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderRight.kt +309 -0
  112. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderTitle.kt +34 -0
  113. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderUser.kt +360 -0
  114. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/SnackBar.kt +132 -0
  115. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/tracking/ScreenTracker.kt +127 -0
  116. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/BackGesture.kt +15 -0
  117. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ComposeLottieAnimation.kt +91 -0
  118. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/OsFontScale.kt +9 -0
  119. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/Platform.kt +69 -0
  120. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Icons.kt +1329 -0
  121. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Resources.kt +55 -0
  122. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Tracking.kt +15 -0
  123. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Utils.kt +105 -0
  124. package/compose/src/iosMain/kotlin/vn/momo/kits/const/HostFont.ios.kt +40 -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 +225 -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,385 @@
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
+ internal fun canPop(): Boolean = navController.previousBackStackEntry != null
84
+
85
+ fun pop(count: Int = 1, callBack: (() -> Unit)? = null) {
86
+ scope.launch {
87
+ repeat(count) {
88
+ if (OverplayComponentRegistry.getOverplayType() == OverplayComponentType.SNACK_BAR){
89
+ dismissScreen()
90
+ OverplayComponentRegistry.hardClearAfterDismiss()
91
+ }
92
+ else if (OverplayComponentRegistry.isOverplayShowing()){
93
+ dismissOverplay()
94
+ } else {
95
+ dismissScreen()
96
+ }
97
+ }
98
+ callBack?.invoke()
99
+ }
100
+ }
101
+ private suspend fun dismissOverplay(isDelay: Boolean = true) {
102
+ OverplayComponentRegistry.clear()
103
+ if (isDelay) delay(300L.milliseconds)
104
+ OverplayComponentRegistry.hardClearAfterDismiss()
105
+ }
106
+
107
+ private suspend fun dismissScreen() {
108
+ if (navController.previousBackStackEntry != null){
109
+ val dismissing = currentScreen()
110
+ navController.popBackStack()
111
+ delay(STACK_TRANSITION_DURATION.milliseconds)
112
+ dismissing?.let { registry.unregisterScreen(it.id) }
113
+ } else {
114
+ maxApi?.dismiss { }
115
+ }
116
+ }
117
+
118
+ fun present(screenName: String, content: @Composable () -> Unit, options: NavigationOptions? = null) {
119
+ val route = registry.register(screenName, content, options)
120
+ navController.navigate(DynamicDialogRoute(route.id))
121
+ }
122
+
123
+ fun reset(screenName: String, content: @Composable () -> Unit, options: NavigationOptions? = null) {
124
+ registry.unregisterAll()
125
+
126
+ val route = registry.register(screenName, content, options)
127
+ navController.navigate(DynamicScreenRoute(route.id)) {
128
+ popUpTo(0) { inclusive = true }
129
+ }
130
+ }
131
+
132
+ fun showModal(
133
+ content: @Composable () -> Unit,
134
+ barrierDismissible: Boolean = true,
135
+ onDismiss: (() -> Unit)? = null
136
+ ){
137
+ val id = currentScreen()?.id ?: -1
138
+ OverplayComponentRegistry.registerOverplay(id, content, OverplayComponentType.MODAL, false, barrierDismissible, onDismiss)
139
+ }
140
+
141
+ fun showBottomSheet(
142
+ content: @Composable () -> Unit,
143
+ isSurface: Boolean = false,
144
+ barrierDismissible: Boolean = true,
145
+ onDismiss: (() -> Unit)? = null,
146
+ bottomSheetHeader: BottomHeader? = null,
147
+ draggable: Boolean = true,
148
+ useBottomInset: Boolean = true,
149
+ footerComponent: (@Composable () -> Unit)? = null
150
+ ){
151
+ val id = currentScreen()?.id ?: -1
152
+ OverplayComponentRegistry.registerOverplay(id, content, OverplayComponentType.BOTTOM_SHEET, isSurface, barrierDismissible, onDismiss, bottomSheetHeader, draggable, useBottomInset, footerComponent)
153
+ }
154
+
155
+ fun showSnackBar(snackBar: SnackBar, onDismiss: (() -> Unit)? = null) {
156
+ val id = currentScreen()?.id ?: -1
157
+ scope.launch {
158
+ OverplayComponentRegistry.registerOverplay(
159
+ id = id,
160
+ content = {
161
+ SnackBar(snackBar, onDismiss)
162
+ },
163
+ type = OverplayComponentType.SNACK_BAR,
164
+ onDismiss = onDismiss
165
+ )
166
+ }
167
+ }
168
+
169
+ fun hideSnackBar() {
170
+ scope.launch {
171
+ OverplayComponentRegistry.clear()
172
+ delay(250L.milliseconds)
173
+ OverplayComponentRegistry.hardClearAfterDismiss()
174
+ }
175
+ }
176
+
177
+ fun dispose(){
178
+ scope.cancel()
179
+ }
180
+ }
181
+
182
+ val LocalNavigator = staticCompositionLocalOf<Navigator> {
183
+ error("No NavigationStack provided")
184
+ }
185
+
186
+ @Serializable
187
+ data class DynamicScreenRoute(val id: Int)
188
+
189
+ @Serializable
190
+ data class DynamicDialogRoute(val id: Int)
191
+
192
+ data class DynamicScreen(
193
+ val id: Int,
194
+ val name: String,
195
+ val content: @Composable () -> Unit,
196
+ var options: NavigationOptions? = null
197
+ )
198
+
199
+ class DynamicScreenRegistry {
200
+ private val screens = mutableMapOf<Int, DynamicScreen>()
201
+
202
+ fun register(screenName: String, content: @Composable () -> Unit, options: NavigationOptions?): DynamicScreenRoute {
203
+ val id = nextGlobalId()
204
+ screens[id] = DynamicScreen(
205
+ id = id,
206
+ name = screenName,
207
+ content = content,
208
+ options = options
209
+ )
210
+ return DynamicScreenRoute(id)
211
+ }
212
+
213
+ fun nextId(): Int = nextGlobalId()
214
+
215
+ fun bind(id: Int, screenName: String, content: @Composable () -> Unit, options: NavigationOptions?) {
216
+ screens[id] = DynamicScreen(
217
+ id = id,
218
+ name = screenName,
219
+ content = content,
220
+ options = options
221
+ )
222
+ advanceGlobalIdPast(id) // keep counter ahead after process-death restore
223
+ }
224
+
225
+ fun unregisterScreen(id: Int) {
226
+ screens.remove(id)
227
+ }
228
+
229
+ // Gesture pops go through composeNavigator.popBackStack() inside NavHost and never reach
230
+ // Navigator.dismissScreen(), so screens would leak without a stack-driven prune.
231
+ fun retainOnly(ids: Set<Int>) {
232
+ val stale = screens.keys.filter { it !in ids }
233
+ for (id in stale) {
234
+ screens.remove(id)
235
+ }
236
+ }
237
+
238
+ fun unregisterAll(){
239
+ val ids = screens.keys.toList()
240
+ for (id in ids) {
241
+ unregisterScreen(id)
242
+ }
243
+ }
244
+
245
+ fun getScreen(id: Int): DynamicScreen? = screens[id]
246
+
247
+ fun setOptions(id: Int, options: NavigationOptions){
248
+ screens[id]?.options = options
249
+ }
250
+
251
+ companion object {
252
+ private var globalIdCounter = 1
253
+
254
+ private fun nextGlobalId(): Int = globalIdCounter++
255
+
256
+ private fun advanceGlobalIdPast(id: Int) {
257
+ if (id >= globalIdCounter) globalIdCounter = id + 1
258
+ }
259
+ }
260
+ }
261
+
262
+ val LocalDynamicScreenRegistry = staticCompositionLocalOf<DynamicScreenRegistry?> { null }
263
+
264
+
265
+ sealed class OverplayComponentParams {
266
+ class Modal(
267
+ val onDismiss: (() -> Unit)? = null,
268
+ val barrierDismissible: Boolean = true
269
+ ) : OverplayComponentParams()
270
+
271
+ class BottomSheet(
272
+ val isSurface: Boolean = false,
273
+ val onDismiss: (() -> Unit)? = null,
274
+ val barrierDismissible: Boolean = true,
275
+ val bottomSheetHeader: BottomHeader? = null,
276
+ val draggable: Boolean = true,
277
+ val useBottomInset: Boolean = true,
278
+ val footerComponent: (@Composable () -> Unit)? = null,
279
+ ) : OverplayComponentParams()
280
+
281
+ class SnackBar : OverplayComponentParams()
282
+ }
283
+
284
+ data class OverplayComponent(
285
+ val id: Int,
286
+ val type: OverplayComponentType? = null,
287
+ val content: @Composable () -> Unit,
288
+ val params: OverplayComponentParams? = null
289
+ )
290
+
291
+ enum class OverplayComponentType {
292
+ MODAL, BOTTOM_SHEET, SNACK_BAR
293
+ }
294
+
295
+ object OverplayComponentRegistry {
296
+ private var currentOverlayComponent : MutableState<OverplayComponent?> = mutableStateOf(null)
297
+ private var requestClose: (() -> Unit)? = null
298
+ internal fun bindClose(handler: (() -> Unit)?) {
299
+ requestClose = handler
300
+ }
301
+
302
+ fun registerOverplay(
303
+ id: Int,
304
+ content: @Composable () -> Unit,
305
+ type: OverplayComponentType,
306
+ isSurface: Boolean = false,
307
+ barrierDismissible: Boolean = true,
308
+ onDismiss: (() -> Unit)?,
309
+ bottomSheetHeader: BottomHeader? = null,
310
+ draggable: Boolean = true,
311
+ useBottomInset: Boolean = true,
312
+ footerComponent: (@Composable () -> Unit)? = null,
313
+ ){
314
+ val params = when(type){
315
+ OverplayComponentType.MODAL -> OverplayComponentParams.Modal(onDismiss, barrierDismissible)
316
+ OverplayComponentType.BOTTOM_SHEET -> OverplayComponentParams.BottomSheet(isSurface, onDismiss, barrierDismissible, bottomSheetHeader, draggable, useBottomInset, footerComponent)
317
+ OverplayComponentType.SNACK_BAR -> OverplayComponentParams.SnackBar()
318
+ }
319
+
320
+ currentOverlayComponent.value = OverplayComponent(
321
+ id = id,
322
+ type = type,
323
+ content = content,
324
+ params = params
325
+ )
326
+ }
327
+
328
+ fun isOverplayShowing(): Boolean = currentOverlayComponent.value != null
329
+
330
+ fun getOverplayType(): OverplayComponentType? = currentOverlayComponent.value?.type
331
+
332
+ fun currentRootId(): Int? = currentOverlayComponent.value?.id
333
+
334
+ fun clear(){
335
+ if (requestClose != null) {
336
+ requestClose?.invoke()
337
+ } else {
338
+ currentOverlayComponent.value = null
339
+ }
340
+ }
341
+
342
+ internal fun hardClearAfterDismiss() {
343
+ currentOverlayComponent.value = null
344
+ requestClose = null
345
+ }
346
+
347
+ @Composable
348
+ internal fun OverlayComponent(){
349
+ val overplay = currentOverlayComponent.value ?: return
350
+
351
+ when (val params = overplay.params) {
352
+ is OverplayComponentParams.BottomSheet -> {
353
+ BottomSheet(
354
+ content = overplay.content,
355
+ header = params.bottomSheetHeader ?: Title(),
356
+ isSurface = params.isSurface,
357
+ barrierDismissible = params.barrierDismissible,
358
+ draggable = params.draggable,
359
+ useBottomInset = params.useBottomInset,
360
+ footerComponent = params.footerComponent,
361
+ onDismiss = params.onDismiss
362
+ )
363
+ }
364
+
365
+ is OverplayComponentParams.Modal -> {
366
+ ModalScreen(
367
+ content = overplay.content,
368
+ barrierDismissible = params.barrierDismissible,
369
+ onDismiss = params.onDismiss
370
+ )
371
+ }
372
+
373
+ is OverplayComponentParams.SnackBar -> {
374
+ Box(
375
+ modifier = Modifier.fillMaxSize(),
376
+ contentAlignment = Alignment.BottomCenter
377
+ ) {
378
+ overplay.content()
379
+ }
380
+ }
381
+
382
+ null -> {}
383
+ }
384
+ }
385
+ }
@@ -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
+