diff --git a/componental/src/androidMain/kotlin/de/halfbit/componental/android/AndroidRestorator.kt b/componental/src/androidMain/kotlin/de/halfbit/componental/android/AndroidRestorator.kt index 4443edd..ef24e41 100644 --- a/componental/src/androidMain/kotlin/de/halfbit/componental/android/AndroidRestorator.kt +++ b/componental/src/androidMain/kotlin/de/halfbit/componental/android/AndroidRestorator.kt @@ -29,6 +29,9 @@ internal class AndroidRestorator( } } + override val canRestore: Boolean + get() = delegate.canRestore + override fun restoreRoute(): ByteArray? { val bytes = delegate.restoreRoute() return bytes diff --git a/componental/src/commonMain/kotlin/de/halfbit/componental/restorator/Restorator.kt b/componental/src/commonMain/kotlin/de/halfbit/componental/restorator/Restorator.kt index 5596d6a..0f751e2 100644 --- a/componental/src/commonMain/kotlin/de/halfbit/componental/restorator/Restorator.kt +++ b/componental/src/commonMain/kotlin/de/halfbit/componental/restorator/Restorator.kt @@ -9,6 +9,7 @@ public fun Restorator(bytes: ByteArray?): Restorator = DefaultRestorator(bytes) public interface Restorator { + public val canRestore: Boolean public fun restoreRoute(): ByteArray? public fun storeRoute(block: () -> ByteArray?) public fun storeAll(): ByteArray @@ -28,6 +29,8 @@ private class DefaultRestorator( } ) + override val canRestore: Boolean = bytes != null + override fun restoreRoute(): ByteArray? = consumable.consume() diff --git a/componental/src/commonMain/kotlin/de/halfbit/componental/restorator/RestoratorOwner.kt b/componental/src/commonMain/kotlin/de/halfbit/componental/restorator/RestoratorOwner.kt index 6529dd1..08f7806 100644 --- a/componental/src/commonMain/kotlin/de/halfbit/componental/restorator/RestoratorOwner.kt +++ b/componental/src/commonMain/kotlin/de/halfbit/componental/restorator/RestoratorOwner.kt @@ -3,3 +3,30 @@ package de.halfbit.componental.restorator public interface RestoratorOwner { public val restorator: Restorator } + +/** + * Wrap initial screen routing code with this builder function to ensure + * it does not interfere with the state restoration. + * + * In the example below the code in lambda will only be executed if the screen + * is created. If the screen is restored (created during the restoration process), + * the code in lambda will not be executed. + * + * ``` + * init { + * routeOnCreate { + * val agent = findAgentById(agentId) + * when { + * agent?.authData == null -> stackRouter.replace(Route.Login) + * agent.settings.name.isEmpty() -> stackRouter.replace(Route.RegisterAgent) + * else -> stackRouter.replace(Route.Order) + * } + * } + * } + * ``` + */ +public inline fun RestoratorOwner.routeOnCreate(block: () -> Unit) { + if (!restorator.canRestore) { + block() + } +} diff --git a/componental/src/commonTest/kotlin/de/halfbit/componental/restorator/RestoratorTest.kt b/componental/src/commonTest/kotlin/de/halfbit/componental/restorator/RestoratorTest.kt index 82bcb8f..462ffc7 100644 --- a/componental/src/commonTest/kotlin/de/halfbit/componental/restorator/RestoratorTest.kt +++ b/componental/src/commonTest/kotlin/de/halfbit/componental/restorator/RestoratorTest.kt @@ -1,9 +1,6 @@ package de.halfbit.componental.restorator -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertNull -import kotlin.test.assertTrue +import kotlin.test.* class RestoratorTest { @@ -35,6 +32,37 @@ class RestoratorTest { assertNull(routeByte) } + @Test + fun `routeOnCreate is not called when restoring`() { + // prepare + val restorator = Restorator(null) + restorator.storeRoute { null } + val bytes = restorator.storeAll() + + // test + val restorator2 = Restorator(bytes) + var called = false + + restoratorOwner(restorator2) + .routeOnCreate { called = true } + + // assert + assertFalse(called) + } + + @Test + fun `routeOnCreate is called when not restoring`() { + // test + val restorator = Restorator(null) + var called = false + + restoratorOwner(restorator) + .routeOnCreate { called = true } + + // assert + assertTrue(called) + } + @Test fun `restore can restore multiple values`() { // prepare @@ -71,6 +99,13 @@ class RestoratorTest { } } +private fun restoratorOwner( + restorator: Restorator +): RestoratorOwner = object : RestoratorOwner { + override val restorator: Restorator + get() = restorator +} + private fun assertByteArrayEquals( expected: ByteArray?, actual: ByteArray?, @@ -85,4 +120,4 @@ private fun assertByteArrayEquals( }?.removeSuffix(", ") assertEquals(expectedList, actualList, message) -} \ No newline at end of file +} diff --git a/convention-plugins/src/main/kotlin/root.publication.gradle.kts b/convention-plugins/src/main/kotlin/root.publication.gradle.kts index 80c24a3..2196323 100644 --- a/convention-plugins/src/main/kotlin/root.publication.gradle.kts +++ b/convention-plugins/src/main/kotlin/root.publication.gradle.kts @@ -4,5 +4,5 @@ plugins { allprojects { group = "de.halfbit" - version = "0.6" + version = "0.7" }