SshKeyGenActivity: use runCatching to replace exception handling

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-09-05 04:13:08 +05:30
parent 32cb1b3af3
commit 2db640d6cb
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80

View file

@ -13,6 +13,8 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.edit import androidx.core.content.edit
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import androidx.lifecycle.lifecycleScope 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.google.android.material.dialog.MaterialAlertDialogBuilder
import com.zeapo.pwdstore.R import com.zeapo.pwdstore.R
import com.zeapo.pwdstore.databinding.ActivitySshKeygenBinding import com.zeapo.pwdstore.databinding.ActivitySshKeygenBinding
@ -109,7 +111,7 @@ class SshKeyGenActivity : AppCompatActivity() {
isEnabled = false isEnabled = false
} }
binding.generate.text = getString(R.string.ssh_key_gen_generating_progress) binding.generate.text = getString(R.string.ssh_key_gen_generating_progress)
val e = try { val result = runCatching {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val requireAuthentication = binding.keyRequireAuthentication.isChecked val requireAuthentication = binding.keyRequireAuthentication.isChecked
if (requireAuthentication) { if (requireAuthentication) {
@ -125,31 +127,29 @@ class SshKeyGenActivity : AppCompatActivity() {
} }
keyGenType.generateKey(requireAuthentication) keyGenType.generateKey(requireAuthentication)
} }
null }
} catch (e: Exception) { getEncryptedPrefs("git_operation").edit {
e.printStackTrace() remove("ssh_key_local_passphrase")
e
} finally {
getEncryptedPrefs("git_operation").edit {
remove("ssh_key_local_passphrase")
}
} }
binding.generate.apply { binding.generate.apply {
text = getString(R.string.ssh_keygen_generate) text = getString(R.string.ssh_keygen_generate)
isEnabled = true isEnabled = true
} }
if (e == null) { result.fold(
val df = ShowSshKeyFragment() success = {
df.show(supportFragmentManager, "public_key") ShowSshKeyFragment().show(supportFragmentManager, "public_key")
} else { },
MaterialAlertDialogBuilder(this) failure = { e ->
.setTitle(getString(R.string.error_generate_ssh_key)) e.printStackTrace()
.setMessage(getString(R.string.ssh_key_error_dialog_text) + e.message) MaterialAlertDialogBuilder(this)
.setPositiveButton(getString(R.string.dialog_ok)) { _, _ -> .setTitle(getString(R.string.error_generate_ssh_key))
finish() .setMessage(getString(R.string.ssh_key_error_dialog_text) + e.message)
} .setPositiveButton(getString(R.string.dialog_ok)) { _, _ ->
.show() finish()
} }
.show()
},
)
hideKeyboard() hideKeyboard()
} }