From cf00838508811cd8e188b73527741afdaf0d3878 Mon Sep 17 00:00:00 2001 From: pablichjenkov Date: Sat, 30 Mar 2024 21:09:04 -0400 Subject: [PATCH] Remove onBackHandler from platform renderes, should be handled in the ViewModel (#357) --- .../component/demo/KoinDiActivityDemo.kt | 1 - .../component/demo/ManualDiActivityDemo.kt | 1 - .../component/AndroidComponentRender.kt | 10 ++-------- .../com/macaosoftware/component/core/Component.kt | 6 ++---- .../macaosoftware/component/IosComponentRender.kt | 5 +---- .../component/BrowserComponentRender.kt | 14 +------------- .../component/DesktopComponentRender.kt | 8 +------- iosApp/iosApp/ContentView.swift | 5 ++--- .../com/macaosoftware/component/demo/Main.kt | 7 +------ .../com/macaosoftware/app/MacaoKoinApplication.kt | 4 +--- .../com/macaosoftware/app/MacaoKoinApplication.kt | 6 ++---- .../app/MacaoKoinComposeViewController.kt | 6 ++---- .../com/macaosoftware/app/MacaoKoinApplication.kt | 10 ++-------- .../com/macaosoftware/app/MacaoKoinApplication.kt | 3 +-- .../com/macaosoftware/app/MacaoApplication.kt | 4 +--- .../com/macaosoftware/app/MacaoApplication.kt | 4 +--- .../app/MacaoComposeViewController.kt | 4 +--- .../com/macaosoftware/app/MacaoApplication.kt | 6 +----- .../com/macaosoftware/app/MacaoApplication.kt | 4 +--- .../com/macaosoftware/component/demo/Bindings.kt | 14 ++++---------- 20 files changed, 27 insertions(+), 95 deletions(-) diff --git a/androidApp/src/androidMain/kotlin/com/macaosoftware/component/demo/KoinDiActivityDemo.kt b/androidApp/src/androidMain/kotlin/com/macaosoftware/component/demo/KoinDiActivityDemo.kt index bae82b66..f3c104a0 100644 --- a/androidApp/src/androidMain/kotlin/com/macaosoftware/component/demo/KoinDiActivityDemo.kt +++ b/androidApp/src/androidMain/kotlin/com/macaosoftware/component/demo/KoinDiActivityDemo.kt @@ -24,7 +24,6 @@ class KoinDiActivityDemo : ComponentActivity() { setContent { MaterialTheme { MacaoKoinApplication( - onBackPress = { finish() }, applicationState = applicationState ) } diff --git a/androidApp/src/androidMain/kotlin/com/macaosoftware/component/demo/ManualDiActivityDemo.kt b/androidApp/src/androidMain/kotlin/com/macaosoftware/component/demo/ManualDiActivityDemo.kt index 5de66d87..75e0f230 100644 --- a/androidApp/src/androidMain/kotlin/com/macaosoftware/component/demo/ManualDiActivityDemo.kt +++ b/androidApp/src/androidMain/kotlin/com/macaosoftware/component/demo/ManualDiActivityDemo.kt @@ -24,7 +24,6 @@ class ManualDiActivityDemo : ComponentActivity() { setContent { MaterialTheme { MacaoApplication( - onBackPress = { finish() }, applicationState = macaoApplicationState ) } diff --git a/component-toolkit/src/androidMain/kotlin/com/macaosoftware/component/AndroidComponentRender.kt b/component-toolkit/src/androidMain/kotlin/com/macaosoftware/component/AndroidComponentRender.kt index a103b444..da169d84 100644 --- a/component-toolkit/src/androidMain/kotlin/com/macaosoftware/component/AndroidComponentRender.kt +++ b/component-toolkit/src/androidMain/kotlin/com/macaosoftware/component/AndroidComponentRender.kt @@ -28,11 +28,9 @@ import com.macaosoftware.plugin.backpress.AndroidBackPressDispatcherPlugin @Composable fun AndroidComponentRender( - rootComponent: Component, - onBackPress: () -> Unit = {} + rootComponent: Component ) { - val updatedOnBackPressed by rememberUpdatedState(onBackPress) val activity = LocalContext.current as ComponentActivity CompositionLocalProvider( @@ -54,7 +52,6 @@ fun AndroidComponentRender( }, initializeBlock = { rootComponent.dispatchAttach() - rootComponent.rootBackPressDelegate = updatedOnBackPressed } ) @@ -104,9 +101,6 @@ private fun AndroidComponentRenderPreview() { it.setNavItems(navItems = drawerItems, selectedIndex = 1) } - AndroidComponentRender( - rootComponent = drawerComponent, - onBackPress = {} - ) + AndroidComponentRender(rootComponent = drawerComponent) } diff --git a/component-toolkit/src/commonMain/kotlin/com/macaosoftware/component/core/Component.kt b/component-toolkit/src/commonMain/kotlin/com/macaosoftware/component/core/Component.kt index 5d368dc7..131236db 100644 --- a/component-toolkit/src/commonMain/kotlin/com/macaosoftware/component/core/Component.kt +++ b/component-toolkit/src/commonMain/kotlin/com/macaosoftware/component/core/Component.kt @@ -101,8 +101,6 @@ abstract class Component : ComponentLifecycle() { // region: BackPress - internal var rootBackPressDelegate: (() -> Unit)? = null - /** * If a Component does not override handleBackPressed() function, the default behavior is to * delegate/forward the back press event upstream, for its parent Component to handle it. @@ -119,8 +117,8 @@ abstract class Component : ComponentLifecycle() { println("${instanceId()}::delegateBackPressedToParent()") parentComponentCopy.handleBackPressed() } else { - println("${instanceId()}::Back Press reached root component unhandled") - rootBackPressDelegate?.invoke() + println("${instanceId()}::Back Press reached parentComponentCopy = null. " + + "Is either root component or unattached component") } } diff --git a/component-toolkit/src/iosMain/kotlin/com/macaosoftware/component/IosComponentRender.kt b/component-toolkit/src/iosMain/kotlin/com/macaosoftware/component/IosComponentRender.kt index d630226c..cb07caf1 100644 --- a/component-toolkit/src/iosMain/kotlin/com/macaosoftware/component/IosComponentRender.kt +++ b/component-toolkit/src/iosMain/kotlin/com/macaosoftware/component/IosComponentRender.kt @@ -17,11 +17,9 @@ import com.macaosoftware.plugin.Lifecycle @Composable fun IosComponentRender( - rootComponent: Component, - onBackPress: () -> Unit = {} + rootComponent: Component ) { - val updatedOnBackPressed by rememberUpdatedState(onBackPress) val lifecycle = remember(rootComponent) { Lifecycle() } CompositionLocalProvider( @@ -44,7 +42,6 @@ fun IosComponentRender( }, initializeBlock = { rootComponent.dispatchAttach() - rootComponent.rootBackPressDelegate = updatedOnBackPressed } ) diff --git a/component-toolkit/src/jsMain/kotlin/com/macaosoftware/component/BrowserComponentRender.kt b/component-toolkit/src/jsMain/kotlin/com/macaosoftware/component/BrowserComponentRender.kt index 99f39c3e..96fdbc63 100644 --- a/component-toolkit/src/jsMain/kotlin/com/macaosoftware/component/BrowserComponentRender.kt +++ b/component-toolkit/src/jsMain/kotlin/com/macaosoftware/component/BrowserComponentRender.kt @@ -3,32 +3,21 @@ package com.macaosoftware.component import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider -import androidx.compose.runtime.getValue import androidx.compose.runtime.remember -import androidx.compose.runtime.rememberUpdatedState import androidx.compose.ui.Modifier import com.macaosoftware.component.core.Component import com.macaosoftware.component.core.deeplink.LocalRootComponentProvider -import com.macaosoftware.component.util.LocalBackPressedDispatcher -import com.macaosoftware.plugin.DefaultBackPressDispatcherPlugin import com.macaosoftware.plugin.Lifecycle import com.macaosoftware.plugin.LifecycleEventObserver @Composable fun BrowserComponentRender( - rootComponent: Component, - onBackPress: () -> Unit = {} + rootComponent: Component ) { - val webBackPressDispatcher = remember(rootComponent) { - // todo: get this from the plugin manager instead - DefaultBackPressDispatcherPlugin() - } - val updatedOnBackPressed by rememberUpdatedState(onBackPress) val lifecycle = remember(rootComponent) { Lifecycle() } CompositionLocalProvider( - LocalBackPressedDispatcher provides webBackPressDispatcher, LocalRootComponentProvider provides rootComponent ) { rootComponent.Content(Modifier.fillMaxSize()) @@ -54,7 +43,6 @@ fun BrowserComponentRender( }, initializeBlock = { rootComponent.dispatchAttach() - rootComponent.rootBackPressDelegate = updatedOnBackPressed } ) diff --git a/component-toolkit/src/jvmMain/kotlin/com/macaosoftware/component/DesktopComponentRender.kt b/component-toolkit/src/jvmMain/kotlin/com/macaosoftware/component/DesktopComponentRender.kt index 2f6a6850..2197b19d 100644 --- a/component-toolkit/src/jvmMain/kotlin/com/macaosoftware/component/DesktopComponentRender.kt +++ b/component-toolkit/src/jvmMain/kotlin/com/macaosoftware/component/DesktopComponentRender.kt @@ -6,9 +6,7 @@ import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider -import androidx.compose.runtime.getValue import androidx.compose.runtime.remember -import androidx.compose.runtime.rememberUpdatedState import androidx.compose.ui.Modifier import androidx.compose.ui.window.WindowState import com.macaosoftware.component.core.Component @@ -21,12 +19,9 @@ import kotlinx.coroutines.Dispatchers @Composable fun DesktopComponentRender( rootComponent: Component, - windowState: WindowState, - onBackPress: () -> Unit = {} + windowState: WindowState ) { - val updatedOnBackPressed by rememberUpdatedState(onBackPress) - val lifecycle = remember(rootComponent) { Lifecycle(CoroutineScope(Dispatchers.Main), windowState) } @@ -49,7 +44,6 @@ fun DesktopComponentRender( }, initializeBlock = { rootComponent.dispatchAttach() - rootComponent.rootBackPressDelegate = updatedOnBackPressed } ) } diff --git a/iosApp/iosApp/ContentView.swift b/iosApp/iosApp/ContentView.swift index 8582f328..dd3e60fd 100644 --- a/iosApp/iosApp/ContentView.swift +++ b/iosApp/iosApp/ContentView.swift @@ -16,9 +16,8 @@ struct ComposeViewController : UIViewControllerRepresentable { func makeUIViewController(context: Context) -> UIViewController { - return BindingsKt.buildKoinDemoViewController( - onBackPress: { exit(0) } - ) + //return BindingsKt.buildDemoViewController() + return BindingsKt.buildKoinDemoViewController() } func updateUIViewController( diff --git a/jsApp/src/jsMain/kotlin/com/macaosoftware/component/demo/Main.kt b/jsApp/src/jsMain/kotlin/com/macaosoftware/component/demo/Main.kt index 347cf53c..6dc5be94 100644 --- a/jsApp/src/jsMain/kotlin/com/macaosoftware/component/demo/Main.kt +++ b/jsApp/src/jsMain/kotlin/com/macaosoftware/component/demo/Main.kt @@ -17,12 +17,7 @@ fun main() { ) CanvasBasedWindow("Macao SDK Demo") { - MacaoKoinApplication( - onBackPress = { - println("Back press dispatched in root node") - }, - applicationState = applicationState - ) + MacaoKoinApplication(applicationState = applicationState) } } } diff --git a/macao-sdk-di-koin/src/androidMain/kotlin/com/macaosoftware/app/MacaoKoinApplication.kt b/macao-sdk-di-koin/src/androidMain/kotlin/com/macaosoftware/app/MacaoKoinApplication.kt index 7391d8a4..5b1861bf 100644 --- a/macao-sdk-di-koin/src/androidMain/kotlin/com/macaosoftware/app/MacaoKoinApplication.kt +++ b/macao-sdk-di-koin/src/androidMain/kotlin/com/macaosoftware/app/MacaoKoinApplication.kt @@ -6,7 +6,6 @@ import com.macaosoftware.component.AndroidComponentRender @Composable fun MacaoKoinApplication( - onBackPress: () -> Unit, applicationState: MacaoKoinApplicationState ) { @@ -23,8 +22,7 @@ fun MacaoKoinApplication( is KoinAppStage.Started -> { AndroidComponentRender( - rootComponent = stage.rootComponent, - onBackPress = onBackPress + rootComponent = stage.rootComponent ) } } diff --git a/macao-sdk-di-koin/src/iosMain/kotlin/com/macaosoftware/app/MacaoKoinApplication.kt b/macao-sdk-di-koin/src/iosMain/kotlin/com/macaosoftware/app/MacaoKoinApplication.kt index 16881f44..27a8ba69 100644 --- a/macao-sdk-di-koin/src/iosMain/kotlin/com/macaosoftware/app/MacaoKoinApplication.kt +++ b/macao-sdk-di-koin/src/iosMain/kotlin/com/macaosoftware/app/MacaoKoinApplication.kt @@ -6,8 +6,7 @@ import com.macaosoftware.component.IosComponentRender @Composable fun MacaoKoinApplication( - applicationState: MacaoKoinApplicationState, - onBackPress: () -> Unit + applicationState: MacaoKoinApplicationState ) { when (val stage = applicationState.stage.value) { @@ -23,8 +22,7 @@ fun MacaoKoinApplication( is KoinAppStage.Started -> { IosComponentRender( - rootComponent = stage.rootComponent, - onBackPress = onBackPress + rootComponent = stage.rootComponent ) } } diff --git a/macao-sdk-di-koin/src/iosMain/kotlin/com/macaosoftware/app/MacaoKoinComposeViewController.kt b/macao-sdk-di-koin/src/iosMain/kotlin/com/macaosoftware/app/MacaoKoinComposeViewController.kt index 48b91fd8..85dd59ac 100644 --- a/macao-sdk-di-koin/src/iosMain/kotlin/com/macaosoftware/app/MacaoKoinComposeViewController.kt +++ b/macao-sdk-di-koin/src/iosMain/kotlin/com/macaosoftware/app/MacaoKoinComposeViewController.kt @@ -4,11 +4,9 @@ import androidx.compose.ui.window.ComposeUIViewController import platform.UIKit.UIViewController fun MacaoKoinComposeViewController( - applicationState: MacaoKoinApplicationState, - onBackPress: () -> Unit = {} + applicationState: MacaoKoinApplicationState ): UIViewController = ComposeUIViewController { MacaoKoinApplication( - applicationState = applicationState, - onBackPress = onBackPress + applicationState = applicationState ) } diff --git a/macao-sdk-di-koin/src/jsMain/kotlin/com/macaosoftware/app/MacaoKoinApplication.kt b/macao-sdk-di-koin/src/jsMain/kotlin/com/macaosoftware/app/MacaoKoinApplication.kt index 5ddf26f7..7d895216 100644 --- a/macao-sdk-di-koin/src/jsMain/kotlin/com/macaosoftware/app/MacaoKoinApplication.kt +++ b/macao-sdk-di-koin/src/jsMain/kotlin/com/macaosoftware/app/MacaoKoinApplication.kt @@ -6,26 +6,20 @@ import com.macaosoftware.component.BrowserComponentRender @Composable fun MacaoKoinApplication( - onBackPress: () -> Unit, applicationState: MacaoKoinApplicationState ) { when (val stage = applicationState.stage.value) { KoinAppStage.Created -> { - SideEffect { - applicationState.start() - } + SideEffect { applicationState.start() } } KoinAppStage.Loading -> { } is KoinAppStage.Started -> { - BrowserComponentRender( - rootComponent = stage.rootComponent, - onBackPress = onBackPress - ) + BrowserComponentRender(rootComponent = stage.rootComponent) } } } diff --git a/macao-sdk-di-koin/src/jvmMain/kotlin/com/macaosoftware/app/MacaoKoinApplication.kt b/macao-sdk-di-koin/src/jvmMain/kotlin/com/macaosoftware/app/MacaoKoinApplication.kt index bd54821d..ac8702b6 100644 --- a/macao-sdk-di-koin/src/jvmMain/kotlin/com/macaosoftware/app/MacaoKoinApplication.kt +++ b/macao-sdk-di-koin/src/jvmMain/kotlin/com/macaosoftware/app/MacaoKoinApplication.kt @@ -26,8 +26,7 @@ fun MacaoKoinApplication( is KoinAppStage.Started -> { DesktopComponentRender( rootComponent = stage.rootComponent, - windowState = windowState, - onBackPress = onBackPress + windowState = windowState ) } } diff --git a/macao-sdk-di-manual/src/androidMain/kotlin/com/macaosoftware/app/MacaoApplication.kt b/macao-sdk-di-manual/src/androidMain/kotlin/com/macaosoftware/app/MacaoApplication.kt index 2060845a..95cd911b 100644 --- a/macao-sdk-di-manual/src/androidMain/kotlin/com/macaosoftware/app/MacaoApplication.kt +++ b/macao-sdk-di-manual/src/androidMain/kotlin/com/macaosoftware/app/MacaoApplication.kt @@ -6,7 +6,6 @@ import com.macaosoftware.component.AndroidComponentRender @Composable fun MacaoApplication( - onBackPress: () -> Unit, applicationState: MacaoApplicationState ) { @@ -23,8 +22,7 @@ fun MacaoApplication( is Stage.Started -> { AndroidComponentRender( - rootComponent = stage.rootComponent, - onBackPress = onBackPress + rootComponent = stage.rootComponent ) } } diff --git a/macao-sdk-di-manual/src/iosMain/kotlin/com/macaosoftware/app/MacaoApplication.kt b/macao-sdk-di-manual/src/iosMain/kotlin/com/macaosoftware/app/MacaoApplication.kt index f12e814f..dd45ef2c 100644 --- a/macao-sdk-di-manual/src/iosMain/kotlin/com/macaosoftware/app/MacaoApplication.kt +++ b/macao-sdk-di-manual/src/iosMain/kotlin/com/macaosoftware/app/MacaoApplication.kt @@ -6,7 +6,6 @@ import com.macaosoftware.component.IosComponentRender @Composable fun MacaoApplication( - onBackPress: () -> Unit, applicationState: MacaoApplicationState ) { @@ -23,8 +22,7 @@ fun MacaoApplication( is Stage.Started -> { IosComponentRender( - rootComponent = stage.rootComponent, - onBackPress = onBackPress + rootComponent = stage.rootComponent ) } } diff --git a/macao-sdk-di-manual/src/iosMain/kotlin/com/macaosoftware/app/MacaoComposeViewController.kt b/macao-sdk-di-manual/src/iosMain/kotlin/com/macaosoftware/app/MacaoComposeViewController.kt index d24ad86f..d137b635 100644 --- a/macao-sdk-di-manual/src/iosMain/kotlin/com/macaosoftware/app/MacaoComposeViewController.kt +++ b/macao-sdk-di-manual/src/iosMain/kotlin/com/macaosoftware/app/MacaoComposeViewController.kt @@ -5,10 +5,8 @@ import platform.UIKit.UIViewController fun MacaoComposeViewController( applicationState: MacaoApplicationState, - onBackPress: () -> Unit = {} ): UIViewController = ComposeUIViewController { MacaoApplication( - applicationState = applicationState, - onBackPress = onBackPress + applicationState = applicationState ) } diff --git a/macao-sdk-di-manual/src/jsMain/kotlin/com/macaosoftware/app/MacaoApplication.kt b/macao-sdk-di-manual/src/jsMain/kotlin/com/macaosoftware/app/MacaoApplication.kt index 335cab87..65ba1920 100644 --- a/macao-sdk-di-manual/src/jsMain/kotlin/com/macaosoftware/app/MacaoApplication.kt +++ b/macao-sdk-di-manual/src/jsMain/kotlin/com/macaosoftware/app/MacaoApplication.kt @@ -6,7 +6,6 @@ import com.macaosoftware.component.BrowserComponentRender @Composable fun MacaoApplication( - onBackPress: () -> Unit, applicationState: MacaoApplicationState ) { @@ -22,10 +21,7 @@ fun MacaoApplication( } is Stage.Started -> { - BrowserComponentRender( - rootComponent = stage.rootComponent, - onBackPress = onBackPress - ) + BrowserComponentRender(rootComponent = stage.rootComponent) } } diff --git a/macao-sdk-di-manual/src/jvmMain/kotlin/com/macaosoftware/app/MacaoApplication.kt b/macao-sdk-di-manual/src/jvmMain/kotlin/com/macaosoftware/app/MacaoApplication.kt index 54abec8d..f4e75922 100644 --- a/macao-sdk-di-manual/src/jvmMain/kotlin/com/macaosoftware/app/MacaoApplication.kt +++ b/macao-sdk-di-manual/src/jvmMain/kotlin/com/macaosoftware/app/MacaoApplication.kt @@ -8,7 +8,6 @@ import com.macaosoftware.component.DesktopComponentRender @Composable fun MacaoApplication( windowState: WindowState, - onBackPress: () -> Unit, applicationState: MacaoApplicationState ) { @@ -26,8 +25,7 @@ fun MacaoApplication( is Stage.Started -> { DesktopComponentRender( rootComponent = stage.rootComponent, - windowState = windowState, - onBackPress = onBackPress + windowState = windowState ) } } diff --git a/shared/src/iosMain/kotlin/com/macaosoftware/component/demo/Bindings.kt b/shared/src/iosMain/kotlin/com/macaosoftware/component/demo/Bindings.kt index 3c2f809f..14514874 100644 --- a/shared/src/iosMain/kotlin/com/macaosoftware/component/demo/Bindings.kt +++ b/shared/src/iosMain/kotlin/com/macaosoftware/component/demo/Bindings.kt @@ -22,9 +22,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.IO import platform.UIKit.UIViewController -fun buildDemoViewController( - onBackPress: () -> Unit = {} -): UIViewController { +fun buildDemoViewController(): UIViewController { val applicationState = MacaoApplicationState( dispatcher = Dispatchers.IO, @@ -33,14 +31,11 @@ fun buildDemoViewController( ) return MacaoComposeViewController( - applicationState = applicationState, - onBackPress = onBackPress + applicationState = applicationState ) } -fun buildKoinDemoViewController( - onBackPress: () -> Unit = {} -): UIViewController { +fun buildKoinDemoViewController(): UIViewController { val applicationState = MacaoKoinApplicationState( dispatcher = Dispatchers.IO, @@ -49,8 +44,7 @@ fun buildKoinDemoViewController( ) return MacaoKoinComposeViewController( - applicationState, - onBackPress + applicationState ) }