Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Showkase navigation breaks when preview contains a BackHandler #267

Merged
merged 6 commits into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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