Skip to content

Commit

Permalink
Add routeOnCreate lambda for better restration
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergej Shafarenka committed Oct 5, 2024
1 parent 1cfe2a8 commit 81e2edf
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ internal class AndroidRestorator(
}
}

override val canRestore: Boolean
get() = delegate.canRestore

override fun restoreRoute(): ByteArray? {
val bytes = delegate.restoreRoute()
return bytes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,6 +29,8 @@ private class DefaultRestorator(
}
)

override val canRestore: Boolean = bytes != null

override fun restoreRoute(): ByteArray? =
consumable.consume()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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?,
Expand All @@ -85,4 +120,4 @@ private fun assertByteArrayEquals(
}?.removeSuffix(", ")

assertEquals(expectedList, actualList, message)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ plugins {

allprojects {
group = "de.halfbit"
version = "0.6"
version = "0.7"
}

0 comments on commit 81e2edf

Please sign in to comment.