diff --git a/LeadKitAdditions/Sources/Controllers/PassCode/Model/PassCodeError.swift b/LeadKitAdditions/Sources/Controllers/PassCode/Model/PassCodeError.swift index 64b3de4..ca7ebed 100644 --- a/LeadKitAdditions/Sources/Controllers/PassCode/Model/PassCodeError.swift +++ b/LeadKitAdditions/Sources/Controllers/PassCode/Model/PassCodeError.swift @@ -21,8 +21,22 @@ // /// Describes error, which may occur during pass code entering +/// - codesNotMatch: Different codes +/// - wrongCode: Value is remaining attemps +/// - tooManyAttempts: Attempts limit reached public enum PassCodeError: Error { case codesNotMatch - case wrongCode + case wrongCode(Int) case tooManyAttempts } + +public extension PassCodeError { + var isTooManyAttempts: Bool { + switch self { + case .tooManyAttempts: + return true + default: + return false + } + } +} diff --git a/LeadKitAdditions/Sources/Controllers/PassCode/View/BasePassCodeViewController.swift b/LeadKitAdditions/Sources/Controllers/PassCode/View/BasePassCodeViewController.swift index 8049467..6fee439 100644 --- a/LeadKitAdditions/Sources/Controllers/PassCode/View/BasePassCodeViewController.swift +++ b/LeadKitAdditions/Sources/Controllers/PassCode/View/BasePassCodeViewController.swift @@ -226,8 +226,8 @@ open class BasePassCodeViewController: UIViewController, ConfigurableController if validationResult.isValid { self?.hideError() - } else if let pasCodeError = validationResult.error { - self?.showError(for: pasCodeError) + } else if let passCodeError = validationResult.error { + self?.showError(for: passCodeError) } }) .disposed(by: disposeBag) diff --git a/LeadKitAdditions/Sources/Controllers/PassCode/ViewModel/BasePassCodeViewModel.swift b/LeadKitAdditions/Sources/Controllers/PassCode/ViewModel/BasePassCodeViewModel.swift index eedffb5..b64955d 100644 --- a/LeadKitAdditions/Sources/Controllers/PassCode/ViewModel/BasePassCodeViewModel.swift +++ b/LeadKitAdditions/Sources/Controllers/PassCode/ViewModel/BasePassCodeViewModel.swift @@ -77,29 +77,8 @@ open class BasePassCodeViewModel: BaseViewModel { }) .disposed(by: disposeBag) - validationResultHolder.asDriver() - .drive(onNext: { [weak self] validationResult in - guard let sSelf = self else { - return - } - - if sSelf.passCodeHolder.type == .change { - if validationResult?.isValid ?? false, - sSelf.passCodeHolder.enterStep == .repeatEnter, - let passCode = validationResult?.passCode { - - sSelf.authSucceed(.passCode(passCode)) - } else { - sSelf.passCodeControllerStateHolder.value = sSelf.passCodeHolder.enterStep - } - } else { - if validationResult?.isValid ?? false, let passCode = validationResult?.passCode { - sSelf.authSucceed(.passCode(passCode)) - } else { - sSelf.passCodeControllerStateHolder.value = sSelf.passCodeHolder.enterStep - } - } - }) + validationResultHolder.asObservable() + .bind(to: validationResultBinder) .disposed(by: disposeBag) } @@ -163,6 +142,29 @@ open class BasePassCodeViewModel: BaseViewModel { } +private extension BasePassCodeViewModel { + var validationResultBinder: Binder { + return Binder(self) { model, validationResult in + let isValid = validationResult?.isValid ?? false + let passCode = validationResult?.passCode + + if model.passCodeHolder.type == .change { + if isValid, model.passCodeHolder.enterStep == .repeatEnter, let passCode = passCode { + model.authSucceed(.passCode(passCode)) + } else { + model.passCodeControllerStateHolder.value = model.passCodeHolder.enterStep + } + } else { + if isValid, let passCode = passCode { + model.authSucceed(.passCode(passCode)) + } else { + model.passCodeControllerStateHolder.value = model.passCodeHolder.enterStep + } + } + } + } +} + extension BasePassCodeViewModel { private func set(passCode: String) { @@ -177,7 +179,7 @@ extension BasePassCodeViewModel { private var shouldUpdateControllerState: Bool { return !passCodeHolder.shouldValidate || !(validationResultHolder.value?.isValid ?? true) || - validationResultHolder.value?.error == .tooManyAttempts + validationResultHolder.value?.error?.isTooManyAttempts ?? false } private func validateIfNeeded() { @@ -191,7 +193,8 @@ extension BasePassCodeViewModel { attemptsNumber += 1 if let passCode = validationResult.passCode, !isEnteredPassCodeValid(passCode) { - validationResult = .invalid(.wrongCode) + assert(passCodeConfiguration.maxAttemptsNumber > attemptsNumber) + validationResult = .invalid(.wrongCode(passCodeConfiguration.maxAttemptsNumber - attemptsNumber)) } if (!validationResult.isValid && attemptsNumber == Int(passCodeConfiguration.maxAttemptsNumber)) ||