-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced RetainedInstanceStore to keep data during configuration ch…
…ange
- Loading branch information
1 parent
7145c54
commit dcf4973
Showing
15 changed files
with
341 additions
and
26 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
...aries/core/src/androidTest/kotlin/com/bumble/appyx/core/node/RetainedInstanceStoreTest.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,62 @@ | ||
package com.bumble.appyx.core.node | ||
|
||
import androidx.lifecycle.Lifecycle | ||
import com.bumble.appyx.core.AppyxTestScenario | ||
import com.bumble.appyx.core.modality.BuildContext | ||
import com.bumble.appyx.core.store.RetainedInstanceStore | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import kotlin.reflect.KClass | ||
|
||
class RetainedInstanceStoreTest { | ||
private val stubRetainedInstanceStore = StubRetainedInstanceStore() | ||
|
||
@get:Rule | ||
val rule = AppyxTestScenario { buildContext -> | ||
TestParentNode(buildContext, stubRetainedInstanceStore) | ||
} | ||
|
||
@Test | ||
fun WHEN_activity_finished_THEN_retained_instance_store_content_is_removed() { | ||
rule.start() | ||
|
||
rule.activityScenario.moveToState(Lifecycle.State.DESTROYED) | ||
|
||
assertTrue(stubRetainedInstanceStore.removeAllCalled) | ||
} | ||
|
||
@Test | ||
fun WHEN_activity_recreated_THEN_retained_instance_store_content_is_not_removed() { | ||
rule.start() | ||
|
||
rule.activityScenario.recreate() | ||
|
||
assertFalse(stubRetainedInstanceStore.removeAllCalled) | ||
} | ||
|
||
class TestParentNode( | ||
buildContext: BuildContext, | ||
retainedInstanceStore: RetainedInstanceStore, | ||
) : Node( | ||
buildContext = buildContext, | ||
retainedInstanceStore = retainedInstanceStore, | ||
) | ||
|
||
class StubRetainedInstanceStore : RetainedInstanceStore { | ||
var removeAllCalled: Boolean = false | ||
|
||
override fun <T : Any> get( | ||
nodeId: String, | ||
clazz: KClass<*>, | ||
disposer: (T) -> Unit, | ||
factory: () -> T | ||
): T = RetainedInstanceStore.get(nodeId, clazz, disposer, factory) | ||
|
||
override fun removeAll(nodeId: String) { | ||
removeAllCalled = true | ||
RetainedInstanceStore.removeAll(nodeId) | ||
} | ||
} | ||
} |
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
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
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
37 changes: 37 additions & 0 deletions
37
libraries/core/src/main/kotlin/com/bumble/appyx/core/store/RetainedInstanceStore.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,37 @@ | ||
package com.bumble.appyx.core.store | ||
|
||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* A simple storage to preserve any objects during configuration change events. | ||
* `factory` function will be invoked immediately on the same thread | ||
* only if an object of the same class within the same Node does not exist. | ||
* | ||
* The framework will manage the lifecycle of provided objects | ||
* and invoke `disposer` function to destroy objects properly. | ||
* | ||
* Sample usage: | ||
* ```kotlin | ||
* val feature = RetainedInstanceStore.get( | ||
* owner = buildContext.identifier, | ||
* factory = { FeatureImpl() }, | ||
* disposer = { feature.dispose() } | ||
* } | ||
* ``` | ||
* or | ||
* * ```kotlin | ||
* * val feature = buildContext.getRetainedInstance( | ||
* * factory = { FeatureImpl() }, | ||
* * disposer = { feature.dispose() } | ||
* * } | ||
* * ``` | ||
*/ | ||
interface RetainedInstanceStore { | ||
|
||
fun <T : Any> get(nodeId: String, clazz: KClass<*>, disposer: (T) -> Unit, factory: () -> T): T | ||
|
||
fun removeAll(nodeId: String) | ||
|
||
companion object : RetainedInstanceStore by RetainedInstanceStoreImpl() | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
libraries/core/src/main/kotlin/com/bumble/appyx/core/store/RetainedInstanceStoreExt.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,16 @@ | ||
package com.bumble.appyx.core.store | ||
|
||
import com.bumble.appyx.core.modality.BuildContext | ||
|
||
inline fun <reified T : Any> RetainedInstanceStore.get( | ||
nodeId: String, | ||
noinline disposer: (T) -> Unit = {}, | ||
noinline factory: () -> T | ||
): T = | ||
get(nodeId, T::class, disposer, factory) | ||
|
||
inline fun <reified T : Any> BuildContext.getRetainedInstance( | ||
noinline disposer: (T) -> Unit = {}, | ||
noinline factory: () -> T | ||
) = | ||
RetainedInstanceStore.get(identifier, disposer, factory) |
28 changes: 28 additions & 0 deletions
28
libraries/core/src/main/kotlin/com/bumble/appyx/core/store/RetainedInstanceStoreImpl.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,28 @@ | ||
package com.bumble.appyx.core.store | ||
|
||
import kotlin.reflect.KClass | ||
|
||
internal class RetainedInstanceStoreImpl : RetainedInstanceStore { | ||
|
||
private val map: MutableMap<String, MutableMap<KClass<*>, ValueHolder<*>>> = HashMap() | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun <T : Any> get(nodeId: String, clazz: KClass<*>, disposer: (T) -> Unit, factory: () -> T): T = | ||
map | ||
.getOrPut(nodeId) { HashMap() } | ||
.getOrPut(clazz) { ValueHolder(factory(), disposer) } | ||
.value as T | ||
|
||
override fun removeAll(nodeId: String) { | ||
map.remove(nodeId)?.values?.forEach { it.dispose() } | ||
} | ||
|
||
private class ValueHolder<T : Any>( | ||
val value: T, | ||
private val disposer: (T) -> Unit | ||
) { | ||
fun dispose() { | ||
disposer(value) | ||
} | ||
} | ||
} |
Oops, something went wrong.