Skip to content

Commit

Permalink
Convert privacy settings into UserSettings (#1247)
Browse files Browse the repository at this point in the history
  • Loading branch information
mchowning authored Aug 8, 2023
1 parent 6b2e24b commit 407a945
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class PrivacyViewModel @Inject constructor(
private val mutableUiState = MutableStateFlow<UiState>(
UiState.Loaded(
analytics = analyticsTracker.getSendUsageStats(),
crashReports = settings.getSendCrashReports(),
linkAccount = settings.getLinkCrashReportsToUser(),
crashReports = settings.sendCrashReports.flow.value,
linkAccount = settings.linkCrashReportsToUser.flow.value,
getUserEmail = { syncManager.getEmail() }
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean>
val sendCrashReports: UserSetting<Boolean>
val linkCrashReportsToUser: UserSetting<Boolean>

fun setEndOfYearShowBadge2022(value: Boolean)
fun getEndOfYearShowBadge2022(): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 407a945

Please sign in to comment.