aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt
diff options
context:
space:
mode:
authorHarsh Shandilya <msfjarvis@gmail.com>2020-07-03 12:54:06 +0530
committerGitHub <noreply@github.com>2020-07-03 09:24:06 +0200
commit25b426157417c97c61c3d2cc0d9fa556f337f22d (patch)
treef53331d7ee2d5b780ebf20a6ccdded53bda42005 /app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt
parentff3d2fcce3f1d74bdb1e13e3634f1ea9a9f9f1e5 (diff)
downloadHardenedPass-25b426157417c97c61c3d2cc0d9fa556f337f22d.tar.gz
HardenedPass-25b426157417c97c61c3d2cc0d9fa556f337f22d.zip
Migrate to ActivityResultContracts (#910)
* Move git directory selection to ActivityResultContracts Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> * global: replace all android.app.Activity references Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> * res: resolve ObsoleteSdkInt lint warning Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> * layout: silence some overdraw warnings Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> * PasswordFragment: address deprecation Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> * PasswordStore: start addressing deprecation warnings Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> * autofill: silence deprecation warnings for legacy implementation I don't want to ever touch these files Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> * Reset scrollTarget after use Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> * Refresh password list after each swipe Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> * Convert if to when Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> * Migrate UserPreference to ActivityResultContracts Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> * Also validate result in git directory selection Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> * AutofillSaveActivity: Switch to ActivityResultContracts Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> * AutofillDecryptActivity: Switch to ActivityResultContracts Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> * AutofillFilterActivity: Switch to ActivityResultContracts Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> * Improve deletion flow - Silently delete empty directory - Always refresh password list upon completion Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> * Uniform naming for activity result handlers Signed-off-by: Harsh Shandilya <me@msfjarvis.dev> Co-authored-by: Fabian Henneke <FabianHenneke@users.noreply.github.com>
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt')
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt70
1 files changed, 42 insertions, 28 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt
index e91df9c6..ff022fab 100644
--- a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt
@@ -14,9 +14,11 @@ import android.view.MenuItem
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
+import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.appcompat.view.ActionMode
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
+import androidx.fragment.app.setFragmentResultListener
import androidx.lifecycle.observe
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.LinearLayoutManager
@@ -47,17 +49,26 @@ class PasswordFragment : Fragment(R.layout.password_recycler_view) {
private val model: SearchableRepositoryViewModel by activityViewModels()
private val binding by viewBinding(PasswordRecyclerViewBinding::bind)
+ private val swipeResult = registerForActivityResult(StartActivityForResult()) {
+ binding.swipeRefresher.isRefreshing = false
+ requireStore().refreshPasswordList()
+ }
- private fun requireStore() = requireActivity() as PasswordStore
+ val currentDir: File
+ get() = model.currentDir.value!!
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
settings = PreferenceManager.getDefaultSharedPreferences(requireContext())
initializePasswordList()
binding.fab.setOnClickListener {
- ItemCreationBottomSheet().apply {
- setTargetFragment(this@PasswordFragment, 1000)
- }.show(parentFragmentManager, "BOTTOM_SHEET")
+ ItemCreationBottomSheet().show(childFragmentManager, "BOTTOM_SHEET")
+ }
+ childFragmentManager.setFragmentResultListener(ITEM_CREATION_REQUEST_KEY, viewLifecycleOwner) { _, bundle ->
+ when (bundle.getString(ACTION_KEY)) {
+ ACTION_FOLDER -> requireStore().createFolder()
+ ACTION_PASSWORD -> requireStore().createPassword()
+ }
}
}
@@ -73,7 +84,7 @@ class PasswordFragment : Fragment(R.layout.password_recycler_view) {
.setAction(R.string.clone_button) {
val intent = Intent(context, GitServerConfigActivity::class.java)
intent.putExtra(BaseGitActivity.REQUEST_ARG_OP, BaseGitActivity.REQUEST_CLONE)
- startActivityForResult(intent, BaseGitActivity.REQUEST_CLONE)
+ swipeResult.launch(intent)
}
.show()
binding.swipeRefresher.isRefreshing = false
@@ -87,7 +98,7 @@ class PasswordFragment : Fragment(R.layout.password_recycler_view) {
}
val intent = Intent(context, GitOperationActivity::class.java)
intent.putExtra(BaseGitActivity.REQUEST_ARG_OP, operationId)
- startActivityForResult(intent, operationId)
+ swipeResult.launch(intent)
}
}
@@ -129,22 +140,26 @@ class PasswordFragment : Fragment(R.layout.password_recycler_view) {
// and not on folder navigations since the latter leads to too many removal animations.
(recyclerView.itemAnimator as OnOffItemAnimator).isEnabled = result.isFiltered
recyclerAdapter.submitList(result.passwordItems) {
- if (result.isFiltered) {
- // When the result is filtered, we always scroll to the top since that is where
- // the best fuzzy match appears.
- recyclerView.scrollToPosition(0)
- } else if (scrollTarget != null) {
- scrollTarget?.let {
- recyclerView.scrollToPosition(recyclerAdapter.getPositionForFile(it))
+ when {
+ result.isFiltered -> {
+ // When the result is filtered, we always scroll to the top since that is where
+ // the best fuzzy match appears.
+ recyclerView.scrollToPosition(0)
}
- scrollTarget == null
- } else {
- // When the result is not filtered and there is a saved scroll position for it,
- // we try to restore it.
- recyclerViewStateToRestore?.let {
- recyclerView.layoutManager!!.onRestoreInstanceState(it)
+ scrollTarget != null -> {
+ scrollTarget?.let {
+ recyclerView.scrollToPosition(recyclerAdapter.getPositionForFile(it))
+ }
+ scrollTarget = null
+ }
+ else -> {
+ // When the result is not filtered and there is a saved scroll position for it,
+ // we try to restore it.
+ recyclerViewStateToRestore?.let {
+ recyclerView.layoutManager!!.onRestoreInstanceState(it)
+ }
+ recyclerViewStateToRestore = null
}
- recyclerViewStateToRestore = null
}
}
}
@@ -244,9 +259,7 @@ class PasswordFragment : Fragment(R.layout.password_recycler_view) {
}
}
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
- binding.swipeRefresher.isRefreshing = false
- }
+ private fun requireStore() = requireActivity() as PasswordStore
/**
* Returns true if the back press was handled by the [Fragment].
@@ -262,16 +275,17 @@ class PasswordFragment : Fragment(R.layout.password_recycler_view) {
return true
}
- val currentDir: File
- get() = model.currentDir.value!!
-
fun dismissActionMode() {
actionMode?.finish()
}
- fun createFolder() = requireStore().createFolder()
+ companion object {
+ const val ITEM_CREATION_REQUEST_KEY = "creation_key"
+ const val ACTION_KEY = "action"
+ const val ACTION_FOLDER = "folder"
+ const val ACTION_PASSWORD = "password"
+ }
- fun createPassword() = requireStore().createPassword()
fun navigateTo(file: File) {
requireStore().clearSearch()