aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.java
blob: 25304f10edd44b8ec3783887389c04c2114096fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright © 2014-2019 The Android Password Store Authors. All Rights Reserved.
 * SPDX-License-Identifier: GPL-2.0
 */
package com.zeapo.pwdstore;

import android.content.Context;
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;
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.FolderRecyclerAdapter;
import com.zeapo.pwdstore.utils.PasswordItem;
import com.zeapo.pwdstore.utils.PasswordRepository;
import java.io.File;
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>
 */
public class SelectFolderFragment extends Fragment {

    // store the pass files list in a stack
    private Stack<File> pathStack;
    private FolderRecyclerAdapter recyclerAdapter;
    private RecyclerView recyclerView;
    private OnFragmentInteractionListener mListener;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon
     * screen orientation changes).
     */
    public SelectFolderFragment() {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String path = getArguments().getString("Path");

        pathStack = new Stack<>();
        recyclerAdapter =
                new FolderRecyclerAdapter(
                        mListener,
                        PasswordRepository.getPasswords(
                                new File(path),
                                PasswordRepository.getRepositoryDirectory(requireActivity()),
                                getSortOrder()));
    }

    @Override
    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
        recyclerView = view.findViewById(R.id.pass_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));

        // use divider
        recyclerView.addItemDecoration(
                new DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL));

        // Set the adapter
        recyclerView.setAdapter(recyclerAdapter);
        FloatingActionButton fab = view.findViewById(R.id.fab);
        if (fab != null) fab.hide();

        registerForContextMenu(recyclerView);
        return view;
    }

    @Override
    public void onAttach(final Context context) {
        super.onAttach(context);
        try {
            mListener =
                    item -> {
                        if (item.getType() == PasswordItem.TYPE_CATEGORY) {
                            // push the category were we're going
                            pathStack.push(item.getFile());

                            recyclerView.scrollToPosition(0);
                            recyclerAdapter.clear();
                            recyclerAdapter.addAll(
                                    PasswordRepository.getPasswords(
                                            item.getFile(),
                                            PasswordRepository.getRepositoryDirectory(context),
                                            getSortOrder()));

                            ((AppCompatActivity) requireActivity())
                                    .getSupportActionBar()
                                    .setDisplayHomeAsUpEnabled(true);
                        }
                    };
        } catch (ClassCastException e) {
            throw new ClassCastException(
                    context.toString() + " must implement OnFragmentInteractionListener");
        }
    }

    /**
     * gets the current directory
     *
     * @return the current directory
     */
    public File getCurrentDir() {
        if (pathStack.isEmpty()) return PasswordRepository.getRepositoryDirectory(requireContext());
        else return pathStack.peek();
    }

    private PasswordRepository.PasswordSortOrder getSortOrder() {
        return PasswordRepository.PasswordSortOrder.getSortOrder(
                PreferenceManager.getDefaultSharedPreferences(requireContext()));
    }

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(PasswordItem item);
    }
}