Change code for list and solve bugs

This commit is contained in:
J-Jamet committed 2018-02-26 13:16:30 +01:00
1 parent d994fbafcf
commit 8858a5cdca
4 files changed
+144 -137

No files matched your search

@@ -29,6 +29,7 @@ import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
@@ -91,37 +92,12 @@ public class GroupActivity extends ListNodesActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ( isFinishing() ) {
return;
}
setResult(KeePass.EXIT_NORMAL);
Log.w(TAG, "Creating tree view");
PwGroupId pwGroupId = (PwGroupId) getIntent().getSerializableExtra(KEY_ENTRY);
Database db = App.getDB();
readOnly = db.readOnly;
PwGroup root = db.pm.rootGroup;
if ( pwGroupId == null ) {
mCurrentGroup = root;
} else {
mCurrentGroup = db.pm.groups.get(pwGroupId);
}
Log.w(TAG, "Retrieved tree");
if ( mCurrentGroup == null ) {
Log.w(TAG, "Group was null");
return;
}
addGroupEnabled = !readOnly;
addEntryEnabled = !readOnly;
isRoot = (mCurrentGroup == root);
if ( !mCurrentGroup.allowAddEntryIfIsRoot() )
addEntryEnabled = !isRoot && addEntryEnabled;
// Construct main view
rootView = new ListNodesWithAddButtonView(this);
rootView.enableAddGroup(addGroupEnabled);
@@ -152,11 +128,50 @@ public class GroupActivity extends ListNodesActivity
setGroupTitle();
setGroupIcon();
NodeAdapter nodeAdapter = new NodeAdapter(this, mCurrentGroup, true);
nodeAdapter.setOnNodeClickListener(this);
nodeAdapter.setNodeMenuListener(new NodeAdapter.NodeMenuListener() {
@Override
public boolean onOpenMenuClick(PwNode node) {
Log.w(TAG, "Finished creating tree");
if (isRoot) {
showWarnings();
}
}
protected PwGroup initCurrentGroup() {
PwGroup currentGroup;
Database db = App.getDB();
readOnly = db.readOnly;
PwGroup root = db.pm.rootGroup;
Log.w(TAG, "Creating tree view");
PwGroupId pwGroupId = (PwGroupId) getIntent().getSerializableExtra(KEY_ENTRY);
if ( pwGroupId == null ) {
currentGroup = root;
} else {
currentGroup = db.pm.groups.get(pwGroupId);
}
addGroupEnabled = !readOnly;
addEntryEnabled = !readOnly;
isRoot = (currentGroup == root);
if ( !currentGroup.allowAddEntryIfIsRoot() )
addEntryEnabled = !isRoot && addEntryEnabled;
return currentGroup;
}
@Override
protected RecyclerView defineNodeList() {
return (RecyclerView) findViewById(R.id.nodes_list);
}
@Override
protected void addOptionsToAdapter(NodeAdapter nodeAdapter) {
super.addOptionsToAdapter(nodeAdapter);
nodeAdapter.setActivateContextMenu(true);
nodeAdapter.setNodeMenuListener(new NodeAdapter.NodeMenuListener() {
@Override
public boolean onOpenMenuClick(PwNode node) {
mAdapter.registerANodeToUpdate(node);
switch (node.getType()) {
case GROUP:
@@ -166,29 +181,29 @@ public class GroupActivity extends ListNodesActivity
EntryActivity.Launch(GroupActivity.this, (PwEntry) node);
break;
}
return true;
}
return true;
}
@Override
public boolean onEditMenuClick(PwNode node) {
mAdapter.registerANodeToUpdate(node);
switch (node.getType()) {
case GROUP:
@Override
public boolean onEditMenuClick(PwNode node) {
mAdapter.registerANodeToUpdate(node);
switch (node.getType()) {
case GROUP:
editGroupDialogAction = EditGroupDialogAction.UPDATE;
GroupEditDialogFragment groupEditDialogFragment =
GroupEditDialogFragment groupEditDialogFragment =
GroupEditDialogFragment.build(node);
groupEditDialogFragment.show(getSupportFragmentManager(),
groupEditDialogFragment.show(getSupportFragmentManager(),
GroupEditDialogFragment.TAG_CREATE_GROUP);
break;
case ENTRY:
EntryEditActivity.Launch(GroupActivity.this, (PwEntry) node);
break;
}
return true;
}
break;
case ENTRY:
EntryEditActivity.Launch(GroupActivity.this, (PwEntry) node);
break;
}
return true;
}
@Override
public boolean onDeleteMenuClick(PwNode node) {
@Override
public boolean onDeleteMenuClick(PwNode node) {
switch (node.getType()) {
case GROUP:
deleteGroup((PwGroup) node);
@@ -197,16 +212,17 @@ public class GroupActivity extends ListNodesActivity
deleteEntry((PwEntry) node);
break;
}
return true;
}
});
setNodeAdapter(nodeAdapter);
Log.w(TAG, "Finished creating tree");
if (isRoot) {
showWarnings();
}
}
return true;
}
});
}
@Override
protected void onResume() {
super.onResume();
// Show button on resume
rootView.showButton();
}
@Override
public void onSortSelected(SortNodeEnum sortNodeEnum, boolean ascending, boolean groupsBefore, boolean recycleBinBottom) {
@@ -59,18 +59,21 @@ public abstract class ListNodesActivity extends LockCloseListActivity
implements AssignMasterKeyDialogFragment.AssignPasswordDialogListener,
NodeAdapter.OnNodeClickCallback,
SortDialogFragment.SortSelectionListener {
protected RecyclerView mList;
protected NodeAdapter mAdapter;
public static final String KEY_ENTRY = "entry";
public static final String KEY_ENTRY = "entry";
protected PwGroup mCurrentGroup;
protected NodeAdapter mAdapter;
private SharedPreferences prefs;
protected PwGroup mCurrentGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ( isFinishing() ) {
return;
}
// Likely the app has been killed exit the activity
if ( ! App.getDB().Loaded() ) {
@@ -85,14 +88,27 @@ public abstract class ListNodesActivity extends LockCloseListActivity
setContentView(R.layout.list_nodes);
setResult(KeePass.EXIT_NORMAL);
styleScrollBars();
}
protected void styleScrollBars() {
ensureCorrectListView();
mList.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
// TODO mList.setTextFilterEnabled(true);
mCurrentGroup = initCurrentGroup();
mAdapter = new NodeAdapter(this);
addOptionsToAdapter(mAdapter);
}
protected abstract PwGroup initCurrentGroup();
protected abstract RecyclerView defineNodeList();
protected void addOptionsToAdapter(NodeAdapter nodeAdapter) {
mAdapter.setOnNodeClickListener(this);
}
@Override
protected void onResume() {
super.onResume();
// Add elements to the list
mAdapter.rebuildList(mCurrentGroup);
assignListToNodeAdapter(defineNodeList());
}
protected void setGroupTitle() {
if ( mCurrentGroup != null ) {
@@ -110,15 +126,11 @@ public abstract class ListNodesActivity extends LockCloseListActivity
}
}
private void ensureCorrectListView(){
mList = (RecyclerView) findViewById(R.id.nodes_list);
mList.setLayoutManager(new LinearLayoutManager(this));
}
protected void setNodeAdapter(NodeAdapter adapter) {
ensureCorrectListView();
mAdapter = adapter;
mList.setAdapter(adapter);
protected void assignListToNodeAdapter(RecyclerView recyclerView) {
recyclerView.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
// TODO mList.setTextFilterEnabled(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mAdapter);
}
@Override
@@ -55,29 +55,18 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
private NodeMenuListener nodeMenuListener;
private boolean activateContextMenu;
/**
* Create node list adapter without contextMenu
* @param context Context to use
* @param mainNode Main node as group to build children
*/
public NodeAdapter(final Context context, PwGroup mainNode) {
this(context, mainNode, false);
}
/**
* Create node list adapter with contextMenu or not
* @param context Context to use
* @param mainNode Main node as group to build children
* @param mainNode true if contextMenu need to be shown
*/
public NodeAdapter(final Context context, PwGroup mainNode, boolean activateContextMenu) {
public NodeAdapter(final Context context) {
this.inflater = LayoutInflater.from(context);
this.context = context;
this.textSize = PrefsUtil.getListTextSize(context);
this.listSort = PrefsUtil.getListSort(context);
this.groupsBeforeSort = PrefsUtil.getGroupsBeforeSort(context);
this.ascendingSort = PrefsUtil.getAscendingSort(context);
this.activateContextMenu = activateContextMenu;
this.activateContextMenu = false;
this.nodePositionToUpdate = -1;
this.nodeSortedList = new SortedList<>(PwNode.class, new SortedListAdapterCallback<PwNode>(this) {
@@ -93,11 +82,14 @@ public class NodeAdapter extends RecyclerView.Adapter<BasicViewHolder> {
return item1.equals(item2);
}
});
this.nodeSortedList.addAll(mainNode.getDirectChildren());
}
public void setActivateContextMenu(boolean activate) {
this.activateContextMenu = activate;
}
/**
* Rebuild the list by clear and build again for the group
* Rebuild the list by clear and build children from the group
*/
public void rebuildList(PwGroup group) {
this.nodeSortedList.clear();
@@ -22,6 +22,7 @@ package com.keepassdroid.search;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
@@ -29,36 +30,19 @@ import android.view.MenuItem;
import android.view.View;
import com.keepassdroid.activities.ListNodesActivity;
import com.keepassdroid.adapters.NodeAdapter;
import com.keepassdroid.app.App;
import com.keepassdroid.database.Database;
import com.keepassdroid.database.PwGroup;
import com.keepassdroid.utils.MenuUtil;
import com.kunzisoft.keepass.KeePass;
import com.kunzisoft.keepass.R;
public class SearchResultsActivity extends ListNodesActivity {
private Database mDb;
private View listView;
private View notFoundView;
private RecyclerView listView;
@Override
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ( isFinishing() ) {
return;
}
setResult(KeePass.EXIT_NORMAL);
mDb = App.getDB();
// Likely the app has been killed exit the activity
if ( ! mDb.Loaded() ) {
finish();
}
setContentView(getLayoutInflater().inflate(R.layout.search_results, null));
@@ -69,13 +53,35 @@ public class SearchResultsActivity extends ListNodesActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
listView = findViewById(R.id.nodes_list);
notFoundView = findViewById(R.id.not_found_container);
listView = (RecyclerView) findViewById(R.id.nodes_list);
View notFoundView = findViewById(R.id.not_found_container);
performSearch(getSearchStr(getIntent()));
if ( mCurrentGroup == null || mCurrentGroup.numbersOfChildEntries() < 1 ) {
listView.setVisibility(View.GONE);
notFoundView.setVisibility(View.VISIBLE);
} else {
listView.setVisibility(View.VISIBLE);
notFoundView.setVisibility(View.GONE);
}
setGroupTitle();
}
@Override
protected PwGroup initCurrentGroup() {
Database mDb = App.getDB();
// Likely the app has been killed exit the activity
if ( ! mDb.Loaded() ) {
finish();
}
return mDb.Search(getSearchStr(getIntent()).trim());
}
@Override
protected RecyclerView defineNodeList() {
return listView;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
@@ -92,27 +98,8 @@ public class SearchResultsActivity extends ListNodesActivity {
case android.R.id.home:
finish();
}
return super.onOptionsItemSelected(item);
}
private void performSearch(String query) {
mCurrentGroup = mDb.Search(query.trim());
if ( mCurrentGroup == null || mCurrentGroup.numbersOfChildEntries() < 1 ) {
listView.setVisibility(View.GONE);
notFoundView.setVisibility(View.VISIBLE);
} else {
listView.setVisibility(View.VISIBLE);
notFoundView.setVisibility(View.GONE);
}
setGroupTitle();
NodeAdapter nodeAdapter = new NodeAdapter(this, mCurrentGroup);
nodeAdapter.setOnNodeClickListener(this);
setNodeAdapter(nodeAdapter);
}
private String getSearchStr(Intent queryIntent) {
// get and process search query here