-
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 3685fa6
Showing
20 changed files
with
405 additions
and
27 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Configuration change | ||
|
||
To retain objects during configuration change you can use the `RetainedInstanceStore` class. | ||
|
||
## How does it work? | ||
|
||
The `RetainedInstanceStore` stores the objects within a singleton. The node manages whether the content should be removed by checking whether the `Activity` is being recreated due to a configuration change or not. | ||
|
||
These are the following scenarios: | ||
- If the `Activity` is recreated: the retained instance is returned instead of a new instance. | ||
- If the `Activity` is destroyed: the retained instance is removed and disposed. | ||
|
||
## Example | ||
|
||
Here is an example of how you can use the `RetainedInstanceStore`: | ||
|
||
```kotlin | ||
import com.bumble.appyx.core.builder.Builder | ||
import com.bumble.appyx.core.modality.BuildContext | ||
import com.bumble.appyx.core.node.Node | ||
import com.bumble.appyx.core.store.getRetainedInstance | ||
|
||
class RetainedInstancesBuilder : Builder<String>() { | ||
|
||
override fun build(buildContext: BuildContext, payload: String): Node { | ||
val feature = buildContext.getRetainedInstance { | ||
RetainedInstancesFeature(payload) | ||
} | ||
|
||
val view = RetainedInstancesViewImpl() | ||
val interactor = RetainedInstancesInteractor( | ||
feature = feature, | ||
view = view | ||
) | ||
|
||
return RetainedInstancesNode( | ||
buildContext = buildContext, | ||
view = view, | ||
plugins = listOf(interactor) | ||
) | ||
} | ||
} | ||
``` |
64 changes: 64 additions & 0 deletions
64
...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,64 @@ | ||
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.Before | ||
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) | ||
} | ||
|
||
@Before | ||
fun setup() { | ||
rule.start() | ||
} | ||
|
||
@Test | ||
fun WHEN_activity_finished_THEN_retained_instance_store_content_is_removed() { | ||
rule.activityScenario.moveToState(Lifecycle.State.DESTROYED) | ||
|
||
assertTrue(stubRetainedInstanceStore.removeAllCalled) | ||
} | ||
|
||
@Test | ||
fun WHEN_activity_recreated_THEN_retained_instance_store_content_is_not_removed() { | ||
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.