Skip to content

Commit

Permalink
PmDescription and PmParams replaced with serializable arguments (PmAr…
Browse files Browse the repository at this point in the history
…gs).
  • Loading branch information
dmdevgo committed Jan 10, 2024
1 parent 606f486 commit 04eef57
Show file tree
Hide file tree
Showing 58 changed files with 495 additions and 550 deletions.
1 change: 1 addition & 0 deletions libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ kotlin-test = { group = "org.jetbrains.kotlin", name = "kotlin-test", version.re
kotlinx-coroutines-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "coroutines" }
kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "coroutines" }
kotlinx-coroutines-swing = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-swing", version.ref = "coroutines" }
kotlinx-serialization-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-core", version.ref = "kotlinx-serialization" }
kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
kotlinx-serialization-protobuf = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-protobuf", version.ref = "kotlinx-serialization" }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020-2023 Dmitriy Gorbunov ([email protected])
* Copyright (c) 2020-2024 Dmitriy Gorbunov ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -74,10 +74,10 @@ internal class DialogGroupNavigatorImpl(
hostPm.SaveableFlow(
key = key,
initialValueProvider = { listOf() },
saveTypeMapper = { dialogs -> dialogs.map { it.description } },
restoreTypeMapper = { descriptions ->
descriptions.mapNotNull { description ->
dialogNavigators.find { it.dialog?.description == description }?.dialog
saveTypeMapper = { dialogs -> dialogs.map { it.pmArgs } },
restoreTypeMapper = { pmArgsList ->
pmArgsList.mapNotNull { pmArgs ->
dialogNavigators.find { it.dialog?.pmArgs == pmArgs }?.dialog
}
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020-2023 Dmitriy Gorbunov ([email protected])
* Copyright (c) 2020-2024 Dmitriy Gorbunov ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -91,7 +91,7 @@ internal class DialogNavigatorImpl<D : PresentationModel, R : PmMessage>(
private val _dialog: MutableStateFlow<D?> = hostPm.SaveableFlow(
key = key,
initialValueProvider = { null },
saveTypeMapper = { it?.description },
saveTypeMapper = { it?.pmArgs },
restoreTypeMapper = { it?.let { hostPm.AttachedChild(it) as D } }
)
override val dialogFlow: StateFlow<D?> = _dialog.asStateFlow()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020-2023 Dmitriy Gorbunov ([email protected])
* Copyright (c) 2020-2024 Dmitriy Gorbunov ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,7 +25,6 @@
package me.dmdev.premo.navigation

import kotlinx.coroutines.flow.StateFlow
import me.dmdev.premo.PmDescription
import me.dmdev.premo.PmMessageHandler
import me.dmdev.premo.PresentationModel

Expand All @@ -39,13 +38,13 @@ interface MasterDetailNavigation<M, D>
}

fun <M : PresentationModel, D : PresentationModel> PresentationModel.MasterDetailNavigation(
masterDescription: PmDescription,
masterPm: M,
key: String = DEFAULT_MASTER_DETAIL_NAVIGATOR_KEY,
backHandler: (MasterDetailNavigator<M, D>) -> Boolean = DEFAULT_MASTER_DETAIL_NAVIGATOR_BACK_HANDLER,
initHandlers: PmMessageHandler.(navigator: MasterDetailNavigator<M, D>) -> Unit = {}
): MasterDetailNavigation<M, D> {
return MasterDetailNavigator(
masterDescription = masterDescription,
masterPm = masterPm,
key = key,
backHandler = backHandler,
initHandlers = initHandlers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020-2023 Dmitriy Gorbunov ([email protected])
* Copyright (c) 2020-2024 Dmitriy Gorbunov ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -28,7 +28,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import me.dmdev.premo.AttachedChild
import me.dmdev.premo.PmDescription
import me.dmdev.premo.PmArgs
import me.dmdev.premo.PmMessageHandler
import me.dmdev.premo.PresentationModel
import me.dmdev.premo.SaveableFlow
Expand Down Expand Up @@ -57,14 +57,14 @@ fun MasterDetailNavigator<*, *>.handleBack(): Boolean {
}

fun <M : PresentationModel, D : PresentationModel> PresentationModel.MasterDetailNavigator(
masterDescription: PmDescription,
masterPm: M,
key: String = DEFAULT_MASTER_DETAIL_NAVIGATOR_KEY,
backHandler: (MasterDetailNavigator<M, D>) -> Boolean = DEFAULT_MASTER_DETAIL_NAVIGATOR_BACK_HANDLER,
initHandlers: PmMessageHandler.(navigator: MasterDetailNavigator<M, D>) -> Unit = {}
): MasterDetailNavigator<M, D> {
val navigator = MasterDetailNavigatorImpl<M, D>(
hostPm = this,
masterDescription = masterDescription,
masterPm = masterPm,
key = key
)
messageHandler.handle<BackMessage> { backHandler.invoke(navigator) }
Expand All @@ -80,19 +80,23 @@ internal val DEFAULT_MASTER_DETAIL_NAVIGATOR_BACK_HANDLER: (MasterDetailNavigato

internal class MasterDetailNavigatorImpl<M, D>(
private val hostPm: PresentationModel,
masterDescription: PmDescription,
masterPm: M,
key: String
) : MasterDetailNavigator<M, D>
where M : PresentationModel,
D : PresentationModel {

override val master: M = hostPm.AttachedChild(masterDescription)
override val master: M = masterPm

init {
masterPm.attachToParent()
}

private val _detailFlow: MutableStateFlow<D?> = hostPm.SaveableFlow(
key = "${key}_detail_pm",
initialValueProvider = { null },
saveType = typeOf<PmDescription>(),
saveTypeMapper = { it?.description },
saveType = typeOf<PmArgs>(),
saveTypeMapper = { it?.pmArgs },
restoreTypeMapper = { it?.let { hostPm.AttachedChild(it) as D } }
)
override val detailFlow: StateFlow<D?> = _detailFlow.asStateFlow()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020-2023 Dmitriy Gorbunov ([email protected])
* Copyright (c) 2020-2024 Dmitriy Gorbunov ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,7 +25,6 @@
package me.dmdev.premo.navigation

import kotlinx.coroutines.flow.StateFlow
import me.dmdev.premo.PmDescription
import me.dmdev.premo.PresentationModel

interface SetNavigation {
Expand All @@ -38,13 +37,13 @@ interface SetNavigation {
}

fun PresentationModel.SetNavigation(
vararg initialDescriptions: PmDescription,
initValues: () -> List<PresentationModel>,
key: String = DEFAULT_SET_NAVIGATOR_KEY,
backHandler: (SetNavigator) -> Boolean = DEFAULT_SET_NAVIGATOR_BACK_HANDLER,
onChangeCurrent: (index: Int, navigator: SetNavigator) -> Unit = DEFAULT_SET_NAVIGATOR_ON_CHANGE_CURRENT
): SetNavigation {
return SetNavigator(
initialDescriptions = initialDescriptions,
initValues = initValues,
key = key,
backHandler = backHandler,
onChangeCurrent = onChangeCurrent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ package me.dmdev.premo.navigation
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import me.dmdev.premo.PmDescription
import me.dmdev.premo.PmLifecycle.State.CREATED
import me.dmdev.premo.PmLifecycle.State.DESTROYED
import me.dmdev.premo.PmLifecycle.State.IN_FOREGROUND
Expand All @@ -53,14 +52,14 @@ fun SetNavigator.handleBack(): Boolean {
}

fun PresentationModel.SetNavigator(
vararg initialDescriptions: PmDescription,
initValues: () -> List<PresentationModel>,
key: String = DEFAULT_SET_NAVIGATOR_KEY,
backHandler: (SetNavigator) -> Boolean = DEFAULT_SET_NAVIGATOR_BACK_HANDLER,
onChangeCurrent: (index: Int, navigator: SetNavigator) -> Unit = DEFAULT_SET_NAVIGATOR_ON_CHANGE_CURRENT
): SetNavigator {
val navigator = SetNavigatorImpl(
hostPm = this,
initialValues = initialDescriptions.asList(),
initValues = initValues,
key = key,
onChangeCurrent = onChangeCurrent
)
Expand All @@ -80,16 +79,16 @@ internal val DEFAULT_SET_NAVIGATOR_ON_CHANGE_CURRENT: (index: Int, navigator: Se

internal class SetNavigatorImpl(
private val hostPm: PresentationModel,
initialValues: List<PmDescription>,
initValues: () -> List<PresentationModel>,
key: String,
private val onChangeCurrent: (index: Int, navigator: SetNavigator) -> Unit
) : SetNavigator {

private val _valuesFlow: MutableStateFlow<List<PresentationModel>> = hostPm.SaveableFlow(
key = "${key}_values",
initialValueProvider = { initialValues.map { hostPm.Child(it) } },
saveTypeMapper = { values -> values.map { it.description } },
restoreTypeMapper = { descriptions -> descriptions.map { hostPm.Child(it) } }
initialValueProvider = { initValues() },
saveTypeMapper = { values -> values.map { it.pmArgs } },
restoreTypeMapper = { pmArgs -> pmArgs.map { hostPm.Child(it) } }
)
override val valuesFlow: StateFlow<List<PresentationModel>> = _valuesFlow.asStateFlow()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020-2023 Dmitriy Gorbunov ([email protected])
* Copyright (c) 2020-2024 Dmitriy Gorbunov ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -27,7 +27,6 @@ package me.dmdev.premo.navigation
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import me.dmdev.premo.ExperimentalPremoApi
import me.dmdev.premo.PmDescription
import me.dmdev.premo.PmMessageHandler
import me.dmdev.premo.PresentationModel

Expand All @@ -42,41 +41,13 @@ interface StackNavigation {
}

fun PresentationModel.StackNavigation(
initialDescription: PmDescription? = null,
initBackStack: () -> List<PresentationModel>,
key: String = DEFAULT_STACK_NAVIGATOR_KEY,
backHandler: (StackNavigator) -> Boolean = DEFAULT_STACK_NAVIGATOR_BACK_HANDLER,
initHandlers: PmMessageHandler.(navigator: StackNavigator) -> Unit = {}
): StackNavigation {
return StackNavigator(
initialBackStack = initialDescription?.let { listOf(it) } ?: listOf(),
key = key,
backHandler = backHandler,
initHandlers = initHandlers
)
}

fun PresentationModel.StackNavigation(
vararg initialDescriptions: PmDescription,
key: String = DEFAULT_STACK_NAVIGATOR_KEY,
backHandler: (StackNavigator) -> Boolean = DEFAULT_STACK_NAVIGATOR_BACK_HANDLER,
initHandlers: PmMessageHandler.(navigator: StackNavigator) -> Unit = {}
): StackNavigation {
return StackNavigator(
initialBackStack = initialDescriptions.asList(),
key = key,
backHandler = backHandler,
initHandlers = initHandlers
)
}

fun PresentationModel.StackNavigation(
initialBackStack: List<PmDescription>,
key: String = DEFAULT_STACK_NAVIGATOR_KEY,
backHandler: (StackNavigator) -> Boolean = DEFAULT_STACK_NAVIGATOR_BACK_HANDLER,
initHandlers: PmMessageHandler.(navigator: StackNavigator) -> Unit = {}
): StackNavigation {
return StackNavigator(
initialBackStack = initialBackStack,
initBackStack = initBackStack,
key = key,
backHandler = backHandler,
initHandlers = initHandlers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import me.dmdev.premo.ExperimentalPremoApi
import me.dmdev.premo.PmDescription
import me.dmdev.premo.PmLifecycle.State.CREATED
import me.dmdev.premo.PmLifecycle.State.DESTROYED
import me.dmdev.premo.PmLifecycle.State.IN_FOREGROUND
Expand Down Expand Up @@ -83,28 +82,14 @@ fun StackNavigator.handleBack(): Boolean {
}

fun PresentationModel.StackNavigator(
vararg initialDescriptions: PmDescription,
key: String = DEFAULT_STACK_NAVIGATOR_KEY,
backHandler: (StackNavigator) -> Boolean = DEFAULT_STACK_NAVIGATOR_BACK_HANDLER,
initHandlers: PmMessageHandler.(navigator: StackNavigator) -> Unit = {}
): StackNavigator {
return StackNavigator(
initialBackStack = initialDescriptions.asList(),
key = key,
backHandler = backHandler,
initHandlers = initHandlers
)
}

fun PresentationModel.StackNavigator(
initialBackStack: List<PmDescription>,
initBackStack: () -> List<PresentationModel> = { listOf() },
key: String = DEFAULT_STACK_NAVIGATOR_KEY,
backHandler: (StackNavigator) -> Boolean = DEFAULT_STACK_NAVIGATOR_BACK_HANDLER,
initHandlers: PmMessageHandler.(navigator: StackNavigator) -> Unit = {}
): StackNavigator {
val navigator = StackNavigatorImpl(
hostPm = this,
initialBackStack = initialBackStack,
initBackStack = initBackStack,
key = key
)
messageHandler.handle<BackMessage> { backHandler.invoke(navigator) }
Expand All @@ -119,16 +104,16 @@ internal val DEFAULT_STACK_NAVIGATOR_BACK_HANDLER: (StackNavigator) -> Boolean =

internal class StackNavigatorImpl(
private val hostPm: PresentationModel,
initialBackStack: List<PmDescription>,
initBackStack: () -> List<PresentationModel> = { listOf() },
key: String
) : StackNavigator {

private val _backStackFlow: MutableStateFlow<List<PresentationModel>> =
hostPm.stateHandler.SaveableFlow(
key = "${key}_backstack",
initialValueProvider = { initialBackStack.map { hostPm.Child(it) } },
saveTypeMapper = { backStack -> backStack.map { it.description } },
restoreTypeMapper = { descriptions -> descriptions.map { hostPm.Child(it) } }
initialValueProvider = { initBackStack() },
saveTypeMapper = { backStack -> backStack.map { it.pmArgs } },
restoreTypeMapper = { backStack -> backStack.map { hostPm.Child(it) } }
)

override val backStackFlow: StateFlow<List<PresentationModel>> = _backStackFlow
Expand Down
Loading

0 comments on commit 04eef57

Please sign in to comment.