Skip to content

Commit 59561a1

Browse files
committed
library: Optimize PullToRefresh component
1 parent d87664d commit 59561a1

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

composeApp/src/commonMain/kotlin/SecondPage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fun SecondPage(
3434
LaunchedEffect(pullToRefreshState.isRefreshing) {
3535
if (pullToRefreshState.isRefreshing) {
3636
isRefreshing = true
37-
delay(100)
37+
delay(300)
3838
pullToRefreshState.completeRefreshing {
3939
isRefreshing = false
4040
}

composeApp/src/commonMain/kotlin/component/OtherComponent.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ fun OtherComponent(padding: PaddingValues) {
4343
var text3 by remember { mutableStateOf("") }
4444
var progress by remember { mutableStateOf(0.5f) }
4545
val progressDisable by remember { mutableStateOf(0.5f) }
46-
val tabTexts =
47-
listOf("tab1", "tab2", "tab3", "tab4", "tab5", "tab6", "tab7", "tab8", "tab9", "tab10")
46+
val tabTexts = listOf("tab1", "tab2", "tab3", "tab4", "tab5", "tab6")
4847
var selectedTabIndex1 by remember { mutableStateOf(0) }
4948

5049
SmallTitle(text = "Button")
@@ -194,6 +193,4 @@ fun OtherComponent(padding: PaddingValues) {
194193
style = MiuixTheme.textStyles.paragraph
195194
)
196195
}
197-
198-
199196
}

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/basic/PullToRefresh.kt

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,12 @@ fun RefreshHeader(
177177

178178
val textAlpha = when (pullToRefreshState.refreshState) {
179179
RefreshState.Pulling -> {
180-
if (pullToRefreshState.pullProgress > 0.5f) (pullToRefreshState.pullProgress - 0.5f) * 2 else 0f
180+
if (pullToRefreshState.pullProgress > 0.5f) (pullToRefreshState.pullProgress - 0.5f) else 0f
181+
}
182+
183+
RefreshState.RefreshComplete -> {
184+
println(1f - refreshCompleteAnimProgress * 0.6f)
185+
1f - refreshCompleteAnimProgress * 1.2f
181186
}
182187

183188
else -> 1f
@@ -189,7 +194,7 @@ fun RefreshHeader(
189194
RefreshState.Pulling -> circleSize * pullProgress
190195
RefreshState.ThresholdReached -> circleSize + (dragOffset - thresholdOffset).toDp()
191196
RefreshState.Refreshing -> circleSize
192-
RefreshState.RefreshComplete -> circleSize
197+
RefreshState.RefreshComplete -> circleSize.coerceIn(0.dp, circleSize - circleSize * refreshCompleteAnimProgress)
193198
}.coerceAtMost(maxDrag.toDp())
194199
}
195200

@@ -198,14 +203,12 @@ fun RefreshHeader(
198203
horizontalAlignment = Alignment.CenterHorizontally
199204
) {
200205
RefreshContent(
201-
modifier = Modifier
202-
.height(headerHeight)
203-
.padding(top = 6.dp),
206+
modifier = Modifier.height(headerHeight),
204207
circleSize = circleSize
205208
) {
206-
val ringStrokeWidthPx = circleSize.toPx() / 10
209+
val ringStrokeWidthPx = circleSize.toPx() / 11
207210
val indicatorRadiusPx = size.minDimension / 2
208-
val center = Offset(size.width / 2, size.height / 2)
211+
val center = Offset(size.width / 2, size.height / 1.8f)
209212

210213
when (pullToRefreshState.refreshState) {
211214
RefreshState.Idle -> return@RefreshContent
@@ -251,15 +254,17 @@ fun RefreshHeader(
251254
}
252255

253256
AnimatedVisibility(
254-
visible = pullToRefreshState.refreshState != RefreshState.Idle
257+
visible = pullProgress >= 0.5f
258+
&& pullToRefreshState.refreshState != RefreshState.Idle
259+
&& pullToRefreshState.refreshState != RefreshState.RefreshComplete
255260
) {
256261
Text(
257262
text = refreshText,
258263
style = refreshTextStyle,
259264
color = color,
260265
modifier = Modifier
261-
.padding(top = 12.dp)
262266
.alpha(textAlpha)
267+
.padding(top = 6.dp)
263268
)
264269
}
265270
}
@@ -423,9 +428,11 @@ private fun DrawScope.drawRefreshCompleteState(
423428
refreshCompleteProgress: Float
424429
) {
425430
val animatedRadius = radius * (1f - refreshCompleteProgress)
431+
val alphaColor = color.copy(alpha = 1f - refreshCompleteProgress)
432+
426433
if (animatedRadius > 0) {
427434
drawCircle(
428-
color = color,
435+
color = alphaColor,
429436
radius = animatedRadius,
430437
center = center,
431438
style = Stroke(strokeWidth, cap = StrokeCap.Round)

0 commit comments

Comments
 (0)