forked from mozilla-mobile/android-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue mozilla-mobile#9838: Introduce CreditCardValidationDelegate and…
… implement onCreditCardSave in GeckoCreditCardsAddressesStorageDelegate - Introduces `CreditCardValidationDelegate` and a default implementation in `DefaultCreditCardValidationDelegate` - Adds a `wipeLocal()` to `CreditCardsAddressesStorage` to clear out local state in between tests - Implements `onCreditCardSave` in `GeckoCreditCardsAddressesStorageDelegate`
- Loading branch information
1 parent
5aad028
commit 78b9e18
Showing
8 changed files
with
400 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...main/java/mozilla/components/service/sync/autofill/DefaultCreditCardValidationDelegate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.service.sync.autofill | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Deferred | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.async | ||
import mozilla.components.concept.storage.CreditCard | ||
import mozilla.components.concept.storage.CreditCardValidationDelegate | ||
import mozilla.components.concept.storage.CreditCardValidationDelegate.Result | ||
import mozilla.components.concept.storage.CreditCardsAddressesStorage | ||
|
||
/** | ||
* A delegate that will check against the [CreditCardsAddressesStorage] to determine if a given | ||
* [CreditCard] can be persisted and returns information about why it can or cannot. | ||
*/ | ||
class DefaultCreditCardValidationDelegate( | ||
private val storage: Lazy<CreditCardsAddressesStorage>, | ||
private val scope: CoroutineScope = CoroutineScope(Dispatchers.IO) | ||
) : CreditCardValidationDelegate { | ||
|
||
override fun validate(newCreditCard: CreditCard): Deferred<Result> { | ||
return scope.async { | ||
val creditCards = storage.value.getAllCreditCards() | ||
|
||
val foundCreditCard = if (creditCards.isEmpty()) { | ||
// No credit cards exist in the storage, create a new credit card to the storage. | ||
null | ||
} else { | ||
// Attempt to find a credit card with a matching guid or card number with the | ||
// new credit card. If an existing credit card is found, update it with the new | ||
// credit card entry. Otherwise, create a new credit card entry in the storage. | ||
creditCards.find { it.guid == newCreditCard.guid || it.cardNumber == newCreditCard.cardNumber } | ||
} | ||
|
||
if (foundCreditCard == null) Result.CanBeCreated else Result.CanBeUpdated( | ||
foundCreditCard | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.