This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #10335: Introduce Jetpack Compose bindings for lib-state.
- Loading branch information
Showing
5 changed files
with
235 additions
and
2 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
141 changes: 141 additions & 0 deletions
141
...ib/state/src/androidTest/java/mozilla/components/lib/state/ext/ComposeExtensionsKtTest.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,141 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.lib.state.ext | ||
|
||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import kotlinx.coroutines.runBlocking | ||
import mozilla.components.lib.state.Action | ||
import mozilla.components.lib.state.State | ||
import mozilla.components.lib.state.Store | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class ComposeExtensionsKtTest { | ||
@get:Rule | ||
val rule = createComposeRule() | ||
|
||
@Test | ||
fun usingInitialValue() { | ||
val store = Store( | ||
initialState = TestState(counter = 42), | ||
reducer = ::reducer | ||
) | ||
|
||
var value: Int? = null | ||
|
||
rule.setContent { | ||
val composeState = store.observeAsState { state -> state.counter * 2 } | ||
value = composeState.value | ||
} | ||
|
||
assertEquals(84, value) | ||
} | ||
|
||
@Test | ||
fun receivingUpdates() { | ||
val store = Store( | ||
initialState = TestState(counter = 42), | ||
reducer = ::reducer | ||
) | ||
|
||
var value: Int? = null | ||
|
||
rule.setContent { | ||
val composeState = store.observeAsState { state -> state.counter * 2 } | ||
value = composeState.value | ||
} | ||
|
||
store.dispatchBlockingOnIdle(TestAction.IncrementAction) | ||
|
||
rule.runOnIdle { | ||
assertEquals(86, value) | ||
} | ||
} | ||
|
||
@Test | ||
fun receivingUpdatesForPartialStateUpdateOnly() { | ||
val store = Store( | ||
initialState = TestState(counter = 42), | ||
reducer = ::reducer | ||
) | ||
|
||
var value: Int? = null | ||
|
||
rule.setContent { | ||
val composeState = store.observeAsState( | ||
map = { state -> state.counter * 2 }, | ||
observe = { state -> state.text } | ||
) | ||
value = composeState.value | ||
} | ||
|
||
assertEquals(84, value) | ||
|
||
store.dispatchBlockingOnIdle(TestAction.IncrementAction) | ||
|
||
rule.runOnIdle { | ||
// State value didn't change because value returned by `observer` function did not change | ||
assertEquals(84, value) | ||
} | ||
|
||
store.dispatchBlockingOnIdle(TestAction.SetTextAction("Hello World")) | ||
|
||
rule.runOnIdle { | ||
// Now, after the value from the observer function changed, we are seeing the new value | ||
assertEquals(86, value) | ||
} | ||
|
||
store.dispatchBlockingOnIdle(TestAction.SetValueAction(23)) | ||
|
||
rule.runOnIdle { | ||
// Observer function result is the same, no state update | ||
assertEquals(86, value) | ||
} | ||
|
||
store.dispatchBlockingOnIdle(TestAction.SetTextAction("Hello World")) | ||
|
||
rule.runOnIdle { | ||
// Text was updated to the same value, observer function result is the same, no state update | ||
assertEquals(86, value) | ||
} | ||
|
||
store.dispatchBlockingOnIdle(TestAction.SetTextAction("Hello World Again")) | ||
|
||
rule.runOnIdle { | ||
// Now, after the value from the observer function changed, we are seeing the new value | ||
assertEquals(46, value) | ||
} | ||
} | ||
|
||
private fun Store<TestState, TestAction>.dispatchBlockingOnIdle(action: TestAction) { | ||
rule.runOnIdle { | ||
val job = dispatch(action) | ||
runBlocking { job.join() } | ||
} | ||
} | ||
} | ||
|
||
fun reducer(state: TestState, action: TestAction): TestState = when (action) { | ||
is TestAction.IncrementAction -> state.copy(counter = state.counter + 1) | ||
is TestAction.DecrementAction -> state.copy(counter = state.counter - 1) | ||
is TestAction.SetValueAction -> state.copy(counter = action.value) | ||
is TestAction.SetTextAction -> state.copy(text = action.text) | ||
} | ||
|
||
data class TestState( | ||
val counter: Int, | ||
val text: String = "" | ||
) : State | ||
|
||
sealed class TestAction : Action { | ||
object IncrementAction : TestAction() | ||
object DecrementAction : TestAction() | ||
data class SetValueAction(val value: Int) : TestAction() | ||
data class SetTextAction(val text: String) : TestAction() | ||
} |
74 changes: 74 additions & 0 deletions
74
components/lib/state/src/main/java/mozilla/components/lib/state/ext/ComposeExtensions.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,74 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.lib.state.ext | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.platform.LocalLifecycleOwner | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleOwner | ||
import mozilla.components.lib.state.Action | ||
import mozilla.components.lib.state.State | ||
import mozilla.components.lib.state.Store | ||
import androidx.compose.runtime.State as ComposeState | ||
|
||
/** | ||
* Starts observing this [Store] and represents the mapped state (using [map]) via [ComposeState]. | ||
* | ||
* Every time the mapped [Store] state changes, the returned [ComposeState] will be updated causing | ||
* recomposition of every [ComposeState.value] usage. | ||
* | ||
* The [Store] observer will automatically be removed when this composable disposes or the current | ||
* [LifecycleOwner] moves to the [Lifecycle.State.DESTROYED] state. | ||
*/ | ||
@Composable | ||
fun <S : State, A : Action, R> Store<S, A>.observeAsState(map: (S) -> R): ComposeState<R?> { | ||
val lifecycleOwner = LocalLifecycleOwner.current | ||
val state = remember { mutableStateOf<R?>(map(state)) } | ||
|
||
DisposableEffect(this, lifecycleOwner) { | ||
val subscription = observe(lifecycleOwner) { browserState -> | ||
state.value = map(browserState) | ||
} | ||
onDispose { subscription?.unsubscribe() } | ||
} | ||
|
||
return state | ||
} | ||
|
||
/** | ||
* Starts observing this [Store] and represents the mapped state (using [map]) via [ComposeState]. | ||
* | ||
* Everytime the [Store] state changes and the result of the [observe] function changes for this | ||
* state, the returned [ComposeState] will be updated causing recomposition of every | ||
* [ComposeState.value] usage. | ||
* | ||
* The [Store] observer will automatically be removed when this composable disposes or the current | ||
* [LifecycleOwner] moves to the [Lifecycle.State.DESTROYED] state. | ||
*/ | ||
@Composable | ||
fun <S : State, A : Action, O, R> Store<S, A>.observeAsState( | ||
observe: (S) -> O, | ||
map: (S) -> R | ||
): ComposeState<R?> { | ||
val lifecycleOwner = LocalLifecycleOwner.current | ||
var lastValue = observe(state) | ||
val state = remember { mutableStateOf<R?>(map(state)) } | ||
|
||
DisposableEffect(this, lifecycleOwner) { | ||
val subscription = observe(lifecycleOwner) { browserState -> | ||
val newValue = observe(browserState) | ||
if (newValue != lastValue) { | ||
state.value = map(browserState) | ||
lastValue = newValue | ||
} | ||
} | ||
onDispose { subscription?.unsubscribe() } | ||
} | ||
|
||
return state | ||
} |
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