diff options
author | Harsh Shandilya <msfjarvis@gmail.com> | 2019-11-03 19:10:45 +0530 |
---|---|---|
committer | Harsh Shandilya <msfjarvis@gmail.com> | 2019-11-03 20:46:03 +0530 |
commit | a520b3db64db564e6bd639ab2c745015c3010c43 (patch) | |
tree | 9984714ac8075f2ab6101e69781279d8c0aa7639 | |
parent | 25029c905570aa393b1e4ba88a5155922217d9b0 (diff) |
Convert SelectFolderFragment to Kotlin
Signed-off-by: Harsh Shandilya <msfjarvis@gmail.com>
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.java | 132 | ||||
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.kt | 117 |
2 files changed, 117 insertions, 132 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.java b/app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.java deleted file mode 100644 index c9b6b5f2..00000000 --- a/app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.java +++ /dev/null @@ -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); - } -} diff --git a/app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.kt b/app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.kt new file mode 100644 index 00000000..2668517b --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/SelectFolderFragment.kt @@ -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) + } +} |