Skip to content

Commit

Permalink
Refactor visibleBackstack to renderGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
roudikk committed Jan 30, 2023
1 parent 008f199 commit b0aab97
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private fun Navigator.NavContainerContent(
) = Box(modifier = modifier) {
val canGoBack by navigator.canGoBack()
val lifecycleManager = rememberDefaultLifecycleManager(navigator = navigator)
val visibleBackstack = lifecycleManager.renderGroup
val renderGroup = lifecycleManager.renderGroup

val backEnabled by remember(canGoBack, navigator.overrideBackPress) {
derivedStateOf { canGoBack && navigator.overrideBackPress }
Expand All @@ -80,7 +80,7 @@ private fun Navigator.NavContainerContent(

// Screen content
ScreenContainer(
screenEntry = visibleBackstack.screenEntry
screenEntry = renderGroup.screenEntry
) { entry ->
NavEntryContainer(
lifecycleManager = lifecycleManager,
Expand All @@ -90,7 +90,7 @@ private fun Navigator.NavContainerContent(

// Bottom sheet content
BottomSheetContainer(
bottomSheetEntry = visibleBackstack.bottomSheetEntry,
bottomSheetEntry = renderGroup.bottomSheetEntry,
bottomSheetScrimColor = bottomSheetScrimColor,
container = bottomSheetContainer,
) { entry ->
Expand All @@ -106,7 +106,7 @@ private fun Navigator.NavContainerContent(

// Dialog content
DialogContainer(
dialogEntry = visibleBackstack.dialogEntry,
dialogEntry = renderGroup.dialogEntry,
container = dialogContainer,
) { entry ->
NavEntryContainer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fun rememberDefaultLifecycleManager(
entriesAfter.all { entry -> navigator.navigationNode(entry) is Dialog }
}?.let(createLifecycleEntry)

val visibleBackstack = DefaultRenderGroup(
val renderGroup = DefaultRenderGroup(
screenEntry = screenEntry,
dialogEntry = dialogEntry,
bottomSheetEntry = bottomSheetEntry
Expand All @@ -67,7 +67,7 @@ fun rememberDefaultLifecycleManager(
backstack.getOrNull(backstack.lastIndex - 1)
?.let(navigator::navigationNode) !is BottomSheet

visibleBackstack.entries.forEach {
renderGroup.entries.forEach {
// If the current destination is a bottom sheet or a dialog
// we need to pause whatever is behind it. In the case of a dialog
// we might have a bottom sheet and/or a screen behind it, whereas a bottom sheet
Expand All @@ -83,22 +83,22 @@ fun rememberDefaultLifecycleManager(
}
}

visibleBackstack
renderGroup
},
/**
* Make sure all entries' lifecycle is up to date.
*
* All entries that are not in the current visibleBackstack will be in the destroyed state.
* All entries that are not in the current renderGroup will be in the destroyed state.
*
* We then check the entries that are in the visibleBackstack:
* We then check the entries that are in the render group:
* - If the entry is the current last entry in the [Navigator] backstack, it's resumed.
* - If the entry is not the current last entry, then it's paused.
*/
updateLifecycles = { visibleBackstack, lifeCycleEntries ->
lifeCycleEntries.filter { it !in visibleBackstack.entries }
updateLifecycles = { renderGroup, lifeCycleEntries ->
lifeCycleEntries.filter { it !in renderGroup.entries }
.forEach { it.maxLifecycleState = Lifecycle.State.CREATED }

visibleBackstack.entries.forEach {
renderGroup.entries.forEach {
if (it.id == navigator.backstack.lastOrNull()?.id) {
it.maxLifecycleState = Lifecycle.State.RESUMED
} else {
Expand Down
15 changes: 11 additions & 4 deletions guia/src/main/java/com/roudikk/guia/lifecycle/LifecycleManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,23 @@ class LifecycleManager<RG : RenderGroup> internal constructor(
private fun savedStateKey(id: String) = "back-stack-manager-$id"
}

fun interface GetRenderGroup<VB : RenderGroup> {
/**
* Generates a [RenderGroup] given a [Navigator]'s current backstack.
*/
fun interface GetRenderGroup<RG : RenderGroup> {
operator fun invoke(
backstack: List<BackstackEntry>,
createEntry: (BackstackEntry) -> LifecycleEntry
): VB
): RG
}

fun interface UpdateLifecycles<VB : RenderGroup> {
/**
* Updates the lifecycle of a [LifecycleManager]'s current lifecycle entries given the
* current [RenderGroup].
*/
fun interface UpdateLifecycles<RG : RenderGroup> {
operator fun invoke(
visibleBackstack: VB,
renderGroup: RG,
lifecycleEntries: List<LifecycleEntry>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ internal fun rememberCardLifecycleManager(navigator: Navigator): LifecycleManage
.map(createEntry)
)
},
updateLifecycles = { visibleBackstack, entries ->
entries.filter { it !in visibleBackstack.entries }
updateLifecycles = { renderGroup, entries ->
entries.filter { it !in renderGroup.entries }
.forEach { it.maxLifecycleState = Lifecycle.State.CREATED }

visibleBackstack.entries.forEach {
renderGroup.entries.forEach {
if (it.id == navigator.backstack.first().id) {
it.maxLifecycleState = Lifecycle.State.RESUMED
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ fun rememberViewPagerLifecycleManager(navigator: Navigator): LifecycleManager<Vi
right = right?.let(createEntry)
)
},
updateLifecycles = { visibleBackstack, entries ->
entries.filter { it !in visibleBackstack.entries }
updateLifecycles = { renderGroup, entries ->
entries.filter { it !in renderGroup.entries }
.forEach { it.maxLifecycleState = Lifecycle.State.CREATED }

visibleBackstack.entries.forEach {
if (it.id == visibleBackstack.center?.id) {
renderGroup.entries.forEach {
if (it.id == renderGroup.center?.id) {
it.maxLifecycleState = Lifecycle.State.RESUMED
} else {
it.maxLifecycleState = Lifecycle.State.STARTED
Expand Down

0 comments on commit b0aab97

Please sign in to comment.