summaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2020-09-05 04:13:08 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2020-09-05 22:48:59 +0530
commit2db640d6cb490c5471d5497ffeb6ab68179bebd8 (patch)
tree4fce794cdd9a7d77d9047403b78e3bd7e441f7f4 /app/src/main
parent32cb1b3af3f534475d078abfd1e5fd3bd57f7c89 (diff)
SshKeyGenActivity: use runCatching to replace exception handling
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/sshkeygen/SshKeyGenActivity.kt42
1 files changed, 21 insertions, 21 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/sshkeygen/SshKeyGenActivity.kt b/app/src/main/java/com/zeapo/pwdstore/sshkeygen/SshKeyGenActivity.kt
index 810a8925..c7277455 100644
--- a/app/src/main/java/com/zeapo/pwdstore/sshkeygen/SshKeyGenActivity.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/sshkeygen/SshKeyGenActivity.kt
@@ -13,6 +13,8 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.edit
import androidx.core.content.getSystemService
import androidx.lifecycle.lifecycleScope
+import com.github.michaelbull.result.fold
+import com.github.michaelbull.result.runCatching
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.zeapo.pwdstore.R
import com.zeapo.pwdstore.databinding.ActivitySshKeygenBinding
@@ -109,7 +111,7 @@ class SshKeyGenActivity : AppCompatActivity() {
isEnabled = false
}
binding.generate.text = getString(R.string.ssh_key_gen_generating_progress)
- val e = try {
+ val result = runCatching {
withContext(Dispatchers.IO) {
val requireAuthentication = binding.keyRequireAuthentication.isChecked
if (requireAuthentication) {
@@ -125,31 +127,29 @@ class SshKeyGenActivity : AppCompatActivity() {
}
keyGenType.generateKey(requireAuthentication)
}
- null
- } catch (e: Exception) {
- e.printStackTrace()
- e
- } finally {
- getEncryptedPrefs("git_operation").edit {
- remove("ssh_key_local_passphrase")
- }
+ }
+ getEncryptedPrefs("git_operation").edit {
+ remove("ssh_key_local_passphrase")
}
binding.generate.apply {
text = getString(R.string.ssh_keygen_generate)
isEnabled = true
}
- if (e == null) {
- val df = ShowSshKeyFragment()
- df.show(supportFragmentManager, "public_key")
- } else {
- MaterialAlertDialogBuilder(this)
- .setTitle(getString(R.string.error_generate_ssh_key))
- .setMessage(getString(R.string.ssh_key_error_dialog_text) + e.message)
- .setPositiveButton(getString(R.string.dialog_ok)) { _, _ ->
- finish()
- }
- .show()
- }
+ result.fold(
+ success = {
+ ShowSshKeyFragment().show(supportFragmentManager, "public_key")
+ },
+ failure = { e ->
+ e.printStackTrace()
+ MaterialAlertDialogBuilder(this)
+ .setTitle(getString(R.string.error_generate_ssh_key))
+ .setMessage(getString(R.string.ssh_key_error_dialog_text) + e.message)
+ .setPositiveButton(getString(R.string.dialog_ok)) { _, _ ->
+ finish()
+ }
+ .show()
+ },
+ )
hideKeyboard()
}