Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to ActivityResultContracts #910

Merged
merged 22 commits into from Jul 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6c49eca
Move git directory selection to ActivityResultContracts
msfjarvis Jun 23, 2020
6b657ff
global: replace all android.app.Activity references
msfjarvis Jun 23, 2020
e18661f
res: resolve ObsoleteSdkInt lint warning
msfjarvis Jun 23, 2020
36a9928
layout: silence some overdraw warnings
msfjarvis Jun 23, 2020
8ead3be
PasswordFragment: address deprecation
msfjarvis Jun 18, 2020
9ae9472
PasswordStore: start addressing deprecation warnings
msfjarvis Jun 25, 2020
a47e4ef
autofill: silence deprecation warnings for legacy implementation
msfjarvis Jun 25, 2020
17e4fee
Reset scrollTarget after use
msfjarvis Jul 1, 2020
646f97c
Refresh password list after each swipe
msfjarvis Jul 1, 2020
a80f899
Convert if to when
msfjarvis Jul 1, 2020
ac420c0
Migrate UserPreference to ActivityResultContracts
msfjarvis Jul 1, 2020
b9d0463
Also validate result in git directory selection
msfjarvis Jul 1, 2020
f3b549e
AutofillSaveActivity: Switch to ActivityResultContracts
msfjarvis Jul 1, 2020
1dd484b
AutofillDecryptActivity: Switch to ActivityResultContracts
msfjarvis Jul 1, 2020
ae2d383
AutofillFilterActivity: Switch to ActivityResultContracts
msfjarvis Jul 1, 2020
b186334
Improve deletion flow
msfjarvis Jul 1, 2020
702003b
Uniform naming for activity result handlers
msfjarvis Jul 2, 2020
55e0795
Merge branch 'develop' into refactor/deprecation
msfjarvis Jul 2, 2020
05c9062
Merge branch 'develop' into refactor/deprecation
msfjarvis Jul 2, 2020
e038201
Merge branch 'develop' into refactor/deprecation
msfjarvis Jul 2, 2020
cf00eaf
Merge branch 'develop' into refactor/deprecation
FabianHenneke Jul 2, 2020
ff928d0
Merge branch 'develop' into refactor/deprecation
msfjarvis Jul 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 42 additions & 28 deletions app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
}

Expand All @@ -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
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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
}
}
}
Expand Down Expand Up @@ -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].
Expand All @@ -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()
Expand Down