Skip to content

Commit

Permalink
Remove onBackHandler from platform renderes, should be handled in the…
Browse files Browse the repository at this point in the history
… ViewModel (#357)
  • Loading branch information
pablichjenkov authored Mar 31, 2024
1 parent d447e73 commit cf00838
Show file tree
Hide file tree
Showing 20 changed files with 27 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class KoinDiActivityDemo : ComponentActivity() {
setContent {
MaterialTheme {
MacaoKoinApplication(
onBackPress = { finish() },
applicationState = applicationState
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class ManualDiActivityDemo : ComponentActivity() {
setContent {
MaterialTheme {
MacaoApplication(
onBackPress = { finish() },
applicationState = macaoApplicationState
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -54,7 +52,6 @@ fun AndroidComponentRender(
},
initializeBlock = {
rootComponent.dispatchAttach()
rootComponent.rootBackPressDelegate = updatedOnBackPressed
}
)

Expand Down Expand Up @@ -104,9 +101,6 @@ private fun AndroidComponentRenderPreview() {
it.setNavItems(navItems = drawerItems, selectedIndex = 1)
}

AndroidComponentRender(
rootComponent = drawerComponent,
onBackPress = {}
)
AndroidComponentRender(rootComponent = drawerComponent)

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -44,7 +42,6 @@ fun IosComponentRender(
},
initializeBlock = {
rootComponent.dispatchAttach()
rootComponent.rootBackPressDelegate = updatedOnBackPressed
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -54,7 +43,6 @@ fun BrowserComponentRender(
},
initializeBlock = {
rootComponent.dispatchAttach()
rootComponent.rootBackPressDelegate = updatedOnBackPressed
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -49,7 +44,6 @@ fun DesktopComponentRender(
},
initializeBlock = {
rootComponent.dispatchAttach()
rootComponent.rootBackPressDelegate = updatedOnBackPressed
}
)
}
Expand Down
5 changes: 2 additions & 3 deletions iosApp/iosApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ fun main() {
)

CanvasBasedWindow("Macao SDK Demo") {
MacaoKoinApplication(
onBackPress = {
println("Back press dispatched in root node")
},
applicationState = applicationState
)
MacaoKoinApplication(applicationState = applicationState)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.macaosoftware.component.AndroidComponentRender

@Composable
fun MacaoKoinApplication(
onBackPress: () -> Unit,
applicationState: MacaoKoinApplicationState
) {

Expand All @@ -23,8 +22,7 @@ fun MacaoKoinApplication(

is KoinAppStage.Started -> {
AndroidComponentRender(
rootComponent = stage.rootComponent,
onBackPress = onBackPress
rootComponent = stage.rootComponent
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import com.macaosoftware.component.IosComponentRender

@Composable
fun MacaoKoinApplication(
applicationState: MacaoKoinApplicationState,
onBackPress: () -> Unit
applicationState: MacaoKoinApplicationState
) {

when (val stage = applicationState.stage.value) {
Expand All @@ -23,8 +22,7 @@ fun MacaoKoinApplication(

is KoinAppStage.Started -> {
IosComponentRender(
rootComponent = stage.rootComponent,
onBackPress = onBackPress
rootComponent = stage.rootComponent
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ fun MacaoKoinApplication(
is KoinAppStage.Started -> {
DesktopComponentRender(
rootComponent = stage.rootComponent,
windowState = windowState,
onBackPress = onBackPress
windowState = windowState
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.macaosoftware.component.AndroidComponentRender

@Composable
fun MacaoApplication(
onBackPress: () -> Unit,
applicationState: MacaoApplicationState
) {

Expand All @@ -23,8 +22,7 @@ fun MacaoApplication(

is Stage.Started -> {
AndroidComponentRender(
rootComponent = stage.rootComponent,
onBackPress = onBackPress
rootComponent = stage.rootComponent
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.macaosoftware.component.IosComponentRender

@Composable
fun MacaoApplication(
onBackPress: () -> Unit,
applicationState: MacaoApplicationState
) {

Expand All @@ -23,8 +22,7 @@ fun MacaoApplication(

is Stage.Started -> {
IosComponentRender(
rootComponent = stage.rootComponent,
onBackPress = onBackPress
rootComponent = stage.rootComponent
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import platform.UIKit.UIViewController

fun MacaoComposeViewController(
applicationState: MacaoApplicationState,
onBackPress: () -> Unit = {}
): UIViewController = ComposeUIViewController {
MacaoApplication(
applicationState = applicationState,
onBackPress = onBackPress
applicationState = applicationState
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.macaosoftware.component.BrowserComponentRender

@Composable
fun MacaoApplication(
onBackPress: () -> Unit,
applicationState: MacaoApplicationState
) {

Expand All @@ -22,10 +21,7 @@ fun MacaoApplication(
}

is Stage.Started -> {
BrowserComponentRender(
rootComponent = stage.rootComponent,
onBackPress = onBackPress
)
BrowserComponentRender(rootComponent = stage.rootComponent)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.macaosoftware.component.DesktopComponentRender
@Composable
fun MacaoApplication(
windowState: WindowState,
onBackPress: () -> Unit,
applicationState: MacaoApplicationState
) {

Expand All @@ -26,8 +25,7 @@ fun MacaoApplication(
is Stage.Started -> {
DesktopComponentRender(
rootComponent = stage.rootComponent,
windowState = windowState,
onBackPress = onBackPress
windowState = windowState
)
}
}
Expand Down
Loading

0 comments on commit cf00838

Please sign in to comment.