Skip to content

Commit

Permalink
Fixed SettingsViewModel needing login repository in noSync flavor
Browse files Browse the repository at this point in the history
  • Loading branch information
maltaisn committed Apr 27, 2020
1 parent 0313638 commit 551a07c
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Event<Int>>()
Expand All @@ -43,8 +41,8 @@ class SettingsViewModel @Inject constructor(
val exportDataEvent: LiveData<Event<String>>
get() = _exportDataEvent

private val _clearDataDialogEvent = MutableLiveData<Event<Boolean>>()
val clearDataDialogEvent: LiveData<Event<Boolean>>
private val _clearDataDialogEvent = MutableLiveData<Event<Int>>()
val clearDataDialogEvent: LiveData<Event<Int>>
get() = _clearDataDialogEvent


Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -43,4 +45,7 @@ abstract class SyncFlavorModule {
@Binds
abstract fun bindsNoteRefreshBehavior(behavior: NoSyncNoteRefreshBehavior): NoteRefreshBehavior

@Binds
abstract fun bindsClearDataBehavior(behavior: NoSyncClearDataBehavior): ClearDataBehavior

}
Original file line number Diff line number Diff line change
@@ -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

}
5 changes: 5 additions & 0 deletions app/src/sync/kotlin/com/maltaisn/notes/di/SyncFlavorModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -45,4 +47,7 @@ abstract class SyncFlavorModule {
@Binds
abstract fun bindsNoteRefreshBehavior(behavior: SyncNoteRefreshBehavior): NoteRefreshBehavior

@Binds
abstract fun bindsClearDataBehavior(behavior: SyncClearDataBehavior): ClearDataBehavior

}
Original file line number Diff line number Diff line change
@@ -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
}

}

0 comments on commit 551a07c

Please sign in to comment.