Skip to content

Commit 7492b4b

Browse files
committed
[#15] Upward scroll fling value not consumed when refresh callback Is called
Consuming the fling value when the refresh starts since it holds no significant meaning in this context. #15
1 parent b68ebd4 commit 7492b4b

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

demo/src/main/java/dev/materii/pullrefresh/demo/MainActivity.kt

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class MainActivity : ComponentActivity() {
136136
flipped = flipped,
137137
pullRefreshState = pullRefreshState,
138138
modifier = Modifier.fillMaxSize(),
139+
isRefreshing = isRefreshing
139140
)
140141
}
141142
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
package dev.materii.pullrefresh.demo.sample
22

3-
import androidx.compose.foundation.layout.Box
3+
import androidx.compose.foundation.background
44
import androidx.compose.foundation.layout.fillMaxSize
5-
import androidx.compose.foundation.rememberScrollState
6-
import androidx.compose.foundation.verticalScroll
5+
import androidx.compose.foundation.layout.fillMaxWidth
6+
import androidx.compose.foundation.layout.padding
7+
import androidx.compose.foundation.lazy.LazyColumn
8+
import androidx.compose.foundation.shape.RoundedCornerShape
79
import androidx.compose.material3.MaterialTheme
810
import androidx.compose.material3.Text
911
import androidx.compose.runtime.Composable
10-
import androidx.compose.ui.Alignment
1112
import androidx.compose.ui.Modifier
12-
import dev.materii.pullrefresh.DragRefreshLayout
13+
import androidx.compose.ui.unit.dp
1314
import dev.materii.pullrefresh.DragRefreshIndicator
15+
import dev.materii.pullrefresh.DragRefreshLayout
1416
import dev.materii.pullrefresh.PullRefreshState
1517

1618
@Composable
1719
fun DragRefreshSample(
1820
flipped: Boolean,
1921
pullRefreshState: PullRefreshState,
20-
modifier: Modifier = Modifier
22+
modifier: Modifier = Modifier,
23+
isRefreshing: Boolean
2124
) {
2225
DragRefreshLayout(
2326
modifier = modifier,
@@ -31,13 +34,22 @@ fun DragRefreshSample(
3134
)
3235
}
3336
) {
34-
Box(
35-
modifier = Modifier
36-
.fillMaxSize()
37-
.verticalScroll(rememberScrollState()),
38-
contentAlignment = Alignment.Center
37+
LazyColumn(
38+
Modifier.fillMaxSize(),
39+
userScrollEnabled = !isRefreshing
3940
) {
40-
Text(text = "Pull ${if (flipped) "up" else "down"} to refresh")
41+
items(100) {
42+
Text(
43+
modifier = Modifier
44+
.fillMaxWidth()
45+
.padding(5.dp)
46+
.background(
47+
color = MaterialTheme.colorScheme.outline,
48+
shape = RoundedCornerShape(10.dp)
49+
)
50+
.padding(10.dp), text = "No. $it"
51+
)
52+
}
4153
}
4254
}
4355
}

pullrefresh/src/commonMain/kotlin/dev/materii/pullrefresh/PullRefreshState.kt

+10-4
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,23 @@ class PullRefreshState internal constructor(
125125
internal fun onRelease(velocity: Float): Float {
126126
if (refreshing) return 0f // Already refreshing, do nothing
127127

128-
if (adjustedDistancePulled > threshold) {
128+
val isRefreshTiming = adjustedDistancePulled > threshold
129+
if (isRefreshTiming) {
129130
onRefreshState.value()
130131
}
131132
animateIndicatorTo(0f)
132133
val consumed = when {
133134
// We are flinging without having dragged the pull refresh (for example a fling inside
134135
// a list) - don't consume
135136
distancePulled == 0f -> 0f
136-
// If the velocity is negative, the fling is upwards, and we don't want to prevent the
137-
// the list from scrolling
138-
velocity < 0f -> 0f
137+
velocity < 0f -> if (isRefreshTiming) {
138+
// We need to prevent the fling upward when the refresh starts.
139+
velocity
140+
}else {
141+
// If the velocity is negative, the fling is upwards, and we don't want to prevent
142+
// the list from scrolling
143+
0f
144+
}
139145
// We are showing the indicator, and the fling is downwards - consume everything
140146
else -> velocity
141147
}

0 commit comments

Comments
 (0)