Add dialog when permanently delete nodes

This commit is contained in:
J-Jamet
2019-11-14 09:59:55 +01:00
parent 8afcf043ee
commit ae28ebe701
5 changed files with 131 additions and 18 deletions

View File

@@ -42,10 +42,7 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import com.kunzisoft.keepass.R import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.dialogs.GroupEditDialogFragment import com.kunzisoft.keepass.activities.dialogs.*
import com.kunzisoft.keepass.activities.dialogs.IconPickerDialogFragment
import com.kunzisoft.keepass.activities.dialogs.ReadOnlyDialog
import com.kunzisoft.keepass.activities.dialogs.SortDialogFragment
import com.kunzisoft.keepass.activities.helpers.EntrySelectionHelper import com.kunzisoft.keepass.activities.helpers.EntrySelectionHelper
import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper
import com.kunzisoft.keepass.activities.lock.LockingActivity import com.kunzisoft.keepass.activities.lock.LockingActivity
@@ -77,6 +74,7 @@ class GroupActivity : LockingActivity(),
IconPickerDialogFragment.IconPickerListener, IconPickerDialogFragment.IconPickerListener,
ListNodesFragment.NodeClickListener, ListNodesFragment.NodeClickListener,
ListNodesFragment.NodesActionMenuListener, ListNodesFragment.NodesActionMenuListener,
DeleteNodesDialogFragment.DeleteNodeListener,
ListNodesFragment.OnScrollListener, ListNodesFragment.OnScrollListener,
SortDialogFragment.SortSelectionListener { SortDialogFragment.SortSelectionListener {
@@ -245,13 +243,17 @@ class GroupActivity : LockingActivity(),
// Add trash in views list if it doesn't exists // Add trash in views list if it doesn't exists
if (database.isRecycleBinEnabled) { if (database.isRecycleBinEnabled) {
val recycleBin = database.recycleBin val recycleBin = database.recycleBin
if (mCurrentGroup != null && recycleBin != null val currentGroup = mCurrentGroup
&& mCurrentGroup!!.parent == null if (currentGroup != null && recycleBin != null
&& mCurrentGroup != recycleBin) { && currentGroup != recycleBin) {
if (mListNodesFragment?.contains(recycleBin) == true) // Recycle bin already here, simply update it
if (mListNodesFragment?.contains(recycleBin) == true) {
mListNodesFragment?.updateNode(recycleBin) mListNodesFragment?.updateNode(recycleBin)
else }
// Recycle bin not here, verify if parents are similar to add it
else if (currentGroup.parent == recycleBin.parent) {
mListNodesFragment?.addNode(recycleBin) mListNodesFragment?.addNode(recycleBin)
}
} }
} }
} }
@@ -596,12 +598,29 @@ class GroupActivity : LockingActivity(),
} }
override fun onDeleteMenuClick(nodes: List<NodeVersioned>): Boolean { override fun onDeleteMenuClick(nodes: List<NodeVersioned>): Boolean {
val database = mDatabase
if (database != null
&& database.isRecycleBinEnabled
&& database.recycleBin != mCurrentGroup) {
// If recycle bin enabled and not in recycle bin, move in recycle bin
progressDialogThread?.startDatabaseDeleteNodes(
nodes,
!mReadOnly
)
} else {
// open the dialog to confirm deletion
DeleteNodesDialogFragment.getInstance(nodes)
.show(supportFragmentManager, "deleteNodesDialogFragment")
}
finishNodeAction()
return true
}
override fun permanentlyDeleteNodes(nodes: List<NodeVersioned>) {
progressDialogThread?.startDatabaseDeleteNodes( progressDialogThread?.startDatabaseDeleteNodes(
nodes, nodes,
!mReadOnly !mReadOnly
) )
finishNodeAction()
return true
} }
override fun onResume() { override fun onResume() {

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2019 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.kunzisoft.keepass.activities.dialogs
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.NodeVersioned
import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService
import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.getBundleFromListNodes
import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.getListNodesFromBundle
class DeleteNodesDialogFragment : DialogFragment() {
private var mNodesToDelete: List<NodeVersioned> = ArrayList()
private var mListener: DeleteNodeListener? = null
override fun onAttach(context: Context) {
super.onAttach(context)
try {
mListener = context as DeleteNodeListener
} catch (e: ClassCastException) {
throw ClassCastException(context.toString()
+ " must implement " + DeleteNodeListener::class.java.name)
}
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
arguments?.apply {
if (containsKey(DatabaseTaskNotificationService.GROUPS_ID_KEY)
&& containsKey(DatabaseTaskNotificationService.ENTRIES_ID_KEY)) {
mNodesToDelete = getListNodesFromBundle(Database.getInstance(), this)
}
} ?: savedInstanceState?.apply {
if (containsKey(DatabaseTaskNotificationService.GROUPS_ID_KEY)
&& containsKey(DatabaseTaskNotificationService.ENTRIES_ID_KEY)) {
mNodesToDelete = getListNodesFromBundle(Database.getInstance(), savedInstanceState)
}
}
activity?.let { activity ->
// Use the Builder class for convenient dialog construction
val builder = AlertDialog.Builder(activity)
builder.setMessage(getString(R.string.warning_permanently_delete_nodes))
builder.setPositiveButton(android.R.string.yes) { _, _ ->
mListener?.permanentlyDeleteNodes(mNodesToDelete)
}
builder.setNegativeButton(android.R.string.no) { _, _ -> dismiss() }
// Create the AlertDialog object and return it
return builder.create()
}
return super.onCreateDialog(savedInstanceState)
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putAll(getBundleFromListNodes(mNodesToDelete))
}
interface DeleteNodeListener {
fun permanentlyDeleteNodes(nodes: List<NodeVersioned>)
}
companion object {
fun getInstance(nodesToDelete: List<NodeVersioned>): DeleteNodesDialogFragment {
return DeleteNodesDialogFragment().apply {
arguments = getBundleFromListNodes(nodesToDelete)
}
}
}
}

View File

@@ -422,7 +422,7 @@ class PwDatabaseV4 : PwDatabase<UUID, UUID, PwGroupV4, PwEntryV4> {
if (!isRecycleBinEnabled) if (!isRecycleBinEnabled)
return false return false
if (recycleBin == null) if (recycleBin == null)
return true return true // TODO Create recycle bin
if (!node.isContainedIn(recycleBin!!)) if (!node.isContainedIn(recycleBin!!))
return true return true
return false return false

View File

@@ -509,23 +509,23 @@ class DatabaseTaskNotificationService : NotificationService(), ProgressTaskUpdat
} }
fun getBundleFromListNodes(nodes: List<NodeVersioned>): Bundle { fun getBundleFromListNodes(nodes: List<NodeVersioned>): Bundle {
val groupsIdToCopy = ArrayList<PwNodeId<*>>() val groupsId = ArrayList<PwNodeId<*>>()
val entriesIdToCopy = ArrayList<PwNodeId<UUID>>() val entriesId = ArrayList<PwNodeId<UUID>>()
nodes.forEach { nodeVersioned -> nodes.forEach { nodeVersioned ->
when (nodeVersioned.type) { when (nodeVersioned.type) {
Type.GROUP -> { Type.GROUP -> {
(nodeVersioned as GroupVersioned).nodeId?.let { groupId -> (nodeVersioned as GroupVersioned).nodeId?.let { groupId ->
groupsIdToCopy.add(groupId) groupsId.add(groupId)
} }
} }
Type.ENTRY -> { Type.ENTRY -> {
entriesIdToCopy.add((nodeVersioned as EntryVersioned).nodeId) entriesId.add((nodeVersioned as EntryVersioned).nodeId)
} }
} }
} }
return Bundle().apply { return Bundle().apply {
putParcelableArrayList(GROUPS_ID_KEY, groupsIdToCopy) putParcelableArrayList(GROUPS_ID_KEY, groupsId)
putParcelableArrayList(ENTRIES_ID_KEY, entriesIdToCopy) putParcelableArrayList(ENTRIES_ID_KEY, entriesId)
} }
} }
} }

View File

@@ -242,6 +242,7 @@
<string name="warning_unmounted">Mount the SD card to create or load a database.</string> <string name="warning_unmounted">Mount the SD card to create or load a database.</string>
<string name="warning_empty_password">Do you really want no password unlocking protection?</string> <string name="warning_empty_password">Do you really want no password unlocking protection?</string>
<string name="warning_no_encryption_key">Are you sure you do not want to use any encryption key?</string> <string name="warning_no_encryption_key">Are you sure you do not want to use any encryption key?</string>
<string name="warning_permanently_delete_nodes">Are you sure you want to permanently delete the selected nodes?</string>
<string name="version_label">Version %1$s</string> <string name="version_label">Version %1$s</string>
<string name="build_label">Build %1$s</string> <string name="build_label">Build %1$s</string>
<string name="configure_biometric">Biometric prompt is supported but not set up.</string> <string name="configure_biometric">Biometric prompt is supported but not set up.</string>