Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.ryanmoelter.magellanx.compose

import android.app.Activity
import androidx.activity.ComponentActivity
import androidx.activity.OnBackPressedCallback
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.lifecycle.DefaultLifecycleObserver
Expand Down Expand Up @@ -55,6 +56,16 @@ public fun ComponentActivity.setContentNavigable(navigable: Navigable<@Composabl
val lifecycleAdapter = ActivityLifecycleComposeAdapter(navigable, this)
navigable.attachAndAddToStaticMap(lifecycleAdapter, lifecycle)
setContent { navigable.Content() }

onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(false) {
init {
isEnabled = navigable.willHandleBack { willHandleBack -> isEnabled = willHandleBack }
}

override fun handleOnBackPressed() {
navigable.backPressed()
}
})
}

private fun Navigable<@Composable () -> Unit>.attachAndAddToStaticMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public open class ComposeNavigator :

AnimatedContent(
targetState = currentNavigable,
label = "ComposeNavigator container",
transitionSpec = currentTransitionSpec.getTransitionForDirection(currentDirection),
) { navigable ->
DisposableEffect(
Expand Down Expand Up @@ -203,6 +204,10 @@ public open class ComposeNavigator :

override fun onBackPressed(): Boolean = currentNavigable?.backPressed() ?: false || goBack()

override fun willHandleBack(changeWillHandleBack: (Boolean) -> Unit): Boolean {
return super.willHandleBack(changeWillHandleBack)
}

public open fun atRoot(): Boolean = backStack.size <= 1
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public interface LifecycleAware {
public fun hide() {}
public fun destroy() {}
public fun backPressed(): Boolean = false
public fun willHandleBack(changeWillHandleBack: (Boolean) -> Unit): Boolean = false
}