Form: use runCatching to replace exception handling
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
0bc60999c3
commit
90c9ce1b91
2 changed files with 17 additions and 12 deletions
|
@ -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) {
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue