Skip to content

Commit

Permalink
refactor(kmp): migrate adaptive layout to Compose Multiplatform.
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardPaligot committed Dec 11, 2024
1 parent 7ef1600 commit 6c22514
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 45 deletions.
4 changes: 2 additions & 2 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ dependencies {
implementation(libs.androidx.appcompat)
implementation(libs.androidx.browser)

implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.material3.windowsizeclass)
implementation(libs.jetbrains.compose.material3.windowsizeclass)
implementation(compose.ui)
implementation(compose.components.resources)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.profile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,14 @@ class PresentationLibraryPlugin: Plugin<Project> {
browser()
}
sourceSets.commonMain.dependencies {
implementation(project(":style:components:adaptive"))
implementation(compose.dependencies.material3)
implementation(libs.findBundle("jetbrains-compose-adaptive").get())
implementation(libs.findLibrary("jetbrains-kotlinx-collections").get())
implementation(libs.findLibrary("jetbrains-lifecycle-viewmodel-compose").get())
implementation(libs.findLibrary("koin-compose-viewmodel").get())
}
sourceSets.androidMain.dependencies {
implementation(project(":style:components:adaptive"))

implementation(project.dependencies.platform(libs.findLibrary("androidx-compose-bom").get()))
implementation(libs.findLibrary("androidx-compose-material3-windowsizeclass").get())
implementation(libs.findBundle("androidx-compose-adaptive").get())
implementation(libs.findLibrary("androidx-activity-compose").get())
implementation(libs.findLibrary("androidx-navigation-compose").get())

implementation(project.dependencies.platform(libs.findLibrary("google-firebase-bom").get()))
Expand Down
3 changes: 1 addition & 2 deletions features/main/main/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ dependencies {

implementation(libs.koin.androidx.compose)

implementation(libs.bundles.jetbrains.compose.adaptive)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.material3.windowsizeclass)
implementation(libs.bundles.androidx.compose.adaptive)
implementation(libs.androidx.compose.material3.adaptive.navigation.suite)
implementation(libs.androidx.compose.runtime.livedata)
implementation(compose.material3)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.paligot.confily.partners.presentation

import androidx.activity.compose.BackHandler
import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi
Expand All @@ -11,7 +10,10 @@ import androidx.compose.material3.adaptive.layout.ListDetailPaneScaffoldRole
import androidx.compose.material3.adaptive.layout.calculatePaneScaffoldDirective
import androidx.compose.material3.adaptive.navigation.rememberListDetailPaneScaffoldNavigator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import com.paligot.confily.style.components.adaptive.BackHandler
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
Expand All @@ -22,10 +24,11 @@ fun PartnersAdaptive(
modifier: Modifier = Modifier,
state: LazyGridState = rememberLazyGridState()
) {
val scope = rememberCoroutineScope()
val scaffoldDirective = calculatePaneScaffoldDirective(currentWindowAdaptiveInfo())
val navigator = rememberListDetailPaneScaffoldNavigator<String>(scaffoldDirective)
BackHandler(navigator.canNavigateBack()) {
navigator.navigateBack()
scope.launch { navigator.navigateBack() }
}
ListDetailPaneScaffold(
directive = navigator.scaffoldDirective,
Expand All @@ -35,14 +38,16 @@ fun PartnersAdaptive(
AnimatedPane {
PartnersGridVM(
onPartnerClick = {
navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, content = it)
scope.launch {
navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, contentKey = it)
}
},
state = state
)
}
},
detailPane = {
navigator.currentDestination?.content?.let {
navigator.currentDestination?.contentKey?.let {
AnimatedPane {
PartnerDetailVM(
partnerId = it,
Expand All @@ -52,7 +57,7 @@ fun PartnersAdaptive(
@Composable {
Back {
if (navigator.canNavigateBack()) {
navigator.navigateBack()
scope.launch { navigator.navigateBack() }
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions features/schedules/schedules-sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ dependencies {

implementation(platform(libs.androidx.compose.bom))
implementation(compose.material3)
implementation(libs.androidx.compose.material3.windowsizeclass)
implementation(libs.bundles.androidx.compose.adaptive)
implementation(libs.bundles.jetbrains.compose.adaptive)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.workmanager.ktx)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.paligot.confily.speakers.presentation

import androidx.activity.compose.BackHandler
import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi
Expand All @@ -11,7 +10,10 @@ import androidx.compose.material3.adaptive.layout.ListDetailPaneScaffoldRole
import androidx.compose.material3.adaptive.layout.calculatePaneScaffoldDirective
import androidx.compose.material3.adaptive.navigation.rememberListDetailPaneScaffoldNavigator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import com.paligot.confily.style.components.adaptive.BackHandler
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3AdaptiveApi::class)
@Composable
Expand All @@ -22,27 +24,28 @@ fun SpeakerAdaptive(
modifier: Modifier = Modifier,
state: LazyGridState = rememberLazyGridState()
) {
val scope = rememberCoroutineScope()
val scaffoldDirective = calculatePaneScaffoldDirective(currentWindowAdaptiveInfo())
val navigator = rememberListDetailPaneScaffoldNavigator<String>(scaffoldDirective)
BackHandler(navigator.canNavigateBack()) {
navigator.navigateBack()
scope.launch { navigator.navigateBack() }
}
ListDetailPaneScaffold(
directive = navigator.scaffoldDirective,
value = navigator.scaffoldValue,
modifier = modifier,
listPane = {
AnimatedPane {
SpeakersGridVM(
onSpeakerClicked = {
navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, content = it)
},
state = state
)
}
SpeakersGridVM(
onSpeakerClicked = {
scope.launch {
navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, contentKey = it)
}
},
state = state
)
},
detailPane = {
navigator.currentDestination?.content?.let {
navigator.currentDestination?.contentKey?.let {
AnimatedPane {
SpeakerDetailVM(
speakerId = it,
Expand All @@ -52,7 +55,7 @@ fun SpeakerAdaptive(
@Composable {
Back {
if (navigator.canNavigateBack()) {
navigator.navigateBack()
scope.launch { navigator.navigateBack() }
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions features/speakers/speakers-sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ dependencies {

implementation(libs.androidx.appcompat)

implementation(libs.bundles.jetbrains.compose.adaptive)
implementation(platform(libs.androidx.compose.bom))
implementation(compose.material3)
implementation(libs.androidx.compose.material3.windowsizeclass)
implementation(libs.bundles.androidx.compose.adaptive)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.workmanager.ktx)
Expand Down
19 changes: 10 additions & 9 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ androidx-benchmark = "1.3.3"
androidx-browser = "1.8.0"
androidx-camera = "1.4.0"
androidx-compose-bom = "2024.11.00"
androidx-compose-adaptive = "1.0.0"
androidx-compose-adaptive-navigation = "1.3.1"
androidx-espresso = "3.6.1"
androidx-glance = "1.1.1"
Expand Down Expand Up @@ -47,6 +46,7 @@ google-services = "4.4.2"
google-services-wearable = "19.0.0"
google-zxing = "3.5.3"
jetbrains-compose = "1.7.1"
jetbrains-compose-adaptive = "1.1.0-alpha01"
jetbrains-lifecycle = "2.8.0"
jetbrains-kotlin = "2.1.0"
jetbrains-kotlinx-browser = "0.3"
Expand Down Expand Up @@ -81,10 +81,6 @@ androidx-camera-lifecycle = { group = "androidx.camera", name = "camera-lifecycl
androidx-camera-view = { group = "androidx.camera", name = "camera-view", version.ref = "androidx-camera" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "androidx-compose-bom" }
androidx-compose-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-compose-material3-windowsizeclass = { group = "androidx.compose.material3", name = "material3-window-size-class" }
androidx-compose-material3-adaptive = { group = "androidx.compose.material3.adaptive", name = "adaptive", version.ref = "androidx-compose-adaptive" }
androidx-compose-material3-adaptive-layout = { group = "androidx.compose.material3.adaptive", name = "adaptive-layout", version.ref = "androidx-compose-adaptive" }
androidx-compose-material3-adaptive-navigation = { group = "androidx.compose.material3.adaptive", name = "adaptive-navigation", version.ref = "androidx-compose-adaptive" }
androidx-compose-material3-adaptive-navigation-suite = { group = "androidx.compose.material3", name = "material3-adaptive-navigation-suite", version.ref = "androidx-compose-adaptive-navigation" }
androidx-compose-runtime-livedata = { group = "androidx.compose.runtime", name = "runtime-livedata" }
androidx-core-splashscreen = { group = "androidx.core", name = "core-splashscreen", version.ref = "androidx-splashscreen" }
Expand Down Expand Up @@ -128,6 +124,10 @@ detekt-gradlePlugin = { group = "io.gitlab.arturbosch.detekt", name = "detekt-gr

jetbrains-compose-gradlePlugin = { group = "org.jetbrains.compose", name = "compose-gradle-plugin", version.ref = "jetbrains-compose" }
jetbrains-compose-compiler-gradlePlugin = { group = "org.jetbrains.kotlin", name = "compose-compiler-gradle-plugin", version.ref = "jetbrains-kotlin" }
jetbrains-compose-material3-adaptive = { group = "org.jetbrains.compose.material3.adaptive", name = "adaptive", version.ref = "jetbrains-compose-adaptive" }
jetbrains-compose-material3-adaptive-layout = { group = "org.jetbrains.compose.material3.adaptive", name = "adaptive-layout", version.ref = "jetbrains-compose-adaptive" }
jetbrains-compose-material3-adaptive-navigation = { group = "org.jetbrains.compose.material3.adaptive", name = "adaptive-navigation", version.ref = "jetbrains-compose-adaptive" }
jetbrains-compose-material3-windowsizeclass = { group = "org.jetbrains.compose.material3", name = "material3-window-size-class", version.ref = "jetbrains-compose" }
jetbrains-lifecycle-viewmodel-compose = { group = "org.jetbrains.androidx.lifecycle", name = "lifecycle-viewmodel-compose", version.ref = "jetbrains-lifecycle" }
jetbrains-kotlin-gradlePlugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "jetbrains-kotlin" }
jetbrains-kotlin-serialization-gradlePlugin = { group = "org.jetbrains.kotlin", name = "kotlin-serialization", version.ref = "jetbrains-kotlin" }
Expand Down Expand Up @@ -212,10 +212,11 @@ squareup-okio = { group = "com.squareup.okio", name = "okio", version.ref = "squ
zxing-android-embedded = { group = "com.journeyapps", name = "zxing-android-embedded", version.ref = "zxing-android-embedded" }

[bundles]
androidx-compose-adaptive = [
"androidx-compose-material3-adaptive",
"androidx-compose-material3-adaptive-layout",
"androidx-compose-material3-adaptive-navigation"
jetbrains-compose-adaptive = [
"jetbrains-compose-material3-adaptive",
"jetbrains-compose-material3-adaptive-layout",
"jetbrains-compose-material3-adaptive-navigation",
"jetbrains-compose-material3-windowsizeclass"
]
androidx-glance = [
"androidx-glance-appwidget",
Expand Down
29 changes: 24 additions & 5 deletions style/components/adaptive/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl

plugins {
id("confily.android.library")
id("confily.multiplatform.library")
id("confily.android.library.compose")
id("confily.quality")
}
Expand All @@ -8,8 +10,25 @@ android {
namespace = "com.paligot.confily.style.components.adaptive"
}

dependencies {
implementation(platform(libs.androidx.compose.bom))
implementation(libs.bundles.androidx.compose.adaptive)
implementation(libs.androidx.compose.material3.windowsizeclass)
kotlin {
androidTarget()

@OptIn(ExperimentalWasmDsl::class)
wasmJs {
useCommonJs()
browser()
}

sourceSets {
val commonMain by getting {
dependencies {
implementation(libs.bundles.jetbrains.compose.adaptive)
}
}
val androidMain by getting {
dependencies {
implementation(libs.androidx.activity.compose)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.paligot.confily.style.components.adaptive

import androidx.compose.runtime.Composable

@Composable
actual fun BackHandler(enabled: Boolean, onBack: () -> Unit) {
androidx.activity.compose.BackHandler(enabled = enabled, onBack = onBack)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.paligot.confily.style.components.adaptive

import androidx.compose.runtime.Composable

@Composable
expect fun BackHandler(
enabled: Boolean = true,
onBack: () -> Unit
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.paligot.confily.style.components.adaptive

import androidx.compose.runtime.Composable

@Composable
actual fun BackHandler(enabled: Boolean, onBack: () -> Unit) {
// no-op
}

0 comments on commit 6c22514

Please sign in to comment.