aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2020-09-05 05:14:50 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2020-09-05 22:49:03 +0530
commit104c6550f651434cb60393ac167c7d3b46014442 (patch)
treed47a41c3b82b607a481c2e296aba6adc24c5ab04
parent9bc1d59dd225a4b482ad8177c80bef5af946a9f0 (diff)
UserPreference: use runCatching to replace exception handling
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/UserPreference.kt19
1 files changed, 10 insertions, 9 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt
index 6319ee51..35c9e248 100644
--- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt
@@ -37,6 +37,9 @@ import androidx.preference.SwitchPreferenceCompat
import com.github.ajalt.timberkt.Timber.tag
import com.github.ajalt.timberkt.d
import com.github.ajalt.timberkt.w
+import com.github.michaelbull.result.getOr
+import com.github.michaelbull.result.onFailure
+import com.github.michaelbull.result.runCatching
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.zeapo.pwdstore.autofill.AutofillPreferenceActivity
import com.zeapo.pwdstore.autofill.oreo.BrowserAutofillSupportLevel
@@ -89,7 +92,7 @@ class UserPreference : AppCompatActivity() {
val gitConfigPreference = findPreference<Preference>(PreferenceKeys.GIT_CONFIG)
val sshKeyPreference = findPreference<Preference>(PreferenceKeys.SSH_KEY)
val sshKeygenPreference = findPreference<Preference>(PreferenceKeys.SSH_KEYGEN)
- viewSshKeyPreference = findPreference<Preference>(PreferenceKeys.SSH_SEE_KEY)
+ viewSshKeyPreference = findPreference(PreferenceKeys.SSH_SEE_KEY)
clearSavedPassPreference = findPreference(PreferenceKeys.CLEAR_SAVED_PASS)
val deleteRepoPreference = findPreference<Preference>(PreferenceKeys.GIT_DELETE_REPO)
val externalGitRepositoryPreference = findPreference<Preference>(PreferenceKeys.GIT_EXTERNAL)
@@ -203,10 +206,10 @@ class UserPreference : AppCompatActivity() {
.setMessage(resources.getString(R.string.dialog_delete_msg, repoDir))
.setCancelable(false)
.setPositiveButton(R.string.dialog_delete) { dialogInterface, _ ->
- try {
+ runCatching {
PasswordRepository.getRepositoryDirectory().deleteRecursively()
PasswordRepository.closeRepository()
- } catch (ignored: Exception) {
+ }.onFailure {
// TODO Handle the different cases of exceptions
}
@@ -263,13 +266,11 @@ class UserPreference : AppCompatActivity() {
}
showTimePreference?.onPreferenceChangeListener = ChangeListener { _, newValue: Any? ->
- try {
+ runCatching {
val isEnabled = newValue.toString().toInt() != 0
clearClipboard20xPreference?.isVisible = isEnabled
true
- } catch (e: NumberFormatException) {
- false
- }
+ }.getOr(false)
}
showTimePreference?.summaryProvider = Preference.SummaryProvider<Preference> {
@@ -541,7 +542,7 @@ class UserPreference : AppCompatActivity() {
private fun importSshKey() {
registerForActivityResult(OpenDocument()) { uri: Uri? ->
if (uri == null) return@registerForActivityResult
- try {
+ runCatching {
SshKey.import(uri)
Toast.makeText(
@@ -551,7 +552,7 @@ class UserPreference : AppCompatActivity() {
).show()
setResult(RESULT_OK)
finish()
- } catch (e: Exception) {
+ }.onFailure { e ->
MaterialAlertDialogBuilder(this)
.setTitle(resources.getString(R.string.ssh_key_error_dialog_title))
.setMessage(e.message)