From 407a9455fa9a7f1156e28bdb6c5689aacbc670f8 Mon Sep 17 00:00:00 2001 From: Matt Chowning Date: Tue, 8 Aug 2023 15:34:32 -0400 Subject: [PATCH] Convert privacy settings into UserSettings (#1247) --- .../pocketcasts/PocketCastsApplication.kt | 4 +-- .../settings/privacy/PrivacyViewModel.kt | 4 +-- .../settings/privacy/UserAnalyticsSettings.kt | 4 +-- .../pocketcasts/analytics/AnalyticsTracker.kt | 4 +-- .../analytics/FirebaseAnalyticsTracker.kt | 2 +- .../pocketcasts/preferences/Settings.kt | 15 +++----- .../pocketcasts/preferences/SettingsImpl.kt | 36 ++++++++----------- .../wear/PocketCastsWearApplication.kt | 4 +-- .../ui/settings/PrivacySettingsViewModel.kt | 6 ++-- 9 files changed, 34 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/au/com/shiftyjelly/pocketcasts/PocketCastsApplication.kt b/app/src/main/java/au/com/shiftyjelly/pocketcasts/PocketCastsApplication.kt index 81b32ffd0e9..426a61a181b 100644 --- a/app/src/main/java/au/com/shiftyjelly/pocketcasts/PocketCastsApplication.kt +++ b/app/src/main/java/au/com/shiftyjelly/pocketcasts/PocketCastsApplication.kt @@ -114,12 +114,12 @@ class PocketCastsApplication : Application(), Configuration.Provider { } SentryAndroid.init(this) { options -> - options.dsn = if (settings.getSendCrashReports()) settings.getSentryDsn() else "" + options.dsn = if (settings.sendCrashReports.flow.value) settings.getSentryDsn() else "" options.setTag(SentryHelper.GLOBAL_TAG_APP_PLATFORM, AppPlatform.MOBILE.value) } // Link email to Sentry crash reports only if the user has opted in - if (settings.getLinkCrashReportsToUser()) { + if (settings.linkCrashReportsToUser.flow.value) { syncManager.getEmail()?.let { syncEmail -> val user = User().apply { email = syncEmail } Sentry.setUser(user) diff --git a/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/privacy/PrivacyViewModel.kt b/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/privacy/PrivacyViewModel.kt index 38ad9027532..1ee2a043ee9 100644 --- a/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/privacy/PrivacyViewModel.kt +++ b/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/privacy/PrivacyViewModel.kt @@ -33,8 +33,8 @@ class PrivacyViewModel @Inject constructor( private val mutableUiState = MutableStateFlow( UiState.Loaded( analytics = analyticsTracker.getSendUsageStats(), - crashReports = settings.getSendCrashReports(), - linkAccount = settings.getLinkCrashReportsToUser(), + crashReports = settings.sendCrashReports.flow.value, + linkAccount = settings.linkCrashReportsToUser.flow.value, getUserEmail = { syncManager.getEmail() } ) ) diff --git a/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/privacy/UserAnalyticsSettings.kt b/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/privacy/UserAnalyticsSettings.kt index d4a5098b2ca..65001350c03 100644 --- a/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/privacy/UserAnalyticsSettings.kt +++ b/modules/features/settings/src/main/java/au/com/shiftyjelly/pocketcasts/settings/privacy/UserAnalyticsSettings.kt @@ -34,13 +34,13 @@ class UserAnalyticsSettings @Inject constructor( } else { SentryAndroid.init(context) { it.dsn = "" } } - settings.setSendCrashReports(enabled) + settings.sendCrashReports.set(enabled) } fun updateLinkAccountSetting(enabled: Boolean) { val user = if (enabled) User().apply { email = syncManager.getEmail() } else null Sentry.setUser(user) - settings.setLinkCrashReportsToUser(enabled) + settings.linkCrashReportsToUser.set(enabled) } } diff --git a/modules/services/analytics/src/main/java/au/com/shiftyjelly/pocketcasts/analytics/AnalyticsTracker.kt b/modules/services/analytics/src/main/java/au/com/shiftyjelly/pocketcasts/analytics/AnalyticsTracker.kt index f646b764b4c..73b92391b4c 100644 --- a/modules/services/analytics/src/main/java/au/com/shiftyjelly/pocketcasts/analytics/AnalyticsTracker.kt +++ b/modules/services/analytics/src/main/java/au/com/shiftyjelly/pocketcasts/analytics/AnalyticsTracker.kt @@ -35,12 +35,12 @@ object AnalyticsTracker { fun setSendUsageStats(send: Boolean) { if (send != getSendUsageStats()) { - settings.setSendUsageStats(send) + settings.collectAnalytics.set(send) if (!send) { trackers.forEach { it.clearAllData() } } } } - fun getSendUsageStats() = settings.getSendUsageStats() + fun getSendUsageStats() = settings.collectAnalytics.flow.value } diff --git a/modules/services/analytics/src/main/java/au/com/shiftyjelly/pocketcasts/analytics/FirebaseAnalyticsTracker.kt b/modules/services/analytics/src/main/java/au/com/shiftyjelly/pocketcasts/analytics/FirebaseAnalyticsTracker.kt index 131f3516a76..8820c36cbde 100644 --- a/modules/services/analytics/src/main/java/au/com/shiftyjelly/pocketcasts/analytics/FirebaseAnalyticsTracker.kt +++ b/modules/services/analytics/src/main/java/au/com/shiftyjelly/pocketcasts/analytics/FirebaseAnalyticsTracker.kt @@ -261,7 +261,7 @@ object FirebaseAnalyticsTracker { } private fun logEvent(name: String, bundle: Bundle? = Bundle()) { - if (settings.getSendUsageStats()) { + if (settings.collectAnalytics.flow.value) { firebaseAnalytics.logEvent(name, bundle) Timber.d("Analytic event $name $bundle") diff --git a/modules/services/preferences/src/main/java/au/com/shiftyjelly/pocketcasts/preferences/Settings.kt b/modules/services/preferences/src/main/java/au/com/shiftyjelly/pocketcasts/preferences/Settings.kt index d8182ee6558..76dd36674be 100644 --- a/modules/services/preferences/src/main/java/au/com/shiftyjelly/pocketcasts/preferences/Settings.kt +++ b/modules/services/preferences/src/main/java/au/com/shiftyjelly/pocketcasts/preferences/Settings.kt @@ -467,16 +467,11 @@ interface Settings { fun setTimesToShowBatteryWarning(value: Int) fun getTimesToShowBatteryWarning(): Int - // Only the AnalyticsTracker object should update or retrieve SendUsageState directly. Everything else - // should update/access this setting through the AnalyticsTracker. - fun setSendUsageStats(value: Boolean) - fun getSendUsageStats(): Boolean - - fun setSendCrashReports(value: Boolean) - fun getSendCrashReports(): Boolean - - fun setLinkCrashReportsToUser(value: Boolean) - fun getLinkCrashReportsToUser(): Boolean + // Only the AnalyticsTracker object should update SendUsageState directly. Everything else + // should update this setting through the AnalyticsTracker. + val collectAnalytics: UserSetting + val sendCrashReports: UserSetting + val linkCrashReportsToUser: UserSetting fun setEndOfYearShowBadge2022(value: Boolean) fun getEndOfYearShowBadge2022(): Boolean diff --git a/modules/services/preferences/src/main/java/au/com/shiftyjelly/pocketcasts/preferences/SettingsImpl.kt b/modules/services/preferences/src/main/java/au/com/shiftyjelly/pocketcasts/preferences/SettingsImpl.kt index 4325c2dda69..c82b9f8eee9 100644 --- a/modules/services/preferences/src/main/java/au/com/shiftyjelly/pocketcasts/preferences/SettingsImpl.kt +++ b/modules/services/preferences/src/main/java/au/com/shiftyjelly/pocketcasts/preferences/SettingsImpl.kt @@ -65,9 +65,6 @@ class SettingsImpl @Inject constructor( companion object { private const val DEVICE_ID_KEY = "DeviceIdKey" private const val SHOWN_BATTERY_WARNING_KEY = "ShownBetteryWarningKey" - private const val SEND_USAGE_STATS_KEY = "SendUsageStatsKey" - private const val SEND_CRASH_REPORTS_KEY = "SendCrashReportsKey" - private const val LINK_CRASH_REPORTS_TO_USER_KEY = "LinkCrashReportsToUserKey" private const val END_OF_YEAR_SHOW_BADGE_2022_KEY = "EndOfYearShowBadge2022Key" private const val END_OF_YEAR_MODAL_HAS_BEEN_SHOWN_KEY = "EndOfYearModalHasBeenShownKey" private const val DONE_INITIAL_ONBOARDING_KEY = "CompletedOnboardingKey" @@ -1298,26 +1295,23 @@ class SettingsImpl @Inject constructor( override fun getTimesToShowBatteryWarning(): Int = getInt(SHOWN_BATTERY_WARNING_KEY, 4) - override fun setSendUsageStats(value: Boolean) { - setBoolean(SEND_USAGE_STATS_KEY, value) - } - - override fun getSendUsageStats(): Boolean = - getBoolean(SEND_USAGE_STATS_KEY, true) - - override fun setSendCrashReports(value: Boolean) { - setBoolean(SEND_CRASH_REPORTS_KEY, value) - } - - override fun getSendCrashReports(): Boolean = - getBoolean(SEND_CRASH_REPORTS_KEY, true) + override val collectAnalytics = UserSetting.BoolPref( + sharedPrefKey = "SendUsageStatsKey", + defaultValue = true, + sharedPrefs = sharedPreferences, + ) - override fun setLinkCrashReportsToUser(value: Boolean) { - setBoolean(LINK_CRASH_REPORTS_TO_USER_KEY, value) - } + override val sendCrashReports = UserSetting.BoolPref( + sharedPrefKey = "SendCrashReportsKey", + defaultValue = true, + sharedPrefs = sharedPreferences, + ) - override fun getLinkCrashReportsToUser(): Boolean = - getBoolean(LINK_CRASH_REPORTS_TO_USER_KEY, false) + override val linkCrashReportsToUser = UserSetting.BoolPref( + sharedPrefKey = "LinkCrashReportsToUserKey", + defaultValue = false, + sharedPrefs = sharedPreferences, + ) override fun setEndOfYearShowBadge2022(value: Boolean) { setBoolean(END_OF_YEAR_SHOW_BADGE_2022_KEY, value) diff --git a/wear/src/main/kotlin/au/com/shiftyjelly/pocketcasts/wear/PocketCastsWearApplication.kt b/wear/src/main/kotlin/au/com/shiftyjelly/pocketcasts/wear/PocketCastsWearApplication.kt index ed52f70b0c3..cb9027d1216 100644 --- a/wear/src/main/kotlin/au/com/shiftyjelly/pocketcasts/wear/PocketCastsWearApplication.kt +++ b/wear/src/main/kotlin/au/com/shiftyjelly/pocketcasts/wear/PocketCastsWearApplication.kt @@ -68,12 +68,12 @@ class PocketCastsWearApplication : Application(), Configuration.Provider { private fun setupSentry() { SentryAndroid.init(this) { options -> - options.dsn = if (settings.getSendCrashReports()) settings.getSentryDsn() else "" + options.dsn = if (settings.sendCrashReports.flow.value) settings.getSentryDsn() else "" options.setTag(SentryHelper.GLOBAL_TAG_APP_PLATFORM, AppPlatform.WEAR.value) } // Link email to Sentry crash reports only if the user has opted in - if (settings.getLinkCrashReportsToUser()) { + if (settings.linkCrashReportsToUser.flow.value) { syncManager.getEmail()?.let { syncEmail -> val user = User().apply { email = syncEmail } Sentry.setUser(user) diff --git a/wear/src/main/kotlin/au/com/shiftyjelly/pocketcasts/wear/ui/settings/PrivacySettingsViewModel.kt b/wear/src/main/kotlin/au/com/shiftyjelly/pocketcasts/wear/ui/settings/PrivacySettingsViewModel.kt index bbfc3a5aa46..9cf31c9ba13 100644 --- a/wear/src/main/kotlin/au/com/shiftyjelly/pocketcasts/wear/ui/settings/PrivacySettingsViewModel.kt +++ b/wear/src/main/kotlin/au/com/shiftyjelly/pocketcasts/wear/ui/settings/PrivacySettingsViewModel.kt @@ -23,9 +23,9 @@ class PrivacySettingsViewModel @Inject constructor( private val _state = MutableStateFlow( State( - sendAnalytics = settings.getSendUsageStats(), - sendCrashReports = settings.getSendCrashReports(), - linkCrashReportsToUser = settings.getLinkCrashReportsToUser(), + sendAnalytics = settings.collectAnalytics.flow.value, + sendCrashReports = settings.sendCrashReports.flow.value, + linkCrashReportsToUser = settings.linkCrashReportsToUser.flow.value, ) ) val state = _state.asStateFlow()