diff options
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java')
| -rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java | 155 |
1 files changed, 91 insertions, 64 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java index 33424353..98803f29 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.java @@ -6,7 +6,6 @@ import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; - import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; @@ -14,22 +13,21 @@ import androidx.preference.PreferenceManager; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; - import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.zeapo.pwdstore.utils.PasswordItem; import com.zeapo.pwdstore.utils.PasswordRecyclerAdapter; import com.zeapo.pwdstore.utils.PasswordRepository; - import java.io.File; import java.util.ArrayList; import java.util.Stack; /** * A fragment representing a list of Items. - * <p/> - * Large screen devices (such as tablets) are supported by replacing the ListView - * with a GridView. - * <p/> + * + * <p>Large screen devices (such as tablets) are supported by replacing the ListView with a + * GridView. + * + * <p> */ public class PasswordFragment extends Fragment { @@ -43,11 +41,10 @@ public class PasswordFragment extends Fragment { private SharedPreferences settings; /** - * Mandatory empty constructor for the fragment manager to instantiate the - * fragment (e.g. upon screen orientation changes). + * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon + * screen orientation changes). */ - public PasswordFragment() { - } + public PasswordFragment() {} @Override public void onCreate(Bundle savedInstanceState) { @@ -58,13 +55,19 @@ public class PasswordFragment extends Fragment { passListStack = new Stack<>(); scrollPosition = new Stack<>(); pathStack = new Stack<>(); - recyclerAdapter = new PasswordRecyclerAdapter((PasswordStore) requireActivity(), mListener, - PasswordRepository.getPasswords(new File(path), PasswordRepository.getRepositoryDirectory(requireContext()), getSortOrder())); + recyclerAdapter = + new PasswordRecyclerAdapter( + (PasswordStore) requireActivity(), + mListener, + PasswordRepository.getPasswords( + new File(path), + PasswordRepository.getRepositoryDirectory(requireContext()), + getSortOrder())); } @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { + public View onCreateView( + @NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.password_recycler_view, container, false); // use a linear layout manager @@ -74,7 +77,8 @@ public class PasswordFragment extends Fragment { recyclerView.setLayoutManager(mLayoutManager); // use divider - recyclerView.addItemDecoration(new DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL)); + recyclerView.addItemDecoration( + new DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL)); // Set the adapter recyclerView.setAdapter(recyclerAdapter); @@ -90,55 +94,77 @@ public class PasswordFragment extends Fragment { public void onAttach(final Context context) { super.onAttach(context); try { - mListener = item -> { - if (item.getType() == PasswordItem.TYPE_CATEGORY) { - // push the current password list (non filtered plz!) - passListStack.push(pathStack.isEmpty() ? - PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(context), getSortOrder()) : - PasswordRepository.getPasswords(pathStack.peek(), PasswordRepository.getRepositoryDirectory(context), getSortOrder())); - //push the category were we're going - pathStack.push(item.getFile()); - scrollPosition.push(recyclerView.getVerticalScrollbarPosition()); - - recyclerView.scrollToPosition(0); - recyclerAdapter.clear(); - recyclerAdapter.addAll(PasswordRepository.getPasswords(item.getFile(), PasswordRepository.getRepositoryDirectory(context), getSortOrder())); - - ((AppCompatActivity) requireActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); - } else { - if (getArguments().getBoolean("matchWith", false)) { - ((PasswordStore) requireActivity()).matchPasswordWithApp(item); - } else { - ((PasswordStore) requireActivity()).decryptPassword(item); - } - } - }; + mListener = + item -> { + if (item.getType() == PasswordItem.TYPE_CATEGORY) { + // push the current password list (non filtered plz!) + passListStack.push( + pathStack.isEmpty() + ? PasswordRepository.getPasswords( + PasswordRepository.getRepositoryDirectory( + context), + getSortOrder()) + : PasswordRepository.getPasswords( + pathStack.peek(), + PasswordRepository.getRepositoryDirectory( + context), + getSortOrder())); + // push the category were we're going + pathStack.push(item.getFile()); + scrollPosition.push(recyclerView.getVerticalScrollbarPosition()); + + recyclerView.scrollToPosition(0); + recyclerAdapter.clear(); + recyclerAdapter.addAll( + PasswordRepository.getPasswords( + item.getFile(), + PasswordRepository.getRepositoryDirectory(context), + getSortOrder())); + + ((AppCompatActivity) requireActivity()) + .getSupportActionBar() + .setDisplayHomeAsUpEnabled(true); + } else { + if (getArguments().getBoolean("matchWith", false)) { + ((PasswordStore) requireActivity()).matchPasswordWithApp(item); + } else { + ((PasswordStore) requireActivity()).decryptPassword(item); + } + } + }; } catch (ClassCastException e) { throw new ClassCastException(context + " must implement OnFragmentInteractionListener"); } } - /** - * clears the adapter content and sets it back to the root view - */ + /** clears the adapter content and sets it back to the root view */ public void updateAdapter() { passListStack.clear(); pathStack.clear(); scrollPosition.clear(); recyclerAdapter.clear(); - recyclerAdapter.addAll(PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(requireContext()), getSortOrder())); - - ((AppCompatActivity) requireActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false); + recyclerAdapter.addAll( + PasswordRepository.getPasswords( + PasswordRepository.getRepositoryDirectory(requireContext()), + getSortOrder())); + + ((AppCompatActivity) requireActivity()) + .getSupportActionBar() + .setDisplayHomeAsUpEnabled(false); } - /** - * refreshes the adapter with the latest opened category - */ + /** refreshes the adapter with the latest opened category */ public void refreshAdapter() { recyclerAdapter.clear(); - recyclerAdapter.addAll(pathStack.isEmpty() ? - PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(requireContext()), getSortOrder()) : - PasswordRepository.getPasswords(pathStack.peek(), PasswordRepository.getRepositoryDirectory(requireContext()), getSortOrder())); + recyclerAdapter.addAll( + pathStack.isEmpty() + ? PasswordRepository.getPasswords( + PasswordRepository.getRepositoryDirectory(requireContext()), + getSortOrder()) + : PasswordRepository.getPasswords( + pathStack.peek(), + PasswordRepository.getRepositoryDirectory(requireContext()), + getSortOrder())); } /** @@ -158,13 +184,19 @@ public class PasswordFragment extends Fragment { * recursively filters a directory and extract all the matching items * * @param filter the filter to apply - * @param dir the directory to filter + * @param dir the directory to filter */ private void recursiveFilter(String filter, File dir) { // on the root the pathStack is empty - ArrayList<PasswordItem> passwordItems = dir == null ? - PasswordRepository.getPasswords(PasswordRepository.getRepositoryDirectory(requireContext()), getSortOrder()) : - PasswordRepository.getPasswords(dir, PasswordRepository.getRepositoryDirectory(requireContext()), getSortOrder()); + ArrayList<PasswordItem> passwordItems = + dir == null + ? PasswordRepository.getPasswords( + PasswordRepository.getRepositoryDirectory(requireContext()), + getSortOrder()) + : PasswordRepository.getPasswords( + dir, + PasswordRepository.getRepositoryDirectory(requireContext()), + getSortOrder()); boolean rec = settings.getBoolean("filter_recursively", true); for (PasswordItem item : passwordItems) { @@ -181,12 +213,9 @@ public class PasswordFragment extends Fragment { } } - /** - * Goes back one level back in the path - */ + /** Goes back one level back in the path */ public void popBack() { - if (passListStack.isEmpty()) - return; + if (passListStack.isEmpty()) return; recyclerView.scrollToPosition(scrollPosition.pop()); recyclerAdapter.clear(); @@ -200,10 +229,8 @@ public class PasswordFragment extends Fragment { * @return the current directory */ public File getCurrentDir() { - if (pathStack.isEmpty()) - return PasswordRepository.getRepositoryDirectory(requireContext()); - else - return pathStack.peek(); + if (pathStack.isEmpty()) return PasswordRepository.getRepositoryDirectory(requireContext()); + else return pathStack.peek(); } public boolean isNotEmpty() { |
