Convert SelectFolderFragment to Kotlin
Signed-off-by: Harsh Shandilya <msfjarvis@gmail.com>
This commit is contained in:
parent
25029c9055
commit
a520b3db64
2 changed files with 117 additions and 132 deletions
|
@ -1,132 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright © 2014-2019 The Android Password Store Authors. All Rights Reserved.
|
|
||||||
* SPDX-License-Identifier: GPL-3.0-only
|
|
||||||
*/
|
|
||||||
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.ui.adapters.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);
|
|
||||||
}
|
|
||||||
}
|
|
117
app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.kt
Normal file
117
app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.kt
Normal file
|
@ -0,0 +1,117 @@
|
||||||
|
/*
|
||||||
|
* Copyright © 2014-2019 The Android Password Store Authors. All Rights Reserved.
|
||||||
|
* SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
*/
|
||||||
|
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.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.ui.adapters.FolderRecyclerAdapter
|
||||||
|
import com.zeapo.pwdstore.utils.PasswordItem
|
||||||
|
import com.zeapo.pwdstore.utils.PasswordRepository
|
||||||
|
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getPasswords
|
||||||
|
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepositoryDirectory
|
||||||
|
import com.zeapo.pwdstore.utils.PasswordRepository.PasswordSortOrder.Companion.getSortOrder
|
||||||
|
import java.io.File
|
||||||
|
import java.util.Stack
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A fragment representing a list of Items.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Large screen devices (such as tablets) are supported by replacing the ListView with a
|
||||||
|
* GridView.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
class SelectFolderFragment : Fragment() {
|
||||||
|
// store the pass files list in a stack
|
||||||
|
private lateinit var pathStack: Stack<File>
|
||||||
|
private lateinit var recyclerAdapter: FolderRecyclerAdapter
|
||||||
|
private lateinit var recyclerView: RecyclerView
|
||||||
|
private lateinit var mListener: OnFragmentInteractionListener
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
val path = requireArguments().getString("Path")
|
||||||
|
pathStack = Stack()
|
||||||
|
recyclerAdapter = FolderRecyclerAdapter(
|
||||||
|
mListener,
|
||||||
|
getPasswords(
|
||||||
|
File(path),
|
||||||
|
getRepositoryDirectory(requireActivity())!!,
|
||||||
|
sortOrder))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreateView(
|
||||||
|
inflater: LayoutInflater,
|
||||||
|
container: ViewGroup?,
|
||||||
|
savedInstanceState: Bundle?
|
||||||
|
): View? {
|
||||||
|
val view = inflater.inflate(R.layout.password_recycler_view, container, false)
|
||||||
|
// use a linear layout manager
|
||||||
|
recyclerView = view.findViewById(R.id.pass_recycler)
|
||||||
|
recyclerView.layoutManager = LinearLayoutManager(requireContext())
|
||||||
|
// use divider
|
||||||
|
recyclerView.addItemDecoration(
|
||||||
|
DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL))
|
||||||
|
// Set the adapter
|
||||||
|
recyclerView.adapter = recyclerAdapter
|
||||||
|
val fab: FloatingActionButton = view.findViewById(R.id.fab)
|
||||||
|
fab.hide()
|
||||||
|
registerForContextMenu(recyclerView)
|
||||||
|
return view
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onAttach(context: Context) {
|
||||||
|
super.onAttach(context)
|
||||||
|
try {
|
||||||
|
mListener = object : OnFragmentInteractionListener {
|
||||||
|
override fun onFragmentInteraction(item: PasswordItem) {
|
||||||
|
if (item.type == PasswordItem.TYPE_CATEGORY) {
|
||||||
|
// push the category were we're going
|
||||||
|
pathStack.push(item.file)
|
||||||
|
recyclerView.scrollToPosition(0)
|
||||||
|
recyclerAdapter.clear()
|
||||||
|
recyclerAdapter.addAll(
|
||||||
|
getPasswords(
|
||||||
|
item.file,
|
||||||
|
getRepositoryDirectory(context)!!,
|
||||||
|
sortOrder))
|
||||||
|
(requireActivity() as AppCompatActivity).supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: ClassCastException) {
|
||||||
|
throw ClassCastException(
|
||||||
|
"$context must implement OnFragmentInteractionListener")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets the current directory
|
||||||
|
*
|
||||||
|
* @return the current directory
|
||||||
|
*/
|
||||||
|
val currentDir: File?
|
||||||
|
get() = if (pathStack.isEmpty()) getRepositoryDirectory(requireContext()) else pathStack.peek()
|
||||||
|
|
||||||
|
private val sortOrder: PasswordRepository.PasswordSortOrder
|
||||||
|
get() = getSortOrder(PreferenceManager.getDefaultSharedPreferences(requireContext()))
|
||||||
|
|
||||||
|
interface OnFragmentInteractionListener {
|
||||||
|
fun onFragmentInteraction(item: PasswordItem)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue