Bug 2037736 - Added a store to the settings and implemented storeProvider for persisted state#255
Open
fmasalha wants to merge 1 commit into
Open
Bug 2037736 - Added a store to the settings and implemented storeProvider for persisted state#255fmasalha wants to merge 1 commit into
fmasalha wants to merge 1 commit into
Conversation
…ider for persisted state
Contributor
|
View this pull request in Lando to land it once approved. |
segunfamisa
reviewed
May 15, 2026
Comment on lines
+101
to
+196
| /** | ||
| * State for the AI Controls section of the settings screen. | ||
| */ | ||
| data class AIControlsState(val featuresEnabled: Map<AIFeatureMetadata.FeatureId, Boolean> = mapOf()) | ||
|
|
||
| /** | ||
| * State for the settings screen. | ||
| */ | ||
| data class SettingsState( | ||
| val aiControlsState: AIControlsState = AIControlsState(), | ||
| val summarizeSettingsState: SummarizeSettingsState = SummarizeSettingsState(), | ||
| ) : State | ||
|
|
||
| /** | ||
| * Actions that can be dispatched on the settings [SettingsStore]. | ||
| */ | ||
| sealed class SettingsAction : Action { | ||
| /** Dispatched once when the settings screen is first created. */ | ||
| data object SettingsViewCreated : SettingsAction() | ||
|
|
||
| /** Emitted when an AI feature's enabled state has been loaded. */ | ||
| data class AIControlsFeatureStateLoaded( | ||
| val enabled: Boolean, | ||
| val id: AIFeatureMetadata.FeatureId, | ||
| ) : SettingsAction() | ||
|
|
||
| /** Emitted when the page-summaries settings have been loaded from disk. */ | ||
| data class PageSummariesSettingsLoaded( | ||
| val isFeatureEnabled: Boolean, | ||
| val isGestureEnabled: Boolean, | ||
| ) : SettingsAction() | ||
| } | ||
|
|
||
| /** | ||
| * Middleware that observes external sources (AI feature registry, summarization settings) | ||
| * and dispatches load actions into the settings [SettingsStore]. | ||
| */ | ||
| class SettingsMiddleware( | ||
| val featureRegistry: AIFeatureRegistry, | ||
| val summarizationSettings: SummarizationSettings, | ||
| val scope: CoroutineScope, | ||
| ) : Middleware<SettingsState, SettingsAction> { | ||
| override fun invoke( | ||
| store: Store<SettingsState, SettingsAction>, | ||
| next: (SettingsAction) -> Unit, | ||
| action: SettingsAction, | ||
| ) { | ||
| next(action) | ||
| when (action) { | ||
| is SettingsAction.SettingsViewCreated -> { | ||
| featureRegistry.getFeatures().forEach { feature -> | ||
| scope.launch { | ||
| feature.isEnabled.collect { | ||
| store.dispatch(SettingsAction.AIControlsFeatureStateLoaded(it, feature.id)) | ||
| } | ||
| } | ||
| } | ||
| scope.launch { | ||
| combine( | ||
| summarizationSettings.getFeatureEnabledUserStatus(), | ||
| summarizationSettings.getGestureEnabledUserStatus(), | ||
| ) { isFeatureEnabled, isGestureEnabled -> | ||
| SettingsAction.PageSummariesSettingsLoaded( | ||
| isFeatureEnabled = isFeatureEnabled, | ||
| isGestureEnabled = isGestureEnabled, | ||
| ) | ||
| }.collect { store.dispatch(it) } | ||
| } | ||
| } | ||
|
|
||
| is SettingsAction.AIControlsFeatureStateLoaded -> Unit | ||
| is SettingsAction.PageSummariesSettingsLoaded -> Unit | ||
| } | ||
| } | ||
| } | ||
|
|
||
| typealias SettingsStore = Store<SettingsState, SettingsAction> | ||
|
|
||
| /** | ||
| * Reducer for [SettingsStore] — applies [SettingsAction]s to [SettingsState]. | ||
| */ | ||
| fun settingsReducer(state: SettingsState, action: SettingsAction): SettingsState = when (action) { | ||
| is SettingsAction.AIControlsFeatureStateLoaded -> state.copy( | ||
| aiControlsState = state.aiControlsState.copy( | ||
| featuresEnabled = state.aiControlsState.featuresEnabled + (action.id to action.enabled), | ||
| ), | ||
| ) | ||
| is SettingsAction.PageSummariesSettingsLoaded -> state.copy( | ||
| summarizeSettingsState = state.summarizeSettingsState.copy( | ||
| isFeatureEnabled = action.isFeatureEnabled, | ||
| isGestureEnabled = action.isGestureEnabled, | ||
| ), | ||
| ) | ||
| SettingsAction.SettingsViewCreated -> state | ||
| } | ||
|
|
Contributor
There was a problem hiding this comment.
I think we should put these in different files - Middleware, State, etc. We should not put them here
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://treeherder.mozilla.org/jobs?repo=try&landoInstance=lando-prod-2025&landoCommitID=46701