Solve bugs when update node in list, start encapsulate code

This commit is contained in:
Jeremy committed 2018-02-12 00:22:03 +01:00
1 parent 795d6fa334
commit d568604117
18 files changed
+388 -158

No files matched your search

@@ -1,3 +1,23 @@
/*
* Copyright 2018 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 2 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.keepassdroid.adapters;
import android.content.Context;
@@ -13,6 +33,7 @@ import android.view.View;
import android.view.ViewGroup;
import com.keepassdroid.app.App;
import com.keepassdroid.database.PwGroup;
import com.keepassdroid.database.PwNode;
import com.keepassdroid.settings.PrefsUtil;
import com.kunzisoft.keepass.R;
@@ -27,25 +48,30 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
private Context context;
private LayoutInflater inflater;
private float textSize;
private boolean sortByName;
private OnNodeClickCallback onNodeClickCallback;
private int nodePositionToUpdate;
private NodeMenuListener nodeMenuListener;
public NodeAdapter(final Context context, PwNode mainNode) {
public NodeAdapter(final Context context, PwGroup mainNode) {
this.inflater = LayoutInflater.from(context);
this.context = context;
this.textSize = PrefsUtil.getListTextSize(context);
this.sortByName = PrefsUtil.isListSortByName(context);
this.nodePositionToUpdate = -1;
this.nodeSortedList = new SortedList<>(PwNode.class, new SortedListAdapterCallback<PwNode>(this) {
@Override public int compare(PwNode item1, PwNode item2) {
if(PrefsUtil.isListSortByName(context))
return item1.compareTo(item2);
// Choose sort depend of preferences
if(sortByName)
return new PwNode.NodeNameComparator().compare(item1, item2);
else
return item1.compareTo(item2); // TODO Different sort
return new PwNode.NodeCreationComparator().compare(item1, item2);
}
@Override public boolean areContentsTheSame(PwNode oldItem, PwNode newItem) {
return oldItem.equals(newItem);
return oldItem.isContentVisuallyTheSame(newItem);
}
@Override public boolean areItemsTheSame(PwNode item1, PwNode item2) {
@@ -55,22 +81,51 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
this.nodeSortedList.addAll(mainNode.getDirectChildren());
}
/**
* Rebuild the list by clear and build again for the group
*/
public void rebuildList(PwGroup group) {
this.nodeSortedList.clear();
this.nodeSortedList.addAll(group.getDirectChildren());
}
/**
* Add a node to the list
* @param node Node to add
*/
public void addNode(PwNode node) {
nodeSortedList.add(node);
}
public void updateNode(PwNode node) {
/**
* Update the last Node clicked in the list
*/
public void updateLastNodeClicked() {
// Don't really update here, sorted list knows each original ref, so we just notify a change
try {
nodeSortedList.updateItemAt(nodeSortedList.indexOf(node), node);
notifyItemChanged(nodePositionToUpdate);
nodeSortedList.recalculatePositionOfItemAt(nodePositionToUpdate);
} catch (IndexOutOfBoundsException e) {
Log.e(NodeAdapter.class.getName(), e.getMessage());
}
}
/**
* Remove node in the list
* @param node Node to delete
*/
public void removeNode(PwNode node) {
nodeSortedList.remove(node);
}
/**
* Notify a change sort of the list
*/
// TODO as interface
public void notifyChangeSort() {
this.sortByName = PrefsUtil.isListSortByName(context);
}
@Override
public int getItemViewType(int position) {
return nodeSortedList.get(position).getType().ordinal();
@@ -112,19 +167,39 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
return nodeSortedList.size();
}
/**
* Assign a listener when a node is clicked
*/
public void setOnNodeClickListener(OnNodeClickCallback onNodeClickCallback) {
this.onNodeClickCallback = onNodeClickCallback;
}
/**
* Assign a listener when an element of menu is clicked
*/
public void setNodeMenuListener(NodeMenuListener nodeMenuListener) {
this.nodeMenuListener = nodeMenuListener;
}
/**
* Callback listener to redefine to do an action when a node is click
*/
public interface OnNodeClickCallback {
void onNodeClick(PwNode node);
}
public class OnNodeClickListener implements View.OnClickListener {
/**
* Menu listener to redefine to do an action in menu
*/
public interface NodeMenuListener {
boolean onOpenMenuClick(PwNode node);
boolean onDeleteMenuClick(PwNode node);
}
/**
* Utility class for node listener
*/
private class OnNodeClickListener implements View.OnClickListener {
private PwNode node;
OnNodeClickListener(PwNode node) {
@@ -133,16 +208,15 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
@Override
public void onClick(View v) {
nodePositionToUpdate = nodeSortedList.indexOf(node);
if (onNodeClickCallback != null)
onNodeClickCallback.onNodeClick(node);
}
}
public interface NodeMenuListener {
boolean onOpenMenuClick(PwNode node);
boolean onDeleteMenuClick(PwNode node);
}
/**
* Utility class for menu listener
*/
private class ContextMenuBuilder implements View.OnCreateContextMenuListener {
private PwNode node;