From cd271e6f04e8e7010f5112377fbcd96c77f407d4 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Sun, 7 Jul 2019 20:22:06 +0200 Subject: [PATCH] Kotlinized ListNodesFragment --- .../keepass/activities/GroupActivity.kt | 30 +- .../keepass/activities/ListNodesFragment.java | 294 ------------------ .../keepass/activities/ListNodesFragment.kt | 277 +++++++++++++++++ 3 files changed, 294 insertions(+), 307 deletions(-) delete mode 100644 app/src/main/java/com/kunzisoft/keepass/activities/ListNodesFragment.java create mode 100644 app/src/main/java/com/kunzisoft/keepass/activities/ListNodesFragment.kt diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt index 55bc7e07d..8f229a27e 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt @@ -818,7 +818,8 @@ class GroupActivity : LockingActivity(), GroupEditDialogFragment.EditGroupListen override fun onActionNodeFinish(actionNodeValues: ActionNodeValues) { runOnUiThread { if (actionNodeValues.success) { - listNodesFragment?.addNode(actionNodeValues.newNode) + if (actionNodeValues.newNode != null) + listNodesFragment?.addNode(actionNodeValues.newNode) } } } @@ -828,7 +829,8 @@ class GroupActivity : LockingActivity(), GroupEditDialogFragment.EditGroupListen override fun onActionNodeFinish(actionNodeValues: ActionNodeValues) { runOnUiThread { if (actionNodeValues.success) { - listNodesFragment?.updateNode(actionNodeValues.oldNode, actionNodeValues.newNode) + if (actionNodeValues.oldNode!= null && actionNodeValues.newNode != null) + listNodesFragment?.updateNode(actionNodeValues.oldNode, actionNodeValues.newNode) } } } @@ -838,19 +840,21 @@ class GroupActivity : LockingActivity(), GroupEditDialogFragment.EditGroupListen override fun onActionNodeFinish(actionNodeValues: ActionNodeValues) { runOnUiThread { if (actionNodeValues.success) { - listNodesFragment?.removeNode(actionNodeValues.oldNode) + if (actionNodeValues.oldNode != null) + listNodesFragment?.removeNode(actionNodeValues.oldNode) actionNodeValues.oldNode?.let { oldNode -> - val parent = oldNode.parent - val database = App.currentDatabase - if (database.isRecycleBinAvailable && database.isRecycleBinEnabled) { - val recycleBin = database.recycleBin - // Add trash if it doesn't exists - if (parent == recycleBin - && mCurrentGroup != null - && mCurrentGroup!!.parent == null - && mCurrentGroup != recycleBin) { - listNodesFragment?.addNode(parent) + oldNode.parent?.let { parent -> + val database = App.currentDatabase + if (database.isRecycleBinAvailable && database.isRecycleBinEnabled) { + val recycleBin = database.recycleBin + // Add trash if it doesn't exists + if (parent == recycleBin + && mCurrentGroup != null + && mCurrentGroup!!.parent == null + && mCurrentGroup != recycleBin) { + listNodesFragment?.addNode(parent) + } } } } diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/ListNodesFragment.java b/app/src/main/java/com/kunzisoft/keepass/activities/ListNodesFragment.java deleted file mode 100644 index 9d82dcc76..000000000 --- a/app/src/main/java/com/kunzisoft/keepass/activities/ListNodesFragment.java +++ /dev/null @@ -1,294 +0,0 @@ -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.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.database.SortNodeEnum; -import com.kunzisoft.keepass.database.element.GroupVersioned; -import com.kunzisoft.keepass.database.element.NodeVersioned; -import com.kunzisoft.keepass.dialogs.SortDialogFragment; -import com.kunzisoft.keepass.settings.PreferencesUtil; -import com.kunzisoft.keepass.stylish.StylishFragment; - -public class ListNodesFragment extends StylishFragment implements - SortDialogFragment.SortSelectionListener { - - private static final String TAG = ListNodesFragment.class.getName(); - - private static final String GROUP_KEY = "GROUP_KEY"; - private static final String IS_SEARCH = "IS_SEARCH"; - - private NodeAdapter.NodeClickCallback nodeClickCallback; - private NodeAdapter.NodeMenuListener nodeMenuListener; - private OnScrollListener onScrollListener; - - private RecyclerView listView; - private GroupVersioned currentGroup; - private NodeAdapter mAdapter; - - private View notFoundView; - private boolean isASearchResult; - - // Preferences for sorting - private SharedPreferences prefs; - - private boolean readOnly; - - public static ListNodesFragment newInstance(GroupVersioned group, boolean readOnly, boolean isASearch) { - Bundle bundle = new Bundle(); - if (group != null) { - bundle.putParcelable(GROUP_KEY, group); - } - bundle.putBoolean(IS_SEARCH, isASearch); - ReadOnlyHelper.INSTANCE.putReadOnlyInBundle(bundle, readOnly); - 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); - - if ( getActivity() != null ) { - setHasOptionsMenu(true); - - readOnly = ReadOnlyHelper.INSTANCE.retrieveReadOnlyFromInstanceStateOrArguments(savedInstanceState, getArguments()); - - if (getArguments() != null) { - // Contains all the group in element - if (getArguments().containsKey(GROUP_KEY)) { - currentGroup = getArguments().getParcelable(GROUP_KEY); - } - - if (getArguments().containsKey(IS_SEARCH)) { - isASearchResult = getArguments().getBoolean(IS_SEARCH); - } - } - - mAdapter = new NodeAdapter(getContextThemed(), getActivity().getMenuInflater()); - mAdapter.setReadOnly(readOnly); - mAdapter.setIsASearchResult(isASearchResult); - mAdapter.setOnNodeClickListener(nodeClickCallback); - - if (nodeMenuListener != null) { - mAdapter.setActivateContextMenu(true); - mAdapter.setNodeMenuListener(nodeMenuListener); - } - prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); - } - } - - @Override - public void onSaveInstanceState(@NonNull Bundle outState) { - ReadOnlyHelper.INSTANCE.onSaveInstanceState(outState, readOnly); - super.onSaveInstanceState(outState); - } - - @Nullable - @Override - public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - super.onCreateView(inflater, container, savedInstanceState); - - // To apply theme - View rootView = inflater.cloneInContext(getContextThemed()) - .inflate(R.layout.list_nodes_fragment, container, false); - listView = rootView.findViewById(R.id.nodes_list); - notFoundView = rootView.findViewById(R.id.not_found_container); - - 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(); - - rebuildList(); - - if (isASearchResult && mAdapter.isEmpty()) { - // To show the " no search entry found " - listView.setVisibility(View.GONE); - notFoundView.setVisibility(View.VISIBLE); - } else { - listView.setVisibility(View.VISIBLE); - notFoundView.setVisibility(View.GONE); - } - } - - public void rebuildList() { - // Add elements to the list - if (currentGroup != null) - mAdapter.rebuildList(currentGroup); - 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(currentGroup); - } - - @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; - - /* - // 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) { - NodeVersioned newNode = data.getParcelableExtra(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(currentGroup); - } - } 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(NodeVersioned newNode) { - mAdapter.addNode(newNode); - } - - public void updateNode(NodeVersioned oldNode, NodeVersioned newNode) { - mAdapter.updateNode(oldNode, newNode); - } - - public void removeNode(NodeVersioned pwNode) { - mAdapter.removeNode(pwNode); - } - - public GroupVersioned getMainGroup() { - return currentGroup; - } - - 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/activities/ListNodesFragment.kt b/app/src/main/java/com/kunzisoft/keepass/activities/ListNodesFragment.kt new file mode 100644 index 000000000..465c11c04 --- /dev/null +++ b/app/src/main/java/com/kunzisoft/keepass/activities/ListNodesFragment.kt @@ -0,0 +1,277 @@ +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.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.database.SortNodeEnum +import com.kunzisoft.keepass.database.element.GroupVersioned +import com.kunzisoft.keepass.database.element.NodeVersioned +import com.kunzisoft.keepass.dialogs.SortDialogFragment +import com.kunzisoft.keepass.settings.PreferencesUtil +import com.kunzisoft.keepass.stylish.StylishFragment + +class ListNodesFragment : StylishFragment(), SortDialogFragment.SortSelectionListener { + + private var nodeClickCallback: NodeAdapter.NodeClickCallback? = null + private var nodeMenuListener: NodeAdapter.NodeMenuListener? = null + private var onScrollListener: OnScrollListener? = null + + private var listView: RecyclerView? = null + var mainGroup: GroupVersioned? = null + private set + private var mAdapter: NodeAdapter? = null + + private var notFoundView: View? = null + private var isASearchResult: Boolean = false + + // Preferences for sorting + private var prefs: SharedPreferences? = null + + private var readOnly: Boolean = false + + val isEmpty: Boolean + get() = mAdapter == null || mAdapter?.itemCount?:0 <= 0 + + override fun onAttach(context: Context?) { + super.onAttach(context) + try { + nodeClickCallback = context as NodeAdapter.NodeClickCallback? + } catch (e: ClassCastException) { + // The activity doesn't implement the interface, throw exception + throw ClassCastException(context?.toString() + + " must implement " + NodeAdapter.NodeClickCallback::class.java.name) + } + + try { + nodeMenuListener = context as NodeAdapter.NodeMenuListener? + } catch (e: ClassCastException) { + nodeMenuListener = null + // Context menu can be omit + Log.w(TAG, context?.toString() + + " must implement " + NodeAdapter.NodeMenuListener::class.java.name) + } + + try { + onScrollListener = context as OnScrollListener? + } catch (e: ClassCastException) { + onScrollListener = null + // Context menu can be omit + Log.w(TAG, context?.toString() + + " must implement " + RecyclerView.OnScrollListener::class.java.name) + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + activity?.let { currentActivity -> + setHasOptionsMenu(true) + + readOnly = ReadOnlyHelper.retrieveReadOnlyFromInstanceStateOrArguments(savedInstanceState, arguments) + + arguments?.let { args -> + // Contains all the group in element + if (args.containsKey(GROUP_KEY)) { + mainGroup = args.getParcelable(GROUP_KEY) + } + if (args.containsKey(IS_SEARCH)) { + isASearchResult = args.getBoolean(IS_SEARCH) + } + } + + mAdapter = NodeAdapter(getContextThemed(), currentActivity.menuInflater) + mAdapter?.apply { + setReadOnly(readOnly) + setIsASearchResult(isASearchResult) + setOnNodeClickListener(nodeClickCallback) + nodeMenuListener?.let { menuListener -> + setActivateContextMenu(true) + setNodeMenuListener(menuListener) + } + } + prefs = PreferenceManager.getDefaultSharedPreferences(context) + } + } + + override fun onSaveInstanceState(outState: Bundle) { + ReadOnlyHelper.onSaveInstanceState(outState, readOnly) + super.onSaveInstanceState(outState) + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + super.onCreateView(inflater, container, savedInstanceState) + + // To apply theme + val rootView = inflater.cloneInContext(getContextThemed()) + .inflate(R.layout.list_nodes_fragment, container, false) + listView = rootView.findViewById(R.id.nodes_list) + notFoundView = rootView.findViewById(R.id.not_found_container) + + onScrollListener?.let { onScrollListener -> + listView?.addOnScrollListener(object : RecyclerView.OnScrollListener() { + override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) { + super.onScrolled(recyclerView, dx, dy) + onScrollListener.onScrolled(dy) + } + }) + } + + return rootView + } + + override fun onResume() { + super.onResume() + + rebuildList() + + if (isASearchResult && mAdapter!= null && mAdapter!!.isEmpty) { + // To show the " no search entry found " + listView?.visibility = View.GONE + notFoundView?.visibility = View.VISIBLE + } else { + listView?.visibility = View.VISIBLE + notFoundView?.visibility = View.GONE + } + } + + fun rebuildList() { + // Add elements to the list + mainGroup?.let { + mAdapter?.rebuildList(mainGroup) + } + listView?.apply { + scrollBarStyle = View.SCROLLBARS_INSIDE_INSET + layoutManager = LinearLayoutManager(context) + adapter = mAdapter + } + } + + override fun onSortSelected(sortNodeEnum: SortNodeEnum, ascending: Boolean, groupsBefore: Boolean, recycleBinBottom: Boolean) { + // Toggle setting + prefs?.edit()?.apply { + putString(getString(R.string.sort_node_key), sortNodeEnum.name) + putBoolean(getString(R.string.sort_ascending_key), ascending) + putBoolean(getString(R.string.sort_group_before_key), groupsBefore) + putBoolean(getString(R.string.sort_recycle_bin_bottom_key), recycleBinBottom) + apply() + } + + // Tell the adapter to refresh it's list + mAdapter?.notifyChangeSort(sortNodeEnum, ascending, groupsBefore) + mAdapter?.rebuildList(mainGroup) + } + + override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) { + inflater?.inflate(R.menu.tree, menu) + + super.onCreateOptionsMenu(menu, inflater) + } + + override fun onOptionsItemSelected(item: MenuItem?): Boolean { + when (item?.itemId) { + + R.id.menu_sort -> { + val sortDialogFragment: SortDialogFragment + + /* + // 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(context), + PreferencesUtil.getAscendingSort(context), + PreferencesUtil.getGroupsBeforeSort(context)) + //} + + sortDialogFragment.show(childFragmentManager, "sortDialog") + return true + } + + else -> return super.onOptionsItemSelected(item) + } + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + + when (requestCode) { + EntryEditActivity.ADD_OR_UPDATE_ENTRY_REQUEST_CODE -> { + if (resultCode == EntryEditActivity.ADD_ENTRY_RESULT_CODE + || resultCode == EntryEditActivity.UPDATE_ENTRY_RESULT_CODE) { + data?.getParcelableExtra(EntryEditActivity.ADD_OR_UPDATE_ENTRY_KEY)?.let { newNode -> + if (resultCode == EntryEditActivity.ADD_ENTRY_RESULT_CODE) + mAdapter?.addNode(newNode) + if (resultCode == EntryEditActivity.UPDATE_ENTRY_RESULT_CODE) { + //mAdapter.updateLastNodeRegister(newNode); + mAdapter?.rebuildList(mainGroup) + } + } ?: Log.e(this.javaClass.name, "New node can be retrieve in Activity Result") + } + } + } + } + + fun addNode(newNode: NodeVersioned) { + mAdapter?.addNode(newNode) + } + + fun updateNode(oldNode: NodeVersioned, newNode: NodeVersioned) { + mAdapter?.updateNode(oldNode, newNode) + } + + fun removeNode(pwNode: NodeVersioned) { + mAdapter?.removeNode(pwNode) + } + + 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. + */ + fun onScrolled(dy: Int) + } + + companion object { + + private val TAG = ListNodesFragment::class.java.name + + private const val GROUP_KEY = "GROUP_KEY" + private const val IS_SEARCH = "IS_SEARCH" + + fun newInstance(group: GroupVersioned?, readOnly: Boolean, isASearch: Boolean): ListNodesFragment { + val bundle = Bundle() + if (group != null) { + bundle.putParcelable(GROUP_KEY, group) + } + bundle.putBoolean(IS_SEARCH, isASearch) + ReadOnlyHelper.putReadOnlyInBundle(bundle, readOnly) + val listNodesFragment = ListNodesFragment() + listNodesFragment.arguments = bundle + return listNodesFragment + } + } +}