You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.1 KiB
Kotlin
69 lines
2.1 KiB
Kotlin
11 months ago
|
/* 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/. */
|
||
|
|
||
10 months ago
|
package org.mozilla.fenix.shopping.store
|
||
11 months ago
|
|
||
|
import mozilla.components.lib.state.Store
|
||
|
|
||
|
/**
|
||
|
* Store for review quality check feature.
|
||
11 months ago
|
*
|
||
10 months ago
|
* @param middleware The list of middlewares to use.
|
||
11 months ago
|
*/
|
||
11 months ago
|
class ReviewQualityCheckStore(
|
||
10 months ago
|
middleware: List<ReviewQualityCheckMiddleware>,
|
||
11 months ago
|
) : Store<ReviewQualityCheckState, ReviewQualityCheckAction>(
|
||
11 months ago
|
initialState = ReviewQualityCheckState.Initial,
|
||
10 months ago
|
middleware = middleware,
|
||
11 months ago
|
reducer = ::reducer,
|
||
|
) {
|
||
|
init {
|
||
|
dispatch(ReviewQualityCheckAction.Init)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private fun reducer(
|
||
|
state: ReviewQualityCheckState,
|
||
|
action: ReviewQualityCheckAction,
|
||
|
): ReviewQualityCheckState {
|
||
|
if (action is ReviewQualityCheckAction.UpdateAction) {
|
||
|
return mapStateForUpdateAction(state, action)
|
||
|
}
|
||
|
|
||
|
return state
|
||
|
}
|
||
|
|
||
|
private fun mapStateForUpdateAction(
|
||
|
state: ReviewQualityCheckState,
|
||
|
action: ReviewQualityCheckAction.UpdateAction,
|
||
|
): ReviewQualityCheckState {
|
||
|
when (action) {
|
||
|
is ReviewQualityCheckAction.UpdateUserPreferences -> {
|
||
|
return if (action.hasUserOptedIn) {
|
||
|
if (state is ReviewQualityCheckState.OptedIn) {
|
||
|
state.copy(productRecommendationsPreference = action.isProductRecommendationsEnabled)
|
||
|
} else {
|
||
|
ReviewQualityCheckState.OptedIn(
|
||
|
productRecommendationsPreference = action.isProductRecommendationsEnabled,
|
||
|
)
|
||
|
}
|
||
|
} else {
|
||
|
ReviewQualityCheckState.NotOptedIn
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ReviewQualityCheckAction.OptOut -> {
|
||
|
return ReviewQualityCheckState.NotOptedIn
|
||
|
}
|
||
|
|
||
|
ReviewQualityCheckAction.ToggleProductRecommendation -> {
|
||
|
return if (state is ReviewQualityCheckState.OptedIn) {
|
||
|
state.copy(productRecommendationsPreference = !state.productRecommendationsPreference)
|
||
|
} else {
|
||
|
state
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|