Skip to content

Commit a631b91

Browse files
committed
DirectLoadingStrategy is just for testing
1 parent 63f55f8 commit a631b91

File tree

8 files changed

+70
-23
lines changed

8 files changed

+70
-23
lines changed

redwood-lazylayout-compose/api/redwood-lazylayout-compose.api

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public final class app/cash/redwood/lazylayout/compose/RefreshableLazyListKt {
5252
public final class app/cash/redwood/lazylayout/compose/ScrollOptimizedLoadingStrategy : app/cash/redwood/lazylayout/compose/LoadingStrategy {
5353
public static final field $stable I
5454
public fun <init> ()V
55-
public fun <init> (IIIIZ)V
56-
public synthetic fun <init> (IIIIZILkotlin/jvm/internal/DefaultConstructorMarker;)V
55+
public fun <init> (IIII)V
56+
public synthetic fun <init> (IIIIILkotlin/jvm/internal/DefaultConstructorMarker;)V
5757
public fun getFirstIndex ()I
5858
public final fun getLastIndex ()I
5959
public final fun getProgrammaticScrollIndex ()Lapp/cash/redwood/lazylayout/api/ScrollItemIndex;

redwood-lazylayout-compose/api/redwood-lazylayout-compose.klib.api

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ abstract interface app.cash.redwood.lazylayout.compose/LoadingStrategy { // app.
2525
}
2626

2727
final class app.cash.redwood.lazylayout.compose/ScrollOptimizedLoadingStrategy : app.cash.redwood.lazylayout.compose/LoadingStrategy { // app.cash.redwood.lazylayout.compose/ScrollOptimizedLoadingStrategy|null[0]
28-
constructor <init>(kotlin/Int = ..., kotlin/Int = ..., kotlin/Int = ..., kotlin/Int = ..., kotlin/Boolean = ...) // app.cash.redwood.lazylayout.compose/ScrollOptimizedLoadingStrategy.<init>|<init>(kotlin.Int;kotlin.Int;kotlin.Int;kotlin.Int;kotlin.Boolean){}[0]
28+
constructor <init>(kotlin/Int = ..., kotlin/Int = ..., kotlin/Int = ..., kotlin/Int = ...) // app.cash.redwood.lazylayout.compose/ScrollOptimizedLoadingStrategy.<init>|<init>(kotlin.Int;kotlin.Int;kotlin.Int;kotlin.Int){}[0]
2929

3030
final var firstIndex // app.cash.redwood.lazylayout.compose/ScrollOptimizedLoadingStrategy.firstIndex|{}firstIndex[0]
3131
final fun <get-firstIndex>(): kotlin/Int // app.cash.redwood.lazylayout.compose/ScrollOptimizedLoadingStrategy.firstIndex.<get-firstIndex>|<get-firstIndex>(){}[0]

redwood-lazylayout-compose/src/commonMain/kotlin/app/cash/redwood/lazylayout/compose/ScrollOptimizedLoadingStrategy.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public class ScrollOptimizedLoadingStrategy(
4242
private val scrollInProgressPreloadItemCount: Int = SCROLL_IN_PROGRESS_PRELOAD_ITEM_COUNT,
4343
private val primaryPreloadItemCount: Int = PRIMARY_PRELOAD_ITEM_COUNT,
4444
private val secondaryPreloadItemCount: Int = SECONDARY_PRELOAD_ITEM_COUNT,
45-
private val preloadItems: Boolean = true,
4645
) : LoadingStrategy {
4746
/**
4847
* Update this to trigger a programmatic scroll. This may be updated multiple times, including
@@ -92,11 +91,6 @@ public class ScrollOptimizedLoadingStrategy(
9291

9392
// Expand the range depending on scroll direction.
9493
when {
95-
// Ignore preloads.
96-
!preloadItems -> {
97-
// No-op
98-
}
99-
10094
isScrollingDown -> {
10195
begin -= scrollInProgressPreloadItemCount
10296
end += primaryPreloadItemCount

redwood-lazylayout-compose/src/commonTest/kotlin/app/cash/redwood/lazylayout/compose/LazyListTest.kt

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@ class LazyListTest {
116116
TestSchemaTester {
117117
var index5ComposeCount = 0
118118
setContent {
119-
val lazyListState = rememberLazyListState(
120-
ScrollOptimizedLoadingStrategy(preloadItems = false),
121-
)
119+
val lazyListState = rememberLazyListState(DirectLoadingStrategy())
122120
LazyColumn(
123121
state = lazyListState,
124122
placeholder = { Text("Placeholder") },
@@ -149,21 +147,12 @@ class LazyListTest {
149147
awaitSnapshot()
150148
assertThat(index5ComposeCount).isEqualTo(1)
151149

152-
// Even when it's scrolled off-screen.
150+
// It isn't recomposed when it's out of the loaded range.
153151
lazyList.onViewportChanged(6, 10)
154152
awaitSnapshot()
155153
assertThat(index5ComposeCount).isEqualTo(1)
156154

157-
// The item at index 5 remains composed because (6, 10) is contiguous with (4, 8).
158-
lazyList.onViewportChanged(4, 8)
159-
awaitSnapshot()
160-
assertThat(index5ComposeCount).isEqualTo(1)
161-
162-
// Scrolling to a non-contiguous range causes a recomposition.
163-
lazyList.onViewportChanged(9, 13)
164-
awaitSnapshot()
165-
assertThat(index5ComposeCount).isEqualTo(1)
166-
155+
// When it's scrolled back into range, it's recomposed.
167156
lazyList.onViewportChanged(4, 8)
168157
awaitSnapshot()
169158
assertThat(index5ComposeCount).isEqualTo(2)

redwood-lazylayout-testing/api/redwood-lazylayout-testing.api

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
public final class app/cash/redwood/lazylayout/compose/DirectLoadingStrategy : app/cash/redwood/lazylayout/compose/LoadingStrategy {
2+
public fun <init> ()V
3+
public fun getFirstIndex ()I
4+
public fun loadRange (I)Lkotlin/ranges/IntRange;
5+
public fun onUserScroll (II)V
6+
public fun scrollTo (I)V
7+
}
8+
19
public final class app/cash/redwood/lazylayout/testing/LazyListValue : app/cash/redwood/testing/WidgetValue {
210
public synthetic fun <init> (Lapp/cash/redwood/Modifier;ZLkotlin/jvm/functions/Function2;IIIILapp/cash/redwood/ui/Margin;ILapp/cash/redwood/lazylayout/api/ScrollItemIndex;Ljava/util/List;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
311
public synthetic fun <init> (Lapp/cash/redwood/Modifier;ZLkotlin/jvm/functions/Function2;IIIILapp/cash/redwood/ui/Margin;ILapp/cash/redwood/lazylayout/api/ScrollItemIndex;Ljava/util/List;Ljava/util/List;Lkotlin/jvm/internal/DefaultConstructorMarker;)V

redwood-lazylayout-testing/api/redwood-lazylayout-testing.klib.api

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
// - Show declarations: true
77

88
// Library unique name: <app.cash.redwood:redwood-lazylayout-testing>
9+
final class app.cash.redwood.lazylayout.compose/DirectLoadingStrategy : app.cash.redwood.lazylayout.compose/LoadingStrategy { // app.cash.redwood.lazylayout.compose/DirectLoadingStrategy|null[0]
10+
constructor <init>() // app.cash.redwood.lazylayout.compose/DirectLoadingStrategy.<init>|<init>(){}[0]
11+
12+
final val firstIndex // app.cash.redwood.lazylayout.compose/DirectLoadingStrategy.firstIndex|{}firstIndex[0]
13+
final fun <get-firstIndex>(): kotlin/Int // app.cash.redwood.lazylayout.compose/DirectLoadingStrategy.firstIndex.<get-firstIndex>|<get-firstIndex>(){}[0]
14+
15+
final fun loadRange(kotlin/Int): kotlin.ranges/IntRange // app.cash.redwood.lazylayout.compose/DirectLoadingStrategy.loadRange|loadRange(kotlin.Int){}[0]
16+
final fun onUserScroll(kotlin/Int, kotlin/Int) // app.cash.redwood.lazylayout.compose/DirectLoadingStrategy.onUserScroll|onUserScroll(kotlin.Int;kotlin.Int){}[0]
17+
final fun scrollTo(kotlin/Int) // app.cash.redwood.lazylayout.compose/DirectLoadingStrategy.scrollTo|scrollTo(kotlin.Int){}[0]
18+
}
19+
920
final class app.cash.redwood.lazylayout.testing/LazyListValue : app.cash.redwood.testing/WidgetValue { // app.cash.redwood.lazylayout.testing/LazyListValue|null[0]
1021
constructor <init>(app.cash.redwood/Modifier = ..., kotlin/Boolean, kotlin/Function2<kotlin/Int, kotlin/Int, kotlin/Unit>, kotlin/Int, kotlin/Int, app.cash.redwood.layout.api/Constraint, app.cash.redwood.layout.api/Constraint, app.cash.redwood.ui/Margin, app.cash.redwood.layout.api/CrossAxisAlignment, app.cash.redwood.lazylayout.api/ScrollItemIndex, kotlin.collections/List<app.cash.redwood.testing/WidgetValue> = ..., kotlin.collections/List<app.cash.redwood.testing/WidgetValue> = ...) // app.cash.redwood.lazylayout.testing/LazyListValue.<init>|<init>(app.cash.redwood.Modifier;kotlin.Boolean;kotlin.Function2<kotlin.Int,kotlin.Int,kotlin.Unit>;kotlin.Int;kotlin.Int;app.cash.redwood.layout.api.Constraint;app.cash.redwood.layout.api.Constraint;app.cash.redwood.ui.Margin;app.cash.redwood.layout.api.CrossAxisAlignment;app.cash.redwood.lazylayout.api.ScrollItemIndex;kotlin.collections.List<app.cash.redwood.testing.WidgetValue>;kotlin.collections.List<app.cash.redwood.testing.WidgetValue>){}[0]
1122

redwood-lazylayout-testing/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ kotlin {
1111
sourceSets {
1212
commonMain {
1313
dependencies {
14+
api projects.redwoodLazylayoutCompose
1415
api projects.redwoodLazylayoutWidget
1516
}
1617
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2024 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package app.cash.redwood.lazylayout.compose
17+
18+
import androidx.compose.runtime.getValue
19+
import androidx.compose.runtime.mutableStateOf
20+
import androidx.compose.runtime.setValue
21+
22+
/**
23+
* A loading strategy that's appropriate for tests because it's simple and predictable.
24+
*
25+
* This is not suitable for production use it must show a placeholder before an item is loaded.
26+
*/
27+
public class DirectLoadingStrategy : LoadingStrategy {
28+
private var loadRange: IntRange by mutableStateOf(0..0)
29+
30+
public override val firstIndex: Int
31+
get() = loadRange.first
32+
33+
override fun scrollTo(firstIndex: Int) {
34+
require(firstIndex >= 0)
35+
loadRange = firstIndex..firstIndex + (loadRange.last - loadRange.first)
36+
}
37+
38+
override fun onUserScroll(firstIndex: Int, lastIndex: Int) {
39+
loadRange = firstIndex..lastIndex
40+
}
41+
42+
override fun loadRange(itemCount: Int): IntRange =
43+
loadRange.first.coerceIn(0, itemCount - 1)..loadRange.last.coerceIn(0, itemCount - 1)
44+
}

0 commit comments

Comments
 (0)