Bug 1850407 - Add nimbus flag for review checker product recommendations
[gecko.git] / mobile / android / fenix / app / src / main / java / org / mozilla / fenix / shopping / middleware / ReviewQualityCheckPreferences.kt
blob279f3293db04fae24b0a605e1e5743ef4974468b
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
12 /**
13  * Interface to get and set preferences for the review quality check feature.
14  */
15 interface ReviewQualityCheckPreferences {
16     /**
17      * Returns true if the user has opted in to the review quality check feature.
18      */
19     suspend fun enabled(): Boolean
21     /**
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.
24      */
25     suspend fun productRecommendationsEnabled(): Boolean?
27     /**
28      * Sets whether the user has opted in to the review quality check feature.
29      */
30     suspend fun setEnabled(isEnabled: Boolean)
32     /**
33      * Sets user preference to turn on/off product recommendations.
34      */
35     suspend fun setProductRecommendationsEnabled(isEnabled: Boolean)
37     /**
38      * Updates the condition to display the opted in CFR.
39      */
40     suspend fun updateCFRCondition(time: Long)
43 /**
44  * Implementation of [ReviewQualityCheckPreferences] that uses [Settings] to store/fetch
45  * preferences.
46  *
47  * @param settings The [Settings] instance to use.
48  */
49 class ReviewQualityCheckPreferencesImpl(
50     private val settings: Settings,
51 ) : ReviewQualityCheckPreferences {
53     override suspend fun enabled(): Boolean = withContext(Dispatchers.IO) {
54         settings.isReviewQualityCheckEnabled
55     }
57     override suspend fun productRecommendationsEnabled(): Boolean? = withContext(Dispatchers.IO) {
58         if (FxNimbus.features.shoppingExperience.value().productRecommendations) {
59             settings.isReviewQualityCheckProductRecommendationsEnabled
60         } else {
61             null
62         }
63     }
65     override suspend fun setEnabled(isEnabled: Boolean) {
66         settings.isReviewQualityCheckEnabled = isEnabled
67     }
69     override suspend fun setProductRecommendationsEnabled(isEnabled: Boolean) {
70         settings.isReviewQualityCheckProductRecommendationsEnabled = isEnabled
71     }
73     override suspend fun updateCFRCondition(time: Long) = with(settings) {
74         if (reviewQualityCheckOptInTimeInMillis == 0L) {
75             reviewQualityCheckOptInTimeInMillis = time
76             shouldShowReviewQualityCheckCFR = true
77         }
78     }