-
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 50d47f4
Showing
20 changed files
with
587 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) | ||
) | ||
} | ||
} | ||
``` |
137 changes: 137 additions & 0 deletions
137
...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,137 @@ | ||
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 com.bumble.appyx.core.store.getRetainedInstance | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class RetainedInstanceStoreTest { | ||
private val stubRetainedInstanceStore = StubRetainedInstanceStore() | ||
private var beforeNodeCreatedFunc: ((BuildContext) -> Unit)? = null | ||
|
||
@get:Rule | ||
val rule = AppyxTestScenario { buildContext -> | ||
beforeNodeCreatedFunc?.invoke(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.clearStoreCalled) | ||
} | ||
|
||
@Test | ||
fun WHEN_activity_recreated_THEN_retained_instance_store_content_is_not_removed() { | ||
rule.start() | ||
|
||
rule.activityScenario.recreate() | ||
|
||
assertFalse(stubRetainedInstanceStore.clearStoreCalled) | ||
} | ||
|
||
@Test | ||
fun GIVEN_counter_stored_and_incremented_AND_activity_finished_WHEN_stored_counter_incremented_THEN_counter_value_is_1() { | ||
var nodeBuildInvocationCount = 0 | ||
var factoryInvocationCount = 0 | ||
var disposerCalled = false | ||
var buildContext: BuildContext? = null | ||
|
||
val getRetainedCounterFunc = { | ||
requireNotNull(buildContext) { "Build context not set" } | ||
.getRetainedInstance( | ||
disposer = { | ||
disposerCalled = true | ||
}, | ||
factory = { | ||
factoryInvocationCount++ | ||
Counter() | ||
} | ||
) | ||
} | ||
beforeNodeCreatedFunc = { | ||
buildContext = it | ||
nodeBuildInvocationCount++ | ||
} | ||
rule.start() | ||
getRetainedCounterFunc().increment() | ||
rule.activityScenario.moveToState(Lifecycle.State.DESTROYED) | ||
|
||
getRetainedCounterFunc().increment() | ||
|
||
assertEquals(1, getRetainedCounterFunc().value) | ||
assertEquals(1, nodeBuildInvocationCount) | ||
assertEquals(2, factoryInvocationCount) | ||
assertTrue(disposerCalled) | ||
} | ||
|
||
@Test | ||
fun GIVEN_counter_stored_and_incremented_AND_activity_recreated_WHEN_stored_counter_incremented_THEN_counter_value_is_2() { | ||
var nodeBuildInvocationCount = 0 | ||
var factoryInvocationCount = 0 | ||
var disposerCalled = false | ||
var buildContext: BuildContext? = null | ||
|
||
val getRetainedCounterFunc = { | ||
requireNotNull(buildContext) { "Build context not set" } | ||
.getRetainedInstance( | ||
disposer = { | ||
disposerCalled = true | ||
}, | ||
factory = { | ||
factoryInvocationCount++ | ||
Counter() | ||
} | ||
) | ||
} | ||
beforeNodeCreatedFunc = { | ||
buildContext = it | ||
nodeBuildInvocationCount++ | ||
} | ||
rule.start() | ||
getRetainedCounterFunc().increment() | ||
rule.activityScenario.recreate() | ||
|
||
getRetainedCounterFunc().increment() | ||
|
||
assertEquals(2, getRetainedCounterFunc().value) | ||
assertEquals(2, nodeBuildInvocationCount) | ||
assertEquals(1, factoryInvocationCount) | ||
assertFalse(disposerCalled) | ||
} | ||
|
||
class Counter { | ||
var value: Int = 0 | ||
private set | ||
|
||
fun increment() { | ||
value++ | ||
} | ||
} | ||
|
||
class TestParentNode( | ||
buildContext: BuildContext, | ||
retainedInstanceStore: RetainedInstanceStore, | ||
) : Node( | ||
buildContext = buildContext, | ||
retainedInstanceStore = retainedInstanceStore, | ||
) | ||
|
||
class StubRetainedInstanceStore : RetainedInstanceStore by RetainedInstanceStore { | ||
var clearStoreCalled: Boolean = false | ||
|
||
override fun clearStore(storeId: String) { | ||
clearStoreCalled = true | ||
RetainedInstanceStore.clearStore(storeId) | ||
} | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
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,35 @@ | ||
package com.bumble.appyx.core.store | ||
|
||
/** | ||
* 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( | ||
* storeId = buildContext.identifier, | ||
* factory = { FeatureImpl() }, | ||
* disposer = { feature.dispose() } | ||
* } | ||
* ``` | ||
* or | ||
* * ```kotlin | ||
* * val feature = buildContext.getRetainedInstance( | ||
* * factory = { FeatureImpl() }, | ||
* * disposer = { feature.dispose() } | ||
* * } | ||
* * ``` | ||
*/ | ||
interface RetainedInstanceStore { | ||
|
||
fun <T : Any> get(storeId: String, key: String, disposer: (T) -> Unit = {}, factory: () -> T): T | ||
|
||
fun clearStore(storeId: String) | ||
|
||
companion object : RetainedInstanceStore by RetainedInstanceStoreImpl() | ||
|
||
} |
Oops, something went wrong.