Fix remove action for natural order

This commit is contained in:
J-Jamet
2019-08-19 14:16:29 +02:00
parent 5eec1a276c
commit edf6c2ff07
8 changed files with 80 additions and 18 deletions

View File

@@ -54,6 +54,7 @@ import com.kunzisoft.keepass.autofill.AutofillHelper
import com.kunzisoft.keepass.database.SortNodeEnum import com.kunzisoft.keepass.database.SortNodeEnum
import com.kunzisoft.keepass.database.action.ProgressDialogSaveDatabaseThread import com.kunzisoft.keepass.database.action.ProgressDialogSaveDatabaseThread
import com.kunzisoft.keepass.database.action.node.* import com.kunzisoft.keepass.database.action.node.*
import com.kunzisoft.keepass.database.action.node.ActionNodeDatabaseRunnable.Companion.NODE_POSITION_FOR_ACTION_NATURAL_ORDER_KEY
import com.kunzisoft.keepass.database.element.* import com.kunzisoft.keepass.database.element.*
import com.kunzisoft.keepass.education.GroupActivityEducation import com.kunzisoft.keepass.education.GroupActivityEducation
import com.kunzisoft.keepass.icons.assignDatabaseIcon import com.kunzisoft.keepass.icons.assignDatabaseIcon
@@ -61,7 +62,6 @@ import com.kunzisoft.keepass.magikeyboard.KeyboardHelper
import com.kunzisoft.keepass.magikeyboard.MagikIME import com.kunzisoft.keepass.magikeyboard.MagikIME
import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.timeout.TimeoutHelper import com.kunzisoft.keepass.timeout.TimeoutHelper
import com.kunzisoft.keepass.utils.LOCK_ACTION
import com.kunzisoft.keepass.utils.MenuUtil import com.kunzisoft.keepass.utils.MenuUtil
import com.kunzisoft.keepass.view.AddNodeButtonView import com.kunzisoft.keepass.view.AddNodeButtonView
import net.cachapa.expandablelayout.ExpandableLayout import net.cachapa.expandablelayout.ExpandableLayout
@@ -812,9 +812,18 @@ class GroupActivity : LockingActivity(),
override fun onActionNodeFinish(actionNodeValues: ActionNodeValues) { override fun onActionNodeFinish(actionNodeValues: ActionNodeValues) {
runOnUiThread { runOnUiThread {
if (actionNodeValues.result.isSuccess) { if (actionNodeValues.result.isSuccess) {
actionNodeValues.oldNode?.let { oldNode ->
// If the action register the position, use it to remove the entry view
val positionNode = actionNodeValues.result.data?.getInt(NODE_POSITION_FOR_ACTION_NATURAL_ORDER_KEY)
if (PreferencesUtil.getListSort(this@GroupActivity) == SortNodeEnum.DB
&& positionNode != null) {
mListNodesFragment?.removeNodeAt(positionNode)
} else {
// else use the old Node that was the entry unchanged with the old parent
actionNodeValues.oldNode?.let { oldNode ->
mListNodesFragment?.removeNode(oldNode) mListNodesFragment?.removeNode(oldNode)
}
}
// Add trash in views list if it doesn't exists // Add trash in views list if it doesn't exists
val database = Database.getInstance() val database = Database.getInstance()
@@ -833,7 +842,6 @@ class GroupActivity : LockingActivity(),
} }
} }
} }
}
override fun cancelEditGroup(action: GroupEditDialogFragment.EditGroupDialogAction?, override fun cancelEditGroup(action: GroupEditDialogFragment.EditGroupDialogAction?,
name: String?, name: String?,

View File

@@ -26,7 +26,6 @@ import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.activities.stylish.StylishFragment import com.kunzisoft.keepass.activities.stylish.StylishFragment
import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper import com.kunzisoft.keepass.activities.helpers.ReadOnlyHelper
import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.element.Database
import org.w3c.dom.Node
class ListNodesFragment : StylishFragment(), SortDialogFragment.SortSelectionListener { class ListNodesFragment : StylishFragment(), SortDialogFragment.SortSelectionListener {
@@ -269,6 +268,10 @@ class ListNodesFragment : StylishFragment(), SortDialogFragment.SortSelectionLis
mAdapter?.removeNode(pwNode) mAdapter?.removeNode(pwNode)
} }
fun removeNodeAt(position: Int) {
mAdapter?.removeNodeAt(position)
}
interface OnScrollListener { interface OnScrollListener {
/** /**

View File

@@ -171,6 +171,13 @@ class NodeAdapter
nodeSortedList.remove(node) nodeSortedList.remove(node)
} }
/**
* Remove a node at [position] in the list
*/
fun removeNodeAt(position: Int) {
nodeSortedList.removeItemAt(position)
}
/** /**
* Update a node in the list * Update a node in the list
* @param oldNode Node before the update * @param oldNode Node before the update

View File

@@ -31,7 +31,7 @@ enum class SortNodeEnum {
fun getNodeComparator(ascending: Boolean, groupsBefore: Boolean, recycleBinBottom: Boolean): Comparator<NodeVersioned> { fun getNodeComparator(ascending: Boolean, groupsBefore: Boolean, recycleBinBottom: Boolean): Comparator<NodeVersioned> {
return when (this) { return when (this) {
DB -> NodeNaturalComparator(ascending, groupsBefore, recycleBinBottom) DB -> NodeNaturalComparator(ascending, groupsBefore, false) // Force false because natural order contains recycle bin
TITLE -> NodeTitleComparator(ascending, groupsBefore, recycleBinBottom) TITLE -> NodeTitleComparator(ascending, groupsBefore, recycleBinBottom)
USERNAME -> NodeUsernameComparator(ascending, groupsBefore, recycleBinBottom) USERNAME -> NodeUsernameComparator(ascending, groupsBefore, recycleBinBottom)
CREATION_TIME -> NodeCreationComparator(ascending, groupsBefore, recycleBinBottom) CREATION_TIME -> NodeCreationComparator(ascending, groupsBefore, recycleBinBottom)

View File

@@ -45,4 +45,8 @@ abstract class ActionNodeDatabaseRunnable(
super.onFinishRun(result) super.onFinishRun(result)
} }
companion object {
const val NODE_POSITION_FOR_ACTION_NATURAL_ORDER_KEY = "NODE_POSITION_FOR_ACTION_NATURAL_ORDER_KEY"
}
} }

View File

@@ -19,6 +19,7 @@
*/ */
package com.kunzisoft.keepass.database.action.node package com.kunzisoft.keepass.database.action.node
import android.os.Bundle
import android.support.v4.app.FragmentActivity import android.support.v4.app.FragmentActivity
import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.element.Database
import com.kunzisoft.keepass.database.element.EntryVersioned import com.kunzisoft.keepass.database.element.EntryVersioned
@@ -35,11 +36,19 @@ class DeleteEntryRunnable constructor(
private var mParent: GroupVersioned? = null private var mParent: GroupVersioned? = null
private var mCanRecycle: Boolean = false private var mCanRecycle: Boolean = false
private var mEntryToDeleteBackup: EntryVersioned? = null
private var mNodePosition: Int? = null
override fun nodeAction() { override fun nodeAction() {
mParent = mEntryToDelete.parent mParent = mEntryToDelete.parent
mParent?.touch(modified = false, touchParents = true) mParent?.touch(modified = false, touchParents = true)
// Get the node position
mNodePosition = mEntryToDelete.nodePositionInParent
// Create a copy to keep the old ref and remove it visually
mEntryToDeleteBackup = EntryVersioned(mEntryToDelete)
// Remove Entry from parent // Remove Entry from parent
mCanRecycle = database.canRecycle(mEntryToDelete) mCanRecycle = database.canRecycle(mEntryToDelete)
if (mCanRecycle) { if (mCanRecycle) {
@@ -59,6 +68,16 @@ class DeleteEntryRunnable constructor(
} }
} }
} }
return ActionNodeValues(result, mEntryToDelete, null)
// Add position in bundle to delete the node in view
mNodePosition?.let { position ->
result.data = Bundle().apply {
putInt(NODE_POSITION_FOR_ACTION_NATURAL_ORDER_KEY, position )
}
}
// Return a copy of unchanged entry as old param
// and entry deleted or moved in recycle bin as new param
return ActionNodeValues(result, mEntryToDeleteBackup, mEntryToDelete)
} }
} }

View File

@@ -19,6 +19,7 @@
*/ */
package com.kunzisoft.keepass.database.action.node package com.kunzisoft.keepass.database.action.node
import android.os.Bundle
import android.support.v4.app.FragmentActivity import android.support.v4.app.FragmentActivity
import com.kunzisoft.keepass.database.element.Database import com.kunzisoft.keepass.database.element.Database
@@ -32,10 +33,19 @@ class DeleteGroupRunnable(context: FragmentActivity,
private var mParent: GroupVersioned? = null private var mParent: GroupVersioned? = null
private var mRecycle: Boolean = false private var mRecycle: Boolean = false
private var mGroupToDeleteBackup: GroupVersioned? = null
private var mNodePosition: Int? = null
override fun nodeAction() { override fun nodeAction() {
mParent = mGroupToDelete.parent mParent = mGroupToDelete.parent
mParent?.touch(modified = false, touchParents = true) mParent?.touch(modified = false, touchParents = true)
// Get the node position
mNodePosition = mGroupToDelete.nodePositionInParent
// Create a copy to keep the old ref and remove it visually
mGroupToDeleteBackup = GroupVersioned(mGroupToDelete)
// Remove Group from parent // Remove Group from parent
mRecycle = database.canRecycle(mGroupToDelete) mRecycle = database.canRecycle(mGroupToDelete)
if (mRecycle) { if (mRecycle) {
@@ -56,6 +66,16 @@ class DeleteGroupRunnable(context: FragmentActivity,
// TODO database.undoDeleteGroupFrom(mGroup, mParent); // TODO database.undoDeleteGroupFrom(mGroup, mParent);
} }
} }
return ActionNodeValues(result, mGroupToDelete, null)
// Add position in bundle to delete the node in view
mNodePosition?.let { position ->
result.data = Bundle().apply {
putInt(NODE_POSITION_FOR_ACTION_NATURAL_ORDER_KEY, position )
}
}
// Return a copy of unchanged group as old param
// and group deleted or moved in recycle bin as new param
return ActionNodeValues(result, mGroupToDeleteBackup, mGroupToDelete)
} }
} }

View File

@@ -21,6 +21,7 @@ package com.kunzisoft.keepass.tasks
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.os.Bundle
import android.util.Log import android.util.Log
import android.widget.Toast import android.widget.Toast
@@ -88,5 +89,5 @@ abstract class ActionRunnable(private var nestedActionRunnable: ActionRunnable?
/** /**
* Class to manage result from ActionRunnable * Class to manage result from ActionRunnable
*/ */
data class Result(var isSuccess: Boolean = true, var message: String? = null) data class Result(var isSuccess: Boolean = true, var message: String? = null, var data: Bundle? = null)
} }