Form: use runCatching to replace exception handling

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-09-05 04:36:51 +05:30
parent 0bc60999c3
commit 90c9ce1b91
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
2 changed files with 17 additions and 12 deletions

View file

@ -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) {

View file

@ -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
}
AutofillMatcher.getMatchesFor(context, formOrigin).fold(
success = { matchedFiles ->
callback.onSuccess(makeFillResponse(context, matchedFiles))
},
failure = { e ->
e(e)
callback.onSuccess(makePublisherChangedResponse(context, e))
}
)
}
}