Created
July 19, 2018 15:01
-
-
Save CyxouD/f3e036b02c042926aee1d0f352698813 to your computer and use it in GitHub Desktop.
Organize between with View (view: AddEditCardContract.View) and Model (repository: DataSource). Performs logic which is not View-dependant and process data from Model before passing it to View
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
class AddEditCardPresenter(override val repository: DataSource, | |
val view: AddEditCardContract.View, | |
override val schedulerProvider: BaseSchedulerProvider, | |
override val mCompositeDisposable: CompositeDisposable = CompositeDisposable()) : AddEditCardContract.Presenter { | |
init { | |
view.setPresenter(this) | |
} | |
override fun subscribe() { | |
} | |
override fun getCreditCardLogo(creditCardNumber: String) { | |
if (creditCardNumber.contains("*")) return | |
val creditCardEnum = CreditCardEnum.getCreditCardByNumber(creditCardNumber) | |
if (creditCardEnum != CreditCardEnum.UNKNOWN) { | |
view.showCreditCardLogo(creditCardEnum) | |
} else { | |
view.showNoCreditCardLogo() | |
} | |
} | |
override fun validateCreditCardNumber(creditCardNumber: String) { | |
val creditCardEnum = CreditCardEnum.getCreditCardByNumber(creditCardNumber) | |
if (creditCardEnum != CreditCardEnum.UNKNOWN) { | |
view.showCreditCardNumberValidatedSuccessfully() | |
} else { | |
view.showCreditCardNumberFailedToValidate() | |
} | |
} | |
override fun validateCreditCardHolder(creditCardHolder: String) { | |
val validCardHolder = Pattern.compile("^((?:[A-Za-z]+ ?){1,3})$").matcher(creditCardHolder).matches() | |
if (validCardHolder) { | |
view.showCreditCardHolderValidatedSuccessfully() | |
} else { | |
view.showCreditCardHolderFailedToValidate() | |
} | |
} | |
override fun validateCreditCardExpiryDate(creditExpiryDate: String) { | |
//TODO also validate if year and month is not more than current date | |
val validExpiryDate = Pattern.compile("^(0[1-9]|1[0-2])/[0-9]{2}$").matcher(creditExpiryDate).matches() | |
if (validExpiryDate) { | |
view.showCreditCardExpiryDateValidatedSuccessfully() | |
} else { | |
view.showCreditCardExpiryDateFailedToValidate() | |
} | |
} | |
override fun validateCreditCardCVV(creditCVV: String) { | |
val validCVV = Pattern.compile("^[0-9]{3,4}$").matcher(creditCVV).matches() | |
if (validCVV) { | |
view.showCreditCardCvvValidatedSuccessfully() | |
} else { | |
view.showCreditCardCvvFailedToValidate() | |
} | |
} | |
override fun validateCreditCardTypeAndPriority(creditCardType: String, creditCardPriority: String) { | |
val creditCardTypeValid = creditCardType.isNotEmpty() | |
val creditCardPriorityValid = creditCardPriority.isNotEmpty() | |
if (!creditCardPriorityValid) { | |
view.showCreditCardPriorityIsEmpty() | |
} | |
if (!creditCardTypeValid) { | |
view.showCreditCardTypeIsEmpty() | |
} | |
if (creditCardPriorityValid && creditCardTypeValid) { | |
view.showCreditCardPriorityAndTypeValidatedSuccessfully() | |
} | |
} | |
override fun saveCreditCard(number: String, | |
holderName: String, | |
expiryDate: String, | |
cvv: String, | |
type: AddEditCardContract.CardType, | |
isAirplus: Boolean, | |
isPrimary: Boolean) { | |
val creditCard = CreditCard(number = number.filter { !it.isWhitespace() }, | |
holderName = holderName, | |
company = if (isAirplus) CreditCardEnum.AIRPLUS.naming else null, | |
cvc = cvv, | |
expiryDate = expiryDate.replace("/", "-"),//stripe format | |
type = when (type) { | |
PERSONAL -> "Personal" | |
BUSINESS -> "Business" | |
}, | |
isPrimary = isPrimary | |
) | |
repository.saveCreditCards(listOf(creditCard)) | |
.subscribeOn(schedulerProvider.io()) | |
.observeOn(schedulerProvider.ui()) | |
.subscribeWith(object : CompletableObserverWrapper<AddEditCardPresenter>(view) { | |
override fun handleError(e: Throwable): Boolean { | |
[email protected]() | |
return false | |
} | |
override fun onComplete() { | |
[email protected]() | |
} | |
}).let { mCompositeDisposable.add(it) } | |
} | |
override fun removeAll() { | |
Utils.removeAll(repository, view, schedulerProvider, mCompositeDisposable) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment