Simplify `PhoneFeature` code (#10810)
parent
74948cb3f1
commit
1a19b06227
@ -0,0 +1,38 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.settings.quicksettings
|
||||
|
||||
import mozilla.components.lib.state.Action
|
||||
import org.mozilla.fenix.settings.PhoneFeature
|
||||
|
||||
/**
|
||||
* Parent [Action] for all the [QuickSettingsFragmentState] changes.
|
||||
*/
|
||||
sealed class QuickSettingsFragmentAction : Action
|
||||
|
||||
/**
|
||||
* All possible [WebsiteInfoState] changes as result of user / system interactions.
|
||||
*/
|
||||
sealed class WebsiteInfoAction : QuickSettingsFragmentAction()
|
||||
|
||||
/**
|
||||
* All possible [WebsitePermissionsState] changes as result of user / system interactions.
|
||||
*/
|
||||
sealed class WebsitePermissionAction : QuickSettingsFragmentAction() {
|
||||
/**
|
||||
* Change resulting from toggling a specific [WebsitePermission] for the current website.
|
||||
*
|
||||
* @param updatedFeature [PhoneFeature] backing a certain [WebsitePermission].
|
||||
* Allows to easily identify which permission changed
|
||||
* **Must be the name of one of the properties of [WebsitePermissionsState]**.
|
||||
* @param updatedStatus [String] the new [WebsitePermission#status] which will be shown to the user.
|
||||
* @param updatedEnabledStatus [Boolean] the new [WebsitePermission#enabled] which will be shown to the user.
|
||||
*/
|
||||
class TogglePermission(
|
||||
val updatedFeature: PhoneFeature,
|
||||
val updatedStatus: String,
|
||||
val updatedEnabledStatus: Boolean
|
||||
) : WebsitePermissionAction()
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.settings.quicksettings
|
||||
|
||||
/**
|
||||
* Parent Reducer for all [QuickSettingsFragmentState]s of all Views shown in this Fragment.
|
||||
*/
|
||||
fun quickSettingsFragmentReducer(
|
||||
state: QuickSettingsFragmentState,
|
||||
action: QuickSettingsFragmentAction
|
||||
): QuickSettingsFragmentState {
|
||||
return when (action) {
|
||||
is WebsiteInfoAction -> {
|
||||
// There is no possible action that can change this View's state while it is displayed to the user.
|
||||
// Every time the View is recreated it starts with a fresh state. This is the only way to display
|
||||
// something different.
|
||||
state
|
||||
}
|
||||
is WebsitePermissionAction -> state.copy(
|
||||
websitePermissionsState = WebsitePermissionsStateReducer.reduce(
|
||||
state.websitePermissionsState,
|
||||
action
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
object WebsitePermissionsStateReducer {
|
||||
/**
|
||||
* Handles creating a new [WebsitePermissionsState] based on the specific [WebsitePermissionAction]
|
||||
*/
|
||||
fun reduce(
|
||||
state: WebsitePermissionsState,
|
||||
action: WebsitePermissionAction
|
||||
): WebsitePermissionsState {
|
||||
return when (action) {
|
||||
is WebsitePermissionAction.TogglePermission -> {
|
||||
val key = action.updatedFeature
|
||||
val newWebsitePermission = state.getValue(key).copy(
|
||||
status = action.updatedStatus,
|
||||
isEnabled = action.updatedEnabledStatus
|
||||
)
|
||||
|
||||
state + Pair(key, newWebsitePermission)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.settings.quicksettings
|
||||
|
||||
import androidx.annotation.ColorRes
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.annotation.StringRes
|
||||
import mozilla.components.lib.state.State
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.settings.PhoneFeature
|
||||
|
||||
/**
|
||||
* [State] containing all data displayed to the user by this Fragment.
|
||||
*
|
||||
* Partitioned further to contain mutiple states for each standalone View this Fragment holds.
|
||||
*/
|
||||
data class QuickSettingsFragmentState(
|
||||
val webInfoState: WebsiteInfoState,
|
||||
val websitePermissionsState: WebsitePermissionsState
|
||||
) : State
|
||||
|
||||
/**
|
||||
* [State] to be rendered by [WebsiteInfoView] indicating whether the connection is secure or not.
|
||||
*
|
||||
* @param websiteUrl [String] the URL of the current web page.
|
||||
* @param websiteTitle [String] the title of the current web page.
|
||||
* @param websiteSecurityUiValues UI values to represent the security of the website.
|
||||
*/
|
||||
data class WebsiteInfoState(
|
||||
val websiteUrl: String,
|
||||
val websiteTitle: String,
|
||||
val websiteSecurityUiValues: WebsiteSecurityUiValues,
|
||||
val certificateName: String
|
||||
) : State
|
||||
|
||||
enum class WebsiteSecurityUiValues(
|
||||
@StringRes val securityInfoRes: Int,
|
||||
@DrawableRes val iconRes: Int,
|
||||
@ColorRes val iconTintRes: Int
|
||||
) {
|
||||
SECURE(
|
||||
R.string.quick_settings_sheet_secure_connection,
|
||||
R.drawable.mozac_ic_lock,
|
||||
R.color.photonGreen50
|
||||
),
|
||||
INSECURE(
|
||||
R.string.quick_settings_sheet_insecure_connection,
|
||||
R.drawable.mozac_ic_globe,
|
||||
R.color.photonRed50
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* [State] to be rendered by [WebsitePermissionsView] displaying all explicitly allowed or blocked
|
||||
* website permissions.
|
||||
*/
|
||||
typealias WebsitePermissionsState = Map<PhoneFeature, WebsitePermission>
|
||||
|
||||
/**
|
||||
* Wrapper over a website permission encompassing all it's needed state to be rendered on the screen.
|
||||
*
|
||||
* Contains a limited number of implementations because there is a known, finite number of permissions
|
||||
* we need to display to the user.
|
||||
*
|
||||
* @property status The *allowed* / *blocked* permission status to be shown to the user.
|
||||
* @property isVisible Whether this permission should be shown to the user.
|
||||
* @property isEnabled Visual indication about whether this permission is *enabled* / *disabled*
|
||||
* @property isBlockedByAndroid Whether the corresponding *dangerous* Android permission is granted
|
||||
* for the app by the user or not.
|
||||
*/
|
||||
data class WebsitePermission(
|
||||
val phoneFeature: PhoneFeature,
|
||||
val status: String,
|
||||
val isVisible: Boolean,
|
||||
val isEnabled: Boolean,
|
||||
val isBlockedByAndroid: Boolean
|
||||
)
|
Loading…
Reference in New Issue