From eda9b9ce8bfb087d73c8a41fb51cead524b2589d Mon Sep 17 00:00:00 2001 From: Alex Vanyo Date: Fri, 5 Aug 2022 14:34:39 -0700 Subject: [PATCH] Remove DelegateTwoPaneStrategy --- .../google/accompanist/adaptive/TwoPane.kt | 21 -- .../accompanist/adaptive/TwoPaneTest.kt | 200 ------------------ .../sample/adaptive/BasicTwoPaneSample.kt | 25 ++- 3 files changed, 12 insertions(+), 234 deletions(-) diff --git a/adaptive/src/main/java/com/google/accompanist/adaptive/TwoPane.kt b/adaptive/src/main/java/com/google/accompanist/adaptive/TwoPane.kt index b5aed06d0..639b90621 100644 --- a/adaptive/src/main/java/com/google/accompanist/adaptive/TwoPane.kt +++ b/adaptive/src/main/java/com/google/accompanist/adaptive/TwoPane.kt @@ -394,24 +394,3 @@ public fun FixedOffsetVerticalTwoPaneStrategy( ) ) } - -/** - * Returns a [DelegateTwoPaneStrategy] that will delegate [TwoPaneStrategy.calculateSplitResult] to - * the [firstStrategy] when [useFirstStrategy] returns `true`. Otherwise, it will delegate to - * the [secondStrategy]. - */ -public fun DelegateTwoPaneStrategy( - firstStrategy: TwoPaneStrategy, - secondStrategy: TwoPaneStrategy, - useFirstStrategy: ( - density: Density, - layoutDirection: LayoutDirection, - layoutCoordinates: LayoutCoordinates - ) -> Boolean, -): TwoPaneStrategy = TwoPaneStrategy { density, layoutDirection, layoutCoordinates -> - if (useFirstStrategy(density, layoutDirection, layoutCoordinates)) { - firstStrategy - } else { - secondStrategy - }.calculateSplitResult(density, layoutDirection, layoutCoordinates) -} diff --git a/adaptive/src/sharedTest/kotlin/com/google/accompanist/adaptive/TwoPaneTest.kt b/adaptive/src/sharedTest/kotlin/com/google/accompanist/adaptive/TwoPaneTest.kt index c09789ca8..3ad20b5dd 100644 --- a/adaptive/src/sharedTest/kotlin/com/google/accompanist/adaptive/TwoPaneTest.kt +++ b/adaptive/src/sharedTest/kotlin/com/google/accompanist/adaptive/TwoPaneTest.kt @@ -1459,206 +1459,6 @@ class TwoPaneTest { 0.001f ) } - - @Test - fun delegate_renders_correctly_when_changing_size_ltr() { - var width by mutableStateOf(900.dp) - var height by mutableStateOf(1200.dp) - - lateinit var density: Density - lateinit var twoPaneCoordinates: LayoutCoordinates - lateinit var firstCoordinates: LayoutCoordinates - lateinit var secondCoordinates: LayoutCoordinates - - composeTestRule.setContent { - density = LocalDensity.current - CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) { - TwoPane( - first = { - Spacer( - Modifier - .background(Color.Red) - .fillMaxSize() - .onPlaced { firstCoordinates = it } - ) - }, - second = { - Spacer( - Modifier - .background(Color.Blue) - .fillMaxSize() - .onPlaced { secondCoordinates = it } - ) - }, - // Strategy: If we are taller than we are wide, use a vertical strategy with the - // second slot in the bottom 1/4. Otherwise, use a horizontal strategy with the - // second slot at the end 1/4. - strategy = DelegateTwoPaneStrategy( - firstStrategy = FractionVerticalTwoPaneStrategy(0.75f), - secondStrategy = FractionHorizontalTwoPaneStrategy(0.75f), - useFirstStrategy = { _, _, layoutCoordinates -> - layoutCoordinates.size.height >= layoutCoordinates.size.width - } - ), - modifier = Modifier - .requiredSize(width, height) - .onPlaced { twoPaneCoordinates = it } - ) - } - } - - // Initial state: we are taller than we are wide, so second should in the bottom 1/4 of the - // screen - compareRectWithTolerance( - with(density) { - DpRect( - DpOffset(0.dp, 0.dp), - DpSize(900.dp, 900.dp) - ).toRect().round().toRect() - }, - twoPaneCoordinates.localBoundingBoxOf(firstCoordinates), - 0.001f - ) - - compareRectWithTolerance( - with(density) { - DpRect( - DpOffset(0.dp, 900.dp), - DpSize(900.dp, 300.dp) - ).toRect().round().toRect() - }, - twoPaneCoordinates.localBoundingBoxOf(secondCoordinates), - 0.001f - ) - - // Swap width and height - width = 1200.dp - height = 900.dp - - composeTestRule.waitForIdle() - - compareRectWithTolerance( - with(density) { - DpRect( - DpOffset(0.dp, 0.dp), - DpSize(900.dp, 900.dp) - ).toRect().round().toRect() - }, - twoPaneCoordinates.localBoundingBoxOf(firstCoordinates), - 0.001f - ) - - compareRectWithTolerance( - with(density) { - DpRect( - DpOffset(900.dp, 0.dp), - DpSize(300.dp, 900.dp) - ).toRect().round().toRect() - }, - twoPaneCoordinates.localBoundingBoxOf(secondCoordinates), - 0.001f - ) - } - - @Test - fun delegate_renders_correctly_when_changing_size_rtl() { - var width by mutableStateOf(900.dp) - var height by mutableStateOf(1200.dp) - - lateinit var density: Density - lateinit var twoPaneCoordinates: LayoutCoordinates - lateinit var firstCoordinates: LayoutCoordinates - lateinit var secondCoordinates: LayoutCoordinates - - composeTestRule.setContent { - density = LocalDensity.current - CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) { - TwoPane( - first = { - Spacer( - Modifier - .background(Color.Red) - .fillMaxSize() - .onPlaced { firstCoordinates = it } - ) - }, - second = { - Spacer( - Modifier - .background(Color.Blue) - .fillMaxSize() - .onPlaced { secondCoordinates = it } - ) - }, - // Strategy: If we are taller than we are wide, use a vertical strategy with the - // second slot in the bottom 1/4. Otherwise, use a horizontal strategy with the - // second slot at the end 1/4. - strategy = DelegateTwoPaneStrategy( - firstStrategy = FractionVerticalTwoPaneStrategy(0.75f), - secondStrategy = FractionHorizontalTwoPaneStrategy(0.75f), - useFirstStrategy = { _, _, layoutCoordinates -> - layoutCoordinates.size.height >= layoutCoordinates.size.width - } - ), - modifier = Modifier - .requiredSize(width, height) - .onPlaced { twoPaneCoordinates = it } - ) - } - } - - // Initial state: we are taller than we are wide, so second should in the bottom 1/4 of the - // screen - compareRectWithTolerance( - with(density) { - DpRect( - DpOffset(0.dp, 0.dp), - DpSize(900.dp, 900.dp) - ).toRect().round().toRect() - }, - twoPaneCoordinates.localBoundingBoxOf(firstCoordinates), - 0.001f - ) - - compareRectWithTolerance( - with(density) { - DpRect( - DpOffset(0.dp, 900.dp), - DpSize(900.dp, 300.dp) - ).toRect().round().toRect() - }, - twoPaneCoordinates.localBoundingBoxOf(secondCoordinates), - 0.001f - ) - - // Swap width and height - width = 1200.dp - height = 900.dp - - composeTestRule.waitForIdle() - - compareRectWithTolerance( - with(density) { - DpRect( - DpOffset(300.dp, 0.dp), - DpSize(900.dp, 900.dp) - ).toRect().round().toRect() - }, - twoPaneCoordinates.localBoundingBoxOf(firstCoordinates), - 0.001f - ) - - compareRectWithTolerance( - with(density) { - DpRect( - DpOffset(0.dp, 0.dp), - DpSize(300.dp, 900.dp) - ).toRect().round().toRect() - }, - twoPaneCoordinates.localBoundingBoxOf(secondCoordinates), - 0.001f - ) - } } private fun compareRectWithTolerance( diff --git a/sample/src/main/java/com/google/accompanist/sample/adaptive/BasicTwoPaneSample.kt b/sample/src/main/java/com/google/accompanist/sample/adaptive/BasicTwoPaneSample.kt index a9dff03a8..baefb4261 100644 --- a/sample/src/main/java/com/google/accompanist/sample/adaptive/BasicTwoPaneSample.kt +++ b/sample/src/main/java/com/google/accompanist/sample/adaptive/BasicTwoPaneSample.kt @@ -27,7 +27,6 @@ import androidx.compose.material.Text import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp -import com.google.accompanist.adaptive.DelegateTwoPaneStrategy import com.google.accompanist.adaptive.FractionHorizontalTwoPaneStrategy import com.google.accompanist.adaptive.FractionVerticalTwoPaneStrategy import com.google.accompanist.adaptive.TwoPane @@ -68,18 +67,18 @@ class BasicTwoPaneSample : ComponentActivity() { } }, strategy = TwoPaneStrategy( - fallbackStrategy = DelegateTwoPaneStrategy( - firstStrategy = FractionVerticalTwoPaneStrategy( - splitFraction = 0.75f, - ), - secondStrategy = FractionHorizontalTwoPaneStrategy( - splitFraction = 0.75f, - ), - useFirstStrategy = { _, _, layoutCoordinates -> - // Split vertically if the height is larger than the width - layoutCoordinates.size.height >= layoutCoordinates.size.width - } - ), + fallbackStrategy = { density, layoutDirection, layoutCoordinates -> + // Split vertically if the height is larger than the width + if (layoutCoordinates.size.height >= layoutCoordinates.size.width) { + FractionVerticalTwoPaneStrategy( + splitFraction = 0.75f, + ) + } else { + FractionHorizontalTwoPaneStrategy( + splitFraction = 0.75f, + ) + }.calculateSplitResult(density, layoutDirection, layoutCoordinates) + }, windowGeometry = windowGeometry ), modifier = Modifier.padding(8.dp)