Skip to content

Commit 0bbf19a

Browse files
committed
Bug 2021407 - Adds shared prefs implementation of summarization settings r=segunfamisa
[try](https://treeherder.mozilla.org/jobs?repo=try&landoCommitID=1837750) Pull request: #98
1 parent 7468809 commit 0bbf19a

7 files changed

Lines changed: 74 additions & 7 deletions

File tree

mobile/android/android-components/.buildconfig.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,14 +1408,18 @@ projects:
14081408
path: components/feature/summarize
14091409
publish: true
14101410
upstream_dependencies:
1411+
- components:browser-errorpages
14111412
- components:compose-base
14121413
- components:concept-base
1414+
- components:concept-engine
14131415
- components:concept-fetch
14141416
- components:concept-llm
1417+
- components:concept-storage
14151418
- components:lib-publicsuffixlist
14161419
- components:lib-state
14171420
- components:support-base
14181421
- components:support-ktx
1422+
- components:support-test-fakes
14191423
- components:support-utils
14201424
- components:tooling-lint
14211425
- components:ui-colors

mobile/android/android-components/components/feature/summarize/build.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ android {
2424
}
2525

2626
dependencies {
27+
implementation project(':components:compose-base')
2728
implementation project(':components:concept-llm')
2829
implementation project(':components:lib-state')
29-
implementation project(':components:compose-base')
3030
implementation project(':components:ui-icons')
3131
implementation project(':components:ui-richtext')
3232

@@ -35,10 +35,13 @@ dependencies {
3535
implementation libs.androidx.compose.ui.graphics
3636
implementation libs.androidx.compose.ui.tooling.preview
3737
implementation libs.androidx.compose.material3
38+
implementation libs.androidx.core.ktx
3839
debugImplementation libs.androidx.compose.ui.tooling
3940

4041
testImplementation libs.androidx.test.junit
4142
testImplementation libs.kotlinx.coroutines.test
43+
44+
testImplementation project(':components:support-test-fakes')
4245
}
4346

4447
apply from: '../../../common-config.gradle'

mobile/android/android-components/components/feature/summarize/src/main/java/mozilla/components/feature/summarize/SummarizationMiddleware.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class SummarizationMiddleware(
8080
private suspend fun needsShakeConsent(state: SummarizationState): Boolean =
8181
state is SummarizationState.Inert &&
8282
state.initializedWithShake &&
83-
!settings.getHasConsentedToShake()
83+
!settings.hasConsentedToShake()
8484

8585
private val systemPrompt = "This is the system prompt: "
8686
}

mobile/android/android-components/components/feature/summarize/src/main/java/mozilla/components/feature/summarize/SummarizationSettings.kt

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
package mozilla.components.feature.summarize
66

7+
import android.content.Context
8+
import android.content.Context.MODE_PRIVATE
9+
import android.content.SharedPreferences
10+
import androidx.core.content.edit
11+
712
/**
813
* Defines the contract for persisting and retrieving user settings related to
914
* the summarization feature.
@@ -21,7 +26,7 @@ interface SummarizationSettings {
2126
* @return `true` if the user has previously accepted the shake consent prompt,
2227
* `false` otherwise.
2328
*/
24-
suspend fun getHasConsentedToShake(): Boolean
29+
suspend fun hasConsentedToShake(): Boolean
2530

2631
/**
2732
* Persists the user's consent status for the shake gesture interaction.
@@ -47,11 +52,40 @@ interface SummarizationSettings {
4752
) = object : SummarizationSettings {
4853
var hasConsentedToShake = hasConsentedToShakeInitial
4954

50-
override suspend fun getHasConsentedToShake() = hasConsentedToShake
55+
override suspend fun hasConsentedToShake() = hasConsentedToShake
5156

5257
override suspend fun setHasConsentedToShake(newValue: Boolean) {
5358
hasConsentedToShake = newValue
5459
}
5560
}
61+
62+
/**
63+
* Creates an implementation of [SummarizationSettings] that is backed by [SharedPreferences].
64+
*
65+
* @param context required to create [SharedPreferences].
66+
* @return An in-memory [SummarizationSettings] instance.
67+
*/
68+
fun sharedPrefs(
69+
context: Context,
70+
): SummarizationSettings {
71+
val prefs = context.getSharedPreferences("summarization_prefs", MODE_PRIVATE)
72+
return SharedPrefsBackedSummarizationSettings(prefs)
73+
}
74+
}
75+
}
76+
77+
internal class SharedPrefsBackedSummarizationSettings(
78+
private val prefs: SharedPreferences,
79+
) : SummarizationSettings {
80+
override suspend fun hasConsentedToShake(): Boolean {
81+
return prefs.getBoolean(HAS_CONSENT_TO_SHAKE, false)
82+
}
83+
84+
override suspend fun setHasConsentedToShake(newValue: Boolean) {
85+
prefs.edit { putBoolean(HAS_CONSENT_TO_SHAKE, newValue) }
86+
}
87+
88+
companion object {
89+
const val HAS_CONSENT_TO_SHAKE = "has_consent_to_shake"
5690
}
5791
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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/. */
4+
5+
package mozilla.components.feature.summarize
6+
7+
import kotlinx.coroutines.test.runTest
8+
import mozilla.components.support.test.fakes.android.FakeSharedPreferences
9+
import org.junit.Assert.assertFalse
10+
import org.junit.Assert.assertTrue
11+
import org.junit.Test
12+
13+
class SummarizationSettingsTest {
14+
15+
@Test
16+
fun `test that we can set and retrieve that the user has consented to shake`() = runTest {
17+
val prefs = FakeSharedPreferences()
18+
val settings = SharedPrefsBackedSummarizationSettings(prefs)
19+
20+
assertFalse(settings.hasConsentedToShake())
21+
22+
settings.setHasConsentedToShake(true)
23+
24+
assertTrue(settings.hasConsentedToShake())
25+
}
26+
}

mobile/android/android-components/components/feature/summarize/src/test/java/mozilla/components/feature/summarize/SummarizationStoreTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class SummarizationStoreTest {
5959
)
6060

6161
assertEquals(expected, states)
62-
assertTrue(settings.getHasConsentedToShake())
62+
assertTrue(settings.hasConsentedToShake())
6363
}
6464

6565
@Test
@@ -98,7 +98,7 @@ class SummarizationStoreTest {
9898
)
9999

100100
assertEquals(expected, states)
101-
assertFalse(settings.getHasConsentedToShake())
101+
assertFalse(settings.hasConsentedToShake())
102102
}
103103

104104
@Test

mobile/android/fenix/app/src/main/java/org/mozilla/fenix/summarization/SummarizationFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class SummarizationFragment : BottomSheetDialogFragment() {
5555
SummarizationStoreViewModel.factory(
5656
initializedFromShake = args.fromShake,
5757
llmProvider = provider,
58-
settings = SummarizationSettings.inMemory(),
58+
settings = SummarizationSettings.sharedPrefs(requireContext()),
5959
pageContentExtractor = engineSession.asPageContentExtractor(),
6060
)
6161
}

0 commit comments

Comments
 (0)