From 2db640d6cb490c5471d5497ffeb6ab68179bebd8 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sat, 5 Sep 2020 04:13:08 +0530 Subject: SshKeyGenActivity: use runCatching to replace exception handling Signed-off-by: Harsh Shandilya --- .../zeapo/pwdstore/sshkeygen/SshKeyGenActivity.kt | 42 +++++++++++----------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'app/src/main/java/com') 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() } -- cgit v1.2.3