Skip to content

Commit

Permalink
Refactor FXIOS-9968 Studies opt out on Firefox iOS when telemetry usa…
Browse files Browse the repository at this point in the history
…ge is unchecked (backport #21917) (#22009)

Refactor FXIOS-9968 Studies opt out on Firefox iOS when telemetry usage is unchecked (#21917)

Refactor FXIOS-9968 Studies opt out on Firefox iOS when telemetry usage is unchecked

(cherry picked from commit 4a11949)

Co-authored-by: Nishant Bhasin <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
mergify[bot] and nbhasin2 authored Oct 7, 2024
1 parent fa60e3f commit 6c378ae
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class AppSettingsTableViewController: SettingsTableViewController,

weak var parentCoordinator: SettingsFlowDelegate?

// MARK: - Data Settings
private var sendAnonymousUsageDataSetting: BoolSetting?
private var studiesToggleSetting: BoolSetting?

// MARK: - Initializers
init(with profile: Profile,
and tabManager: TabManager,
Expand All @@ -65,6 +69,7 @@ class AppSettingsTableViewController: SettingsTableViewController,
self.tabManager = tabManager
self.settingsDelegate = delegate
setupNavigationBar()
setupDataSettings()
}

required init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -160,6 +165,31 @@ class AppSettingsTableViewController: SettingsTableViewController,
}
}

// MARK: Data settings setup

private func setupDataSettings() {
let anonymousUsageDataSetting = SendAnonymousUsageDataSetting(
prefs: profile.prefs,
delegate: settingsDelegate,
theme: themeManager.getCurrentTheme(for: windowUUID),
settingsDelegate: parentCoordinator
)

let studiesSetting = StudiesToggleSetting(
prefs: profile.prefs,
delegate: settingsDelegate,
theme: themeManager.getCurrentTheme(for: windowUUID),
settingsDelegate: parentCoordinator
)

anonymousUsageDataSetting.shouldSendUsageData = { value in
studiesSetting.updateSetting(for: value)
}

sendAnonymousUsageDataSetting = anonymousUsageDataSetting
studiesToggleSetting = studiesSetting
}

// MARK: - Generate Settings

override func generateSettings() -> [SettingSection] {
Expand Down Expand Up @@ -318,17 +348,12 @@ class AppSettingsTableViewController: SettingsTableViewController,
}

private func getSupportSettings() -> [SettingSection] {
guard let sendAnonymousUsageDataSetting, let studiesToggleSetting else { return [] }
let supportSettings = [
ShowIntroductionSetting(settings: self, settingsDelegate: self),
SendFeedbackSetting(settingsDelegate: parentCoordinator),
SendAnonymousUsageDataSetting(prefs: profile.prefs,
delegate: settingsDelegate,
theme: themeManager.getCurrentTheme(for: windowUUID),
settingsDelegate: parentCoordinator),
StudiesToggleSetting(prefs: profile.prefs,
delegate: settingsDelegate,
theme: themeManager.getCurrentTheme(for: windowUUID),
settingsDelegate: parentCoordinator),
sendAnonymousUsageDataSetting,
studiesToggleSetting,
OpenSupportPageSetting(delegate: settingsDelegate,
theme: themeManager.getCurrentTheme(for: windowUUID),
settingsDelegate: parentCoordinator),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Shared
class SendAnonymousUsageDataSetting: BoolSetting {
private weak var settingsDelegate: SupportSettingsDelegate?

var shouldSendUsageData: ((Bool) -> Void)?

init(prefs: Prefs,
delegate: SettingsDelegate?,
theme: Theme,
Expand Down Expand Up @@ -38,18 +40,25 @@ class SendAnonymousUsageDataSetting: BoolSetting {
prefKey: AppConstants.prefSendUsageData,
defaultValue: true,
attributedTitleText: NSAttributedString(string: .SendUsageSettingTitle),
attributedStatusText: statusText,
settingDidChange: {
// AdjustHelper.setEnabled($0)
DefaultGleanWrapper.shared.setUpload(isEnabled: $0)
Experiments.setTelemetrySetting($0)
}
attributedStatusText: statusText
)

setupSettingDidChange()

// We make sure to set this on initialization, in case the setting is turned off
// in which case, we would to make sure that users are opted out of experiments
Experiments.setTelemetrySetting(prefs.boolForKey(AppConstants.prefSendUsageData) ?? true)
}

private func setupSettingDidChange() {
self.settingDidChange = { [weak self] value in
// AdjustHelper.setEnabled($0)
DefaultGleanWrapper.shared.setUpload(isEnabled: value)
Experiments.setTelemetrySetting(value)
self?.shouldSendUsageData?(value)
}
}

override var accessibilityIdentifier: String? {
return AccessibilityIdentifiers.Settings.SendAnonymousUsageData.title
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class StudiesToggleSetting: BoolSetting {
)

self.settingsDelegate = settingsDelegate

super.init(
prefs: prefs,
prefKey: AppConstants.prefStudiesToggle,
Expand All @@ -39,9 +40,36 @@ class StudiesToggleSetting: BoolSetting {
Experiments.setStudiesSetting($0)
}
)
// We make sure to set this on initialization, in case the setting is turned off
// in which case, we would to make sure that users are opted out of experiments
Experiments.setStudiesSetting(prefs.boolForKey(AppConstants.prefStudiesToggle) ?? true)

setupSettingDidChange()

let sendUsageDataPref = prefs.boolForKey(AppConstants.prefSendUsageData) ?? true

// Special Case (EXP-4780) disable studies if usage data is disabled
updateSetting(for: sendUsageDataPref)
}

private func setupSettingDidChange() {
self.settingDidChange = {
Experiments.setStudiesSetting($0)
}
}

func updateSetting(for isUsageEnabled: Bool) {
guard !isUsageEnabled else {
// Note: switch should be enabled only when telemetry usage is enabled
control.setSwitchTappable(to: true)
// We make sure to set this on initialization, in case the setting is turned off
// in which case, we would to make sure that users are opted out of experiments
Experiments.setStudiesSetting(prefs?.boolForKey(AppConstants.prefStudiesToggle) ?? true)
return
}

// Special Case (EXP-4780) disable Studies if usage data is disabled
control.setSwitchTappable(to: false)
control.toggleSwitch(to: false)
writeBool(control.switchView)
Experiments.setStudiesSetting(false)
}

override var accessibilityIdentifier: String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ class PaddedSwitch: UIView {
switchView.isEnabled = isEnabled
}

func setSwitchTappable(to value: Bool) {
switchView.isEnabled = value
}

func toggleSwitch(to value: Bool, animated: Bool = true) {
switchView.setOn(value, animated: animated)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Expand All @@ -206,10 +214,10 @@ class PaddedSwitch: UIView {
class BoolSetting: Setting, FeatureFlaggable {
// Sometimes a subclass will manage its own pref setting. In that case the prefkey will be nil
let prefKey: String?

let prefs: Prefs?

var settingDidChange: ((Bool) -> Void)?
private let defaultValue: Bool?
private let settingDidChange: ((Bool) -> Void)?
private let statusText: NSAttributedString?
private let featureFlagName: NimbusFeatureFlagID?

Expand Down Expand Up @@ -332,7 +340,6 @@ class BoolSetting: Setting, FeatureFlaggable {
func switchValueChanged(_ control: UISwitch) {
writeBool(control)
settingDidChange?(control.isOn)

if let featureFlagName = featureFlagName {
TelemetryWrapper.recordEvent(category: .action,
method: .change,
Expand Down

0 comments on commit 6c378ae

Please sign in to comment.