From 90c9ce1b91f025b499888665dc4f7ef10e656881 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sat, 5 Sep 2020 04:36:51 +0530 Subject: Form: use runCatching to replace exception handling Signed-off-by: Harsh Shandilya --- .../zeapo/pwdstore/autofill/oreo/AutofillMatcher.kt | 11 +++++++---- .../main/java/com/zeapo/pwdstore/autofill/oreo/Form.kt | 18 ++++++++++-------- 2 files changed, 17 insertions(+), 12 deletions(-) (limited to 'app') diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/oreo/AutofillMatcher.kt b/app/src/main/java/com/zeapo/pwdstore/autofill/oreo/AutofillMatcher.kt index b22833a5..1b0ff35d 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/oreo/AutofillMatcher.kt +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/oreo/AutofillMatcher.kt @@ -11,6 +11,9 @@ import androidx.core.content.edit import com.github.ajalt.timberkt.Timber.e import com.github.ajalt.timberkt.d import com.github.ajalt.timberkt.w +import com.github.michaelbull.result.Err +import com.github.michaelbull.result.Ok +import com.github.michaelbull.result.Result import com.zeapo.pwdstore.R import java.io.File @@ -94,18 +97,18 @@ class AutofillMatcher { * first time the user associated an entry with it, an [AutofillPublisherChangedException] * will be thrown. */ - fun getMatchesFor(context: Context, formOrigin: FormOrigin): List { + fun getMatchesFor(context: Context, formOrigin: FormOrigin): Result, AutofillPublisherChangedException> { if (hasFormOriginHashChanged(context, formOrigin)) { - throw AutofillPublisherChangedException(formOrigin) + return Err(AutofillPublisherChangedException(formOrigin)) } val matchPreferences = context.matchPreferences(formOrigin) val matchedFiles = matchPreferences.getStringSet(matchesKey(formOrigin), emptySet())!!.map { File(it) } - return matchedFiles.filter { it.exists() }.also { validFiles -> + return Ok(matchedFiles.filter { it.exists() }.also { validFiles -> matchPreferences.edit { putStringSet(matchesKey(formOrigin), validFiles.map { it.absolutePath }.toSet()) } - } + }) } fun clearMatchesFor(context: Context, formOrigin: FormOrigin) { diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/oreo/Form.kt b/app/src/main/java/com/zeapo/pwdstore/autofill/oreo/Form.kt index 85164f8f..5d24c882 100644 --- a/app/src/main/java/com/zeapo/pwdstore/autofill/oreo/Form.kt +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/oreo/Form.kt @@ -21,6 +21,7 @@ import androidx.annotation.RequiresApi import androidx.core.os.bundleOf import com.github.ajalt.timberkt.d import com.github.ajalt.timberkt.e +import com.github.michaelbull.result.fold import com.zeapo.pwdstore.autofill.oreo.ui.AutofillDecryptActivity import com.zeapo.pwdstore.autofill.oreo.ui.AutofillFilterView import com.zeapo.pwdstore.autofill.oreo.ui.AutofillPublisherChangedActivity @@ -369,13 +370,14 @@ class FillableForm private constructor( * Creates and returns a suitable [FillResponse] to the Autofill framework. */ fun fillCredentials(context: Context, callback: FillCallback) { - val matchedFiles = try { - AutofillMatcher.getMatchesFor(context, formOrigin) - } catch (publisherChangedException: AutofillPublisherChangedException) { - e(publisherChangedException) - callback.onSuccess(makePublisherChangedResponse(context, publisherChangedException)) - return - } - callback.onSuccess(makeFillResponse(context, matchedFiles)) + AutofillMatcher.getMatchesFor(context, formOrigin).fold( + success = { matchedFiles -> + callback.onSuccess(makeFillResponse(context, matchedFiles)) + }, + failure = { e -> + e(e) + callback.onSuccess(makePublisherChangedResponse(context, e)) + } + ) } } -- cgit v1.2.3