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

Improve ShowkaseBrowser codebase and update navigation stack #390

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
'composeCompiler' : '1.5.13',
'composeActivity' : '1.9.0',
'composeConstraintLayout': '1.0.1',
'composeNavigation' : '2.5.1',
'composeNavigation' : '2.7.7',
'detekt' : '1.7.4',
'espresso' : '3.2.0',
'gradle' : '8.1.1',
Expand All @@ -22,10 +22,10 @@ buildscript {
'kotlinXMetadata' : '0.6.0',
'ksp' : "$KSP_VERSION",
'ktx' : '1.1.0',
'lifecycle' : '2.2.0',
'lifecycle' : '2.6.2',
'paparazzi' : '1.2.0',
'picasso' : '2.8',
'appcompat' : '1.4.0',
'appcompat' : '1.6.1',
'testRunner' : '1.4.0',
'testParameterInjector' : '1.8',
'googleTruth' : '1.1.3',
Expand Down Expand Up @@ -66,7 +66,8 @@ buildscript {
'support' : [
'appCompat' : "androidx.appcompat:appcompat:${versions.appcompat}",
'ktx' : "androidx.core:core-ktx:${versions.corektx}",
'lifecycleExtensions': "androidx.lifecycle:lifecycle-extensions:${versions.lifecycle}"
'lifecycleComposeViewModel' : "androidx.lifecycle:lifecycle-viewmodel-compose:${versions.lifecycle}",
'lifecycleComposeRuntime' : "androidx.lifecycle:lifecycle-runtime-compose:${versions.lifecycle}",
],
'test' : [
'androidXTestCore' : "androidx.test:core:${versions.androidXTestCore}",
Expand Down
3 changes: 2 additions & 1 deletion sample-submodule-2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ dependencies {
// Support Libraries
implementation deps.support.appCompat
implementation deps.support.ktx
implementation deps.support.lifecycleExtensions
implementation deps.support.lifecycleComposeViewModel
implementation deps.support.lifecycleComposeRuntime
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space was lost here :)?


// Showkase
implementation project(':showkase')
Expand Down
3 changes: 2 additions & 1 deletion sample-submodule/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ dependencies {
// Support Libraries
implementation deps.support.appCompat
implementation deps.support.ktx
implementation deps.support.lifecycleExtensions
implementation deps.support.lifecycleComposeViewModel
implementation deps.support.lifecycleComposeRuntime
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here


// Showkase
implementation project(':showkase')
Expand Down
3 changes: 2 additions & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ dependencies {
// Support Libraries
implementation deps.support.appCompat
implementation deps.support.ktx
implementation deps.support.lifecycleExtensions
implementation deps.support.lifecycleComposeViewModel
implementation deps.support.lifecycleComposeRuntime
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here


// Compose
implementation deps.compose.activityCompose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,16 @@ internal data class ShowkaseBrowserScreenMetadata(
val searchQuery: String? = null,
)

internal fun MutableState<ShowkaseBrowserScreenMetadata>.clear() {
update {
copy(
isSearchActive = false,
searchQuery = null,
currentComponentKey = null,
currentComponentName = null,
currentComponentStyleName = null,
currentGroup = null
)
}
}

internal fun MutableState<ShowkaseBrowserScreenMetadata>.clearActiveSearch() {
update {
copy(
isSearchActive = false,
searchQuery = null
)
}
}
internal fun ShowkaseBrowserScreenMetadata.clear() = copy(
isSearchActive = false,
searchQuery = null,
currentComponentKey = null,
currentComponentName = null,
currentComponentStyleName = null,
currentGroup = null
)

internal fun <T> MutableState<T>.update(block: T.() -> T) {
value = this.component1().run(block)
}
internal fun ShowkaseBrowserScreenMetadata.clearActiveSearch() = copy(
isSearchActive = false,
searchQuery = null
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.airbnb.android.showkase.ui

import androidx.activity.compose.LocalOnBackPressedDispatcherOwner
import androidx.compose.material.Text
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
Expand All @@ -12,6 +13,7 @@ import androidx.compose.material.MaterialTheme
import androidx.compose.material.darkColors
import androidx.compose.material.lightColors
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
Expand All @@ -32,10 +34,10 @@ fun SimpleTextCard(
onClick = onClick
) {
Text(
text = text,
text = text,
modifier = Modifier.padding(padding4x),
style = TextStyle(
fontSize = 20.sp,
fontSize = 20.sp,
fontFamily = FontFamily.Serif,
fontWeight = FontWeight.Bold
)
Expand All @@ -47,8 +49,10 @@ fun SimpleTextCard(
internal fun ComponentCardTitle(componentName: String) {
Text(
text = componentName,
modifier = Modifier.padding(start = padding4x, end = padding4x, top = padding8x,
bottom = padding1x),
modifier = Modifier.padding(
start = padding4x, end = padding4x, top = padding8x,
bottom = padding1x
),
style = TextStyle(
fontSize = 16.sp,
fontFamily = FontFamily.Serif,
Expand All @@ -63,30 +67,35 @@ internal fun ComponentCard(
onClick: (() -> Unit)? = null,
darkMode: Boolean = false,
) {
val composableModifier = Modifier.generateComposableModifier(metadata)
val composableContainerModifier = Modifier.generateContainerModifier(onClick)
MaterialTheme(
colors = if (darkMode) darkColors() else lightColors()
) {
Card(
shape = MaterialTheme.shapes.large
// 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.
val backPressedDispatcherOwner = rememberOnBackPressedDispatcherOwner()
Copy link
Collaborator Author

@vinaygaba vinaygaba Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this at the top level (which was previously the case) was causing the navigation to not work at all. Instead, I moved this down to specifically happen for the Composables that are responsible for rendering the component previews as that's where we want to override/intercept any back handling that the previews might be doing themselves. Doing so fixed the issue but I'm not 100% on why doing this at the top level didn't work okay.

CompositionLocalProvider(LocalOnBackPressedDispatcherOwner provides backPressedDispatcherOwner) {
val composableModifier = Modifier.generateComposableModifier(metadata)
val composableContainerModifier = Modifier.generateContainerModifier(onClick)
MaterialTheme(
colors = if (darkMode) darkColors() else lightColors()
) {
Box {
Column(modifier = composableModifier) {
metadata.component()
Card(
shape = MaterialTheme.shapes.large
) {
Box {
Column(modifier = composableModifier) {
metadata.component()
}
// Need to add this as part of the stack so that we can intercept the touch of the
// component when we are on the "Group components" screen. If
// composableContainerModifier does not have any clickable modifiers, this column has no
// impact and the touches go through to the component(this happens in the "Component
// Detail" screen.
Column(
modifier = Modifier
.matchParentSize()
.then(composableContainerModifier)
) {}
}
// Need to add this as part of the stack so that we can intercept the touch of the
// component when we are on the "Group components" screen. If
// composableContainerModifier does not have any clickable modifiers, this column has no
// impact and the touches go through to the component(this happens in the "Component
// Detail" screen.
Column(
modifier = Modifier
.matchParentSize()
.then(composableContainerModifier)
) {}
}

}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import com.airbnb.android.showkase.exceptions.ShowkaseException
import com.airbnb.android.showkase.models.ShowkaseBrowserScreenMetadata
import com.airbnb.android.showkase.models.ShowkaseProvider
Expand All @@ -30,16 +32,21 @@ class ShowkaseBrowserActivity : AppCompatActivity() {
groupedTypographyList
) = getShowkaseProviderElements(classKey)

val showkaseBrowserScreenMetadata =
remember { mutableStateOf(ShowkaseBrowserScreenMetadata()) }
var showkaseBrowserScreenMetadata by remember {
mutableStateOf(ShowkaseBrowserScreenMetadata())
}
when {
groupedComponentsList.isNotEmpty() || groupedColorsList.isNotEmpty() ||
groupedTypographyList.isNotEmpty() -> {
ShowkaseBrowserApp(
groupedComponentsList.groupBy { it.group },
groupedColorsList.groupBy { it.colorGroup },
groupedTypographyList.groupBy { it.typographyGroup },
showkaseBrowserScreenMetadata)
showkaseBrowserScreenMetadata = showkaseBrowserScreenMetadata,
onUpdateShowkaseBrowserScreenMetadata = {
showkaseBrowserScreenMetadata = it
}
)
}
else -> {
ShowkaseErrorScreen(
Expand Down
Loading