-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Multiple ScreenLifecycleOwner support for Screen
- Loading branch information
1 parent
94bf561
commit 5deb781
Showing
5 changed files
with
117 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 9 additions & 67 deletions
76
...ger-core/src/commonMain/kotlin/cafe/adriel/voyager/core/lifecycle/ScreenLifecycleOwner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,29 @@ | ||
package cafe.adriel.voyager.core.lifecycle | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import cafe.adriel.voyager.core.annotation.ExperimentalVoyagerApi | ||
import cafe.adriel.voyager.core.annotation.InternalVoyagerApi | ||
import cafe.adriel.voyager.core.screen.Screen | ||
|
||
public interface ScreenLifecycleOwner { | ||
|
||
/** | ||
* Called before rendering the Screen Content. | ||
* | ||
* IMPORTANT: This is only called when ScreenLifecycleOwner is provided by [ScreenLifecycleProvider] or [NavigatorScreenLifecycleProvider]. | ||
*/ | ||
@Composable | ||
public fun ProvideBeforeScreenContent( | ||
provideSaveableState: @Composable (suffixKey: String, content: @Composable () -> Unit) -> Unit, | ||
content: @Composable () -> Unit | ||
): Unit = content() | ||
|
||
/** | ||
* Called on the Screen leaves the stack. | ||
*/ | ||
public fun onDispose(screen: Screen) {} | ||
} | ||
|
||
@InternalVoyagerApi | ||
public object DefaultScreenLifecycleOwner : ScreenLifecycleOwner | ||
|
||
@ExperimentalVoyagerApi | ||
@InternalVoyagerApi | ||
public class ComposedScreenLifecycleOwner( | ||
private val screenLifecycleOwners: List<ScreenLifecycleOwner> | ||
) : ScreenLifecycleOwner { | ||
|
||
@Composable | ||
public fun RecursiveProvideBeforeScreenContent( | ||
screenLifecycleOwner: ScreenLifecycleOwner, | ||
provideSaveableState: @Composable (suffixKey: String, content: @Composable () -> Unit) -> Unit, | ||
content: @Composable () -> Unit, | ||
nextOrNull: () -> ScreenLifecycleOwner?, | ||
) { | ||
val next = remember(screenLifecycleOwner, provideSaveableState, content, nextOrNull) { nextOrNull() } | ||
if(next != null) { | ||
val recursiveContent = @Composable { | ||
RecursiveProvideBeforeScreenContent( | ||
screenLifecycleOwner = next, | ||
provideSaveableState = provideSaveableState, | ||
content = content, | ||
nextOrNull = nextOrNull, | ||
) | ||
} | ||
screenLifecycleOwner.ProvideBeforeScreenContent( | ||
provideSaveableState = { suffixKey, _ -> | ||
provideSaveableState(suffixKey, recursiveContent) | ||
} | ||
) { | ||
recursiveContent() | ||
} | ||
|
||
} else { | ||
screenLifecycleOwner.ProvideBeforeScreenContent( | ||
provideSaveableState = { suffixKey, content -> | ||
provideSaveableState(suffixKey, content) | ||
} | ||
) { | ||
content() | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
override fun ProvideBeforeScreenContent( | ||
provideSaveableState: @Composable (suffixKey: String, content: @Composable () -> Unit) -> Unit, | ||
content: @Composable () -> Unit | ||
) { | ||
if(screenLifecycleOwners.isNotEmpty()) { | ||
val copy = screenLifecycleOwners.toMutableList() | ||
RecursiveProvideBeforeScreenContent( | ||
screenLifecycleOwner = copy.removeFirst(), | ||
provideSaveableState = provideSaveableState, | ||
content = content, | ||
nextOrNull = { copy.removeFirstOrNull() } | ||
) | ||
} else { | ||
content() | ||
} | ||
} | ||
|
||
override fun onDispose(screen: Screen) { | ||
for (screenLifecycleOwner in screenLifecycleOwners) { | ||
screenLifecycleOwner.onDispose(screen) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
.../commonMain/kotlin/cafe/adriel/voyager/core/lifecycle/multipleScreenLifecycleOwnerUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package cafe.adriel.voyager.core.lifecycle | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import cafe.adriel.voyager.core.annotation.ExperimentalVoyagerApi | ||
import cafe.adriel.voyager.core.annotation.InternalVoyagerApi | ||
|
||
@ExperimentalVoyagerApi | ||
@InternalVoyagerApi | ||
@Composable | ||
public fun MultipleProvideBeforeScreenContent( | ||
screenLifecycleOwners: List<ScreenLifecycleOwner>, | ||
provideSaveableState: @Composable (suffixKey: String, content: @Composable () -> Unit) -> Unit, | ||
content: @Composable () -> Unit, | ||
) { | ||
if(screenLifecycleOwners.isNotEmpty()) { | ||
val copy = screenLifecycleOwners.toMutableList() | ||
RecursiveProvideBeforeScreenContent( | ||
screenLifecycleOwner = copy.removeFirst(), | ||
provideSaveableState = provideSaveableState, | ||
content = content, | ||
nextOrNull = { copy.removeFirstOrNull() } | ||
) | ||
} else { | ||
content() | ||
} | ||
} | ||
|
||
@Composable | ||
private fun RecursiveProvideBeforeScreenContent( | ||
screenLifecycleOwner: ScreenLifecycleOwner, | ||
provideSaveableState: @Composable (suffixKey: String, content: @Composable () -> Unit) -> Unit, | ||
content: @Composable () -> Unit, | ||
nextOrNull: () -> ScreenLifecycleOwner?, | ||
) { | ||
val next = remember(screenLifecycleOwner, provideSaveableState, content, nextOrNull) { nextOrNull() } | ||
if(next != null) { | ||
val recursiveContent = @Composable { | ||
RecursiveProvideBeforeScreenContent( | ||
screenLifecycleOwner = next, | ||
provideSaveableState = provideSaveableState, | ||
content = content, | ||
nextOrNull = nextOrNull, | ||
) | ||
} | ||
screenLifecycleOwner.ProvideBeforeScreenContent( | ||
provideSaveableState = { suffixKey, _ -> | ||
provideSaveableState(suffixKey, recursiveContent) | ||
} | ||
) { | ||
recursiveContent() | ||
} | ||
|
||
} else { | ||
screenLifecycleOwner.ProvideBeforeScreenContent( | ||
provideSaveableState = { suffixKey, content -> | ||
provideSaveableState(suffixKey, content) | ||
} | ||
) { | ||
content() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters