diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2020-09-05 20:42:26 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2020-09-05 22:49:38 +0530 |
commit | 3a0bf37c000288a5ef5906b395a50be5fcfa7c7f (patch) | |
tree | b811cd31e5186b8c2f46b96a1a2dda3aa64dbe63 /app/src/main/java/com | |
parent | c183dd82d62debcee99b4ab7ccdfa74df8d865fa (diff) |
PasswordCreationActivity: use runCatching to replace exception handling
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java/com')
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt b/app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt index 368182ee..b5ab0008 100644 --- a/app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/crypto/PasswordCreationActivity.kt @@ -20,7 +20,9 @@ import androidx.core.view.isVisible import androidx.core.widget.doOnTextChanged import androidx.lifecycle.lifecycleScope import com.github.ajalt.timberkt.e +import com.github.michaelbull.result.onFailure import com.github.michaelbull.result.onSuccess +import com.github.michaelbull.result.runCatching import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.zxing.integration.android.IntentIntegrator import com.google.zxing.integration.android.IntentIntegrator.QR_CODE @@ -383,7 +385,7 @@ class PasswordCreationActivity : BasePgpActivity(), OpenPgpServiceConnection.OnB api?.executeApiAsync(data, inputStream, outputStream) { result -> when (result?.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { OpenPgpApi.RESULT_CODE_SUCCESS -> { - try { + runCatching { val file = File(path) // If we're not editing, this file should not already exist! if (!editing && file.exists()) { @@ -396,22 +398,8 @@ class PasswordCreationActivity : BasePgpActivity(), OpenPgpServiceConnection.OnB return@executeApiAsync } - try { - file.outputStream().use { - it.write(outputStream.toByteArray()) - } - } catch (e: IOException) { - e(e) { "Failed to write password file" } - setResult(RESULT_CANCELED) - MaterialAlertDialogBuilder(this@PasswordCreationActivity) - .setTitle(getString(R.string.password_creation_file_fail_title)) - .setMessage(getString(R.string.password_creation_file_write_fail_message)) - .setCancelable(false) - .setPositiveButton(android.R.string.ok) { _, _ -> - finish() - } - .show() - return@executeApiAsync + file.outputStream().use { + it.write(outputStream.toByteArray()) } //associate the new password name with the last name's timestamp in history @@ -467,8 +455,21 @@ class PasswordCreationActivity : BasePgpActivity(), OpenPgpServiceConnection.OnB } } - } catch (e: Exception) { - e(e) { "An Exception occurred" } + }.onFailure { e -> + if (e is IOException) { + e(e) { "Failed to write password file" } + setResult(RESULT_CANCELED) + MaterialAlertDialogBuilder(this@PasswordCreationActivity) + .setTitle(getString(R.string.password_creation_file_fail_title)) + .setMessage(getString(R.string.password_creation_file_write_fail_message)) + .setCancelable(false) + .setPositiveButton(android.R.string.ok) { _, _ -> + finish() + } + .show() + } else { + e(e) + } } } OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED -> { |