Skip to content

Commit

Permalink
DirectLoadingStrategy is just for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
squarejesse committed Jul 31, 2024
1 parent 63f55f8 commit a631b91
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 23 deletions.
4 changes: 2 additions & 2 deletions redwood-lazylayout-compose/api/redwood-lazylayout-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public final class app/cash/redwood/lazylayout/compose/RefreshableLazyListKt {
public final class app/cash/redwood/lazylayout/compose/ScrollOptimizedLoadingStrategy : app/cash/redwood/lazylayout/compose/LoadingStrategy {
public static final field $stable I
public fun <init> ()V
public fun <init> (IIIIZ)V
public synthetic fun <init> (IIIIZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (IIII)V
public synthetic fun <init> (IIIIILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun getFirstIndex ()I
public final fun getLastIndex ()I
public final fun getProgrammaticScrollIndex ()Lapp/cash/redwood/lazylayout/api/ScrollItemIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract interface app.cash.redwood.lazylayout.compose/LoadingStrategy { // app.
}

final class app.cash.redwood.lazylayout.compose/ScrollOptimizedLoadingStrategy : app.cash.redwood.lazylayout.compose/LoadingStrategy { // app.cash.redwood.lazylayout.compose/ScrollOptimizedLoadingStrategy|null[0]
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]
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]

final var firstIndex // app.cash.redwood.lazylayout.compose/ScrollOptimizedLoadingStrategy.firstIndex|{}firstIndex[0]
final fun <get-firstIndex>(): kotlin/Int // app.cash.redwood.lazylayout.compose/ScrollOptimizedLoadingStrategy.firstIndex.<get-firstIndex>|<get-firstIndex>(){}[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public class ScrollOptimizedLoadingStrategy(
private val scrollInProgressPreloadItemCount: Int = SCROLL_IN_PROGRESS_PRELOAD_ITEM_COUNT,
private val primaryPreloadItemCount: Int = PRIMARY_PRELOAD_ITEM_COUNT,
private val secondaryPreloadItemCount: Int = SECONDARY_PRELOAD_ITEM_COUNT,
private val preloadItems: Boolean = true,
) : LoadingStrategy {
/**
* Update this to trigger a programmatic scroll. This may be updated multiple times, including
Expand Down Expand Up @@ -92,11 +91,6 @@ public class ScrollOptimizedLoadingStrategy(

// Expand the range depending on scroll direction.
when {
// Ignore preloads.
!preloadItems -> {
// No-op
}

isScrollingDown -> {
begin -= scrollInProgressPreloadItemCount
end += primaryPreloadItemCount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ class LazyListTest {
TestSchemaTester {
var index5ComposeCount = 0
setContent {
val lazyListState = rememberLazyListState(
ScrollOptimizedLoadingStrategy(preloadItems = false),
)
val lazyListState = rememberLazyListState(DirectLoadingStrategy())
LazyColumn(
state = lazyListState,
placeholder = { Text("Placeholder") },
Expand Down Expand Up @@ -149,21 +147,12 @@ class LazyListTest {
awaitSnapshot()
assertThat(index5ComposeCount).isEqualTo(1)

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

// The item at index 5 remains composed because (6, 10) is contiguous with (4, 8).
lazyList.onViewportChanged(4, 8)
awaitSnapshot()
assertThat(index5ComposeCount).isEqualTo(1)

// Scrolling to a non-contiguous range causes a recomposition.
lazyList.onViewportChanged(9, 13)
awaitSnapshot()
assertThat(index5ComposeCount).isEqualTo(1)

// When it's scrolled back into range, it's recomposed.
lazyList.onViewportChanged(4, 8)
awaitSnapshot()
assertThat(index5ComposeCount).isEqualTo(2)
Expand Down
8 changes: 8 additions & 0 deletions redwood-lazylayout-testing/api/redwood-lazylayout-testing.api
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
public final class app/cash/redwood/lazylayout/compose/DirectLoadingStrategy : app/cash/redwood/lazylayout/compose/LoadingStrategy {
public fun <init> ()V
public fun getFirstIndex ()I
public fun loadRange (I)Lkotlin/ranges/IntRange;
public fun onUserScroll (II)V
public fun scrollTo (I)V
}

public final class app/cash/redwood/lazylayout/testing/LazyListValue : app/cash/redwood/testing/WidgetValue {
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
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
Expand Down
11 changes: 11 additions & 0 deletions redwood-lazylayout-testing/api/redwood-lazylayout-testing.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
// - Show declarations: true

// Library unique name: <app.cash.redwood:redwood-lazylayout-testing>
final class app.cash.redwood.lazylayout.compose/DirectLoadingStrategy : app.cash.redwood.lazylayout.compose/LoadingStrategy { // app.cash.redwood.lazylayout.compose/DirectLoadingStrategy|null[0]
constructor <init>() // app.cash.redwood.lazylayout.compose/DirectLoadingStrategy.<init>|<init>(){}[0]

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

final fun loadRange(kotlin/Int): kotlin.ranges/IntRange // app.cash.redwood.lazylayout.compose/DirectLoadingStrategy.loadRange|loadRange(kotlin.Int){}[0]
final fun onUserScroll(kotlin/Int, kotlin/Int) // app.cash.redwood.lazylayout.compose/DirectLoadingStrategy.onUserScroll|onUserScroll(kotlin.Int;kotlin.Int){}[0]
final fun scrollTo(kotlin/Int) // app.cash.redwood.lazylayout.compose/DirectLoadingStrategy.scrollTo|scrollTo(kotlin.Int){}[0]
}

final class app.cash.redwood.lazylayout.testing/LazyListValue : app.cash.redwood.testing/WidgetValue { // app.cash.redwood.lazylayout.testing/LazyListValue|null[0]
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]

Expand Down
1 change: 1 addition & 0 deletions redwood-lazylayout-testing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ kotlin {
sourceSets {
commonMain {
dependencies {
api projects.redwoodLazylayoutCompose
api projects.redwoodLazylayoutWidget
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2024 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.redwood.lazylayout.compose

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue

/**
* A loading strategy that's appropriate for tests because it's simple and predictable.
*
* This is not suitable for production use it must show a placeholder before an item is loaded.
*/
public class DirectLoadingStrategy : LoadingStrategy {
private var loadRange: IntRange by mutableStateOf(0..0)

public override val firstIndex: Int
get() = loadRange.first

override fun scrollTo(firstIndex: Int) {
require(firstIndex >= 0)
loadRange = firstIndex..firstIndex + (loadRange.last - loadRange.first)
}

override fun onUserScroll(firstIndex: Int, lastIndex: Int) {
loadRange = firstIndex..lastIndex
}

override fun loadRange(itemCount: Int): IntRange =
loadRange.first.coerceIn(0, itemCount - 1)..loadRange.last.coerceIn(0, itemCount - 1)
}

0 comments on commit a631b91

Please sign in to comment.