@momo-kits/native-kits 0.169.8-beta.6-debug → 0.169.8-beta.7

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 (146) hide show
  1. package/ios/Navigation/BottomTab/BottomTabBar.swift +9 -0
  2. package/ios/Navigation/Navigation.swift +25 -0
  3. package/ios/Navigation/StackScreen.swift +18 -5
  4. package/ios/native-kits.podspec +1 -1
  5. package/ios-demo/MoMoUIKitsDemo.podspec +1 -1
  6. package/ios-demo/Sources/MoMoUIKitsDemo/SampleDemos/BottomTabDemo.swift +13 -1
  7. package/package.json +1 -1
  8. package/build.gradle.kts +0 -11
  9. package/compose/build.gradle.kts +0 -198
  10. package/compose/compose.podspec +0 -47
  11. package/compose/src/androidMain/kotlin/vn/momo/kits/const/HostFont.android.kt +0 -19
  12. package/compose/src/androidMain/kotlin/vn/momo/kits/navigation/ScrollToTop.android.kt +0 -6
  13. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/BackGesture.android.kt +0 -12
  14. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/OsFontScale.android.kt +0 -7
  15. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/Platform.android.kt +0 -154
  16. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/ReduceMotion.android.kt +0 -17
  17. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/ReservedRegions.android.kt +0 -6
  18. package/compose/src/commonMain/kotlin/vn/momo/kits/application/Context.kt +0 -110
  19. package/compose/src/commonMain/kotlin/vn/momo/kits/application/DesignSystemKits.kt +0 -62
  20. package/compose/src/commonMain/kotlin/vn/momo/kits/application/LiteScreen.kt +0 -1067
  21. package/compose/src/commonMain/kotlin/vn/momo/kits/application/Localize.kt +0 -267
  22. package/compose/src/commonMain/kotlin/vn/momo/kits/application/WidgetContainer.kt +0 -56
  23. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Avatar.kt +0 -157
  24. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Badge.kt +0 -82
  25. package/compose/src/commonMain/kotlin/vn/momo/kits/components/BadgeDot.kt +0 -32
  26. package/compose/src/commonMain/kotlin/vn/momo/kits/components/BadgeRibbon.kt +0 -338
  27. package/compose/src/commonMain/kotlin/vn/momo/kits/components/BaselineView.kt +0 -175
  28. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Button.kt +0 -356
  29. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Carousel.kt +0 -115
  30. package/compose/src/commonMain/kotlin/vn/momo/kits/components/CheckBox.kt +0 -89
  31. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Chip.kt +0 -128
  32. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Collapse.kt +0 -209
  33. package/compose/src/commonMain/kotlin/vn/momo/kits/components/CupertinoOverscroll.kt +0 -482
  34. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Divider.kt +0 -49
  35. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Icon.kt +0 -86
  36. package/compose/src/commonMain/kotlin/vn/momo/kits/components/IconButton.kt +0 -125
  37. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Image.kt +0 -224
  38. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Information.kt +0 -195
  39. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Input.kt +0 -473
  40. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputDropDown.kt +0 -113
  41. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputMoney.kt +0 -263
  42. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputOTP.kt +0 -235
  43. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputPhoneNumber.kt +0 -228
  44. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputSearch.kt +0 -280
  45. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputTextArea.kt +0 -183
  46. package/compose/src/commonMain/kotlin/vn/momo/kits/components/LazyColumnWithBouncing.kt +0 -343
  47. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Loader.kt +0 -108
  48. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationDot.kt +0 -56
  49. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationNumber.kt +0 -40
  50. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationScroll.kt +0 -82
  51. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationWhiteDot.kt +0 -39
  52. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PopupNotify.kt +0 -363
  53. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PopupPromotion.kt +0 -108
  54. package/compose/src/commonMain/kotlin/vn/momo/kits/components/ProgressInfo.kt +0 -307
  55. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Radio.kt +0 -59
  56. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Rating.kt +0 -87
  57. package/compose/src/commonMain/kotlin/vn/momo/kits/components/ScaleSizeScope.kt +0 -21
  58. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Skeleton.kt +0 -88
  59. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Slider.kt +0 -306
  60. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Stepper.kt +0 -220
  61. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Steps.kt +0 -457
  62. package/compose/src/commonMain/kotlin/vn/momo/kits/components/SuggestAction.kt +0 -122
  63. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Swipe.kt +0 -205
  64. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Switch.kt +0 -89
  65. package/compose/src/commonMain/kotlin/vn/momo/kits/components/TabView.kt +0 -542
  66. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Tag.kt +0 -86
  67. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Text.kt +0 -120
  68. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Title.kt +0 -201
  69. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Tooltip.kt +0 -498
  70. package/compose/src/commonMain/kotlin/vn/momo/kits/components/TrustBanner.kt +0 -176
  71. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Uploader.kt +0 -182
  72. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePicker.kt +0 -193
  73. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePickerTypes.kt +0 -29
  74. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePickerUtils.kt +0 -210
  75. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/WheelPicker.kt +0 -180
  76. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Colors.kt +0 -325
  77. package/compose/src/commonMain/kotlin/vn/momo/kits/const/HostFont.kt +0 -12
  78. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Radius.kt +0 -12
  79. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Spacing.kt +0 -16
  80. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Theme.kt +0 -194
  81. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Typography.kt +0 -292
  82. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Card.kt +0 -54
  83. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/HeaderLayout.kt +0 -63
  84. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Item.kt +0 -50
  85. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/ModalLayout.kt +0 -184
  86. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/ScreenLayout.kt +0 -316
  87. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Section.kt +0 -41
  88. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/AutomationId.kt +0 -52
  89. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Clickable.kt +0 -66
  90. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Conditional.kt +0 -11
  91. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/DeprecatedModifier.kt +0 -104
  92. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Shadow.kt +0 -48
  93. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Size.kt +0 -51
  94. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/BottomSheet.kt +0 -397
  95. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/HostBackGestureBridge.kt +0 -58
  96. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/ModalScreen.kt +0 -153
  97. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/Navigation.kt +0 -118
  98. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/NavigationContainer.kt +0 -205
  99. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/Navigator.kt +0 -422
  100. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/ScrollToTop.kt +0 -8
  101. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/StackScreen.kt +0 -746
  102. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/StackTransition.kt +0 -148
  103. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/SwipeBackHaptic.kt +0 -35
  104. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/BottomTab.kt +0 -242
  105. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/BottomTabBar.kt +0 -391
  106. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/CurvedContainer.kt +0 -100
  107. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/DuoBarCompression.kt +0 -97
  108. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/DuoTabBar.kt +0 -207
  109. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/TabBarMetrics.kt +0 -132
  110. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/FloatingButton.kt +0 -167
  111. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/Header.kt +0 -446
  112. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderBackground.kt +0 -105
  113. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderJourney.kt +0 -106
  114. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderLocation.kt +0 -74
  115. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderRight.kt +0 -398
  116. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderTitle.kt +0 -39
  117. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderUser.kt +0 -363
  118. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/SideBar.kt +0 -197
  119. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/SnackBar.kt +0 -133
  120. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/tracking/ScreenTracker.kt +0 -127
  121. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/BackGesture.kt +0 -12
  122. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ComposeLottieAnimation.kt +0 -114
  123. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/OsFontScale.kt +0 -9
  124. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/Platform.kt +0 -76
  125. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ReduceMotion.kt +0 -7
  126. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ReservedRegions.kt +0 -22
  127. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Icons.kt +0 -1329
  128. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Resources.kt +0 -55
  129. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Tracking.kt +0 -15
  130. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Utils.kt +0 -118
  131. package/compose/src/iosMain/kotlin/vn/momo/kits/const/HostFont.ios.kt +0 -42
  132. package/compose/src/iosMain/kotlin/vn/momo/kits/navigation/HostBackGesture.ios.kt +0 -45
  133. package/compose/src/iosMain/kotlin/vn/momo/kits/navigation/ScrollToTop.ios.kt +0 -32
  134. package/compose/src/iosMain/kotlin/vn/momo/kits/platform/BackGesture.ios.kt +0 -38
  135. package/compose/src/iosMain/kotlin/vn/momo/kits/platform/OsFontScale.ios.kt +0 -66
  136. package/compose/src/iosMain/kotlin/vn/momo/kits/platform/Platform.ios.kt +0 -218
  137. package/compose/src/iosMain/kotlin/vn/momo/kits/platform/ReduceMotion.ios.kt +0 -48
  138. package/compose/src/iosMain/kotlin/vn/momo/kits/platform/ReservedRegions.ios.kt +0 -144
  139. package/gradle/libs.versions.toml +0 -67
  140. package/gradle/wrapper/gradle-wrapper.jar +0 -0
  141. package/gradle/wrapper/gradle-wrapper.properties +0 -8
  142. package/gradle.properties +0 -24
  143. package/gradlew +0 -252
  144. package/gradlew.bat +0 -94
  145. package/ios-demo/README.md +0 -120
  146. package/settings.gradle.kts +0 -64
@@ -1,209 +0,0 @@
1
- package vn.momo.kits.components
2
-
3
- import androidx.compose.animation.AnimatedVisibility
4
- import androidx.compose.animation.core.animateFloatAsState
5
- import androidx.compose.animation.core.tween
6
- import androidx.compose.animation.expandVertically
7
- import androidx.compose.animation.shrinkVertically
8
- import androidx.compose.foundation.background
9
- import androidx.compose.foundation.border
10
- import androidx.compose.foundation.layout.*
11
- import androidx.compose.foundation.shape.RoundedCornerShape
12
- import androidx.compose.runtime.*
13
- import androidx.compose.ui.Alignment
14
- import androidx.compose.ui.Modifier
15
- import androidx.compose.ui.draw.clip
16
- import androidx.compose.ui.draw.rotate
17
- import androidx.compose.ui.graphics.Color
18
- import androidx.compose.ui.text.TextStyle
19
- import androidx.compose.ui.unit.dp
20
- import vn.momo.kits.application.IsShowBaseLineDebug
21
- import vn.momo.kits.const.*
22
- import vn.momo.kits.modifier.activeOpacityClickable
23
- import vn.momo.kits.modifier.conditional
24
- import vn.momo.kits.modifier.setAutomationId
25
-
26
- enum class CollapseTitleSize {
27
- Small, Medium, Large
28
- }
29
-
30
- @Composable
31
- fun Collapse(
32
- title: String,
33
- modifier: Modifier = Modifier,
34
- description: String? = null,
35
- image: String? = null,
36
- imageSize: Int = 24,
37
- subTitle: String? = null,
38
- tagLabel: String? = null,
39
- tagColor: TagColor = TagColor.Default,
40
- showBorder: Boolean = false,
41
- expandDefault: Boolean = false,
42
- titleSize: CollapseTitleSize = CollapseTitleSize.Medium,
43
- useBackgroundCollapseButton: Boolean = false,
44
- backgroundColor: Color? = null,
45
- titleColor: Color? = null,
46
- onPress: ((isExpanded: Boolean) -> Unit)? = null,
47
- accessibilityId: String? = null,
48
- content: (@Composable () -> Unit)? = null,
49
- ) {
50
- val theme = AppTheme.current
51
-
52
- var expanded by remember { mutableStateOf(expandDefault) }
53
-
54
- val chevronRotation by animateFloatAsState(
55
- targetValue = if (expanded) 180f else 0f,
56
- animationSpec = tween(durationMillis = 300),
57
- label = "chevronRotation"
58
- )
59
-
60
- val resolvedBackgroundColor = remember(backgroundColor, theme) {
61
- backgroundColor ?: theme.colors.background.surface
62
- }
63
-
64
- val resolvedTitleStyle: TextStyle = remember(titleSize) {
65
- when (titleSize) {
66
- CollapseTitleSize.Small -> Typography.headerSSemibold
67
- CollapseTitleSize.Medium -> Typography.headerDefaultBold
68
- CollapseTitleSize.Large -> Typography.headerMBold
69
- }
70
- }
71
-
72
- val borderShape = remember { RoundedCornerShape(Radius.S) }
73
-
74
- val automationId = remember(accessibilityId, title) {
75
- accessibilityId ?: "Collapse/$title"
76
- }
77
-
78
- Column(
79
- modifier = modifier
80
- .fillMaxWidth()
81
- .clip(borderShape)
82
- .background(resolvedBackgroundColor, shape = borderShape)
83
- .conditional(showBorder) {
84
- border(
85
- width = 1.dp,
86
- color = theme.colors.border.default,
87
- shape = borderShape
88
- )
89
- }
90
- .conditional(IsShowBaseLineDebug) {
91
- border(1.dp, Colors.blue_03)
92
- }
93
- .setAutomationId(automationId)
94
- ) {
95
- Row(
96
- modifier = Modifier
97
- .fillMaxWidth()
98
- .activeOpacityClickable {
99
- onPress?.invoke(expanded)
100
- expanded = !expanded
101
- }
102
- .padding(horizontal = Spacing.M, vertical = Spacing.S)
103
- .setAutomationId("$automationId|touch"),
104
- verticalAlignment = Alignment.Top
105
- ) {
106
- if (image != null) {
107
- Image(
108
- source = image,
109
- modifier = Modifier
110
- .size(imageSize.dp)
111
- .clip(RoundedCornerShape(Radius.XS))
112
- .setAutomationId("$automationId|img")
113
- )
114
- Spacer(modifier = Modifier.width(Spacing.S))
115
- }
116
-
117
- Column(modifier = Modifier.weight(1f)) {
118
- Text(
119
- text = title,
120
- style = resolvedTitleStyle,
121
- color = titleColor ?: theme.colors.text.default,
122
- accessibilityId = "$automationId|text"
123
- )
124
- if (description != null) {
125
- Text(
126
- text = description,
127
- style = Typography.bodyDefaultRegular,
128
- color = theme.colors.text.secondary,
129
- maxLines = 2,
130
- accessibilityId = "$automationId|description-text"
131
- )
132
- }
133
- }
134
-
135
- val hasInfo = subTitle != null || tagLabel != null
136
- if (hasInfo) {
137
- Spacer(modifier = Modifier.width(Spacing.S))
138
- Box(contentAlignment = Alignment.CenterEnd) {
139
- if (subTitle != null) {
140
- Text(
141
- text = subTitle,
142
- style = Typography.bodyDefaultRegular,
143
- color = theme.colors.text.secondary,
144
- maxLines = 1,
145
- accessibilityId = "$automationId|sub-title-text"
146
- )
147
- } else if (tagLabel != null) {
148
- Tag(
149
- label = tagLabel,
150
- color = tagColor
151
- )
152
- }
153
- }
154
- }
155
-
156
- Spacer(modifier = Modifier.width(Spacing.S))
157
- if (useBackgroundCollapseButton) {
158
- Box(
159
- modifier = Modifier
160
- .size(24.dp)
161
- .background(
162
- color = theme.colors.background.tonal,
163
- shape = RoundedCornerShape(Radius.M)
164
- ),
165
- contentAlignment = Alignment.Center
166
- ) {
167
- Icon(
168
- source = "arrow_chevron_down_small",
169
- size = 16.dp,
170
- color = theme.colors.primary,
171
- modifier = Modifier
172
- .rotate(chevronRotation)
173
- .setAutomationId("$automationId|icon")
174
- )
175
- }
176
- } else {
177
- Icon(
178
- source = "arrow_chevron_down_small",
179
- size = 24.dp,
180
- color = theme.colors.text.default,
181
- modifier = Modifier
182
- .rotate(chevronRotation)
183
- .setAutomationId("$automationId|icon")
184
- )
185
- }
186
- }
187
-
188
- if (content != null) {
189
- AnimatedVisibility(
190
- visible = expanded,
191
- enter = expandVertically(animationSpec = tween(durationMillis = 300)),
192
- exit = shrinkVertically(animationSpec = tween(durationMillis = 300))
193
- ) {
194
- Column(
195
- modifier = Modifier
196
- .fillMaxWidth()
197
- .conditional(showBorder) {
198
- border(
199
- width = 1.dp,
200
- color = theme.colors.border.default
201
- )
202
- }
203
- ) {
204
- content()
205
- }
206
- }
207
- }
208
- }
209
- }
@@ -1,482 +0,0 @@
1
- package vn.momo.kits.components
2
-
3
- import androidx.compose.animation.core.AnimationState
4
- import androidx.compose.animation.core.VectorConverter
5
- import androidx.compose.animation.core.animateTo
6
- import androidx.compose.animation.core.spring
7
- import androidx.compose.foundation.OverscrollEffect
8
- import androidx.compose.material.ExperimentalMaterialApi
9
- import androidx.compose.runtime.Stable
10
- import androidx.compose.runtime.State
11
- import androidx.compose.runtime.getValue
12
- import androidx.compose.runtime.mutableStateOf
13
- import androidx.compose.runtime.setValue
14
- import androidx.compose.ui.Modifier
15
- import androidx.compose.ui.geometry.Offset
16
- import androidx.compose.ui.geometry.Rect
17
- import androidx.compose.ui.geometry.Size
18
- import androidx.compose.ui.geometry.toRect
19
- import androidx.compose.ui.graphics.drawscope.ContentDrawScope
20
- import androidx.compose.ui.graphics.drawscope.clipRect
21
- import androidx.compose.ui.input.nestedscroll.NestedScrollSource
22
- import androidx.compose.ui.input.pointer.PointerEvent
23
- import androidx.compose.ui.input.pointer.PointerEventPass
24
- import androidx.compose.ui.input.pointer.PointerEventType
25
- import androidx.compose.ui.layout.Measurable
26
- import androidx.compose.ui.layout.MeasureResult
27
- import androidx.compose.ui.layout.MeasureScope
28
- import androidx.compose.ui.node.DelegatableNode
29
- import androidx.compose.ui.node.DrawModifierNode
30
- import androidx.compose.ui.node.LayoutAwareModifierNode
31
- import androidx.compose.ui.node.LayoutModifierNode
32
- import androidx.compose.ui.node.PointerInputModifierNode
33
- import androidx.compose.ui.unit.Constraints
34
- import androidx.compose.ui.unit.Density
35
- import androidx.compose.ui.unit.Dp
36
- import androidx.compose.ui.unit.IntOffset
37
- import androidx.compose.ui.unit.IntSize
38
- import androidx.compose.ui.unit.Velocity
39
- import androidx.compose.ui.unit.dp
40
- import androidx.compose.ui.unit.round
41
- import androidx.compose.ui.unit.toOffset
42
- import androidx.compose.ui.unit.toSize
43
- import kotlinx.coroutines.CoroutineScope
44
- import kotlinx.coroutines.cancel
45
- import kotlinx.coroutines.currentCoroutineContext
46
- import kotlinx.coroutines.isActive
47
- import kotlin.math.abs
48
- import kotlin.math.sign
49
-
50
- private enum class CupertinoScrollSource {
51
- DRAG, FLING
52
- }
53
-
54
- private enum class CupertinoOverscrollDirection {
55
- UNKNOWN, VERTICAL, HORIZONTAL
56
- }
57
-
58
- private enum class CupertinoSpringAnimationReason {
59
- FLING_FROM_OVERSCROLL, POSSIBLE_SPRING_IN_THE_END
60
- }
61
-
62
-
63
- private data class CupertinoOverscrollAvailableDelta(
64
- val availableDelta: Float,
65
- val newOverscrollValue: Float
66
- )
67
-
68
- data class PullToRefreshCustomState(
69
- val refreshingState: State<Boolean>,
70
- val onRefresh: () -> Unit,
71
- val threadHold: Dp = 140.dp,
72
- val refreshOffset: Dp = 100.dp,
73
- val sendHaptic: (() -> Unit)? = null,
74
- ) {
75
- val position = mutableStateOf(0f)
76
-
77
- fun updatePercentage(percentage: Float) {
78
- position.value = percentage.coerceIn(0f, 1f)
79
- }
80
- }
81
-
82
- /**
83
- * iOS rubber-band overscroll (from JetBrains compose-multiplatform-core) with optional pull-to-refresh.
84
- * The formulas run in dp, hence [density]; [applyClip] is for consumers that don't clip themselves.
85
- */
86
- internal class CupertinoOverscrollEffect(
87
- val pullRefreshState: PullToRefreshCustomState? = null,
88
- private val density: Float,
89
- val applyClip: Boolean
90
- ) : OverscrollEffect {
91
- // Follows the axis of the latest delta: the API can't end per-axis spring/fling stages at different
92
- // times (Notes of https://github.com/JetBrains/compose-multiplatform-core/pull/609).
93
- private var direction: CupertinoOverscrollDirection = CupertinoOverscrollDirection.UNKNOWN
94
-
95
- private var scrollSize: Size = Size.Zero
96
-
97
- // Raw overscroll: positive top-left, negative bottom-right, zero in range; drawn rubber-banded.
98
- private var overscrollOffsetState = mutableStateOf(Offset.Zero)
99
- private var overscrollOffset: Offset
100
- get() = overscrollOffsetState.value
101
- set(value) {
102
- overscrollOffsetState.value = value
103
- drawCallScheduledByOffsetChange = true
104
- }
105
- private var drawCallScheduledByOffsetChange = true
106
-
107
- private var lastFlingUnconsumedDelta: Offset = Offset.Zero
108
- private val visibleOverscrollOffset: IntOffset
109
- get() = overscrollOffsetState.value.rubberBanded().round()
110
-
111
- override val isInProgress: Boolean
112
- get() = visibleOverscrollOffset.toOffset().getDistance() > 0.5f
113
-
114
- @OptIn(ExperimentalMaterialApi::class)
115
- private val overscrollNode = CupertinoOverscrollNode(
116
- pullRefreshState = pullRefreshState,
117
- offset = { visibleOverscrollOffset },
118
- onNodeRemeasured = { scrollSize = it.toSize() },
119
- onDraw = ::onDraw,
120
- applyClip = applyClip
121
- )
122
- override val node: DelegatableNode get() = overscrollNode
123
-
124
- private fun onDraw() {
125
- // A cancelled scroll can leave the effect half-way: reset it once nothing animates or touches it.
126
- if (!drawCallScheduledByOffsetChange && isInProgress && overscrollNode.pointersDown == 0) {
127
- overscrollOffsetState.value = Offset.Zero
128
- }
129
-
130
- drawCallScheduledByOffsetChange = false
131
- }
132
-
133
- private fun NestedScrollSource.toCupertinoScrollSource(): CupertinoScrollSource? =
134
- when (this) {
135
- NestedScrollSource.UserInput -> CupertinoScrollSource.DRAG
136
- NestedScrollSource.SideEffect -> CupertinoScrollSource.FLING
137
- else -> null
138
- }
139
-
140
- @Stable
141
- private fun availableDelta(
142
- delta: Float,
143
- overscroll: Float,
144
- source: CupertinoScrollSource
145
- ): CupertinoOverscrollAvailableDelta {
146
- if (source == CupertinoScrollSource.FLING) {
147
- return CupertinoOverscrollAvailableDelta(delta, overscroll)
148
- }
149
-
150
- val newOverscroll = overscroll + delta
151
-
152
- return if (delta >= 0f && overscroll <= 0f) {
153
- if (newOverscroll > 0f) {
154
- CupertinoOverscrollAvailableDelta(newOverscroll, 0f)
155
- } else {
156
- CupertinoOverscrollAvailableDelta(0f, newOverscroll)
157
- }
158
- } else if (delta <= 0f && overscroll >= 0f) {
159
- if (newOverscroll < 0f) {
160
- CupertinoOverscrollAvailableDelta(newOverscroll, 0f)
161
- } else {
162
- CupertinoOverscrollAvailableDelta(0f, newOverscroll)
163
- }
164
- } else {
165
- CupertinoOverscrollAvailableDelta(0f, newOverscroll)
166
- }
167
- }
168
-
169
- // Delta left after the overscroll area takes its share; moves [overscrollOffset] as a side effect.
170
- private fun availableDelta(delta: Offset, source: CupertinoScrollSource): Offset {
171
- val (x, overscrollX) = availableDelta(delta.x, overscrollOffset.x, source)
172
- val (y, overscrollY) = availableDelta(delta.y, overscrollOffset.y, source)
173
- overscrollOffset = Offset(overscrollX, overscrollY)
174
-
175
- return Offset(x, y)
176
- }
177
-
178
- private fun applyToScroll(
179
- delta: Offset,
180
- source: CupertinoScrollSource,
181
- performScroll: (Offset) -> Offset
182
- ): Offset {
183
- val deltaLeftForPerformScroll = availableDelta(delta, source)
184
- val deltaConsumedByPerformScroll = performScroll(deltaLeftForPerformScroll)
185
- val unconsumedDelta = deltaLeftForPerformScroll - deltaConsumedByPerformScroll
186
-
187
- return when (source) {
188
- CupertinoScrollSource.DRAG -> {
189
- // The rest goes back into overscroll: a drag can cross the content edge within one frame.
190
- overscrollOffset += unconsumedDelta
191
- lastFlingUnconsumedDelta = Offset.Zero
192
- delta - unconsumedDelta
193
- }
194
-
195
- CupertinoScrollSource.FLING -> {
196
- // Non-zero cancels the fling and starts the spring animation instead.
197
- lastFlingUnconsumedDelta = unconsumedDelta
198
- delta - unconsumedDelta
199
- }
200
- }
201
- }
202
-
203
- override fun applyToScroll(
204
- delta: Offset,
205
- source: NestedScrollSource,
206
- performScroll: (Offset) -> Offset
207
- ): Offset {
208
- springAnimationScope?.cancel()
209
- springAnimationScope = null
210
-
211
- direction = direction.combinedWith(delta.toCupertinoOverscrollDirection())
212
-
213
- return source.toCupertinoScrollSource()?.let {
214
- applyToScroll(delta, it, performScroll)
215
- } ?: performScroll(delta)
216
- }
217
-
218
- override suspend fun applyToFling(
219
- velocity: Velocity,
220
- performFling: suspend (Velocity) -> Velocity
221
- ) {
222
- val availableFlingVelocity = playInitialSpringAnimationIfNeeded(velocity)
223
- val velocityConsumedByFling = performFling(availableFlingVelocity)
224
- val postFlingVelocity = availableFlingVelocity - velocityConsumedByFling
225
-
226
- val unconsumedDelta = lastFlingUnconsumedDelta.toFloat()
227
- if (unconsumedDelta == 0f && overscrollOffset == Offset.Zero) {
228
- return
229
- }
230
- applyPullToRefresh()
231
-
232
- playSpringAnimation(
233
- unconsumedDelta,
234
- postFlingVelocity.toFloat(),
235
- CupertinoSpringAnimationReason.POSSIBLE_SPRING_IN_THE_END
236
- )
237
- }
238
-
239
- private fun applyPullToRefresh() {
240
- val (isRefresh, refresh, threshHold, _, _) = pullRefreshState ?: return
241
- val overscrollY = overscrollOffset.y
242
- if (isRefresh.value || overscrollY < threshHold.value * density) return
243
- refresh.invoke()
244
- }
245
-
246
- private fun Offset.toCupertinoOverscrollDirection(): CupertinoOverscrollDirection {
247
- val hasXPart = abs(x) > 0f
248
- val hasYPart = abs(y) > 0f
249
-
250
- return if (hasXPart xor hasYPart) {
251
- if (hasXPart) {
252
- CupertinoOverscrollDirection.HORIZONTAL
253
- } else {
254
- CupertinoOverscrollDirection.VERTICAL
255
- }
256
- } else {
257
- CupertinoOverscrollDirection.UNKNOWN
258
- }
259
- }
260
-
261
- private fun CupertinoOverscrollDirection.combinedWith(other: CupertinoOverscrollDirection): CupertinoOverscrollDirection =
262
- when (this) {
263
- CupertinoOverscrollDirection.UNKNOWN -> when (other) {
264
- CupertinoOverscrollDirection.UNKNOWN -> CupertinoOverscrollDirection.UNKNOWN
265
- CupertinoOverscrollDirection.VERTICAL -> CupertinoOverscrollDirection.VERTICAL
266
- CupertinoOverscrollDirection.HORIZONTAL -> CupertinoOverscrollDirection.HORIZONTAL
267
- }
268
-
269
- CupertinoOverscrollDirection.VERTICAL -> when (other) {
270
- CupertinoOverscrollDirection.UNKNOWN, CupertinoOverscrollDirection.VERTICAL -> CupertinoOverscrollDirection.VERTICAL
271
- CupertinoOverscrollDirection.HORIZONTAL -> CupertinoOverscrollDirection.HORIZONTAL
272
- }
273
-
274
- CupertinoOverscrollDirection.HORIZONTAL -> when (other) {
275
- CupertinoOverscrollDirection.UNKNOWN, CupertinoOverscrollDirection.HORIZONTAL -> CupertinoOverscrollDirection.HORIZONTAL
276
- CupertinoOverscrollDirection.VERTICAL -> CupertinoOverscrollDirection.VERTICAL
277
- }
278
- }
279
-
280
- private fun Velocity.toFloat(): Float =
281
- toOffset().toFloat()
282
-
283
- private fun Float.toVelocity(): Velocity =
284
- toOffset().toVelocity()
285
-
286
- private fun Offset.toFloat(): Float =
287
- when (direction) {
288
- CupertinoOverscrollDirection.UNKNOWN -> 0f
289
- CupertinoOverscrollDirection.VERTICAL -> y
290
- CupertinoOverscrollDirection.HORIZONTAL -> x
291
- }
292
-
293
- private fun Float.toOffset(): Offset =
294
- when (direction) {
295
- CupertinoOverscrollDirection.UNKNOWN -> Offset.Zero
296
- CupertinoOverscrollDirection.VERTICAL -> Offset(0f, this)
297
- CupertinoOverscrollDirection.HORIZONTAL -> Offset(this, 0f)
298
- }
299
-
300
- private suspend fun playInitialSpringAnimationIfNeeded(initialVelocity: Velocity): Velocity {
301
- val velocity = initialVelocity.toFloat()
302
- val overscroll = overscrollOffset.toFloat()
303
-
304
- return if ((velocity <= 0f && overscroll > 0f) || (velocity >= 0f && overscroll < 0f)) {
305
- playSpringAnimation(
306
- unconsumedDelta = 0f,
307
- velocity,
308
- CupertinoSpringAnimationReason.FLING_FROM_OVERSCROLL
309
- ).toVelocity()
310
- } else {
311
- initialVelocity
312
- }
313
- }
314
-
315
- private var springAnimationScope: CoroutineScope? = null
316
-
317
- private suspend fun playSpringAnimation(
318
- unconsumedDelta: Float,
319
- initialVelocity: Float,
320
- reason: CupertinoSpringAnimationReason
321
- ): Float {
322
- val initialValue = overscrollOffset.toFloat() + unconsumedDelta
323
- val initialSign = sign(initialValue)
324
- var currentVelocity = initialVelocity
325
-
326
- // The spring runs in dp (inputs divided by density); the callback scales back to px.
327
- val visibilityThreshold = 0.5f / density
328
-
329
- val spec = when (reason) {
330
- CupertinoSpringAnimationReason.FLING_FROM_OVERSCROLL -> {
331
- spring(
332
- stiffness = 300f,
333
- visibilityThreshold = visibilityThreshold
334
- )
335
- }
336
-
337
- CupertinoSpringAnimationReason.POSSIBLE_SPRING_IN_THE_END -> {
338
- spring(
339
- stiffness = 120f,
340
- visibilityThreshold = visibilityThreshold
341
- )
342
- }
343
- }
344
-
345
- springAnimationScope?.cancel()
346
- springAnimationScope = CoroutineScope(currentCoroutineContext())
347
- springAnimationScope?.run {
348
- AnimationState(
349
- Float.VectorConverter,
350
- initialValue / density,
351
- initialVelocity / density
352
- ).animateTo(
353
- targetValue = 0f,
354
- animationSpec = spec
355
- ) {
356
- overscrollOffset = pullRefreshState?.let { (refreshing, _, _, refreshOffset) ->
357
- if (!refreshing.value) return@let null
358
- value.coerceAtLeast(refreshOffset.value * density).toOffset()
359
- } ?: (value * density).toOffset()
360
- currentVelocity = velocity * density
361
-
362
- // Flung from overscroll: stop at the edge and hand the velocity back to the fling.
363
- if (reason == CupertinoSpringAnimationReason.FLING_FROM_OVERSCROLL &&
364
- initialSign != 0f &&
365
- sign(value) != initialSign
366
- ) {
367
- this.cancelAnimation()
368
- }
369
- }
370
- springAnimationScope = null
371
- }
372
-
373
- if (currentCoroutineContext().isActive) {
374
- // Critically damped: a slightly offset spring-fling-spring with opposite velocity ends unanimated.
375
- overscrollOffset = Offset.Zero
376
- }
377
-
378
- if (reason == CupertinoSpringAnimationReason.POSSIBLE_SPRING_IN_THE_END) {
379
- currentVelocity = 0f
380
- }
381
-
382
- return currentVelocity
383
- }
384
-
385
- private fun Offset.rubberBanded(): Offset {
386
- if (scrollSize.width == 0f || scrollSize.height == 0f) {
387
- return Offset.Zero
388
- }
389
-
390
- val dpOffset = this / density
391
- val dpSize = scrollSize / density
392
-
393
- return Offset(
394
- rubberBandedValue(dpOffset.x, dpSize.width),
395
- rubberBandedValue(dpOffset.y, dpSize.height)
396
- ) * density
397
- }
398
-
399
- private fun rubberBandedValue(value: Float, dimension: Float) =
400
- sign(value) * (1f - (1f / (abs(value) * RUBBER_BAND_COEFFICIENT / dimension + 1f))) * dimension
401
-
402
- companion object Companion {
403
- private const val RUBBER_BAND_COEFFICIENT = 0.55f
404
- }
405
- }
406
-
407
- private class CupertinoOverscrollNode @OptIn(ExperimentalMaterialApi::class) constructor(
408
- val pullRefreshState: PullToRefreshCustomState?,
409
- val offset: Density.() -> IntOffset,
410
- val onNodeRemeasured: (IntSize) -> Unit,
411
- val onDraw: () -> Unit,
412
- val applyClip: Boolean
413
- ) : Modifier.Node(),
414
- LayoutModifierNode,
415
- LayoutAwareModifierNode,
416
- DrawModifierNode,
417
- PointerInputModifierNode {
418
- override fun onRemeasured(size: IntSize) = onNodeRemeasured(size)
419
-
420
- var pointersDown by mutableStateOf(0)
421
-
422
- override fun onPointerEvent(
423
- pointerEvent: PointerEvent,
424
- pass: PointerEventPass,
425
- bounds: IntSize
426
- ) {
427
- if (pass == PointerEventPass.Final) {
428
- if (pointerEvent.type == PointerEventType.Press) {
429
- pointersDown++
430
- } else if (pointerEvent.type == PointerEventType.Release) {
431
- pointersDown--
432
- }
433
- }
434
- }
435
-
436
- override fun onCancelPointerInput() {
437
- pointersDown = 0
438
- }
439
-
440
- override fun ContentDrawScope.draw() {
441
- onDraw()
442
- if (applyClip) {
443
- val bounds = Rect(-offset().toOffset(), size)
444
- val rect = size.toRect().intersect(bounds)
445
- clipRect(
446
- left = rect.left,
447
- top = rect.top,
448
- right = rect.right,
449
- bottom = rect.bottom,
450
- ) { this@draw.drawContent() }
451
- } else {
452
- this@draw.drawContent()
453
- }
454
- }
455
-
456
- override fun MeasureScope.measure(
457
- measurable: Measurable,
458
- constraints: Constraints
459
- ): MeasureResult {
460
- val placeable = measurable.measure(constraints)
461
- return layout(placeable.width, placeable.height) {
462
- var position = offset()
463
-
464
- with(pullRefreshState) {
465
- this ?: return@with
466
- if (refreshingState.value) {
467
- position = position.copy(
468
- y = position.y.coerceAtLeast(refreshOffset.roundToPx())
469
- )
470
- }
471
- updatePercentage(position.y / refreshOffset.toPx())
472
- }
473
- placeable.placeWithLayer(position)
474
- }
475
- }
476
- }
477
-
478
- private fun Velocity.toOffset(): Offset =
479
- Offset(x, y)
480
-
481
- private fun Offset.toVelocity(): Velocity =
482
- Velocity(x, y)