1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package org.mozilla.fenix.library.historymetadata
7 import mozilla.components.lib.state.Action
8 import mozilla.components.lib.state.State
9 import mozilla.components.lib.state.Store
10 import org.mozilla.fenix.components.AppStore
11 import org.mozilla.fenix.library.history.History
12 import org.mozilla.fenix.library.history.PendingDeletionHistory
13 import org.mozilla.fenix.library.historymetadata.view.HistoryMetadataGroupView
16 * The [Store] for holding the [HistoryMetadataGroupFragmentState] and applying
17 * [HistoryMetadataGroupFragmentAction]s.
19 class HistoryMetadataGroupFragmentStore(initialState: HistoryMetadataGroupFragmentState) :
20 Store<HistoryMetadataGroupFragmentState, HistoryMetadataGroupFragmentAction>(
22 ::historyStateReducer,
26 * Actions to dispatch through the [HistoryMetadataGroupFragmentStore] to modify the
27 * [HistoryMetadataGroupFragmentState] through the [historyStateReducer].
29 sealed class HistoryMetadataGroupFragmentAction : Action {
30 data class UpdateHistoryItems(val items: List<History.Metadata>) :
31 HistoryMetadataGroupFragmentAction()
32 data class Select(val item: History.Metadata) : HistoryMetadataGroupFragmentAction()
33 data class Deselect(val item: History.Metadata) : HistoryMetadataGroupFragmentAction()
36 * Updates the set of items marked for removal from the [AppStore]
37 * to the [HistoryMetadataGroupFragmentStore], to be hidden from the UI.
39 data class UpdatePendingDeletionItems(val pendingDeletionItems: Set<PendingDeletionHistory>) :
40 HistoryMetadataGroupFragmentAction()
41 object DeselectAll : HistoryMetadataGroupFragmentAction()
42 data class Delete(val item: History.Metadata) : HistoryMetadataGroupFragmentAction()
43 object DeleteAll : HistoryMetadataGroupFragmentAction()
46 * Updates the empty state of [HistoryMetadataGroupView].
48 data class ChangeEmptyState(val isEmpty: Boolean) : HistoryMetadataGroupFragmentAction()
52 * The state for [HistoryMetadataGroupFragment].
54 * @property items The list of [History.Metadata] to display.
55 * @property pendingDeletionItems The set of [PendingDeletionHistory] marked for removal.
56 * @property isEmpty Whether or not the screen is empty.
58 data class HistoryMetadataGroupFragmentState(
59 val items: List<History.Metadata>,
60 val pendingDeletionItems: Set<PendingDeletionHistory>,
65 * Reduces the history metadata state from the current state with the provided [action] to be
68 * @param state The current history metadata state.
69 * @param action The action to be performed on the state.
70 * @return the new [HistoryMetadataGroupFragmentState] with the [action] executed.
72 private fun historyStateReducer(
73 state: HistoryMetadataGroupFragmentState,
74 action: HistoryMetadataGroupFragmentAction,
75 ): HistoryMetadataGroupFragmentState {
76 return when (action) {
77 is HistoryMetadataGroupFragmentAction.UpdateHistoryItems ->
78 state.copy(items = action.items)
79 is HistoryMetadataGroupFragmentAction.Select ->
81 items = state.items.toMutableList()
83 if (it == action.item) {
84 it.copy(selected = true)
90 is HistoryMetadataGroupFragmentAction.Deselect ->
92 items = state.items.toMutableList()
94 if (it == action.item) {
95 it.copy(selected = false)
101 is HistoryMetadataGroupFragmentAction.DeselectAll ->
103 items = state.items.toMutableList()
104 .map { it.copy(selected = false) },
106 is HistoryMetadataGroupFragmentAction.Delete -> {
107 val items = state.items.toMutableList()
108 items.remove(action.item)
109 state.copy(items = items)
111 is HistoryMetadataGroupFragmentAction.DeleteAll ->
112 state.copy(items = emptyList())
113 is HistoryMetadataGroupFragmentAction.UpdatePendingDeletionItems ->
114 state.copy(pendingDeletionItems = action.pendingDeletionItems)
115 is HistoryMetadataGroupFragmentAction.ChangeEmptyState -> state.copy(
116 isEmpty = action.isEmpty,