Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show children in previews without using interactive mode #215

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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.addCallback
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.lifecycle.DefaultLifecycleObserver
Expand Down Expand Up @@ -56,6 +57,11 @@ public fun ComponentActivity.setContentNavigable(navigable: Navigable<@Composabl
val lifecycleAdapter = ActivityLifecycleComposeAdapter(navigable, this)
navigable.attachAndAddToStaticMap(lifecycleAdapter, lifecycle)
setContent { navigable.Content() }
onBackPressedDispatcher.addCallback {
if (!navigable.backPressed()) {
[email protected]()
}
}
}

private fun Navigable<@Composable () -> Unit>.attachAndAddToStaticMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import com.ryanmoelter.magellanx.core.Displayable
import com.ryanmoelter.magellanx.core.lifecycle.LifecycleOwner
import com.ryanmoelter.magellanx.core.lifecycle.LifecycleState.Resumed
import com.ryanmoelter.magellanx.core.lifecycle.LifecycleState.Started
import kotlinx.coroutines.flow.map

@Composable
@Suppress("ktlint:standard:function-naming")
Expand All @@ -30,12 +28,8 @@ public fun Displayable(
@Composable
@Suppress("ktlint:standard:function-naming")
public fun LifecycleOwner.WhenStarted(content: @Composable () -> Unit) {
val isStartedFlow =
remember {
currentStateFlow
.map { lifecycleState -> lifecycleState >= Started }
}
val isStarted by isStartedFlow.collectAsState(false)
val currentState by currentStateFlow.collectAsState()
val isStarted = currentState >= Started
if (isStarted) {
content()
}
Expand All @@ -44,12 +38,8 @@ public fun LifecycleOwner.WhenStarted(content: @Composable () -> Unit) {
@Composable
@Suppress("ktlint:standard:function-naming")
public fun LifecycleOwner.WhenResumed(content: @Composable () -> Unit) {
val isResumedFlow =
remember {
currentStateFlow
.map { lifecycleState -> lifecycleState >= Resumed }
}
val isResumed by isResumedFlow.collectAsState(false)
val currentState by currentStateFlow.collectAsState()
val isResumed = currentState >= Resumed
if (isResumed) {
content()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn

public open class ComposeNavigator :
LifecycleAwareComponent(), Displayable<@Composable () -> Unit> {
LifecycleAwareComponent(),
Displayable<@Composable () -> Unit> {
private val createdScope by attachFieldToLifecycle(CreatedLifecycleScope())

/**
Expand Down Expand Up @@ -63,9 +64,9 @@ public open class ComposeNavigator :

override fun onCreate() {
currentNavigableFlow =
backStackFlow.map { it.lastOrNull() }
.map { it?.navigable }
.stateIn(createdScope, SharingStarted.Eagerly, null)
backStackFlow
.map { it.lastOrNull()?.navigable }
.stateIn(createdScope, SharingStarted.Eagerly, backStack.lastOrNull()?.navigable)
}

override val view: (@Composable () -> Unit)
Expand Down Expand Up @@ -93,7 +94,10 @@ public open class ComposeNavigator :
if (backStack.lastOrNull()?.navigable == navigable) {
// Navigable is Resumed if it's on the top of the backstack
lifecycleRegistry.updateMaxState(navigable, LifecycleLimit.NO_LIMIT)
} else {
} else if (children.contains(navigable)) {
// If the navigable is being removed from composition, it might not be attached
// to this parent anymore

// Navigable is Started if it's in the composition, but not on top of the backstack
lifecycleRegistry.updateMaxState(navigable, LifecycleLimit.STARTED)
}
Expand Down Expand Up @@ -168,16 +172,15 @@ public open class ComposeNavigator :
}
}

public open fun goBack(): Boolean {
return if (!atRoot()) {
public open fun goBack(): Boolean =
if (!atRoot()) {
navigate(BACKWARD) { backStack ->
backStack - backStack.last()
}
true
} else {
false
}
}

public open fun goBackTo(navigable: Navigable<@Composable () -> Unit>) {
navigate(BACKWARD) { backStack ->
Expand Down Expand Up @@ -232,12 +235,13 @@ public open class ComposeNavigator :
newBackStack: List<Navigable<*>>,
) {
val oldNavigables = oldBackStack.toSet()
// Don't remove the last Navigable (from) until it's removed from composition in DisposedEffect
val newNavigables = newBackStack.toSet()

(oldNavigables - newNavigables).forEach { oldNavigable ->
val isStarted =
oldNavigable is LifecycleOwner && oldNavigable.currentState >= LifecycleState.Started

// Don't remove the last Navigable (from) until it's removed from composition in DisposedEffect
if (!isStarted) {
lifecycleRegistry.removeFromLifecycle(oldNavigable)
}
Expand Down
Loading