From bec82a8c290ffd0f03922d2d5b7da738e3693053 Mon Sep 17 00:00:00 2001 From: Viachaslau Katsuba Date: Tue, 28 May 2024 14:28:59 +0300 Subject: [PATCH] Refactor platform screen and unwrap extra android nav controller --- .../tiamat/example/MainActivity.kt | 8 ++- .../example/PlatformExamplesScreen.android.kt | 29 +++++++++ .../common/PlatformExampleScreen.android.kt | 60 ------------------- .../kotlin/composegears/tiamat/example/App.kt | 25 +++++--- .../composegears/tiamat/example/MainScreen.kt | 3 +- ...pleScreen.kt => PlatformExamplesScreen.kt} | 16 ++--- .../example/PlatformExamplesScreen.desktop.kt | 14 +++++ .../common/PlatformExampleScreen.desktop.kt | 31 ---------- .../example/PlatformExamplesScreen.ios.kt | 13 ++++ .../common/PlatformExampleScreen.ios.kt | 30 ---------- .../example/PlatformExamplesScreen.wasmJs.kt | 6 ++ .../common/PlatformExampleScreen.wasmJs.kt | 17 ------ gradle.properties | 2 +- 13 files changed, 95 insertions(+), 159 deletions(-) create mode 100644 example/app/composeApp/src/androidMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.android.kt delete mode 100644 example/app/composeApp/src/androidMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.android.kt rename example/app/composeApp/src/commonMain/kotlin/composegears/tiamat/example/{common/PlatformExampleScreen.kt => PlatformExamplesScreen.kt} (79%) create mode 100644 example/app/composeApp/src/desktopMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.desktop.kt delete mode 100644 example/app/composeApp/src/desktopMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.desktop.kt create mode 100644 example/app/composeApp/src/iosMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.ios.kt delete mode 100644 example/app/composeApp/src/iosMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.ios.kt create mode 100644 example/app/composeApp/src/wasmJsMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.wasmJs.kt delete mode 100644 example/app/composeApp/src/wasmJsMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.wasmJs.kt diff --git a/example/app/composeApp/src/androidMain/kotlin/composegears/tiamat/example/MainActivity.kt b/example/app/composeApp/src/androidMain/kotlin/composegears/tiamat/example/MainActivity.kt index 4695644..66b1538 100644 --- a/example/app/composeApp/src/androidMain/kotlin/composegears/tiamat/example/MainActivity.kt +++ b/example/app/composeApp/src/androidMain/kotlin/composegears/tiamat/example/MainActivity.kt @@ -7,7 +7,7 @@ import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.compose.runtime.DisposableEffect import com.composegears.tiamat.navigationFadeInOut -import composegears.tiamat.example.common.PlatformExampleScreen +import composegears.tiamat.example.platform.DeeplinkScreen class MainActivity : ComponentActivity() { @@ -18,7 +18,7 @@ class MainActivity : ComponentActivity() { super.onCreate(savedInstanceState) deepLinkController.onIntent(intent) setContent { - App { rootNavController -> + App { rootNavController, content -> // pass deeplink deeper and handle inside screen val deeplink = deepLinkController.deeplink // using disposable effect as it runs faster then LaunchedEffect @@ -27,9 +27,10 @@ class MainActivity : ComponentActivity() { rootNavController.editBackStack { clear() add(MainScreen) + add(PlatformExamplesScreen) } rootNavController.replace( - dest = PlatformExampleScreen, + dest = DeeplinkScreen, freeArgs = deeplink, // we only animate root content switch // all nested items should use navigationNone() transition to prevent `blink` @@ -39,6 +40,7 @@ class MainActivity : ComponentActivity() { } onDispose { } } + content() } } } diff --git a/example/app/composeApp/src/androidMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.android.kt b/example/app/composeApp/src/androidMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.android.kt new file mode 100644 index 0000000..6e9ea84 --- /dev/null +++ b/example/app/composeApp/src/androidMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.android.kt @@ -0,0 +1,29 @@ +package composegears.tiamat.example + +import composegears.tiamat.example.PlatformConfig.PlatformDestination +import composegears.tiamat.example.platform.AndroidViewLifecycleScreen +import composegears.tiamat.example.platform.DeeplinkScreen +import composegears.tiamat.example.platform.SavedStateScreen +import composegears.tiamat.sample.koin.KoinIntegrationScreen + +actual val platformExamplesConfig = PlatformConfig( + platformName = "Android", + availableScreens = listOf( + PlatformDestination( + name = "Android SavedState", + destination = SavedStateScreen + ), + PlatformDestination( + name = "AndroidView + Lifecycle handle", + destination = AndroidViewLifecycleScreen + ), + PlatformDestination( + name = "Deeplink", + destination = DeeplinkScreen + ), + PlatformDestination( + name = "Koin (ViewModel/SharedViewModel)", + destination = KoinIntegrationScreen + ) + ) +) \ No newline at end of file diff --git a/example/app/composeApp/src/androidMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.android.kt b/example/app/composeApp/src/androidMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.android.kt deleted file mode 100644 index 3166a81..0000000 --- a/example/app/composeApp/src/androidMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.android.kt +++ /dev/null @@ -1,60 +0,0 @@ -package composegears.tiamat.example.common - -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.ui.Modifier -import com.composegears.tiamat.* -import composegears.tiamat.example.DeeplinkData -import composegears.tiamat.example.common.PlatformConfig.PlatformDestination -import composegears.tiamat.example.platform.AndroidViewLifecycleScreen -import composegears.tiamat.example.platform.DeeplinkScreen -import composegears.tiamat.example.platform.SavedStateScreen -import composegears.tiamat.sample.koin.KoinIntegrationScreen - -actual val platformConfig = PlatformConfig( - platformName = "Android", - availableScreens = listOf( - PlatformDestination( - name = "Android SavedState", - destination = SavedStateScreen - ), - PlatformDestination( - name = "AndroidView + Lifecycle handle", - destination = AndroidViewLifecycleScreen - ), - PlatformDestination( - name = "Deeplink", - destination = DeeplinkScreen - ), - PlatformDestination( - name = "Koin (ViewModel/SharedViewModel)", - destination = KoinIntegrationScreen - ) - ) -) - -actual val PlatformExampleScreen: NavDestination by navDestination { - val deeplink = freeArgs() - - val platformNavController = rememberNavController( - startDestination = PlatformScreen, - destinations = platformConfig.destinations() + PlatformScreen - ) { - if (deeplink != null) { - editBackStack { - clear() - add(PlatformScreen) - } - replace( - dest = DeeplinkScreen, - freeArgs = deeplink, - transition = navigationNone() - ) - clearFreeArgs() - } - } - - Navigation( - navController = platformNavController, - modifier = Modifier.fillMaxSize() - ) -} \ No newline at end of file diff --git a/example/app/composeApp/src/commonMain/kotlin/composegears/tiamat/example/App.kt b/example/app/composeApp/src/commonMain/kotlin/composegears/tiamat/example/App.kt index 2bc1163..f1e20c8 100644 --- a/example/app/composeApp/src/commonMain/kotlin/composegears/tiamat/example/App.kt +++ b/example/app/composeApp/src/commonMain/kotlin/composegears/tiamat/example/App.kt @@ -9,12 +9,19 @@ import com.composegears.tiamat.NavController import com.composegears.tiamat.Navigation import com.composegears.tiamat.StorageMode import com.composegears.tiamat.rememberNavController -import composegears.tiamat.example.common.PlatformExampleScreen import composegears.tiamat.example.multimodule.MultiModuleRoot import composegears.tiamat.example.ui.core.AppTheme @Composable -fun App(configure: @Composable (NavController) -> Unit = {}) { +@Suppress("SpreadOperator") +fun App( + configure: @Composable ( + NavController, + @Composable () -> Unit + ) -> Unit = { _, content -> + content() + } +) { AppTheme { Surface { val rootNavController = rememberNavController( @@ -46,14 +53,16 @@ fun App(configure: @Composable (NavController) -> Unit = {}) { MultiModuleRoot, BackStackAlterationRoot, TwoPaneResizableRoot, - PlatformExampleScreen + PlatformExamplesScreen, + *platformExamplesConfig.destinations() ) ) - configure(rootNavController) - Navigation( - navController = rootNavController, - modifier = Modifier.fillMaxSize().systemBarsPadding() - ) + configure(rootNavController) { + Navigation( + navController = rootNavController, + modifier = Modifier.fillMaxSize().systemBarsPadding() + ) + } } } } \ No newline at end of file diff --git a/example/app/composeApp/src/commonMain/kotlin/composegears/tiamat/example/MainScreen.kt b/example/app/composeApp/src/commonMain/kotlin/composegears/tiamat/example/MainScreen.kt index b7ed6b3..6e5f7e4 100644 --- a/example/app/composeApp/src/commonMain/kotlin/composegears/tiamat/example/MainScreen.kt +++ b/example/app/composeApp/src/commonMain/kotlin/composegears/tiamat/example/MainScreen.kt @@ -14,7 +14,6 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import com.composegears.tiamat.navController import com.composegears.tiamat.navDestination -import composegears.tiamat.example.common.PlatformExampleScreen import composegears.tiamat.example.multimodule.MultiModuleRoot val MainScreen by navDestination { @@ -33,7 +32,7 @@ val MainScreen by navDestination { "Multi-module" to { navController.navigate(MultiModuleRoot) }, "Back stack alteration" to { navController.navigate(BackStackAlterationRoot) }, "2 Pane (list + detail, resizable)" to { navController.navigate(TwoPaneResizableRoot) }, - "Platform specific" to { navController.navigate(PlatformExampleScreen) } + "Platform specific" to { navController.navigate(PlatformExamplesScreen) } ) } Box( diff --git a/example/app/composeApp/src/commonMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.kt b/example/app/composeApp/src/commonMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.kt similarity index 79% rename from example/app/composeApp/src/commonMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.kt rename to example/app/composeApp/src/commonMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.kt index dd4342b..f9cef58 100644 --- a/example/app/composeApp/src/commonMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.kt +++ b/example/app/composeApp/src/commonMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.kt @@ -1,4 +1,4 @@ -package composegears.tiamat.example.common +package composegears.tiamat.example import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.PaddingValues @@ -18,16 +18,19 @@ import com.composegears.tiamat.NavDestination import com.composegears.tiamat.navController import com.composegears.tiamat.navDestination import composegears.tiamat.example.ui.core.SimpleScreen +import composegears.tiamat.example.ui.core.TextCaption -val PlatformScreen by navDestination { +val PlatformExamplesScreen by navDestination { val navController = navController() - SimpleScreen("Platform ${platformConfig.platformName}") { - LazyColumn( + SimpleScreen("Platform ${platformExamplesConfig.platformName}") { + if (platformExamplesConfig.availableScreens.isEmpty()) { + TextCaption("Nothing platform specific yet") + } else LazyColumn( contentPadding = PaddingValues(24.dp), verticalArrangement = Arrangement.spacedBy(4.dp) ) { - items(platformConfig.availableScreens) { (name, screen) -> + items(platformExamplesConfig.availableScreens) { (name, screen) -> Button( modifier = Modifier.widthIn(max = 450.dp), onClick = { @@ -45,8 +48,7 @@ val PlatformScreen by navDestination { } } -expect val PlatformExampleScreen: NavDestination -expect val platformConfig: PlatformConfig +expect val platformExamplesConfig: PlatformConfig class PlatformConfig( val platformName: String, diff --git a/example/app/composeApp/src/desktopMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.desktop.kt b/example/app/composeApp/src/desktopMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.desktop.kt new file mode 100644 index 0000000..32690b8 --- /dev/null +++ b/example/app/composeApp/src/desktopMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.desktop.kt @@ -0,0 +1,14 @@ +package composegears.tiamat.example + +import composegears.tiamat.example.PlatformConfig.PlatformDestination +import composegears.tiamat.sample.koin.KoinIntegrationScreen + +actual val platformExamplesConfig = PlatformConfig( + platformName = "Desktop", + availableScreens = listOf( + PlatformDestination( + name = "Koin (ViewModel/SharedViewModel)", + destination = KoinIntegrationScreen + ) + ) +) \ No newline at end of file diff --git a/example/app/composeApp/src/desktopMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.desktop.kt b/example/app/composeApp/src/desktopMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.desktop.kt deleted file mode 100644 index 80395af..0000000 --- a/example/app/composeApp/src/desktopMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.desktop.kt +++ /dev/null @@ -1,31 +0,0 @@ -package composegears.tiamat.example.common - -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.ui.Modifier -import com.composegears.tiamat.Navigation -import com.composegears.tiamat.navDestination -import com.composegears.tiamat.rememberNavController -import composegears.tiamat.example.common.PlatformConfig.PlatformDestination -import composegears.tiamat.sample.koin.KoinIntegrationScreen - -actual val PlatformExampleScreen by navDestination { - val platformNavController = rememberNavController( - startDestination = PlatformScreen, - destinations = platformConfig.destinations() + PlatformScreen - ) - - Navigation( - navController = platformNavController, - modifier = Modifier.fillMaxSize() - ) -} - -actual val platformConfig = PlatformConfig( - platformName = "Desktop", - availableScreens = listOf( - PlatformDestination( - name = "Koin (ViewModel/SharedViewModel)", - destination = KoinIntegrationScreen - ) - ) -) \ No newline at end of file diff --git a/example/app/composeApp/src/iosMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.ios.kt b/example/app/composeApp/src/iosMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.ios.kt new file mode 100644 index 0000000..8c4c012 --- /dev/null +++ b/example/app/composeApp/src/iosMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.ios.kt @@ -0,0 +1,13 @@ +package composegears.tiamat.example + +import composegears.tiamat.sample.koin.KoinIntegrationScreen + +actual val platformExamplesConfig = PlatformConfig( + platformName = "iOS", + availableScreens = listOf( + PlatformConfig.PlatformDestination( + name = "Koin (ViewModel/SharedViewModel)", + destination = KoinIntegrationScreen + ) + ) +) \ No newline at end of file diff --git a/example/app/composeApp/src/iosMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.ios.kt b/example/app/composeApp/src/iosMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.ios.kt deleted file mode 100644 index a7de442..0000000 --- a/example/app/composeApp/src/iosMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.ios.kt +++ /dev/null @@ -1,30 +0,0 @@ -package composegears.tiamat.example.common - -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.ui.Modifier -import com.composegears.tiamat.NavDestination -import com.composegears.tiamat.Navigation -import com.composegears.tiamat.navDestination -import com.composegears.tiamat.rememberNavController -import composegears.tiamat.sample.koin.KoinIntegrationScreen - -actual val platformConfig = PlatformConfig( - platformName = "iOS", - availableScreens = listOf( - PlatformConfig.PlatformDestination( - name = "Koin (ViewModel/SharedViewModel)", - destination = KoinIntegrationScreen - ) - ) -) - -actual val PlatformExampleScreen: NavDestination by navDestination { - val platformNavController = rememberNavController( - startDestination = PlatformScreen, - destinations = platformConfig.destinations() + PlatformScreen - ) - Navigation( - navController = platformNavController, - modifier = Modifier.fillMaxSize() - ) -} \ No newline at end of file diff --git a/example/app/composeApp/src/wasmJsMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.wasmJs.kt b/example/app/composeApp/src/wasmJsMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.wasmJs.kt new file mode 100644 index 0000000..6a9290e --- /dev/null +++ b/example/app/composeApp/src/wasmJsMain/kotlin/composegears/tiamat/example/PlatformExamplesScreen.wasmJs.kt @@ -0,0 +1,6 @@ +package composegears.tiamat.example + +actual val platformExamplesConfig = PlatformConfig( + platformName = "Wasm", + availableScreens = emptyList() +) \ No newline at end of file diff --git a/example/app/composeApp/src/wasmJsMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.wasmJs.kt b/example/app/composeApp/src/wasmJsMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.wasmJs.kt deleted file mode 100644 index 8684034..0000000 --- a/example/app/composeApp/src/wasmJsMain/kotlin/composegears/tiamat/example/common/PlatformExampleScreen.wasmJs.kt +++ /dev/null @@ -1,17 +0,0 @@ -package composegears.tiamat.example.common - -import com.composegears.tiamat.NavDestination -import com.composegears.tiamat.navDestination -import composegears.tiamat.example.ui.core.SimpleScreen -import composegears.tiamat.example.ui.core.TextCaption - -actual val platformConfig = PlatformConfig( - platformName = "Wasm", - availableScreens = emptyList() -) - -actual val PlatformExampleScreen: NavDestination by navDestination { - SimpleScreen("Platform ${platformConfig.platformName}") { - TextCaption("Nothing platform specific yet") - } -} diff --git a/gradle.properties b/gradle.properties index 2073ad3..bf40384 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,8 +8,8 @@ android.nonTransitiveRClass=true android.useAndroidX=true #MPP -kotlin.mpp.androidSourceSetLayoutVersion=2 kotlin.mpp.enableCInteropCommonization=true +kotlin.native.ignoreDisabledTargets=true #WASM org.jetbrains.compose.experimental.wasm.enabled=true