diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2020-09-05 04:36:51 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2020-09-05 22:49:00 +0530 |
commit | 90c9ce1b91f025b499888665dc4f7ef10e656881 (patch) | |
tree | 6db14f878e120b65319c224696d94ebbaae138de /app/src/main | |
parent | 0bc60999c35ed0e3cfc15341f890b4af61b69c37 (diff) |
Form: use runCatching to replace exception handling
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main')
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/autofill/oreo/AutofillMatcher.kt | 11 | ||||
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/autofill/oreo/Form.kt | 18 |
2 files changed, 17 insertions, 12 deletions
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<File> { + fun getMatchesFor(context: Context, formOrigin: FormOrigin): Result<List<File>, 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)) + } + ) } } |