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.shopping.middleware
7 import kotlinx.coroutines.Dispatchers
8 import kotlinx.coroutines.withContext
9 import org.mozilla.fenix.nimbus.FxNimbus
10 import org.mozilla.fenix.utils.Settings
13 * Interface to get and set preferences for the review quality check feature.
15 interface ReviewQualityCheckPreferences {
17 * Returns true if the user has opted in to the review quality check feature.
19 suspend fun enabled(): Boolean
22 * Returns true if the user has turned on product recommendations, false if turned off by the
23 * user, null if the product recommendations feature is disabled.
25 suspend fun productRecommendationsEnabled(): Boolean?
28 * Sets whether the user has opted in to the review quality check feature.
30 suspend fun setEnabled(isEnabled: Boolean)
33 * Sets user preference to turn on/off product recommendations.
35 suspend fun setProductRecommendationsEnabled(isEnabled: Boolean)
38 * Updates the condition to display the opted in CFR.
40 suspend fun updateCFRCondition(time: Long)
44 * Implementation of [ReviewQualityCheckPreferences] that uses [Settings] to store/fetch
47 * @param settings The [Settings] instance to use.
49 class ReviewQualityCheckPreferencesImpl(
50 private val settings: Settings,
51 ) : ReviewQualityCheckPreferences {
53 override suspend fun enabled(): Boolean = withContext(Dispatchers.IO) {
54 settings.isReviewQualityCheckEnabled
57 override suspend fun productRecommendationsEnabled(): Boolean? = withContext(Dispatchers.IO) {
58 if (FxNimbus.features.shoppingExperience.value().productRecommendations) {
59 settings.isReviewQualityCheckProductRecommendationsEnabled
65 override suspend fun setEnabled(isEnabled: Boolean) {
66 settings.isReviewQualityCheckEnabled = isEnabled
69 override suspend fun setProductRecommendationsEnabled(isEnabled: Boolean) {
70 settings.isReviewQualityCheckProductRecommendationsEnabled = isEnabled
73 override suspend fun updateCFRCondition(time: Long) = with(settings) {
74 if (reviewQualityCheckOptInTimeInMillis == 0L) {
75 reviewQualityCheckOptInTimeInMillis = time
76 shouldShowReviewQualityCheckCFR = true