UserPreference: use runCatching to replace exception handling

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-09-05 05:14:50 +05:30
parent 9bc1d59dd2
commit 104c6550f6
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80

View file

@ -37,6 +37,9 @@ import androidx.preference.SwitchPreferenceCompat
import com.github.ajalt.timberkt.Timber.tag import com.github.ajalt.timberkt.Timber.tag
import com.github.ajalt.timberkt.d import com.github.ajalt.timberkt.d
import com.github.ajalt.timberkt.w 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.google.android.material.dialog.MaterialAlertDialogBuilder
import com.zeapo.pwdstore.autofill.AutofillPreferenceActivity import com.zeapo.pwdstore.autofill.AutofillPreferenceActivity
import com.zeapo.pwdstore.autofill.oreo.BrowserAutofillSupportLevel import com.zeapo.pwdstore.autofill.oreo.BrowserAutofillSupportLevel
@ -89,7 +92,7 @@ class UserPreference : AppCompatActivity() {
val gitConfigPreference = findPreference<Preference>(PreferenceKeys.GIT_CONFIG) val gitConfigPreference = findPreference<Preference>(PreferenceKeys.GIT_CONFIG)
val sshKeyPreference = findPreference<Preference>(PreferenceKeys.SSH_KEY) val sshKeyPreference = findPreference<Preference>(PreferenceKeys.SSH_KEY)
val sshKeygenPreference = findPreference<Preference>(PreferenceKeys.SSH_KEYGEN) 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) clearSavedPassPreference = findPreference(PreferenceKeys.CLEAR_SAVED_PASS)
val deleteRepoPreference = findPreference<Preference>(PreferenceKeys.GIT_DELETE_REPO) val deleteRepoPreference = findPreference<Preference>(PreferenceKeys.GIT_DELETE_REPO)
val externalGitRepositoryPreference = findPreference<Preference>(PreferenceKeys.GIT_EXTERNAL) val externalGitRepositoryPreference = findPreference<Preference>(PreferenceKeys.GIT_EXTERNAL)
@ -203,10 +206,10 @@ class UserPreference : AppCompatActivity() {
.setMessage(resources.getString(R.string.dialog_delete_msg, repoDir)) .setMessage(resources.getString(R.string.dialog_delete_msg, repoDir))
.setCancelable(false) .setCancelable(false)
.setPositiveButton(R.string.dialog_delete) { dialogInterface, _ -> .setPositiveButton(R.string.dialog_delete) { dialogInterface, _ ->
try { runCatching {
PasswordRepository.getRepositoryDirectory().deleteRecursively() PasswordRepository.getRepositoryDirectory().deleteRecursively()
PasswordRepository.closeRepository() PasswordRepository.closeRepository()
} catch (ignored: Exception) { }.onFailure {
// TODO Handle the different cases of exceptions // TODO Handle the different cases of exceptions
} }
@ -263,13 +266,11 @@ class UserPreference : AppCompatActivity() {
} }
showTimePreference?.onPreferenceChangeListener = ChangeListener { _, newValue: Any? -> showTimePreference?.onPreferenceChangeListener = ChangeListener { _, newValue: Any? ->
try { runCatching {
val isEnabled = newValue.toString().toInt() != 0 val isEnabled = newValue.toString().toInt() != 0
clearClipboard20xPreference?.isVisible = isEnabled clearClipboard20xPreference?.isVisible = isEnabled
true true
} catch (e: NumberFormatException) { }.getOr(false)
false
}
} }
showTimePreference?.summaryProvider = Preference.SummaryProvider<Preference> { showTimePreference?.summaryProvider = Preference.SummaryProvider<Preference> {
@ -541,7 +542,7 @@ class UserPreference : AppCompatActivity() {
private fun importSshKey() { private fun importSshKey() {
registerForActivityResult(OpenDocument()) { uri: Uri? -> registerForActivityResult(OpenDocument()) { uri: Uri? ->
if (uri == null) return@registerForActivityResult if (uri == null) return@registerForActivityResult
try { runCatching {
SshKey.import(uri) SshKey.import(uri)
Toast.makeText( Toast.makeText(
@ -551,7 +552,7 @@ class UserPreference : AppCompatActivity() {
).show() ).show()
setResult(RESULT_OK) setResult(RESULT_OK)
finish() finish()
} catch (e: Exception) { }.onFailure { e ->
MaterialAlertDialogBuilder(this) MaterialAlertDialogBuilder(this)
.setTitle(resources.getString(R.string.ssh_key_error_dialog_title)) .setTitle(resources.getString(R.string.ssh_key_error_dialog_title))
.setMessage(e.message) .setMessage(e.message)