From 551a07c6141f6211be3dc93b817bd3b549af8f71 Mon Sep 17 00:00:00 2001 From: maltaisn Date: Mon, 27 Apr 2020 09:13:03 -0400 Subject: [PATCH] Fixed SettingsViewModel needing login repository in noSync flavor --- .../notes/ui/settings/ClearDataBehavior.kt | 30 ++++++++++++++++ .../notes/ui/settings/SettingsFragment.kt | 8 ++--- .../notes/ui/settings/SettingsViewModel.kt | 12 +++---- .../com/maltaisn/notes/di/SyncFlavorModule.kt | 5 +++ .../ui/settings/NoSyncClearDataBehavior.kt | 28 +++++++++++++++ .../com/maltaisn/notes/di/SyncFlavorModule.kt | 5 +++ .../ui/settings/SyncClearDataBehavior.kt | 35 +++++++++++++++++++ 7 files changed, 109 insertions(+), 14 deletions(-) create mode 100644 app/src/main/kotlin/com/maltaisn/notes/ui/settings/ClearDataBehavior.kt create mode 100644 app/src/noSync/kotlin/com/maltaisn/notes/ui/settings/NoSyncClearDataBehavior.kt create mode 100644 app/src/sync/kotlin/com/maltaisn/notes/ui/settings/SyncClearDataBehavior.kt diff --git a/app/src/main/kotlin/com/maltaisn/notes/ui/settings/ClearDataBehavior.kt b/app/src/main/kotlin/com/maltaisn/notes/ui/settings/ClearDataBehavior.kt new file mode 100644 index 00000000..9483e5d3 --- /dev/null +++ b/app/src/main/kotlin/com/maltaisn/notes/ui/settings/ClearDataBehavior.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2020 Nicolas Maltais + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.maltaisn.notes.ui.settings + + +/** + * Used to provide flavor dependent refresh behavior of clearing data in [SettingsFragment]. + */ +interface ClearDataBehavior { + + /** + * Message that should be shown when user asks to clear data. + */ + val clearDataMessage: Int + +} diff --git a/app/src/main/kotlin/com/maltaisn/notes/ui/settings/SettingsFragment.kt b/app/src/main/kotlin/com/maltaisn/notes/ui/settings/SettingsFragment.kt index c99779ed..92e52e21 100644 --- a/app/src/main/kotlin/com/maltaisn/notes/ui/settings/SettingsFragment.kt +++ b/app/src/main/kotlin/com/maltaisn/notes/ui/settings/SettingsFragment.kt @@ -75,14 +75,10 @@ class SettingsFragment : PreferenceFragmentCompat(), ConfirmDialog.Callback { startActivity(Intent.createChooser(intent, null)) }) - viewModel.clearDataDialogEvent.observe(viewLifecycleOwner, EventObserver { isSyncMessage -> + viewModel.clearDataDialogEvent.observe(viewLifecycleOwner, EventObserver { message -> ConfirmDialog.newInstance( title = R.string.pref_data_clear, - message = if (isSyncMessage) { - R.string.pref_data_clear_confirm_message_sync - } else { - R.string.pref_data_clear_confirm_message - }, + message = message, btnPositive = R.string.action_clear ).show(childFragmentManager, CLEAR_DATA_DIALOG_TAG) }) diff --git a/app/src/main/kotlin/com/maltaisn/notes/ui/settings/SettingsViewModel.kt b/app/src/main/kotlin/com/maltaisn/notes/ui/settings/SettingsViewModel.kt index 79a6650b..47c4a522 100644 --- a/app/src/main/kotlin/com/maltaisn/notes/ui/settings/SettingsViewModel.kt +++ b/app/src/main/kotlin/com/maltaisn/notes/ui/settings/SettingsViewModel.kt @@ -20,9 +20,7 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.maltaisn.notes.BuildConfig import com.maltaisn.notes.R -import com.maltaisn.notes.model.LoginRepository import com.maltaisn.notes.model.NotesRepository import com.maltaisn.notes.ui.Event import com.maltaisn.notes.ui.send @@ -32,7 +30,7 @@ import javax.inject.Inject class SettingsViewModel @Inject constructor( private val notesRepository: NotesRepository, - private val loginRepository: LoginRepository + private val clearDataBehavior: ClearDataBehavior ) : ViewModel() { private val _messageEvent = MutableLiveData>() @@ -43,8 +41,8 @@ class SettingsViewModel @Inject constructor( val exportDataEvent: LiveData> get() = _exportDataEvent - private val _clearDataDialogEvent = MutableLiveData>() - val clearDataDialogEvent: LiveData> + private val _clearDataDialogEvent = MutableLiveData>() + val clearDataDialogEvent: LiveData> get() = _clearDataDialogEvent @@ -55,9 +53,7 @@ class SettingsViewModel @Inject constructor( } fun clearDataPre() { - val isSyncFlavor = BuildConfig.FLAVOR == BuildConfig.FLAVOR_SYNC - val isSignedIn = loginRepository.isUserSignedIn - _clearDataDialogEvent.send(isSyncFlavor && isSignedIn) + _clearDataDialogEvent.send(clearDataBehavior.clearDataMessage) } fun clearData() { diff --git a/app/src/noSync/kotlin/com/maltaisn/notes/di/SyncFlavorModule.kt b/app/src/noSync/kotlin/com/maltaisn/notes/di/SyncFlavorModule.kt index 350ec36d..e5d695e5 100644 --- a/app/src/noSync/kotlin/com/maltaisn/notes/di/SyncFlavorModule.kt +++ b/app/src/noSync/kotlin/com/maltaisn/notes/di/SyncFlavorModule.kt @@ -24,6 +24,8 @@ import com.maltaisn.notes.ui.home.NoSyncNoteRefreshBehavior import com.maltaisn.notes.ui.home.NoteRefreshBehavior import com.maltaisn.notes.ui.main.LifecycleBehavior import com.maltaisn.notes.ui.main.NoSyncLifecycleBehavior +import com.maltaisn.notes.ui.settings.ClearDataBehavior +import com.maltaisn.notes.ui.settings.NoSyncClearDataBehavior import dagger.Binds import dagger.Module @@ -43,4 +45,7 @@ abstract class SyncFlavorModule { @Binds abstract fun bindsNoteRefreshBehavior(behavior: NoSyncNoteRefreshBehavior): NoteRefreshBehavior + @Binds + abstract fun bindsClearDataBehavior(behavior: NoSyncClearDataBehavior): ClearDataBehavior + } diff --git a/app/src/noSync/kotlin/com/maltaisn/notes/ui/settings/NoSyncClearDataBehavior.kt b/app/src/noSync/kotlin/com/maltaisn/notes/ui/settings/NoSyncClearDataBehavior.kt new file mode 100644 index 00000000..6defd2ff --- /dev/null +++ b/app/src/noSync/kotlin/com/maltaisn/notes/ui/settings/NoSyncClearDataBehavior.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2020 Nicolas Maltais + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.maltaisn.notes.ui.settings + +import com.maltaisn.notes.R +import javax.inject.Inject + + +class NoSyncClearDataBehavior @Inject constructor() : ClearDataBehavior { + + override val clearDataMessage: Int + get() = R.string.pref_data_clear_confirm_message + +} diff --git a/app/src/sync/kotlin/com/maltaisn/notes/di/SyncFlavorModule.kt b/app/src/sync/kotlin/com/maltaisn/notes/di/SyncFlavorModule.kt index 124c40ec..32c8daa8 100644 --- a/app/src/sync/kotlin/com/maltaisn/notes/di/SyncFlavorModule.kt +++ b/app/src/sync/kotlin/com/maltaisn/notes/di/SyncFlavorModule.kt @@ -24,6 +24,8 @@ import com.maltaisn.notes.ui.home.NoteRefreshBehavior import com.maltaisn.notes.ui.home.SyncNoteRefreshBehavior import com.maltaisn.notes.ui.main.LifecycleBehavior import com.maltaisn.notes.ui.main.SyncLifecycleBehavior +import com.maltaisn.notes.ui.settings.ClearDataBehavior +import com.maltaisn.notes.ui.settings.SyncClearDataBehavior import dagger.Binds import dagger.Module @@ -45,4 +47,7 @@ abstract class SyncFlavorModule { @Binds abstract fun bindsNoteRefreshBehavior(behavior: SyncNoteRefreshBehavior): NoteRefreshBehavior + @Binds + abstract fun bindsClearDataBehavior(behavior: SyncClearDataBehavior): ClearDataBehavior + } diff --git a/app/src/sync/kotlin/com/maltaisn/notes/ui/settings/SyncClearDataBehavior.kt b/app/src/sync/kotlin/com/maltaisn/notes/ui/settings/SyncClearDataBehavior.kt new file mode 100644 index 00000000..a87f27b2 --- /dev/null +++ b/app/src/sync/kotlin/com/maltaisn/notes/ui/settings/SyncClearDataBehavior.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2020 Nicolas Maltais + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.maltaisn.notes.ui.settings + +import com.maltaisn.notes.R +import com.maltaisn.notes.model.LoginRepository +import javax.inject.Inject + + +class SyncClearDataBehavior @Inject constructor( + private val loginRepository: LoginRepository +) : ClearDataBehavior { + + override val clearDataMessage: Int + get() = if (loginRepository.isUserSignedIn) { + R.string.pref_data_clear_confirm_message_sync + } else { + R.string.pref_data_clear_confirm_message + } + +}