@momo-kits/native-kits 0.169.15-beta.1 → 0.169.15-beta.2-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 (145) hide show
  1. package/build.gradle.kts +11 -0
  2. package/compose/build.gradle.kts +198 -0
  3. package/compose/compose.podspec +47 -0
  4. package/compose/src/androidMain/kotlin/vn/momo/kits/const/HostFont.android.kt +19 -0
  5. package/compose/src/androidMain/kotlin/vn/momo/kits/navigation/ScrollToTop.android.kt +6 -0
  6. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/BackGesture.android.kt +12 -0
  7. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/OsFontScale.android.kt +7 -0
  8. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/Platform.android.kt +154 -0
  9. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/ReduceMotion.android.kt +17 -0
  10. package/compose/src/androidMain/kotlin/vn/momo/kits/platform/ReservedRegions.android.kt +6 -0
  11. package/compose/src/commonMain/kotlin/vn/momo/kits/application/Context.kt +110 -0
  12. package/compose/src/commonMain/kotlin/vn/momo/kits/application/DesignSystemKits.kt +61 -0
  13. package/compose/src/commonMain/kotlin/vn/momo/kits/application/LiteScreen.kt +1067 -0
  14. package/compose/src/commonMain/kotlin/vn/momo/kits/application/Localize.kt +268 -0
  15. package/compose/src/commonMain/kotlin/vn/momo/kits/application/WidgetContainer.kt +56 -0
  16. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Avatar.kt +157 -0
  17. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Badge.kt +81 -0
  18. package/compose/src/commonMain/kotlin/vn/momo/kits/components/BadgeDot.kt +32 -0
  19. package/compose/src/commonMain/kotlin/vn/momo/kits/components/BadgeRibbon.kt +338 -0
  20. package/compose/src/commonMain/kotlin/vn/momo/kits/components/BaselineView.kt +175 -0
  21. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Button.kt +354 -0
  22. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Carousel.kt +115 -0
  23. package/compose/src/commonMain/kotlin/vn/momo/kits/components/CheckBox.kt +89 -0
  24. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Chip.kt +130 -0
  25. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Collapse.kt +209 -0
  26. package/compose/src/commonMain/kotlin/vn/momo/kits/components/CupertinoOverscroll.kt +482 -0
  27. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Divider.kt +49 -0
  28. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Icon.kt +86 -0
  29. package/compose/src/commonMain/kotlin/vn/momo/kits/components/IconButton.kt +125 -0
  30. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Image.kt +224 -0
  31. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Information.kt +137 -0
  32. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Input.kt +473 -0
  33. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputDropDown.kt +113 -0
  34. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputMoney.kt +262 -0
  35. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputOTP.kt +237 -0
  36. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputPhoneNumber.kt +228 -0
  37. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputSearch.kt +280 -0
  38. package/compose/src/commonMain/kotlin/vn/momo/kits/components/InputTextArea.kt +183 -0
  39. package/compose/src/commonMain/kotlin/vn/momo/kits/components/LazyColumnWithBouncing.kt +342 -0
  40. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Loader.kt +108 -0
  41. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationDot.kt +56 -0
  42. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationNumber.kt +40 -0
  43. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationScroll.kt +82 -0
  44. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PaginationWhiteDot.kt +39 -0
  45. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PopupNotify.kt +362 -0
  46. package/compose/src/commonMain/kotlin/vn/momo/kits/components/PopupPromotion.kt +108 -0
  47. package/compose/src/commonMain/kotlin/vn/momo/kits/components/ProgressInfo.kt +304 -0
  48. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Radio.kt +59 -0
  49. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Rating.kt +87 -0
  50. package/compose/src/commonMain/kotlin/vn/momo/kits/components/ScaleSizeScope.kt +21 -0
  51. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Skeleton.kt +88 -0
  52. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Slider.kt +308 -0
  53. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Stepper.kt +218 -0
  54. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Steps.kt +456 -0
  55. package/compose/src/commonMain/kotlin/vn/momo/kits/components/SuggestAction.kt +122 -0
  56. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Swipe.kt +205 -0
  57. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Switch.kt +89 -0
  58. package/compose/src/commonMain/kotlin/vn/momo/kits/components/TabView.kt +542 -0
  59. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Tag.kt +86 -0
  60. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Text.kt +175 -0
  61. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Title.kt +201 -0
  62. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Tooltip.kt +498 -0
  63. package/compose/src/commonMain/kotlin/vn/momo/kits/components/TrustBanner.kt +176 -0
  64. package/compose/src/commonMain/kotlin/vn/momo/kits/components/Uploader.kt +182 -0
  65. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePicker.kt +193 -0
  66. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePickerTypes.kt +29 -0
  67. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/DateTimePickerUtils.kt +208 -0
  68. package/compose/src/commonMain/kotlin/vn/momo/kits/components/datetimepicker/WheelPicker.kt +180 -0
  69. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Colors.kt +325 -0
  70. package/compose/src/commonMain/kotlin/vn/momo/kits/const/HostFont.kt +12 -0
  71. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Radius.kt +12 -0
  72. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Spacing.kt +16 -0
  73. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Theme.kt +194 -0
  74. package/compose/src/commonMain/kotlin/vn/momo/kits/const/Typography.kt +291 -0
  75. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Card.kt +54 -0
  76. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/HeaderLayout.kt +71 -0
  77. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Item.kt +50 -0
  78. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/ModalLayout.kt +184 -0
  79. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/ScreenLayout.kt +315 -0
  80. package/compose/src/commonMain/kotlin/vn/momo/kits/layout/Section.kt +41 -0
  81. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/AutomationId.kt +54 -0
  82. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Clickable.kt +66 -0
  83. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Conditional.kt +11 -0
  84. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/DeprecatedModifier.kt +104 -0
  85. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Shadow.kt +48 -0
  86. package/compose/src/commonMain/kotlin/vn/momo/kits/modifier/Size.kt +51 -0
  87. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/BottomSheet.kt +397 -0
  88. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/HostBackGestureBridge.kt +58 -0
  89. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/ModalScreen.kt +153 -0
  90. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/Navigation.kt +118 -0
  91. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/NavigationContainer.kt +206 -0
  92. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/Navigator.kt +421 -0
  93. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/ScrollToTop.kt +8 -0
  94. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/StackScreen.kt +758 -0
  95. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/StackTransition.kt +152 -0
  96. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/SwipeBackHaptic.kt +35 -0
  97. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/BottomTab.kt +260 -0
  98. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/BottomTabBar.kt +406 -0
  99. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/CurvedContainer.kt +111 -0
  100. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/DuoBarCompression.kt +93 -0
  101. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/DuoTabBar.kt +208 -0
  102. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/LegacyBottomTabBar.kt +262 -0
  103. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/LegacyCurvedContainer.kt +86 -0
  104. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/bottomtab/TabBarMetrics.kt +133 -0
  105. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/FloatingButton.kt +167 -0
  106. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/Header.kt +446 -0
  107. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderBackground.kt +105 -0
  108. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderJourney.kt +106 -0
  109. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderLocation.kt +74 -0
  110. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderRight.kt +400 -0
  111. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderTitle.kt +39 -0
  112. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/HeaderUser.kt +363 -0
  113. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/SideBar.kt +197 -0
  114. package/compose/src/commonMain/kotlin/vn/momo/kits/navigation/component/SnackBar.kt +133 -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 +12 -0
  117. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ComposeLottieAnimation.kt +114 -0
  118. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/OsFontScale.kt +7 -0
  119. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/Platform.kt +76 -0
  120. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ReduceMotion.kt +7 -0
  121. package/compose/src/commonMain/kotlin/vn/momo/kits/platform/ReservedRegions.kt +21 -0
  122. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Icons.kt +1329 -0
  123. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Resources.kt +55 -0
  124. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Tracking.kt +15 -0
  125. package/compose/src/commonMain/kotlin/vn/momo/kits/utils/Utils.kt +117 -0
  126. package/compose/src/iosMain/kotlin/vn/momo/kits/const/HostFont.ios.kt +42 -0
  127. package/compose/src/iosMain/kotlin/vn/momo/kits/navigation/HostBackGesture.ios.kt +45 -0
  128. package/compose/src/iosMain/kotlin/vn/momo/kits/navigation/ScrollToTop.ios.kt +32 -0
  129. package/compose/src/iosMain/kotlin/vn/momo/kits/platform/BackGesture.ios.kt +38 -0
  130. package/compose/src/iosMain/kotlin/vn/momo/kits/platform/OsFontScale.ios.kt +65 -0
  131. package/compose/src/iosMain/kotlin/vn/momo/kits/platform/Platform.ios.kt +217 -0
  132. package/compose/src/iosMain/kotlin/vn/momo/kits/platform/ReduceMotion.ios.kt +48 -0
  133. package/compose/src/iosMain/kotlin/vn/momo/kits/platform/ReservedRegions.ios.kt +143 -0
  134. package/gradle/libs.versions.toml +67 -0
  135. package/gradle/wrapper/gradle-wrapper.jar +0 -0
  136. package/gradle/wrapper/gradle-wrapper.properties +8 -0
  137. package/gradle.properties +24 -0
  138. package/gradlew +252 -0
  139. package/gradlew.bat +94 -0
  140. package/ios/Navigation/BottomTab/LegacyBottomTabBar.swift +2 -1
  141. package/ios/native-kits.podspec +1 -1
  142. package/ios-demo/MoMoUIKitsDemo.podspec +1 -1
  143. package/ios-demo/README.md +120 -0
  144. package/package.json +1 -1
  145. package/settings.gradle.kts +64 -0
@@ -0,0 +1,209 @@
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
+ }
@@ -0,0 +1,482 @@
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)