diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.java b/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.java index bc5fa6ec4..2df9f4d9b 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.java +++ b/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.java @@ -34,7 +34,6 @@ import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; import android.support.annotation.RequiresApi; -import android.support.v7.widget.RecyclerView; import android.support.v7.widget.SearchView; import android.support.v7.widget.Toolbar; import android.util.Log; @@ -56,7 +55,6 @@ import com.kunzisoft.keepass.database.PwGroupId; import com.kunzisoft.keepass.database.PwIcon; import com.kunzisoft.keepass.database.PwIconStandard; import com.kunzisoft.keepass.database.PwNode; -import com.kunzisoft.keepass.database.SortNodeEnum; import com.kunzisoft.keepass.database.action.AddGroupRunnable; import com.kunzisoft.keepass.database.action.DeleteEntryRunnable; import com.kunzisoft.keepass.database.action.DeleteGroupRunnable; @@ -73,7 +71,12 @@ import com.kunzisoft.keepass.tasks.UpdateProgressTaskStatus; import com.kunzisoft.keepass.view.AddNodeButtonView; public class GroupActivity extends ListNodesActivity - implements GroupEditDialogFragment.EditGroupListener, IconPickerDialogFragment.IconPickerListener { + implements GroupEditDialogFragment.EditGroupListener, + IconPickerDialogFragment.IconPickerListener, + NodeAdapter.NodeMenuListener, + ListNodesFragment.OnScrollListener { + + private static final String TAG = GroupActivity.class.getName(); private static final String GROUP_ID_KEY = "GROUP_ID_KEY"; @@ -87,8 +90,6 @@ public class GroupActivity extends ListNodesActivity protected boolean isRoot = false; protected boolean readOnly = false; - private static final String TAG = "Group Activity:"; - private static final String OLD_GROUP_TO_UPDATE_KEY = "OLD_GROUP_TO_UPDATE_KEY"; private PwGroup oldGroupToUpdate; @@ -136,36 +137,32 @@ public class GroupActivity extends ListNodesActivity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - - Log.w(TAG, "Retrieved tree"); + + Log.i(TAG, "Started creating tree"); if ( mCurrentGroup == null ) { Log.w(TAG, "Group was null"); return; } + // Construct main view + setContentView(getLayoutInflater().inflate(R.layout.list_nodes_with_add_button, null)); + + attachFragmentToContentView(); + if (savedInstanceState != null && savedInstanceState.containsKey(OLD_GROUP_TO_UPDATE_KEY)) { oldGroupToUpdate = (PwGroup) savedInstanceState.getSerializable(OLD_GROUP_TO_UPDATE_KEY); } - // Construct main view - setContentView(getLayoutInflater().inflate(R.layout.list_nodes_with_add_button, null)); - iconView = findViewById(R.id.icon); addNodeButtonView = findViewById(R.id.add_node_button); addNodeButtonView.enableAddGroup(addGroupEnabled); addNodeButtonView.enableAddEntry(addEntryEnabled); - // Hide when scroll - RecyclerView recyclerView = findViewById(R.id.nodes_list); - recyclerView.addOnScrollListener(addNodeButtonView.hideButtonOnScrollListener()); toolbar = findViewById(R.id.toolbar); toolbar.setTitle(""); setSupportActionBar(toolbar); - if ( mCurrentGroup.getParent() != null ) - toolbar.setNavigationIcon(R.drawable.ic_arrow_up_white_24dp); - addNodeButtonView.setAddGroupClickListener(v -> { GroupEditDialogFragment.build() .show(getSupportFragmentManager(), @@ -173,17 +170,15 @@ public class GroupActivity extends ListNodesActivity }); addNodeButtonView.setAddEntryClickListener(v -> EntryEditActivity.launch(GroupActivity.this, mCurrentGroup)); - - setGroupTitle(); - Log.w(TAG, "Finished creating tree"); + Log.i(TAG, "Finished creating tree"); if (isRoot) { showWarnings(); } } - protected PwGroup initCurrentGroup() { + protected PwGroup initializeListNodesFragment() { PwGroup currentGroup; Database db = App.getDB(); readOnly = db.isReadOnly(); @@ -205,71 +200,84 @@ public class GroupActivity extends ListNodesActivity addEntryEnabled = !isRoot && addEntryEnabled; } + listNodesFragment = ListNodesFragment.newInstance(currentGroup); + return currentGroup; } @Override - protected RecyclerView defineNodeList() { - return (RecyclerView) findViewById(R.id.nodes_list); + public void assignToolbarElements() { + super.assignToolbarElements(); + + // Assign the group icon depending of IconPack or custom icon + if ( mCurrentGroup != null ) { + if (IconPackChooser.getSelectedIconPack(this).tintable()) { + // Retrieve the textColor to tint the icon + int[] attrs = {R.attr.textColorInverse}; + TypedArray ta = getTheme().obtainStyledAttributes(attrs); + int iconColor = ta.getColor(0, Color.WHITE); + App.getDB().getDrawFactory().assignDatabaseIconTo(this, iconView, mCurrentGroup.getIcon(), true, iconColor); + } else { + App.getDB().getDrawFactory().assignDatabaseIconTo(this, iconView, mCurrentGroup.getIcon()); + } + + if ( mCurrentGroup.getParent() != null ) + toolbar.setNavigationIcon(R.drawable.ic_arrow_up_white_24dp); + else { + toolbar.setNavigationIcon(null); + } + } } @Override - protected void addOptionsToAdapter(NodeAdapter nodeAdapter) { - super.addOptionsToAdapter(nodeAdapter); + public void onScrolled(int dy) { + if (addNodeButtonView != null) + addNodeButtonView.hideButtonOnScrollListener(dy); + } - nodeAdapter.setActivateContextMenu(true); - nodeAdapter.setNodeMenuListener(new NodeAdapter.NodeMenuListener() { - @Override - public boolean onOpenMenuClick(PwNode node) { - switch (node.getType()) { - case GROUP: - GroupActivity.launch(GroupActivity.this, (PwGroup) node); - break; - case ENTRY: - EntryActivity.launch(GroupActivity.this, (PwEntry) node); - break; - } - return true; - } + @Override + public boolean onOpenMenuClick(PwNode node) { + onNodeClick(node); + return true; + } - @Override - public boolean onEditMenuClick(PwNode node) { - switch (node.getType()) { - case GROUP: - oldGroupToUpdate = (PwGroup) node; - GroupEditDialogFragment.build(node) - .show(getSupportFragmentManager(), - GroupEditDialogFragment.TAG_CREATE_GROUP); - break; - case ENTRY: - EntryEditActivity.launch(GroupActivity.this, (PwEntry) node); - break; - } - return true; - } + @Override + public boolean onEditMenuClick(PwNode node) { + switch (node.getType()) { + case GROUP: + oldGroupToUpdate = (PwGroup) node; + GroupEditDialogFragment.build(node) + .show(getSupportFragmentManager(), + GroupEditDialogFragment.TAG_CREATE_GROUP); + break; + case ENTRY: + EntryEditActivity.launch(GroupActivity.this, (PwEntry) node); + break; + } + return true; + } - @Override - public boolean onDeleteMenuClick(PwNode node) { - switch (node.getType()) { - case GROUP: - deleteGroup((PwGroup) node); - break; - case ENTRY: - deleteEntry((PwEntry) node); - break; - } - return true; - } - }); + @Override + public boolean onDeleteMenuClick(PwNode node) { + switch (node.getType()) { + case GROUP: + deleteGroup((PwGroup) node); + break; + case ENTRY: + deleteEntry((PwEntry) node); + break; + } + return true; } @Override protected void onResume() { super.onResume(); // Refresh the group icon - assignGroupIcon(); + assignToolbarElements(); // Show button on resume - addNodeButtonView.showButton(); + if (addNodeButtonView != null) + addNodeButtonView.showButton(); } /** @@ -279,7 +287,8 @@ public class GroupActivity extends ListNodesActivity private void checkAndPerformedEducation(Menu menu) { // If no node, show education to add new one - if (mAdapter.getItemCount() <= 0) { + if (listNodesFragment != null + && listNodesFragment.isEmpty()) { if (!PreferencesUtil.isEducationNewNodePerformed(this)) { TapTargetView.showFor(this, @@ -407,33 +416,10 @@ public class GroupActivity extends ListNodesActivity protected void onStop() { super.onStop(); // Hide button - addNodeButtonView.hideButton(); + if (addNodeButtonView != null) + addNodeButtonView.hideButton(); } - @Override - public void onSortSelected(SortNodeEnum sortNodeEnum, boolean ascending, boolean groupsBefore, boolean recycleBinBottom) { - super.onSortSelected(sortNodeEnum, ascending, groupsBefore, recycleBinBottom); - // Show button if hide after sort - addNodeButtonView.showButton(); - } - - /** - * Assign the group icon depending of IconPack or custom icon - */ - protected void assignGroupIcon() { - if (mCurrentGroup != null) { - if (IconPackChooser.getSelectedIconPack(this).tintable()) { - // Retrieve the textColor to tint the icon - int[] attrs = {R.attr.textColorInverse}; - TypedArray ta = getTheme().obtainStyledAttributes(attrs); - int iconColor = ta.getColor(0, Color.WHITE); - App.getDB().getDrawFactory().assignDatabaseIconTo(this, iconView, mCurrentGroup.getIcon(), true, iconColor); - } else { - App.getDB().getDrawFactory().assignDatabaseIconTo(this, iconView, mCurrentGroup.getIcon()); - } - } - } - private void deleteEntry(PwEntry entry) { Handler handler = new Handler(); DeleteEntryRunnable task = new DeleteEntryRunnable(this, App.getDB(), entry, @@ -583,7 +569,8 @@ public class GroupActivity extends ListNodesActivity } catch (Exception ignored) {} // TODO custom icon updateGroup.setIcon(iconStandard); - mAdapter.removeNode(oldGroupToUpdate); + if (listNodesFragment != null) + listNodesFragment.removeNode(oldGroupToUpdate); // If group updated save it in the database UpdateGroupRunnable updateGroupRunnable = new UpdateGroupRunnable(this, diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/ListNodesActivity.java b/app/src/main/java/com/kunzisoft/keepass/activities/ListNodesActivity.java index a861b30da..ccd3bd1ed 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/ListNodesActivity.java +++ b/app/src/main/java/com/kunzisoft/keepass/activities/ListNodesActivity.java @@ -22,20 +22,13 @@ package com.kunzisoft.keepass.activities; import android.annotation.SuppressLint; import android.app.assist.AssistStructure; import android.content.Intent; -import android.content.SharedPreferences; -import android.content.SharedPreferences.Editor; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Handler; -import android.preference.PreferenceManager; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; -import android.view.View; import android.widget.TextView; import com.kunzisoft.keepass.R; @@ -53,20 +46,19 @@ import com.kunzisoft.keepass.database.action.OnFinishRunnable; import com.kunzisoft.keepass.dialogs.AssignMasterKeyDialogFragment; import com.kunzisoft.keepass.dialogs.SortDialogFragment; import com.kunzisoft.keepass.password.AssignPasswordHelper; -import com.kunzisoft.keepass.settings.PreferencesUtil; import com.kunzisoft.keepass.tasks.SaveDatabaseProgressTaskDialogFragment; import com.kunzisoft.keepass.tasks.UIToastTask; import com.kunzisoft.keepass.utils.MenuUtil; public abstract class ListNodesActivity extends LockingActivity implements AssignMasterKeyDialogFragment.AssignPasswordDialogListener, - NodeAdapter.OnNodeClickCallback, + NodeAdapter.NodeClickCallback, SortDialogFragment.SortSelectionListener { - protected PwGroup mCurrentGroup; - protected NodeAdapter mAdapter; - - private SharedPreferences prefs; + protected static final String LIST_NODES_FRAGMENT_TAG = "LIST_NODES_FRAGMENT_TAG"; + protected ListNodesFragment listNodesFragment; + + protected PwGroup mCurrentGroup; protected AutofillHelper autofillHelper; @@ -83,18 +75,10 @@ public abstract class ListNodesActivity extends LockingActivity finish(); return; } - - prefs = PreferenceManager.getDefaultSharedPreferences(this); - invalidateOptionsMenu(); + invalidateOptionsMenu(); - // TODO Move in search - setContentView(R.layout.list_nodes); - - mCurrentGroup = initCurrentGroup(); - - mAdapter = new NodeAdapter(this); - addOptionsToAdapter(mAdapter); + mCurrentGroup = initializeListNodesFragment(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { autofillHelper = new AutofillHelper(); @@ -102,42 +86,58 @@ public abstract class ListNodesActivity extends LockingActivity } } - protected abstract PwGroup initCurrentGroup(); + protected abstract PwGroup initializeListNodesFragment(); - protected abstract RecyclerView defineNodeList(); + /** + * Attach the fragment's list of node. + *
+ * R.id.nodes_list_fragment_container must be the id of the container + */ + protected void attachFragmentToContentView() { + getSupportFragmentManager().beginTransaction().replace( + R.id.nodes_list_fragment_container, + listNodesFragment, + LIST_NODES_FRAGMENT_TAG) + .commit(); + } - protected void addOptionsToAdapter(NodeAdapter nodeAdapter) { - mAdapter.setOnNodeClickListener(this); + public void assignToolbarElements() { + if (mCurrentGroup != null) { + String title = mCurrentGroup.getName(); + TextView tv = findViewById(R.id.group_name); + if (title != null && title.length() > 0) { + if (tv != null) { + tv.setText(title); + tv.invalidate(); + } + } else { + if (tv != null) { + tv.setText(getText(R.string.root)); + tv.invalidate(); + } + } + } } @Override - protected void onResume() { - super.onResume(); - // Add elements to the list - mAdapter.rebuildList(mCurrentGroup); - assignListToNodeAdapter(defineNodeList()); - } - - protected void setGroupTitle() { - if ( mCurrentGroup != null ) { - String name = mCurrentGroup.getName(); - TextView tv = findViewById(R.id.group_name); - if ( name != null && name.length() > 0 ) { - if ( tv != null ) { - tv.setText(name); - } - } else { - if ( tv != null ) { - tv.setText(getText(R.string.root)); - } - } - } + public boolean onCreateOptionsMenu(Menu menu) { + super.onCreateOptionsMenu(menu); + + MenuInflater inflater = getMenuInflater(); + MenuUtil.contributionMenuInflater(inflater, menu); + inflater.inflate(R.menu.default_menu, menu); + + return true; } - protected void assignListToNodeAdapter(RecyclerView recyclerView) { - recyclerView.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET); - recyclerView.setLayoutManager(new LinearLayoutManager(this)); - recyclerView.setAdapter(mAdapter); + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch ( item.getItemId() ) { + default: + // Check the time lock before launching settings + MenuUtil.onDefaultMenuOptionsItemSelected(this, item, true); + return super.onOptionsItemSelected(item); + } } @Override @@ -150,7 +150,7 @@ public abstract class ListNodesActivity extends LockingActivity if (assistStructure != null) { switch (node.getType()) { case GROUP: - GroupActivity.launch(this, (PwGroup) node, assistStructure); + openGroup((PwGroup) node); break; case ENTRY: // Build response with the entry selected @@ -163,7 +163,7 @@ public abstract class ListNodesActivity extends LockingActivity if ( assistStructure == null ){ switch (node.getType()) { case GROUP: - GroupActivity.launch(this, (PwGroup) node); + openGroup((PwGroup) node); break; case ENTRY: EntryActivity.launch(this, (PwEntry) node); @@ -172,69 +172,21 @@ public abstract class ListNodesActivity extends LockingActivity } } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - super.onCreateOptionsMenu(menu); - - MenuInflater inflater = getMenuInflater(); - MenuUtil.contributionMenuInflater(inflater, menu); - inflater.inflate(R.menu.tree, menu); - inflater.inflate(R.menu.default_menu, menu); - - return true; - } - - @Override - public void onSortSelected(SortNodeEnum sortNodeEnum, boolean ascending, boolean groupsBefore, boolean recycleBinBottom) { - // Toggle setting - Editor editor = prefs.edit(); - editor.putString(getString(R.string.sort_node_key), sortNodeEnum.name()); - editor.putBoolean(getString(R.string.sort_ascending_key), ascending); - editor.putBoolean(getString(R.string.sort_group_before_key), groupsBefore); - editor.putBoolean(getString(R.string.sort_recycle_bin_bottom_key), recycleBinBottom); - editor.apply(); - - // Tell the adapter to refresh it's list - mAdapter.notifyChangeSort(sortNodeEnum, ascending, groupsBefore); - mAdapter.rebuildList(mCurrentGroup); + private void openGroup(PwGroup group) { + ListNodesFragment newListNodeFragment = ListNodesFragment.newInstance(group.getId()); + getSupportFragmentManager().beginTransaction() + .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, + R.anim.slide_in_left, R.anim.slide_out_right) + .replace(R.id.nodes_list_fragment_container, + newListNodeFragment, + LIST_NODES_FRAGMENT_TAG) + .addToBackStack(LIST_NODES_FRAGMENT_TAG) + .commit(); + listNodesFragment = newListNodeFragment; + mCurrentGroup = group; + assignToolbarElements(); } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch ( item.getItemId() ) { - - case R.id.menu_sort: - SortDialogFragment sortDialogFragment; - - PwDatabase database = App.getDB().getPwDatabase(); - /* - // TODO Recycle bin bottom - if (database.isRecycleBinAvailable() && database.isRecycleBinEnabled()) { - sortDialogFragment = - SortDialogFragment.getInstance( - PrefsUtil.getListSort(this), - PrefsUtil.getAscendingSort(this), - PrefsUtil.getGroupsBeforeSort(this), - PrefsUtil.getRecycleBinBottomSort(this)); - } else { - */ - sortDialogFragment = - SortDialogFragment.getInstance( - PreferencesUtil.getListSort(this), - PreferencesUtil.getAscendingSort(this), - PreferencesUtil.getGroupsBeforeSort(this)); - //} - - sortDialogFragment.show(getSupportFragmentManager(), "sortDialog"); - return true; - - default: - // Check the time lock before launching settings - MenuUtil.onDefaultMenuOptionsItemSelected(this, item, true); - return super.onOptionsItemSelected(item); - } - } - @Override public void onAssignKeyDialogPositiveClick( boolean masterPasswordChecked, String masterPassword, @@ -253,29 +205,16 @@ public abstract class ListNodesActivity extends LockingActivity } + @Override + public void onSortSelected(SortNodeEnum sortNodeEnum, boolean ascending, boolean groupsBefore, boolean recycleBinBottom) { + if (listNodesFragment != null) + listNodesFragment.onSortSelected(sortNodeEnum, ascending, groupsBefore, recycleBinBottom); + } + @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); - switch (requestCode) { - case EntryEditActivity.ADD_OR_UPDATE_ENTRY_REQUEST_CODE: - if (resultCode == EntryEditActivity.ADD_ENTRY_RESULT_CODE || - resultCode == EntryEditActivity.UPDATE_ENTRY_RESULT_CODE) { - PwNode newNode = (PwNode) data.getSerializableExtra(EntryEditActivity.ADD_OR_UPDATE_ENTRY_KEY); - if (newNode != null) { - if (resultCode == EntryEditActivity.ADD_ENTRY_RESULT_CODE) - mAdapter.addNode(newNode); - if (resultCode == EntryEditActivity.UPDATE_ENTRY_RESULT_CODE) { - //mAdapter.updateLastNodeRegister(newNode); - mAdapter.rebuildList(mCurrentGroup); - } - } else { - Log.e(this.getClass().getName(), "New node can be retrieve in Activity Result"); - } - } - break; - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { AutofillHelper.onActivityResultSetResultAndFinish(this, requestCode, resultCode, data); } @@ -310,7 +249,8 @@ public abstract class ListNodesActivity extends LockingActivity runOnUiThread(() -> { if (mSuccess) { - mAdapter.addNode(newNode); + if (listNodesFragment != null) + listNodesFragment.addNode(newNode); } else { displayMessage(ListNodesActivity.this); } @@ -330,7 +270,8 @@ public abstract class ListNodesActivity extends LockingActivity runOnUiThread(() -> { if (mSuccess) { - mAdapter.updateNode(oldNode, newNode); + if (listNodesFragment != null) + listNodesFragment.updateNode(oldNode, newNode); } else { displayMessage(ListNodesActivity.this); } @@ -354,7 +295,10 @@ public abstract class ListNodesActivity extends LockingActivity runOnUiThread(() -> { if ( mSuccess) { - mAdapter.removeNode(pwNode); + + if (listNodesFragment != null) + listNodesFragment.removeNode(pwNode); + PwGroup parent = pwNode.getParent(); Database db = App.getDB(); PwDatabase database = db.getPwDatabase(); @@ -366,7 +310,9 @@ public abstract class ListNodesActivity extends LockingActivity && mCurrentGroup != null && mCurrentGroup.getParent() == null && !mCurrentGroup.equals(recycleBin)) { - mAdapter.addNode(parent); + + if (listNodesFragment != null) + listNodesFragment.addNode(parent); } } } else { @@ -379,4 +325,15 @@ public abstract class ListNodesActivity extends LockingActivity }); } } + + @Override + public void onBackPressed() { + super.onBackPressed(); + + listNodesFragment = (ListNodesFragment) getSupportFragmentManager().findFragmentByTag(LIST_NODES_FRAGMENT_TAG); + // to refresh fragment + listNodesFragment.onResume(); + mCurrentGroup = listNodesFragment.getMainGroup(); + assignToolbarElements(); + } } diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/ListNodesFragment.java b/app/src/main/java/com/kunzisoft/keepass/activities/ListNodesFragment.java new file mode 100644 index 000000000..e5e728dac --- /dev/null +++ b/app/src/main/java/com/kunzisoft/keepass/activities/ListNodesFragment.java @@ -0,0 +1,287 @@ +package com.kunzisoft.keepass.activities; + +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; + +import com.kunzisoft.keepass.R; +import com.kunzisoft.keepass.adapters.NodeAdapter; +import com.kunzisoft.keepass.app.App; +import com.kunzisoft.keepass.database.Database; +import com.kunzisoft.keepass.database.PwDatabase; +import com.kunzisoft.keepass.database.PwGroup; +import com.kunzisoft.keepass.database.PwGroupId; +import com.kunzisoft.keepass.database.PwNode; +import com.kunzisoft.keepass.database.SortNodeEnum; +import com.kunzisoft.keepass.dialogs.SortDialogFragment; +import com.kunzisoft.keepass.settings.PreferencesUtil; + +public class ListNodesFragment extends Fragment implements + SortDialogFragment.SortSelectionListener { + + private static final String TAG = ListNodesFragment.class.getName(); + + private static final String GROUP_KEY = "GROUP_KEY"; + private static final String GROUP_ID_KEY = "GROUP_ID_KEY"; + + private NodeAdapter.NodeClickCallback nodeClickCallback; + private NodeAdapter.NodeMenuListener nodeMenuListener; + private OnScrollListener onScrollListener; + + private RecyclerView listView; + protected PwGroup mCurrentGroup; + protected NodeAdapter mAdapter; + + // Preferences for sorting + private SharedPreferences prefs; + + public static ListNodesFragment newInstance(PwGroup group) { + Bundle bundle = new Bundle(); + if (group != null) { + bundle.putSerializable(GROUP_KEY, group); + } + ListNodesFragment listNodesFragment = new ListNodesFragment(); + listNodesFragment.setArguments(bundle); + return listNodesFragment; + } + + public static ListNodesFragment newInstance(PwGroupId groupId) { + Bundle bundle=new Bundle(); + if (groupId != null) { + bundle.putSerializable(GROUP_ID_KEY, groupId); + } + ListNodesFragment listNodesFragment = new ListNodesFragment(); + listNodesFragment.setArguments(bundle); + return listNodesFragment; + } + + @Override + public void onAttach(Context context) { + super.onAttach(context); + try { + nodeClickCallback = (NodeAdapter.NodeClickCallback) context; + } catch (ClassCastException e) { + // The activity doesn't implement the interface, throw exception + throw new ClassCastException(context.toString() + + " must implement " + NodeAdapter.NodeClickCallback.class.getName()); + } + try { + nodeMenuListener = (NodeAdapter.NodeMenuListener) context; + } catch (ClassCastException e) { + nodeMenuListener = null; + // Context menu can be omit + Log.w(TAG, context.toString() + + " must implement " + NodeAdapter.NodeMenuListener.class.getName()); + } + try { + onScrollListener = (OnScrollListener) context; + } catch (ClassCastException e) { + onScrollListener = null; + // Context menu can be omit + Log.w(TAG, context.toString() + + " must implement " + RecyclerView.OnScrollListener.class.getName()); + } + } + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setHasOptionsMenu(true); + + mCurrentGroup = initCurrentGroup(); + mAdapter = new NodeAdapter(getContext()); + mAdapter.setOnNodeClickListener(nodeClickCallback); + + if (nodeMenuListener != null) { + mAdapter.setActivateContextMenu(true); + mAdapter.setNodeMenuListener(nodeMenuListener); + } + + prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + } + + protected PwGroup initCurrentGroup() { + + Database db = App.getDB(); + PwGroup root = db.getPwDatabase().getRootGroup(); + PwGroup currentGroup = null; + + if (getArguments() != null) { + if (getArguments().containsKey(GROUP_KEY)) { + currentGroup = (PwGroup) getArguments().getSerializable(GROUP_KEY); + } + if (getArguments().containsKey(GROUP_ID_KEY)) { + PwGroupId pwGroupId = (PwGroupId) getArguments().getSerializable(GROUP_ID_KEY); + if ( pwGroupId != null ) + currentGroup = db.getPwDatabase().getGroupByGroupId(pwGroupId); + } + } + + if ( currentGroup == null ) { + currentGroup = root; + } + + return currentGroup; + } + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + + View rootView = inflater.inflate(R.layout.list_nodes_fragment, container, false); + listView = rootView.findViewById(R.id.nodes_list); + + if (onScrollListener != null) { + listView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + onScrollListener.onScrolled(dy); + } + }); + } + + return rootView; + } + + @Override + public void onResume() { + super.onResume(); + + // Add elements to the list + mAdapter.rebuildList(mCurrentGroup); + assignListToNodeAdapter(listView); + } + + protected void assignListToNodeAdapter(RecyclerView recyclerView) { + recyclerView.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET); + recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + recyclerView.setAdapter(mAdapter); + } + + @Override + public void onSortSelected(SortNodeEnum sortNodeEnum, boolean ascending, boolean groupsBefore, boolean recycleBinBottom) { + // Toggle setting + SharedPreferences.Editor editor = prefs.edit(); + editor.putString(getString(R.string.sort_node_key), sortNodeEnum.name()); + editor.putBoolean(getString(R.string.sort_ascending_key), ascending); + editor.putBoolean(getString(R.string.sort_group_before_key), groupsBefore); + editor.putBoolean(getString(R.string.sort_recycle_bin_bottom_key), recycleBinBottom); + editor.apply(); + + // Tell the adapter to refresh it's list + mAdapter.notifyChangeSort(sortNodeEnum, ascending, groupsBefore); + mAdapter.rebuildList(mCurrentGroup); + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + inflater.inflate(R.menu.tree, menu); + + super.onCreateOptionsMenu(menu, inflater); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch ( item.getItemId() ) { + + case R.id.menu_sort: + SortDialogFragment sortDialogFragment; + + PwDatabase database = App.getDB().getPwDatabase(); + /* + // TODO Recycle bin bottom + if (database.isRecycleBinAvailable() && database.isRecycleBinEnabled()) { + sortDialogFragment = + SortDialogFragment.getInstance( + PrefsUtil.getListSort(this), + PrefsUtil.getAscendingSort(this), + PrefsUtil.getGroupsBeforeSort(this), + PrefsUtil.getRecycleBinBottomSort(this)); + } else { + */ + sortDialogFragment = + SortDialogFragment.getInstance( + PreferencesUtil.getListSort(getContext()), + PreferencesUtil.getAscendingSort(getContext()), + PreferencesUtil.getGroupsBeforeSort(getContext())); + //} + + sortDialogFragment.show(getChildFragmentManager(), "sortDialog"); + return true; + + default: + return super.onOptionsItemSelected(item); + } + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + + switch (requestCode) { + case EntryEditActivity.ADD_OR_UPDATE_ENTRY_REQUEST_CODE: + if (resultCode == EntryEditActivity.ADD_ENTRY_RESULT_CODE || + resultCode == EntryEditActivity.UPDATE_ENTRY_RESULT_CODE) { + PwNode newNode = (PwNode) data.getSerializableExtra(EntryEditActivity.ADD_OR_UPDATE_ENTRY_KEY); + if (newNode != null) { + if (resultCode == EntryEditActivity.ADD_ENTRY_RESULT_CODE) + mAdapter.addNode(newNode); + if (resultCode == EntryEditActivity.UPDATE_ENTRY_RESULT_CODE) { + //mAdapter.updateLastNodeRegister(newNode); + mAdapter.rebuildList(mCurrentGroup); + } + } else { + Log.e(this.getClass().getName(), "New node can be retrieve in Activity Result"); + } + } + break; + } + } + + public boolean isEmpty() { + return mAdapter == null || mAdapter.getItemCount() <= 0; + } + + public void addNode(PwNode newNode) { + mAdapter.addNode(newNode); + } + + public void updateNode(PwNode oldNode, PwNode newNode) { + mAdapter.updateNode(oldNode, newNode); + } + + public void removeNode(PwNode pwNode) { + mAdapter.removeNode(pwNode); + } + + public PwGroup getMainGroup() { + return mCurrentGroup; + } + + public interface OnScrollListener { + + /** + * Callback method to be invoked when the RecyclerView has been scrolled. This will be + * called after the scroll has completed. + * + * @param dy The amount of vertical scroll. + */ + void onScrolled(int dy); + } +} diff --git a/app/src/main/java/com/kunzisoft/keepass/adapters/NodeAdapter.java b/app/src/main/java/com/kunzisoft/keepass/adapters/NodeAdapter.java index d6e10ecc2..e13e8bbaf 100644 --- a/app/src/main/java/com/kunzisoft/keepass/adapters/NodeAdapter.java +++ b/app/src/main/java/com/kunzisoft/keepass/adapters/NodeAdapter.java @@ -52,7 +52,7 @@ public class NodeAdapter extends RecyclerView.Adapter { private boolean groupsBeforeSort; private boolean ascendingSort; - private OnNodeClickCallback onNodeClickCallback; + private NodeClickCallback nodeClickCallback; private NodeMenuListener nodeMenuListener; private boolean activateContextMenu; @@ -66,10 +66,7 @@ public class NodeAdapter extends RecyclerView.Adapter { public NodeAdapter(final Context context) { this.inflater = LayoutInflater.from(context); this.context = context; - this.textSize = PreferencesUtil.getListTextSize(context); - this.listSort = PreferencesUtil.getListSort(context); - this.groupsBeforeSort = PreferencesUtil.getGroupsBeforeSort(context); - this.ascendingSort = PreferencesUtil.getAscendingSort(context); + assignPreferences(); this.activateContextMenu = false; this.nodeSortedList = new SortedList<>(PwNode.class, new SortedListAdapterCallback(this) { @@ -101,11 +98,19 @@ public class NodeAdapter extends RecyclerView.Adapter { this.activateContextMenu = activate; } + private void assignPreferences() { + this.textSize = PreferencesUtil.getListTextSize(context); + this.listSort = PreferencesUtil.getListSort(context); + this.groupsBeforeSort = PreferencesUtil.getGroupsBeforeSort(context); + this.ascendingSort = PreferencesUtil.getAscendingSort(context); + } + /** * Rebuild the list by clear and build children from the group */ public void rebuildList(PwGroup group) { this.nodeSortedList.clear(); + assignPreferences(); if (group != null) { this.nodeSortedList.addAll(group.getDirectChildren()); } @@ -208,8 +213,8 @@ public class NodeAdapter extends RecyclerView.Adapter { /** * Assign a listener when a node is clicked */ - public void setOnNodeClickListener(OnNodeClickCallback onNodeClickCallback) { - this.onNodeClickCallback = onNodeClickCallback; + public void setOnNodeClickListener(NodeClickCallback nodeClickCallback) { + this.nodeClickCallback = nodeClickCallback; } /** @@ -222,7 +227,7 @@ public class NodeAdapter extends RecyclerView.Adapter { /** * Callback listener to redefine to do an action when a node is click */ - public interface OnNodeClickCallback { + public interface NodeClickCallback { void onNodeClick(PwNode node); } @@ -247,8 +252,8 @@ public class NodeAdapter extends RecyclerView.Adapter { @Override public void onClick(View v) { - if (onNodeClickCallback != null) - onNodeClickCallback.onNodeClick(node); + if (nodeClickCallback != null) + nodeClickCallback.onNodeClick(node); } } diff --git a/app/src/main/java/com/kunzisoft/keepass/search/SearchResultsActivity.java b/app/src/main/java/com/kunzisoft/keepass/search/SearchResultsActivity.java index f18e2f32d..4d17c6a2a 100644 --- a/app/src/main/java/com/kunzisoft/keepass/search/SearchResultsActivity.java +++ b/app/src/main/java/com/kunzisoft/keepass/search/SearchResultsActivity.java @@ -22,7 +22,6 @@ package com.kunzisoft.keepass.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; @@ -31,6 +30,7 @@ import android.view.View; import com.kunzisoft.keepass.R; import com.kunzisoft.keepass.activities.ListNodesActivity; +import com.kunzisoft.keepass.activities.ListNodesFragment; import com.kunzisoft.keepass.app.App; import com.kunzisoft.keepass.database.Database; import com.kunzisoft.keepass.database.PwGroup; @@ -38,8 +38,6 @@ import com.kunzisoft.keepass.utils.MenuUtil; public class SearchResultsActivity extends ListNodesActivity { - private RecyclerView listView; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -53,33 +51,30 @@ public class SearchResultsActivity extends ListNodesActivity { getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); - listView = findViewById(R.id.nodes_list); + attachFragmentToContentView(); + View notFoundView = findViewById(R.id.not_found_container); + View listContainer = findViewById(R.id.nodes_list_fragment_container); if ( mCurrentGroup == null || mCurrentGroup.numbersOfChildEntries() < 1 ) { - listView.setVisibility(View.GONE); + listContainer.setVisibility(View.GONE); notFoundView.setVisibility(View.VISIBLE); } else { - listView.setVisibility(View.VISIBLE); + listContainer.setVisibility(View.VISIBLE); notFoundView.setVisibility(View.GONE); } - - setGroupTitle(); } @Override - protected PwGroup initCurrentGroup() { + protected PwGroup initializeListNodesFragment() { Database mDb = App.getDB(); // Likely the app has been killed exit the activity if ( ! mDb.getLoaded() ) { finish(); } - return mDb.search(getSearchStr(getIntent()).trim()); - } - - @Override - protected RecyclerView defineNodeList() { - return listView; + PwGroup group = mDb.search(getSearchStr(getIntent()).trim()); + listNodesFragment = ListNodesFragment.newInstance(group); + return group; } @Override @@ -87,7 +82,6 @@ public class SearchResultsActivity extends ListNodesActivity { MenuInflater inflater = getMenuInflater(); MenuUtil.contributionMenuInflater(inflater, menu); - inflater.inflate(R.menu.tree, menu); inflater.inflate(R.menu.default_menu, menu); return true; diff --git a/app/src/main/java/com/kunzisoft/keepass/view/AddNodeButtonView.java b/app/src/main/java/com/kunzisoft/keepass/view/AddNodeButtonView.java index 55659fe34..7bcc65caf 100644 --- a/app/src/main/java/com/kunzisoft/keepass/view/AddNodeButtonView.java +++ b/app/src/main/java/com/kunzisoft/keepass/view/AddNodeButtonView.java @@ -26,7 +26,6 @@ import android.support.design.widget.FloatingActionButton; import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewPropertyAnimatorListener; import android.support.v4.view.animation.FastOutSlowInInterpolator; -import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.MotionEvent; @@ -124,20 +123,14 @@ public class AddNodeButtonView extends RelativeLayout { return super.onTouchEvent(event); } - public RecyclerView.OnScrollListener hideButtonOnScrollListener() { - return new RecyclerView.OnScrollListener() { - @Override - public void onScrolled(RecyclerView recyclerView, int dx, int dy) { - super.onScrolled(recyclerView, dx, dy); - if (state.equals(State.CLOSE)) { - if (dy > 0 && addButtonView.getVisibility() == View.VISIBLE) { - hideButton(); - } else if (dy < 0 && addButtonView.getVisibility() != View.VISIBLE) { - showButton(); - } - } + public void hideButtonOnScrollListener(int dy) { + if (state.equals(State.CLOSE)) { + if (dy > 0 && addButtonView.getVisibility() == View.VISIBLE) { + hideButton(); + } else if (dy < 0 && addButtonView.getVisibility() != View.VISIBLE) { + showButton(); } - }; + } } public void showButton() { diff --git a/app/src/main/res/layout/list_nodes.xml b/app/src/main/res/layout/list_nodes.xml deleted file mode 100644 index 540f4408f..000000000 --- a/app/src/main/res/layout/list_nodes.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/list_nodes_fragment.xml b/app/src/main/res/layout/list_nodes_fragment.xml new file mode 100644 index 000000000..4aecac136 --- /dev/null +++ b/app/src/main/res/layout/list_nodes_fragment.xml @@ -0,0 +1,5 @@ + + \ No newline at end of file diff --git a/app/src/main/res/layout/list_nodes_with_add_button.xml b/app/src/main/res/layout/list_nodes_with_add_button.xml index aeaf77f06..c9be096e1 100644 --- a/app/src/main/res/layout/list_nodes_with_add_button.xml +++ b/app/src/main/res/layout/list_nodes_with_add_button.xml @@ -63,8 +63,8 @@ - - + android:layout_height="wrap_content" /> \ No newline at end of file