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

Added children convenience method with Parcelable navigation state #279

Merged
merged 1 commit into from
Nov 27, 2022
Merged
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 @@ -12,6 +12,7 @@ import com.arkivanov.essenty.lifecycle.doOnDestroy
import com.arkivanov.essenty.parcelable.Parcelable
import com.arkivanov.essenty.parcelable.ParcelableContainer
import com.arkivanov.essenty.parcelable.Parcelize
import com.arkivanov.essenty.parcelable.consumeRequired
import com.arkivanov.essenty.statekeeper.consume

/**
Expand Down Expand Up @@ -133,6 +134,35 @@ fun <C : Any, T : Any, E : Any, N : NavState<C>, S : Any> ComponentContext.child
return state
}

/**
* A convenience method for the main [children] method. Allows having [Parcelable] navigation state [N],
* so it's automatically saved and restored. This method can be used if the custom save/restore logic
* is not required.
*/
@ExperimentalDecomposeApi
inline fun <C : Parcelable, T : Any, E : Any, reified N, S : Any> ComponentContext.children(
source: NavigationSource<E>,
key: String,
noinline initialNavState: () -> N,
noinline navTransformer: (navState: N, event: E) -> N,
noinline onEventComplete: (event: E, newNavState: N, oldNavState: N) -> Unit,
noinline backTransformer: (navState: N) -> (() -> N)?,
noinline stateMapper: (navState: N, children: List<Child<C, T>>) -> S,
noinline childFactory: (configuration: C, componentContext: ComponentContext) -> T,
): Value<S> where N : NavState<C>, N : Parcelable =
children(
source = source,
key = key,
initialNavState = initialNavState,
saveNavState = { ParcelableContainer(it) },
restoreNavState = { it.consumeRequired(N::class) },
navTransformer = navTransformer,
onEventComplete = onEventComplete,
backTransformer = backTransformer,
stateMapper = stateMapper,
childFactory = childFactory,
)

@Parcelize
private class SavedState(
val navState: ParcelableContainer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import com.arkivanov.decompose.router.children.SimpleNavigation
import com.arkivanov.decompose.router.children.children
import com.arkivanov.decompose.value.Value
import com.arkivanov.essenty.parcelable.Parcelable
import com.arkivanov.essenty.parcelable.ParcelableContainer
import com.arkivanov.essenty.parcelable.Parcelize
import com.arkivanov.essenty.parcelable.consumeRequired
import com.arkivanov.sample.shared.customnavigation.CustomNavigationComponent.Children
import com.arkivanov.sample.shared.customnavigation.CustomNavigationComponent.Mode
import com.arkivanov.sample.shared.customnavigation.KittenComponent.ImageType
Expand All @@ -24,8 +22,8 @@ class DefaultCustomNavigationComponent(

private val navigation = SimpleNavigation<(NavigationState) -> NavigationState>()

private val _children =
children<Config, KittenComponent, (NavigationState) -> NavigationState, NavigationState, Children<Config, KittenComponent>>(
private val _children: Value<Children<Config, KittenComponent>> =
children(
source = navigation,
key = "carousel",
initialNavState = {
Expand All @@ -37,8 +35,6 @@ class DefaultCustomNavigationComponent(
mode = Mode.CAROUSEL,
)
},
saveNavState = ::ParcelableContainer,
restoreNavState = { it.consumeRequired(NavigationState::class) },
navTransformer = { navState, transformer -> transformer(navState) },
onEventComplete = { _, _, _ -> },
backTransformer = {
Expand Down