This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #9838: Introduce CreditCardValidationDelegate and implement onC…
…reditCardSave in GeckoCreditCardsAddressesStorageDelegate - Introduces `CreditCardValidationDelegate` and a default implementation in `DefaultCreditCardValidationDelegate` - Adds `wipeLocalAddresses()` and `wipeLocalCreditCards()` to `CreditCardsAddressesStorage` to clear out local state in between tests - Implements `onCreditCardSave` in `GeckoCreditCardsAddressesStorageDelegate`
- Loading branch information
1 parent
5b3677a
commit 719b601
Showing
8 changed files
with
426 additions
and
18 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
42 changes: 42 additions & 0 deletions
42
...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,42 @@ | ||
/* 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.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
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> | ||
) : CreditCardValidationDelegate { | ||
|
||
private val coroutineContext by lazy { Dispatchers.IO } | ||
|
||
override suspend fun validate(creditCard: CreditCard): Result = | ||
withContext(coroutineContext) { | ||
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 == creditCard.guid || it.cardNumber == creditCard.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
Oops, something went wrong.