From 75ca982d5b00b008abc3d1ba368558c112185a85 Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Mon, 12 Sep 2022 13:27:19 +0200 Subject: [PATCH] Adding option to reset settings --- .../android/dfu/analytics/DfuAnalytics.kt | 1 + .../no/nordicsemi/android/dfu/analytics/Events.kt | 4 ++++ .../dfu/profile/settings/view/SettingsScreen.kt | 14 ++++++++++++++ .../settings/view/SettingsScreenViewEvent.kt | 4 ++++ .../settings/viewmodel/SettingsViewModel.kt | 8 ++++++++ profile_dfu/src/main/res/values/strings.xml | 1 + 6 files changed, 32 insertions(+) diff --git a/lib_analytics/src/main/java/no/nordicsemi/android/dfu/analytics/DfuAnalytics.kt b/lib_analytics/src/main/java/no/nordicsemi/android/dfu/analytics/DfuAnalytics.kt index d1148977..4bf49f86 100644 --- a/lib_analytics/src/main/java/no/nordicsemi/android/dfu/analytics/DfuAnalytics.kt +++ b/lib_analytics/src/main/java/no/nordicsemi/android/dfu/analytics/DfuAnalytics.kt @@ -46,6 +46,7 @@ class DfuAnalytics @Inject constructor( AppOpenEvent, HandleDeepLinkEvent, InstallationStartedEvent, + ResetSettingsEvent, DFUAbortedEvent, DFUSuccessEvent -> analytics.logEvent(event.eventName) is DFUErrorEvent -> analytics.logEvent(event.eventName, event.createBundle()) diff --git a/lib_analytics/src/main/java/no/nordicsemi/android/dfu/analytics/Events.kt b/lib_analytics/src/main/java/no/nordicsemi/android/dfu/analytics/Events.kt index 3e62d6d5..f7843565 100644 --- a/lib_analytics/src/main/java/no/nordicsemi/android/dfu/analytics/Events.kt +++ b/lib_analytics/src/main/java/no/nordicsemi/android/dfu/analytics/Events.kt @@ -56,6 +56,10 @@ object HandleDeepLinkEvent : DfuEvent { override val eventName: String = "HANDLE_DEEP_LINK_EVENT" } +object ResetSettingsEvent: DfuEvent { + override val eventName: String = "SETTINGS_RESET" +} + sealed interface DFUResultEvent : DfuEvent object DFUSuccessEvent : DFUResultEvent { diff --git a/profile_dfu/src/main/java/no/nordicsemi/android/dfu/profile/settings/view/SettingsScreen.kt b/profile_dfu/src/main/java/no/nordicsemi/android/dfu/profile/settings/view/SettingsScreen.kt index ac0e2445..de92e8bf 100644 --- a/profile_dfu/src/main/java/no/nordicsemi/android/dfu/profile/settings/view/SettingsScreen.kt +++ b/profile_dfu/src/main/java/no/nordicsemi/android/dfu/profile/settings/view/SettingsScreen.kt @@ -36,6 +36,11 @@ import androidx.compose.foundation.layout.* import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.text.KeyboardOptions import androidx.compose.foundation.verticalScroll +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.outlined.Backup +import androidx.compose.material.icons.outlined.Restore +import androidx.compose.material.icons.outlined.Settings +import androidx.compose.material.icons.outlined.SettingsBackupRestore import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.runtime.saveable.rememberSaveable @@ -50,6 +55,7 @@ import no.nordicsemi.android.common.analytics.view.AnalyticsPermissionSwitch import no.nordicsemi.android.common.theme.view.NordicAppBar import no.nordicsemi.android.dfu.BuildConfig.VERSION_NAME import no.nordicsemi.android.dfu.profile.R +import no.nordicsemi.android.dfu.profile.main.view.OnSettingsButtonClick import no.nordicsemi.android.dfu.profile.settings.viewmodel.SettingsViewModel @Composable @@ -70,6 +76,14 @@ internal fun SettingsScreen() { NordicAppBar( text = stringResource(R.string.dfu_settings), onNavigationButtonClick = { onEvent(NavigateUp) }, + actions = { + IconButton(onClick = { onEvent(OnResetButtonClick) }) { + Icon( + imageVector = Icons.Outlined.SettingsBackupRestore, + contentDescription = stringResource(id = R.string.dfu_settings_reset) + ) + } + } ) // Scrollable Column diff --git a/profile_dfu/src/main/java/no/nordicsemi/android/dfu/profile/settings/view/SettingsScreenViewEvent.kt b/profile_dfu/src/main/java/no/nordicsemi/android/dfu/profile/settings/view/SettingsScreenViewEvent.kt index 5d15ce61..2751f525 100644 --- a/profile_dfu/src/main/java/no/nordicsemi/android/dfu/profile/settings/view/SettingsScreenViewEvent.kt +++ b/profile_dfu/src/main/java/no/nordicsemi/android/dfu/profile/settings/view/SettingsScreenViewEvent.kt @@ -31,6 +31,8 @@ package no.nordicsemi.android.dfu.profile.settings.view +import no.nordicsemi.android.dfu.profile.main.view.DFUViewEvent + sealed interface SettingsScreenViewEvent object OnPacketsReceiptNotificationSwitchClick : SettingsScreenViewEvent @@ -56,3 +58,5 @@ object OnAboutDfuClick : SettingsScreenViewEvent object OnAboutAppClick : SettingsScreenViewEvent object NavigateUp : SettingsScreenViewEvent + +object OnResetButtonClick : SettingsScreenViewEvent diff --git a/profile_dfu/src/main/java/no/nordicsemi/android/dfu/profile/settings/viewmodel/SettingsViewModel.kt b/profile_dfu/src/main/java/no/nordicsemi/android/dfu/profile/settings/viewmodel/SettingsViewModel.kt index 30b28e77..f2a2848b 100644 --- a/profile_dfu/src/main/java/no/nordicsemi/android/dfu/profile/settings/viewmodel/SettingsViewModel.kt +++ b/profile_dfu/src/main/java/no/nordicsemi/android/dfu/profile/settings/viewmodel/SettingsViewModel.kt @@ -59,6 +59,7 @@ class SettingsViewModel @Inject constructor( fun onEvent(event: SettingsScreenViewEvent) { when (event) { NavigateUp -> navigationManager.navigateUp() + OnResetButtonClick -> restoreDefaultSettings() OnAboutAppClick -> navigationManager.navigateTo(DfuWelcomeScreen) OnAboutDfuClick -> navigationManager.openLink(INFOCENTER_LINK) OnExternalMcuDfuSwitchClick -> onExternalMcuDfuSwitchClick() @@ -73,6 +74,13 @@ class SettingsViewModel @Inject constructor( } } + private fun restoreDefaultSettings() { + viewModelScope.launch { + repository.storeSettings(DFUSettings().copy(showWelcomeScreen = false)) + } + analytics.logEvent(ResetSettingsEvent) + } + private fun onExternalMcuDfuSwitchClick() { val currentValue = state.value.externalMcuDfu val newSettings = state.value.copy(externalMcuDfu = !currentValue) diff --git a/profile_dfu/src/main/res/values/strings.xml b/profile_dfu/src/main/res/values/strings.xml index 23780106..b6c731cc 100644 --- a/profile_dfu/src/main/res/values/strings.xml +++ b/profile_dfu/src/main/res/values/strings.xml @@ -77,6 +77,7 @@ Open settings screen. Navigate up + Restore default values Settings Other Force scanning