[fenix] Close https://github.com/mozilla-mobile/fenix/issues/20704: Add AppStore to Components
parent
14c1eac2f9
commit
5d8a920b5f
@ -0,0 +1,22 @@
|
|||||||
|
/* 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 org.mozilla.fenix.components
|
||||||
|
|
||||||
|
import mozilla.components.lib.state.Middleware
|
||||||
|
import mozilla.components.lib.state.Store
|
||||||
|
import org.mozilla.fenix.components.appstate.AppAction
|
||||||
|
import org.mozilla.fenix.components.appstate.AppState
|
||||||
|
import org.mozilla.fenix.components.appstate.AppStoreReducer
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A [Store] that holds the [AppState] for the app and reduces [AppAction]s
|
||||||
|
* dispatched to the store.
|
||||||
|
*
|
||||||
|
* This store is not persisted to disk and is scoped to the life-cycle of the application.
|
||||||
|
*/
|
||||||
|
class AppStore(
|
||||||
|
initialState: AppState = AppState(),
|
||||||
|
middlewares: List<Middleware<AppState, AppAction>> = emptyList()
|
||||||
|
) : Store<AppState, AppAction>(initialState, AppStoreReducer::reduce, middlewares)
|
@ -0,0 +1,15 @@
|
|||||||
|
/* 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 org.mozilla.fenix.components.appstate
|
||||||
|
|
||||||
|
import mozilla.components.lib.state.Action
|
||||||
|
import org.mozilla.fenix.components.AppStore
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [Action] implementation related to [AppStore].
|
||||||
|
*/
|
||||||
|
sealed class AppAction : Action {
|
||||||
|
data class UpdateInactiveExpanded(val expanded: Boolean) : AppAction()
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
/* 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 org.mozilla.fenix.components.appstate
|
||||||
|
|
||||||
|
import mozilla.components.lib.state.State
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Value type that represents the state of the tabs tray.
|
||||||
|
*
|
||||||
|
* @property inactiveTabsExpanded A flag to know if the Inactive Tabs section of the Tabs Tray
|
||||||
|
* should be expanded when the tray is opened.
|
||||||
|
*/
|
||||||
|
data class AppState(
|
||||||
|
val inactiveTabsExpanded: Boolean = false
|
||||||
|
) : State
|
@ -0,0 +1,17 @@
|
|||||||
|
/* 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 org.mozilla.fenix.components.appstate
|
||||||
|
|
||||||
|
import org.mozilla.fenix.components.AppStore
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reducer for [AppStore].
|
||||||
|
*/
|
||||||
|
internal object AppStoreReducer {
|
||||||
|
fun reduce(state: AppState, action: AppAction): AppState = when (action) {
|
||||||
|
is AppAction.UpdateInactiveExpanded ->
|
||||||
|
state.copy(inactiveTabsExpanded = action.expanded)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
/* 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 org.mozilla.fenix.components.appstate
|
||||||
|
|
||||||
|
import mozilla.components.support.test.ext.joinBlocking
|
||||||
|
import mozilla.components.support.test.middleware.CaptureActionsMiddleware
|
||||||
|
import org.junit.Assert.assertFalse
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Test
|
||||||
|
import org.mozilla.fenix.components.AppStore
|
||||||
|
|
||||||
|
class AppActionTest {
|
||||||
|
|
||||||
|
private val capture = CaptureActionsMiddleware<AppState, AppAction>()
|
||||||
|
private val store = AppStore(middlewares = listOf(capture))
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `WHEN UpdateInactiveExpanded is dispatched THEN update inactiveTabsExpanded`() {
|
||||||
|
assertFalse(store.state.inactiveTabsExpanded)
|
||||||
|
|
||||||
|
store.dispatch(AppAction.UpdateInactiveExpanded(true)).joinBlocking()
|
||||||
|
|
||||||
|
assertTrue(store.state.inactiveTabsExpanded)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue