-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added support to have navigator scoped screen model
- Loading branch information
Thiago dos Santos
authored and
Thiago dos Santos
committed
Sep 23, 2023
1 parent
5e1cd06
commit 1e3bfb0
Showing
5 changed files
with
102 additions
and
0 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
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
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
64 changes: 64 additions & 0 deletions
64
...tor/src/commonMain/kotlin/cafe/adriel/voyager/navigator/lifecycle/NavigatorScreenModel.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,64 @@ | ||
package cafe.adriel.voyager.navigator.lifecycle | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisallowComposableCalls | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import cafe.adriel.voyager.core.model.ScreenModel | ||
import cafe.adriel.voyager.core.model.getScreenModel | ||
import cafe.adriel.voyager.core.model.rememberScreenModel | ||
import cafe.adriel.voyager.core.screen.Screen | ||
import cafe.adriel.voyager.navigator.LocalNavigator | ||
import cafe.adriel.voyager.navigator.Navigator | ||
import cafe.adriel.voyager.navigator.currentOrThrow | ||
import cafe.adriel.voyager.navigator.screen.NavigatorScreen | ||
|
||
/** | ||
* Lookup for a [ScreenModel] that was remembered in the current [LocalNavigator] or in your parents. | ||
* If an instance was not found, an [IllegalStateException] will be thrown | ||
* | ||
* @param tag A custom tag used to "tag" the [ScreenModel] | ||
* @throws IllegalStateException if now [ScreenModel] was found in [LocalNavigator] or in your parents | ||
* | ||
* @return The [ScreenModel] instance found | ||
*/ | ||
@Suppress("UnusedReceiverParameter") | ||
@Throws(IllegalStateException::class) | ||
@Composable | ||
public inline fun <reified T : ScreenModel> Screen.navigatorScreenModel( | ||
tag: String? = null | ||
): T { | ||
val navigator = LocalNavigator.currentOrThrow | ||
var screenModel: T? by remember(tag) { mutableStateOf(null) } | ||
if (screenModel == null) { | ||
screenModel = generateSequence(seed = navigator) { it.parent } | ||
.mapNotNull { nav -> emptyScreen(nav).getScreenModel(tag) as? T } | ||
.firstOrNull() | ||
} | ||
return checkNotNull(screenModel) { | ||
"${T::class} was not found in $navigator and it parents" | ||
} | ||
} | ||
|
||
/** | ||
* Remember a [ScreenModel] that will be scoped to current [LocalNavigator] | ||
* | ||
* @param tag A custom tag used to "tag" the [ScreenModel] | ||
* @param factory A function to create a new instance if one is not remembered yet | ||
* | ||
* @return The [ScreenModel] instance | ||
*/ | ||
@Suppress("UnusedReceiverParameter") | ||
@Composable | ||
public inline fun <reified T : ScreenModel> Screen.rememberNavigatorScreenModel( | ||
tag: String? = null, | ||
crossinline factory: @DisallowComposableCalls () -> T | ||
): T { | ||
val navigator = LocalNavigator.currentOrThrow | ||
val screen = emptyScreen(navigator) | ||
return screen.rememberScreenModel(tag, factory) | ||
} | ||
|
||
public fun emptyScreen(navigator: Navigator): Screen = NavigatorScreen(navigator) |
23 changes: 23 additions & 0 deletions
23
...r-navigator/src/commonMain/kotlin/cafe/adriel/voyager/navigator/screen/NavigatorScreen.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,23 @@ | ||
package cafe.adriel.voyager.navigator.screen | ||
|
||
import androidx.compose.runtime.Composable | ||
import cafe.adriel.voyager.core.screen.Screen | ||
import cafe.adriel.voyager.core.screen.ScreenKey | ||
import cafe.adriel.voyager.navigator.Navigator | ||
|
||
/** | ||
* An internal screen to be used as "key" to persist a [cafe.adriel.voyager.core.model.ScreenModel] | ||
* | ||
* @param navigator The current [Navigator] to be scoped | ||
*/ | ||
internal data class NavigatorScreen( | ||
private val navigator: Navigator | ||
) : Screen { | ||
|
||
override val key: ScreenKey = "$navigator" | ||
|
||
@Composable | ||
override fun Content() { | ||
error("Calling NavigatorScreen Content() fun is an error") | ||
} | ||
} |