Skip to content

Commit

Permalink
Showkase navigation breaks when preview contains a BackHandler (#267)
Browse files Browse the repository at this point in the history
* Fix #247

* Address comments

* Fix detekt issue

* Generate new screenshots

* Add updated golden copies of screenshots
  • Loading branch information
vinaygaba authored Oct 30, 2022
1 parent b893d55 commit 3ecf55d
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,7 @@ jobs:
# with:
# name: Api${{ matrix.api-level }}
# path: sample/screenshots/debug/Api${{ matrix.api-level }}
# retention-days: 1
# retention-days: 1



Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 28 additions & 7 deletions sample/src/main/java/com/airbnb/android/showkasesample/Rows.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.airbnb.android.showkasesample

import androidx.activity.compose.BackHandler
import androidx.compose.material.Text
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -37,7 +38,8 @@ fun SimpleRow(title: String, subtitle: String) {
ShowkaseTheme {
Card(
shape = RoundedCornerShape(padding1x),
modifier = Modifier.fillMaxWidth()
modifier = Modifier
.fillMaxWidth()
.padding(padding2x)
) {
ListItem(text = {
Expand All @@ -58,7 +60,8 @@ fun SimpleRow(title: String, subtitle: String) {
)
}, icon = {
Column(
modifier = Modifier.width(60.dp)
modifier = Modifier
.width(60.dp)
.height(60.dp)
.background(color = Color.Gray)
) {}
Expand All @@ -71,7 +74,8 @@ fun SimpleRow(title: String, subtitle: String) {
fun TitleSubtitleThumbnailRow() {
ShowkaseTheme {
Card(
modifier = Modifier.height(120.dp)
modifier = Modifier
.height(120.dp)
.fillMaxWidth()
.padding(padding2x),
shape = RoundedCornerShape(padding1x)
Expand All @@ -95,7 +99,8 @@ fun TitleSubtitleThumbnailRow() {
start.linkTo(image.end, margin = padding4x)
})

Column(modifier = Modifier.height(72.dp)
Column(modifier = Modifier
.height(72.dp)
.width(72.dp)
.background(color = Color.Gray)
.constrainAs(image) {
Expand All @@ -112,7 +117,8 @@ fun TitleSubtitleThumbnailRow() {
fun BottomLabelRow(title: String, subtitle: String, label: String) {
ShowkaseTheme {
Column(
modifier = Modifier.fillMaxWidth()
modifier = Modifier
.fillMaxWidth()
.background(color = MaterialTheme.colors.surface)
.padding(padding4x)
) {
Expand Down Expand Up @@ -158,11 +164,15 @@ fun BottomLabelRow(title: String, subtitle: String, label: String) {
fun ProductCardRow(imageUrl: String, title: String, subtitle: String) {
ShowkaseTheme {
Column(
modifier = Modifier.fillMaxWidth().background(color = MaterialTheme.colors.surface)
modifier = Modifier
.fillMaxWidth()
.background(color = MaterialTheme.colors.surface)
) {
NetworkImage(
imageUrl = imageUrl,
modifier = Modifier.fillMaxWidth().height(200.dp)
modifier = Modifier
.fillMaxWidth()
.height(200.dp)
)
Text(
text = title,
Expand Down Expand Up @@ -241,3 +251,14 @@ fun TitleSubtitleThumbnailRowPreview() {
TitleSubtitleThumbnailRow()
}
}

@ShowkaseComposable@ShowkaseComposable("Title Subtitle with Thumbnail and Back Handler", "Rows")
@Preview
@Composable
fun TitleSubtitleThumbnailRowWithBackHandlingPreview() {
Column {
TitleSubtitleThumbnailRow()
}
BackHandler(onBack = { })
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.airbnb.android.showkase.ui

import android.annotation.SuppressLint
import android.content.res.Configuration
import androidx.activity.OnBackPressedDispatcher
import androidx.activity.OnBackPressedDispatcherOwner
import androidx.activity.compose.LocalOnBackPressedDispatcherOwner
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.expandHorizontally
import androidx.compose.animation.expandIn
Expand Down Expand Up @@ -35,6 +38,8 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.TextStyle
Expand Down Expand Up @@ -69,7 +74,21 @@ internal fun ShowkaseBrowserApp(
val lightModeConfiguration = Configuration(LocalConfiguration.current).apply {
uiMode = Configuration.UI_MODE_NIGHT_NO
}
CompositionLocalProvider(LocalConfiguration provides lightModeConfiguration) {
val lifecycleOwner = LocalLifecycleOwner.current
val backPressedDispatcherOwner = remember {
object : OnBackPressedDispatcherOwner {
override fun getLifecycle() = lifecycleOwner.lifecycle

override fun getOnBackPressedDispatcher() = OnBackPressedDispatcher()
}
}
CompositionLocalProvider(
LocalConfiguration provides lightModeConfiguration,
LocalInspectionMode provides true,
// This is added to make sure that the navigation of the ShowkaseBrowser does not break
// when one of the previews has a back press handler in the implementation of the component.
LocalOnBackPressedDispatcherOwner provides backPressedDispatcherOwner
) {
val navController = rememberNavController()
Scaffold(
drawerContent = null,
Expand Down

0 comments on commit 3ecf55d

Please sign in to comment.