Better inheritance

This commit is contained in:
J-Jamet
2021-08-04 13:24:45 +02:00
parent fa5ae17621
commit 9f7a0d4f17
10 changed files with 100 additions and 100 deletions

View File

@@ -116,6 +116,7 @@ abstract class DatabaseActivity: StylishActivity(), DatabaseRetrieval {
actionTask: String,
result: ActionRunnable.Result
) {
mDatabaseViewModel.onActionFinished(database, actionTask, result)
// optional method implementation
}

View File

@@ -90,10 +90,10 @@ object IconPackChooser {
}
fun setSelectedIconPack(iconDrawableFactory: IconDrawableFactory, iconPackIdString: String?) {
fun setSelectedIconPack(iconPackIdString: String?) {
// TODO Clear Icon pack cache
for (iconPack in iconPackList) {
if (iconPack.id == iconPackIdString) {
iconDrawableFactory.clearCache()
iconPackSelected = iconPack
break
}
@@ -108,8 +108,10 @@ object IconPackChooser {
*/
fun getSelectedIconPack(context: Context, iconDrawableFactory: IconDrawableFactory): IconPack? {
build(context)
if (iconPackSelected == null)
setSelectedIconPack(iconDrawableFactory, PreferencesUtil.getIconPackSelectedId(context))
if (iconPackSelected == null) {
setSelectedIconPack(PreferencesUtil.getIconPackSelectedId(context))
iconDrawableFactory.clearCache()
}
return iconPackSelected
}

View File

@@ -1,52 +0,0 @@
package com.kunzisoft.keepass.settings
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.preference.PreferenceFragmentCompat
import com.kunzisoft.keepass.activities.DatabaseRetrieval
import com.kunzisoft.keepass.activities.lock.resetAppTimeoutWhenViewFocusedOrChanged
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.tasks.ActionRunnable
import com.kunzisoft.keepass.viewmodels.DatabaseViewModel
abstract class DatabasePreferenceFragment : PreferenceFragmentCompat(), DatabaseRetrieval {
private val mDatabaseViewModel: DatabaseViewModel by activityViewModels()
private var mDatabase: Database? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
mDatabaseViewModel.database.observe(viewLifecycleOwner) { database ->
mDatabase = database
onDatabaseRetrieved(database)
}
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.resetAppTimeoutWhenViewFocusedOrChanged(requireContext(), mDatabase)
}
override fun onDatabaseActionFinished(
database: Database,
actionTask: String,
result: ActionRunnable.Result
) {
// Can be overridden by a subclass
}
protected fun saveDatabase(save: Boolean) {
mDatabaseViewModel.saveDatabase(save)
}
protected fun reloadDatabase() {
mDatabaseViewModel.reloadDatabase(false)
}
}

View File

@@ -21,14 +21,18 @@ package com.kunzisoft.keepass.settings
import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.viewmodels.DatabaseViewModel
class MainPreferenceFragment : DatabasePreferenceFragment() {
class MainPreferenceFragment : PreferenceFragmentCompat() {
private var mCallback: Callback? = null
private val mDatabaseViewModel: DatabaseViewModel by activityViewModels()
private var mDatabaseLoaded: Boolean = false
override fun onAttach(context: Context) {
@@ -45,9 +49,17 @@ class MainPreferenceFragment : DatabasePreferenceFragment() {
mCallback = null
super.onDetach()
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
mDatabaseViewModel.database.observe(viewLifecycleOwner) { database ->
mDatabaseLoaded = database?.loaded == true
checkDatabaseLoaded()
}
super.onViewCreated(view, savedInstanceState)
}
override fun onDatabaseRetrieved(database: Database?) {
mDatabaseLoaded = database?.loaded == true
private fun checkDatabaseLoaded() {
findPreference<Preference>(getString(R.string.settings_database_key))
?.isEnabled = mDatabaseLoaded
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
@@ -87,10 +99,6 @@ class MainPreferenceFragment : DatabasePreferenceFragment() {
mCallback?.onNestedPreferenceSelected(NestedSettingsFragment.Screen.DATABASE)
false
}
// TODO Check
if (mDatabaseLoaded) {
isEnabled = false
}
}
findPreference<Preference>(getString(R.string.settings_database_security_key))?.apply {
@@ -106,6 +114,8 @@ class MainPreferenceFragment : DatabasePreferenceFragment() {
false
}
}
checkDatabaseLoaded()
}
interface Callback {

View File

@@ -43,9 +43,7 @@ import com.kunzisoft.keepass.activities.stylish.Stylish
import com.kunzisoft.keepass.app.database.CipherDatabaseAction
import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction
import com.kunzisoft.keepass.biometric.AdvancedUnlockManager
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.education.Education
import com.kunzisoft.keepass.icons.IconDrawableFactory
import com.kunzisoft.keepass.icons.IconPackChooser
import com.kunzisoft.keepass.settings.preference.IconPackListPreference
import com.kunzisoft.keepass.settings.preferencedialogfragment.DurationDialogFragmentCompat
@@ -56,12 +54,6 @@ class NestedAppSettingsFragment : NestedSettingsFragment() {
private var deleteKeysAlertDialog: AlertDialog? = null
private var mIconDrawableFactory: IconDrawableFactory? = null
override fun onDatabaseRetrieved(database: Database?) {
this.mIconDrawableFactory = database?.iconDrawableFactory
}
override fun onCreateScreenPreference(screen: Screen, savedInstanceState: Bundle?, rootKey: String?) {
// Load the preferences from an XML resource
@@ -434,10 +426,7 @@ class NestedAppSettingsFragment : NestedSettingsFragment() {
}
}
if (iconPackEnabled) {
// TODO Check
mIconDrawableFactory?.let {
IconPackChooser.setSelectedIconPack(it, iconPackId)
}
IconPackChooser.setSelectedIconPack(iconPackId)
}
iconPackEnabled
}

View File

@@ -24,13 +24,16 @@ import android.os.Bundle
import android.util.Log
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.activityViewModels
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import androidx.preference.SwitchPreference
import com.kunzisoft.androidclearchroma.ChromaUtil
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.DatabaseRetrieval
import com.kunzisoft.keepass.activities.dialogs.AssignMasterKeyDialogFragment
import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper
import com.kunzisoft.keepass.activities.lock.resetAppTimeoutWhenViewFocusedOrChanged
import com.kunzisoft.keepass.database.crypto.EncryptionAlgorithm
import com.kunzisoft.keepass.database.crypto.kdf.KdfEngine
import com.kunzisoft.keepass.database.element.Database
@@ -41,9 +44,11 @@ import com.kunzisoft.keepass.settings.preference.*
import com.kunzisoft.keepass.settings.preferencedialogfragment.*
import com.kunzisoft.keepass.tasks.ActionRunnable
import com.kunzisoft.keepass.utils.MenuUtil
import com.kunzisoft.keepass.viewmodels.DatabaseViewModel
class NestedDatabaseSettingsFragment : NestedSettingsFragment() {
class NestedDatabaseSettingsFragment : NestedSettingsFragment(), DatabaseRetrieval {
private val mDatabaseViewModel: DatabaseViewModel by activityViewModels()
private var mDatabase: Database? = null
private var mDatabaseReadOnly: Boolean = false
private var mDatabaseAutoSaveEnabled: Boolean = true
@@ -65,6 +70,21 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment() {
private var mMemoryPref: InputKdfSizePreference? = null
private var mParallelismPref: InputKdfNumberPreference? = null
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
mDatabaseViewModel.database.observe(viewLifecycleOwner) { database ->
mDatabase = database
onDatabaseRetrieved(database)
}
mDatabaseViewModel.actionFinished.observe(viewLifecycleOwner) {
onDatabaseActionFinished(it.database, it.actionTask, it.result)
}
view.resetAppTimeoutWhenViewFocusedOrChanged(requireContext(), mDatabase)
}
override fun onCreateScreenPreference(screen: Screen, savedInstanceState: Bundle?, rootKey: String?) {
setHasOptionsMenu(true)
@@ -95,6 +115,14 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment() {
}
}
private fun saveDatabase(save: Boolean) {
mDatabaseViewModel.saveDatabase(save)
}
private fun reloadDatabase() {
mDatabaseViewModel.reloadDatabase(false)
}
override fun onDatabaseRetrieved(database: Database?) {
mDatabase = database
mDatabaseReadOnly = mDatabaseReadOnly || database?.isReadOnly == true
@@ -343,6 +371,7 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment() {
return view
}
// TODO check error
override fun onDatabaseActionFinished(database: Database,
actionTask: String,
result: ActionRunnable.Result) {

View File

@@ -22,13 +22,13 @@ package com.kunzisoft.keepass.settings
import android.content.res.Resources
import android.os.Bundle
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.dialogs.UnderDevelopmentFeatureDialogFragment
import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper
// TODO Move database fragment in sub class
abstract class NestedSettingsFragment : DatabasePreferenceFragment() {
abstract class NestedSettingsFragment : PreferenceFragmentCompat() {
enum class Screen {
APPLICATION, FORM_FILLING, ADVANCED_UNLOCK, APPEARANCE, DATABASE, DATABASE_SECURITY, DATABASE_MASTER_KEY

View File

@@ -133,12 +133,6 @@ open class SettingsActivity
finish()
}
}
else -> {
// TODO Call result in fragment by viewmodel
(supportFragmentManager
.findFragmentByTag(TAG_NESTED) as NestedSettingsFragment?)
?.onDatabaseActionFinished(database, actionTask, result)
}
}
coordinatorLayout?.showActionErrorIfNeeded(result)
}

View File

@@ -60,14 +60,6 @@ abstract class DatabaseSavePreferenceDialogFragmentCompat
this.mDatabase = database
}
override fun onDialogClosed(positiveResult: Boolean) {
onDialogClosed(mDatabase, positiveResult)
}
open fun onDialogClosed(database: Database?, positiveResult: Boolean) {
// To inherit to save element in database
}
override fun onDatabaseActionFinished(
database: Database,
actionTask: String,
@@ -76,6 +68,14 @@ abstract class DatabaseSavePreferenceDialogFragmentCompat
// Not used
}
override fun onDialogClosed(positiveResult: Boolean) {
onDialogClosed(mDatabase, positiveResult)
}
open fun onDialogClosed(database: Database?, positiveResult: Boolean) {
// To inherit to save element in database
}
protected fun saveColor(oldColor: String,
newColor: String) {
mDatabaseViewModel.saveColor(oldColor, newColor, mDatabaseAutoSaveEnable)

View File

@@ -8,12 +8,16 @@ import com.kunzisoft.keepass.database.crypto.kdf.KdfEngine
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.Group
import com.kunzisoft.keepass.database.element.database.CompressionAlgorithm
import com.kunzisoft.keepass.tasks.ActionRunnable
class DatabaseViewModel: ViewModel() {
val database : LiveData<Database?> get() = _database
private val _database = MutableLiveData<Database?>()
val actionFinished : LiveData<ActionResult> get() = _actionFinished
private val _actionFinished = SingleLiveEvent<ActionResult>()
val saveDatabase : LiveData<Boolean> get() = _saveDatabase
private val _saveDatabase = SingleLiveEvent<Boolean>()
@@ -70,6 +74,12 @@ class DatabaseViewModel: ViewModel() {
this._database.value = database
}
fun onActionFinished(database: Database,
actionTask: String,
result: ActionRunnable.Result) {
this._actionFinished.value = ActionResult(database, actionTask, result)
}
fun saveDatabase(save: Boolean) {
_saveDatabase.value = save
}
@@ -167,12 +177,29 @@ class DatabaseViewModel: ViewModel() {
_saveParallelism.value = SuperLong(oldValue, newValue, save)
}
data class SuperString(val oldValue: String, val newValue: String, val save: Boolean)
data class SuperInt(val oldValue: Int, val newValue: Int, val save: Boolean)
data class SuperLong(val oldValue: Long, val newValue: Long, val save: Boolean)
data class SuperCompression(val oldValue: CompressionAlgorithm, val newValue: CompressionAlgorithm, val save: Boolean)
data class SuperEncryption(val oldValue: EncryptionAlgorithm, val newValue: EncryptionAlgorithm, val save: Boolean)
data class SuperKeyDerivation(val oldValue: KdfEngine, val newValue: KdfEngine, val save: Boolean)
data class SuperGroup(val oldValue: Group?, val newValue: Group?, val save: Boolean)
data class ActionResult(val database: Database,
val actionTask: String,
val result: ActionRunnable.Result)
data class SuperString(val oldValue: String,
val newValue: String,
val save: Boolean)
data class SuperInt(val oldValue: Int,
val newValue: Int,
val save: Boolean)
data class SuperLong(val oldValue: Long,
val newValue: Long,
val save: Boolean)
data class SuperCompression(val oldValue: CompressionAlgorithm,
val newValue: CompressionAlgorithm,
val save: Boolean)
data class SuperEncryption(val oldValue: EncryptionAlgorithm,
val newValue: EncryptionAlgorithm,
val save: Boolean)
data class SuperKeyDerivation(val oldValue: KdfEngine,
val newValue: KdfEngine,
val save: Boolean)
data class SuperGroup(val oldValue: Group?,
val newValue: Group?,
val save: Boolean)
}