diff options
| author | Harsh Shandilya <me@msfjarvis.dev> | 2020-10-02 15:48:26 +0530 |
|---|---|---|
| committer | Harsh Shandilya <me@msfjarvis.dev> | 2020-10-02 15:51:14 +0530 |
| commit | cf03c554785ee822be3408edcfb81fa4cf03d0e5 (patch) | |
| tree | f503c31d5e5041dc92dccfa7f2d2b836edce662d /app/src/main/java/com/zeapo/pwdstore/ui | |
| parent | d792fa5135897a57383fbd147c369edc646fa817 (diff) | |
| download | HardenedPass-cf03c554785ee822be3408edcfb81fa4cf03d0e5.tar.gz HardenedPass-cf03c554785ee822be3408edcfb81fa4cf03d0e5.zip | |
all: refactor ActivityResultContracts usages to adhere to API requirements
The newest versions of AndroidX Activity and Fragments correctly enforce the
requirement for all contracts to be registered at class init or before
the lifecycle has reached `Lifecycle.State.STARTED`. To comply with these requirements,
move all instances of `registerForActivityResult` being called at arbitrary points
in the code to be done at class init.
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/ui')
| -rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/ui/dialogs/FolderCreationDialogFragment.kt | 48 |
1 files changed, 26 insertions, 22 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/FolderCreationDialogFragment.kt b/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/FolderCreationDialogFragment.kt index 1eb1c95d..c06a6d63 100644 --- a/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/FolderCreationDialogFragment.kt +++ b/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/FolderCreationDialogFragment.kt @@ -31,6 +31,30 @@ import me.msfjarvis.openpgpktx.util.OpenPgpApi class FolderCreationDialogFragment : DialogFragment() { + private lateinit var newFolder: File + + private val keySelectAction = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> + if (result.resultCode == AppCompatActivity.RESULT_OK) { + result.data?.getStringArrayExtra(OpenPgpApi.EXTRA_KEY_IDS)?.let { keyIds -> + val gpgIdentifierFile = File(newFolder, ".gpg-id") + gpgIdentifierFile.writeText(keyIds.joinToString("\n")) + val repo = PasswordRepository.getRepository(null) + if (repo != null) { + lifecycleScope.launch { + val repoPath = getRepositoryDirectory().absolutePath + requireActivity().commitChange( + getString( + R.string.git_commit_gpg_id, + BasePgpActivity.getLongName(gpgIdentifierFile.parentFile!!.absolutePath, repoPath, gpgIdentifierFile.name) + ), + ) + dismiss() + } + } + } + } + } + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val alertDialogBuilder = MaterialAlertDialogBuilder(requireContext()) alertDialogBuilder.setTitle(R.string.title_create_folder) @@ -53,7 +77,7 @@ class FolderCreationDialogFragment : DialogFragment() { val dialog = requireDialog() val folderNameView = dialog.findViewById<TextInputEditText>(R.id.folder_name_text) val folderNameViewContainer = dialog.findViewById<TextInputLayout>(R.id.folder_name_container) - val newFolder = File("$currentDir/${folderNameView.text}") + newFolder = File("$currentDir/${folderNameView.text}") folderNameViewContainer.error = when { newFolder.isFile -> getString(R.string.folder_creation_err_file_exists) newFolder.isDirectory -> getString(R.string.folder_creation_err_folder_exists) @@ -63,27 +87,7 @@ class FolderCreationDialogFragment : DialogFragment() { newFolder.mkdirs() (requireActivity() as PasswordStore).refreshPasswordList(newFolder) if (dialog.findViewById<MaterialCheckBox>(R.id.set_gpg_key).isChecked) { - val gpgIdentifierFile = File(newFolder, ".gpg-id") - registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> - if (result.resultCode == AppCompatActivity.RESULT_OK) { - result.data?.getStringArrayExtra(OpenPgpApi.EXTRA_KEY_IDS)?.let { keyIds -> - gpgIdentifierFile.writeText(keyIds.joinToString("\n")) - val repo = PasswordRepository.getRepository(null) - if (repo != null) { - lifecycleScope.launch { - val repoPath = getRepositoryDirectory().absolutePath - requireActivity().commitChange( - getString( - R.string.git_commit_gpg_id, - BasePgpActivity.getLongName(gpgIdentifierFile.parentFile!!.absolutePath, repoPath, gpgIdentifierFile.name) - ), - ) - dismiss() - } - } - } - } - }.launch(Intent(requireContext(), GetKeyIdsActivity::class.java)) + keySelectAction.launch(Intent(requireContext(), GetKeyIdsActivity::class.java)) return } else { dismiss() |
