Skip to content

Commit

Permalink
Remove DelegateTwoPaneStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvanyo committed Aug 5, 2022
1 parent a7ce62a commit eda9b9c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 234 deletions.
21 changes: 0 additions & 21 deletions adaptive/src/main/java/com/google/accompanist/adaptive/TwoPane.kt
Expand Up @@ -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)
}
Expand Up @@ -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(
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit eda9b9c

Please sign in to comment.