Analytics: Display the opt in screen from the setting, and cleanup the code

This commit is contained in:
Benoit Marty 2021-12-07 14:22:11 +01:00
parent 219bb89c3a
commit d63f9073b7
2 changed files with 11 additions and 12 deletions

View File

@ -21,12 +21,4 @@ import com.airbnb.mvrx.MavericksState
data class AnalyticsConsentViewState(
val userConsent: Boolean = false,
val didAskUserConsent: Boolean = false
) : MavericksState {
val shouldCheckTheBox: Boolean =
if (didAskUserConsent) {
userConsent
} else {
// default value
true
}
}
) : MavericksState

View File

@ -288,7 +288,7 @@ class VectorSettingsSecurityPrivacyFragment @Inject constructor(
}
private fun observeAnalyticsState() {
analyticsConsentViewModel.onEach(AnalyticsConsentViewState::shouldCheckTheBox) {
analyticsConsentViewModel.onEach(AnalyticsConsentViewState::userConsent) {
analyticsConsent.isChecked = it
}
}
@ -296,8 +296,15 @@ class VectorSettingsSecurityPrivacyFragment @Inject constructor(
private fun setUpAnalytics() {
analyticsCategory.isVisible = analyticsConfig.isEnabled
analyticsConsent.setOnPreferenceClickListener {
analyticsConsentViewModel.handle(AnalyticsConsentViewActions.SetUserConsent(analyticsConsent.isChecked))
analyticsConsent.setOnPreferenceChangeListener { _, newValue ->
val newValueBool = newValue as? Boolean ?: false
if (newValueBool) {
// User want to enable analytics, display the opt in screen
navigator.openAnalyticsOptIn(requireContext())
} else {
// Just disable analytics
analyticsConsentViewModel.handle(AnalyticsConsentViewActions.SetUserConsent(false))
}
true
}
}