Skip to content

Bug 2037736 - Added a store to the settings and implemented storeProvider for persisted state#255

Open
fmasalha wants to merge 1 commit into
mozilla-firefox:autolandfrom
fmasalha:settingsStoreImpl
Open

Bug 2037736 - Added a store to the settings and implemented storeProvider for persisted state#255
fmasalha wants to merge 1 commit into
mozilla-firefox:autolandfrom
fmasalha:settingsStoreImpl

Conversation

@fmasalha
Copy link
Copy Markdown
Contributor

@github-actions
Copy link
Copy Markdown
Contributor

View this pull request in Lando to land it once approved.

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
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should put these in different files - Middleware, State, etc. We should not put them here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants