-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
107 additions
and
9 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
...mpose/src/commonMain/kotlin/com.chrynan.navigation.compose/NavigationStateComposeUtils.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,15 @@ | ||
package com.chrynan.navigation.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.State | ||
import androidx.compose.runtime.collectAsState | ||
import com.chrynan.navigation.NavigationState | ||
import kotlinx.coroutines.flow.StateFlow | ||
|
||
/** | ||
* Converts the [NavigationState.changes] of this [NavigationState] to a Jetpack Compose [State] so that every change | ||
* causes a recomposition of the calling [Composable] function. | ||
*/ | ||
@Composable | ||
fun <T> NavigationState<T>.collectAsState(): State<T> = | ||
(this.changes as? StateFlow<T>)?.collectAsState() ?: this.changes.collectAsState(initial = this.initial) |
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
79 changes: 79 additions & 0 deletions
79
navigation-core/src/commonMain/kotlin/com.chrynan.navigation/NavigationStateStore.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,79 @@ | ||
@file:Suppress("unused") | ||
|
||
package com.chrynan.navigation | ||
|
||
/** | ||
* Represents a store of navigation state information that is useful for a [Navigator]. | ||
*/ | ||
interface NavigationStateStore<Destination : NavigationDestination, Context : NavigationContext<Destination>> { | ||
|
||
/** | ||
* A [NavigationState] of [NavigationEvent]s. | ||
*/ | ||
val event: NavigationState<NavigationEvent<Destination, Context>?> | ||
|
||
/** | ||
* A [NavigationState] of [NavigationDestination]s. | ||
*/ | ||
val destination: NavigationState<Destination> | ||
|
||
/** | ||
* A [NavigationState] of [NavigationContext]s. | ||
*/ | ||
val context: NavigationState<Context> | ||
} | ||
|
||
/** | ||
* A mutable version of a [NavigationStateStore]. | ||
*/ | ||
internal interface MutableNavigationStateStore<Destination : NavigationDestination, Context : NavigationContext<Destination>> : | ||
NavigationStateStore<Destination, Context> { | ||
|
||
/** | ||
* Dispatches the provided navigation [event] which mutates the underlying state values. | ||
*/ | ||
fun dispatch(event: NavigationEvent<Destination, Context>) | ||
|
||
/** | ||
* Resets the underlying state values back to their initial values. | ||
*/ | ||
fun reset() | ||
} | ||
|
||
/** | ||
* Creates a [MutableNavigationStateStore] instance with the provided [initialContext] value. | ||
*/ | ||
internal fun <Destination : NavigationDestination, Context : NavigationContext<Destination>> mutableNavigationStateStoreOf( | ||
initialContext: Context | ||
): MutableNavigationStateStore<Destination, Context> = | ||
MapBasedMutableNavigationStateStore(initialContext = initialContext) | ||
|
||
/** | ||
* A [MutableNavigationStateStore] implementation that stores [NavigationContext]s and their associated | ||
* [NavigationDestination] [Stack]s in an in-memory [Map]. | ||
*/ | ||
internal class MapBasedMutableNavigationStateStore<Destination : NavigationDestination, Context : NavigationContext<Destination>> internal constructor( | ||
initialContext: Context | ||
) : MutableNavigationStateStore<Destination, Context> { | ||
|
||
override val event = mutableNavigationStateOf<NavigationEvent<Destination, Context>?>(initial = null) | ||
override val destination = mutableNavigationStateOf(initial = initialContext.initialDestination) | ||
override val context = mutableNavigationStateOf(initial = initialContext) | ||
|
||
private val map = mutableMapOf(initialContext to mutableStackOf(initialContext.initialDestination)) | ||
|
||
override fun dispatch(event: NavigationEvent<Destination, Context>) { | ||
this.event.update(event) | ||
|
||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun reset() { | ||
event.reset() | ||
destination.reset() | ||
context.reset() | ||
|
||
map.clear() | ||
map[context.initial] = mutableStackOf(context.initial.initialDestination) | ||
} | ||
} |