mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2026-11-21 01:21:34 -08:00
fix: ViewModel implementation #2541
This commit is contained in:
4 files changed
+83
-40
No files matched your search
@@ -3,6 +3,7 @@ package com.kunzisoft.keepass.activities.legacy
|
||||
import android.Manifest
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
@@ -19,10 +20,12 @@ import androidx.lifecycle.repeatOnLifecycle
|
||||
import com.kunzisoft.keepass.R
|
||||
import com.kunzisoft.keepass.activities.dialogs.DatabaseChangedDialogFragment
|
||||
import com.kunzisoft.keepass.activities.dialogs.DatabaseChangedDialogFragment.Companion.DATABASE_CHANGED_DIALOG_TAG
|
||||
import com.kunzisoft.keepass.activities.dialogs.PasswordEncodingDialogFragment
|
||||
import com.kunzisoft.keepass.activities.stylish.StylishActivity
|
||||
import com.kunzisoft.keepass.credentialprovider.EntrySelectionHelper.setActivityResult
|
||||
import com.kunzisoft.keepass.database.ContextualDatabase
|
||||
import com.kunzisoft.keepass.database.DatabaseTaskProvider.Companion.startDatabaseService
|
||||
import com.kunzisoft.keepass.database.MainCredential
|
||||
import com.kunzisoft.keepass.model.SnapFileDatabaseInfo
|
||||
import com.kunzisoft.keepass.tasks.ActionRunnable
|
||||
import com.kunzisoft.keepass.tasks.ProgressTaskDialogFragment
|
||||
@@ -163,6 +166,12 @@ abstract class DatabaseActivity : StylishActivity(), DatabaseRetrieval {
|
||||
uiState.result
|
||||
)
|
||||
}
|
||||
is DatabaseViewModel.ActionState.ShowPasswordEncodingDialog -> {
|
||||
showPasswordEncodingDialog(
|
||||
uiState.databaseUri,
|
||||
uiState.mainCredential
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -252,6 +261,11 @@ abstract class DatabaseActivity : StylishActivity(), DatabaseRetrieval {
|
||||
}
|
||||
}
|
||||
|
||||
private fun showPasswordEncodingDialog(databaseUri: Uri, mainCredential: MainCredential) {
|
||||
PasswordEncodingDialogFragment.getInstance(databaseUri, mainCredential)
|
||||
.show(supportFragmentManager, "passwordEncodingTag")
|
||||
}
|
||||
|
||||
private fun showDatabaseChangedDialog(
|
||||
previousDatabaseInfo: SnapFileDatabaseInfo,
|
||||
newDatabaseInfo: SnapFileDatabaseInfo,
|
||||
|
||||
@@ -90,8 +90,16 @@ abstract class DatabaseLockActivity : DatabaseModeActivity(),
|
||||
|
||||
lifecycleScope.launch {
|
||||
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
mNodesViewModel.nodesToPermanentlyDelete.collect { nodes ->
|
||||
deleteDatabaseNodes(nodes)
|
||||
launch {
|
||||
mNodesViewModel.nodesToPermanentlyDelete.collect { nodes ->
|
||||
deleteDatabaseNodes(nodes)
|
||||
}
|
||||
}
|
||||
launch {
|
||||
mNodesViewModel.shouldShowDeleteDialog.collect { recycleBin ->
|
||||
DeleteNodesDialogFragment.getInstance(recycleBin)
|
||||
.show(supportFragmentManager, "deleteNodesDialogFragment")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -189,21 +197,7 @@ abstract class DatabaseLockActivity : DatabaseModeActivity(),
|
||||
}
|
||||
|
||||
fun assignMainCredential(mainCredential: MainCredential) {
|
||||
// TODO Move in ViewModel
|
||||
mDatabase?.let { database ->
|
||||
database.fileUri?.let { databaseUri ->
|
||||
// Show the progress dialog now or after dialog confirmation
|
||||
val masterCredential = mainCredential.toMasterCredential(contentResolver)
|
||||
val validCredential = database.isValidCredential(masterCredential)
|
||||
masterCredential.clear()
|
||||
if (validCredential) {
|
||||
mDatabaseViewModel.assignMainCredential(databaseUri, mainCredential)
|
||||
} else {
|
||||
PasswordEncodingDialogFragment.getInstance(databaseUri, mainCredential)
|
||||
.show(supportFragmentManager, "passwordEncodingTag")
|
||||
}
|
||||
}
|
||||
}
|
||||
mDatabaseViewModel.assignMainCredential(mainCredential)
|
||||
}
|
||||
|
||||
fun saveDatabase() {
|
||||
@@ -297,22 +291,13 @@ abstract class DatabaseLockActivity : DatabaseModeActivity(),
|
||||
}
|
||||
|
||||
fun deleteNodes(nodes: Nodes, recycleBin: Boolean = false) {
|
||||
// TODO Move in ViewModel
|
||||
mDatabase?.let { database ->
|
||||
// If recycle bin enabled, ensure it exists
|
||||
if (database.isRecycleBinEnabled) {
|
||||
database.ensureRecycleBinExists(resources.getString(R.string.recycle_bin))
|
||||
}
|
||||
// If recycle bin enabled and not in recycle bin, move in recycle bin
|
||||
if (database.eachNodeRecyclable(nodes)) {
|
||||
deleteDatabaseNodes(nodes)
|
||||
}
|
||||
// else open the dialog to confirm deletion
|
||||
else {
|
||||
DeleteNodesDialogFragment.getInstance(recycleBin)
|
||||
.show(supportFragmentManager, "deleteNodesDialogFragment")
|
||||
mNodesViewModel.deleteNodes(nodes)
|
||||
}
|
||||
mNodesViewModel.requestNodesDeletion(
|
||||
database = database,
|
||||
nodes = nodes,
|
||||
recycleBinName = resources.getString(R.string.recycle_bin),
|
||||
recycleBin = recycleBin
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -147,6 +147,21 @@ class DatabaseViewModel(application: Application): AndroidViewModel(application)
|
||||
mDatabaseTaskProvider.startDatabaseCreate(databaseUri, mainCredential)
|
||||
}
|
||||
|
||||
fun assignMainCredential(mainCredential: MainCredential) {
|
||||
database?.let { database ->
|
||||
database.fileUri?.let { databaseUri ->
|
||||
val masterCredential = mainCredential.toMasterCredential(getApplication<Application>().contentResolver)
|
||||
val validCredential = database.isValidCredential(masterCredential)
|
||||
masterCredential.clear()
|
||||
if (validCredential) {
|
||||
assignMainCredential(databaseUri, mainCredential)
|
||||
} else {
|
||||
mActionState.value = ActionState.ShowPasswordEncodingDialog(databaseUri, mainCredential)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun assignMainCredential(
|
||||
databaseUri: Uri?,
|
||||
mainCredential: MainCredential
|
||||
@@ -540,5 +555,9 @@ class DatabaseViewModel(application: Application): AndroidViewModel(application)
|
||||
val actionTask: String,
|
||||
val result: ActionRunnable.Result
|
||||
): ActionState()
|
||||
data class ShowPasswordEncodingDialog(
|
||||
val databaseUri: Uri,
|
||||
val mainCredential: MainCredential
|
||||
): ActionState()
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ package com.kunzisoft.keepass.viewmodels
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.kunzisoft.keepass.database.ContextualDatabase
|
||||
import com.kunzisoft.keepass.database.element.node.Nodes
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
@@ -35,26 +36,50 @@ import kotlinx.coroutines.launch
|
||||
*/
|
||||
class NodesViewModel : ViewModel() {
|
||||
|
||||
private val _nodesToDelete = MutableStateFlow(Nodes())
|
||||
|
||||
/**
|
||||
* Nodes to delete.
|
||||
*/
|
||||
val nodesToDelete: StateFlow<Nodes> = _nodesToDelete.asStateFlow()
|
||||
private val _nodesToDelete = MutableStateFlow(Nodes())
|
||||
|
||||
private val _nodesToPermanentlyDelete = MutableSharedFlow<Nodes>()
|
||||
val nodesToDelete: StateFlow<Nodes> = _nodesToDelete.asStateFlow()
|
||||
|
||||
/**
|
||||
* Nodes to permanently delete.
|
||||
*/
|
||||
private val _nodesToPermanentlyDelete = MutableSharedFlow<Nodes>()
|
||||
val nodesToPermanentlyDelete: SharedFlow<Nodes> = _nodesToPermanentlyDelete.asSharedFlow()
|
||||
|
||||
|
||||
/**
|
||||
* Delete nodes.
|
||||
* @param nodes Nodes to delete.
|
||||
* Shared flow for requesting to show the delete dialog.
|
||||
*/
|
||||
fun deleteNodes(nodes: Nodes) {
|
||||
_nodesToDelete.value = nodes
|
||||
private val _shouldShowDeleteDialog = MutableSharedFlow<Boolean>()
|
||||
val shouldShowDeleteDialog: SharedFlow<Boolean> = _shouldShowDeleteDialog.asSharedFlow()
|
||||
|
||||
/**
|
||||
* Start the deletion process of nodes.
|
||||
* @param database The database where nodes are located.
|
||||
* @param nodes Nodes to delete.
|
||||
* @param recycleBinName The name of the recycle bin to ensure it exists.
|
||||
* @param recycleBin Boolean to indicate if it's a recycle bin action.
|
||||
*/
|
||||
fun requestNodesDeletion(
|
||||
database: ContextualDatabase,
|
||||
nodes: Nodes,
|
||||
recycleBinName: String,
|
||||
recycleBin: Boolean
|
||||
) {
|
||||
if (database.isRecycleBinEnabled) {
|
||||
database.ensureRecycleBinExists(recycleBinName)
|
||||
}
|
||||
if (database.eachNodeRecyclable(nodes)) {
|
||||
permanentlyDeleteNodes(nodes)
|
||||
} else {
|
||||
_nodesToDelete.value = nodes
|
||||
viewModelScope.launch {
|
||||
_shouldShowDeleteDialog.emit(recycleBin)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user