Bug 1848527 - Part 1: Enable OutdatedDocumentation detekt rule in Fenix
[gecko.git] / mobile / android / fenix / app / src / main / java / org / mozilla / fenix / library / historymetadata / HistoryMetadataGroupFragmentStore.kt
blobb90759160a2d50678209ef0e24d1a2db39eadbc1
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
15 /**
16  * The [Store] for holding the [HistoryMetadataGroupFragmentState] and applying
17  * [HistoryMetadataGroupFragmentAction]s.
18  */
19 class HistoryMetadataGroupFragmentStore(initialState: HistoryMetadataGroupFragmentState) :
20     Store<HistoryMetadataGroupFragmentState, HistoryMetadataGroupFragmentAction>(
21         initialState,
22         ::historyStateReducer,
23     )
25 /**
26  * Actions to dispatch through the [HistoryMetadataGroupFragmentStore] to modify the
27  * [HistoryMetadataGroupFragmentState] through the [historyStateReducer].
28  */
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()
35     /**
36      * Updates the set of items marked for removal from the [AppStore]
37      * to the [HistoryMetadataGroupFragmentStore], to be hidden from the UI.
38      */
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()
45     /**
46      * Updates the empty state of [HistoryMetadataGroupView].
47      */
48     data class ChangeEmptyState(val isEmpty: Boolean) : HistoryMetadataGroupFragmentAction()
51 /**
52  * The state for [HistoryMetadataGroupFragment].
53  *
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.
57  */
58 data class HistoryMetadataGroupFragmentState(
59     val items: List<History.Metadata>,
60     val pendingDeletionItems: Set<PendingDeletionHistory>,
61     val isEmpty: Boolean,
62 ) : State
64 /**
65  * Reduces the history metadata state from the current state with the provided [action] to be
66  * performed.
67  *
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.
71  */
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 ->
80             state.copy(
81                 items = state.items.toMutableList()
82                     .map {
83                         if (it == action.item) {
84                             it.copy(selected = true)
85                         } else {
86                             it
87                         }
88                     },
89             )
90         is HistoryMetadataGroupFragmentAction.Deselect ->
91             state.copy(
92                 items = state.items.toMutableList()
93                     .map {
94                         if (it == action.item) {
95                             it.copy(selected = false)
96                         } else {
97                             it
98                         }
99                     },
100             )
101         is HistoryMetadataGroupFragmentAction.DeselectAll ->
102             state.copy(
103                 items = state.items.toMutableList()
104                     .map { it.copy(selected = false) },
105             )
106         is HistoryMetadataGroupFragmentAction.Delete -> {
107             val items = state.items.toMutableList()
108             items.remove(action.item)
109             state.copy(items = items)
110         }
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,
117         )
118     }