Skip to content

Commit ad2d5f7

Browse files
committed
Bug 2021407 - Adds shared prefs implementation of summarization settings
1 parent 258a5d7 commit ad2d5f7

5 files changed

Lines changed: 80 additions & 2 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/SummarizationSettings.kt

Lines changed: 34 additions & 0 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.
@@ -53,5 +58,34 @@ interface SummarizationSettings {
5358
hasConsentedToShake = newValue
5459
}
5560
}
61+
62+
/**
63+
* Creates am 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 getHasConsentedToShake(): 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,37 @@
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.flow.toList
8+
import kotlinx.coroutines.launch
9+
import kotlinx.coroutines.test.runTest
10+
import mozilla.components.feature.summarize.SummarizationState.Finished
11+
import mozilla.components.feature.summarize.SummarizationState.Inert
12+
import mozilla.components.feature.summarize.SummarizationState.ShakeConsentRequired
13+
import mozilla.components.feature.summarize.SummarizationState.Summarized
14+
import mozilla.components.feature.summarize.SummarizationState.Summarizing
15+
import mozilla.components.feature.summarize.fakes.FakeCloudProvider
16+
import mozilla.components.feature.summarize.fakes.FakeLlm
17+
import mozilla.components.support.test.fakes.android.FakeSharedPreferences
18+
import org.junit.Assert.assertEquals
19+
import org.junit.Assert.assertFalse
20+
import org.junit.Assert.assertTrue
21+
import org.junit.Test
22+
import kotlin.time.Duration.Companion.seconds
23+
24+
class SummarizationSettingsTest {
25+
26+
@Test
27+
fun `test that we can set and retrieve that the user has consented to shake`() = runTest {
28+
val prefs = FakeSharedPreferences()
29+
val settings = SharedPrefsBackedSummarizationSettings(prefs)
30+
31+
assertFalse(settings.getHasConsentedToShake())
32+
33+
settings.setHasConsentedToShake(true)
34+
35+
assertTrue(settings.getHasConsentedToShake())
36+
}
37+
}

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)