From c79c740d795a458df748a9ec79ee48ddfe8ddbfd Mon Sep 17 00:00:00 2001 From: Grigory Date: Tue, 20 Jun 2017 17:32:18 +0300 Subject: [PATCH] pull request fix --- .../Protocols/CellFieldJumpingProtocol.swift | 2 +- .../Services/CellFieldsJumpingService.swift | 42 ++++++------------- .../Sources/Services/MaskFieldTextProxy.swift | 12 ++++-- .../ValidationService/ValidationItem.swift | 20 ++++----- .../ValidationService/ValidationService.swift | 4 +- .../CellTextFieldViewModel.swift | 4 +- 6 files changed, 36 insertions(+), 48 deletions(-) diff --git a/LeadKitAdditions/Sources/Protocols/CellFieldJumpingProtocol.swift b/LeadKitAdditions/Sources/Protocols/CellFieldJumpingProtocol.swift index 91e375f..39029f7 100644 --- a/LeadKitAdditions/Sources/Protocols/CellFieldJumpingProtocol.swift +++ b/LeadKitAdditions/Sources/Protocols/CellFieldJumpingProtocol.swift @@ -2,7 +2,7 @@ import RxSwift import RxCocoa import UIKit -typealias UIItemSettingsBlock = (UIItem) -> Void where UIItem: UIView +typealias ItemSettingsBlock = (UIItem) -> Void where UIItem: UIView protocol CellFieldJumpingProtocol: FormCellViewModelProtocol { diff --git a/LeadKitAdditions/Sources/Services/CellFieldsJumpingService.swift b/LeadKitAdditions/Sources/Services/CellFieldsJumpingService.swift index 31a8cac..692ff28 100644 --- a/LeadKitAdditions/Sources/Services/CellFieldsJumpingService.swift +++ b/LeadKitAdditions/Sources/Services/CellFieldsJumpingService.swift @@ -1,22 +1,12 @@ import RxSwift import UIKit -enum CellFieldsToolBarType { - case none - case `default` -} - struct CellFieldsJumpingServiceConfig { - var toolBarType: CellFieldsToolBarType = .default var toolBarNeedArrows = true init() {} - init(toolBarType: CellFieldsToolBarType) { - self.toolBarType = toolBarType - } - static var `default`: CellFieldsJumpingServiceConfig { return CellFieldsJumpingServiceConfig() } @@ -77,12 +67,8 @@ class CellFieldsJumpingService { field.returnButtonType = .next field.shouldGoForward.asObservable() - .subscribe(onNext: { - if let nextActive = cellFields.nextActive(from: offset) { - nextActive.shouldBecomeFirstResponder.onNext() - } else { - self.didDone.onNext() - } + .subscribe(onNext: { [weak self] in + self?.shouldGoForwardAction(from: offset) }) .addDisposableTo(disposeBag) } @@ -99,11 +85,7 @@ class CellFieldsJumpingService { toolBar.shouldGoForward.asObservable() .subscribe(onNext: { [weak self] in - if let nextActive = self?.cellFields.nextActive(from: index) { - nextActive.shouldBecomeFirstResponder.onNext() - } else { - self?.didDone.onNext() - } + self?.shouldGoForwardAction(from: index) }) .addDisposableTo(disposeBag) @@ -124,6 +106,14 @@ class CellFieldsJumpingService { return toolBar } + private func shouldGoForwardAction(from index: Int) { + if let nextActive = cellFields.nextActive(from: index) { + nextActive.shouldBecomeFirstResponder.onNext() + } else { + didDone.onNext() + } + } + } extension Array where Element == CellFieldJumpingProtocol { @@ -137,18 +127,12 @@ extension Array where Element == CellFieldJumpingProtocol { } func nextActive(from index: Int) -> CellFieldJumpingProtocol? { - for (currentIndex, item) in enumerated() where currentIndex > index && item.isActive { - return item - } - return nil + return enumerated().first { $0 > index && $1.isActive }?.element } func previousActive(from index: Int) -> CellFieldJumpingProtocol? { let reversedIndex = count - index - 1 - for (currentIndex, item) in reversed().enumerated() where currentIndex > reversedIndex && item.isActive { - return item - } - return nil + return reversed().enumerated().first { $0 > reversedIndex && $1.isActive }?.element } } diff --git a/LeadKitAdditions/Sources/Services/MaskFieldTextProxy.swift b/LeadKitAdditions/Sources/Services/MaskFieldTextProxy.swift index 1a67202..0ef05cf 100644 --- a/LeadKitAdditions/Sources/Services/MaskFieldTextProxy.swift +++ b/LeadKitAdditions/Sources/Services/MaskFieldTextProxy.swift @@ -7,7 +7,13 @@ class MaskFieldTextProxy: NSObject { private var disposeBag = DisposeBag() let text = Variable("") - let isComplete = Variable(false) + fileprivate let isCompleteHolder = Variable(false) + var isComplete: Bool { + return isCompleteHolder.value + } + var isCompleteObservable: Observable { + return isCompleteHolder.asObservable() + } private(set) var field: UITextField? @@ -36,7 +42,7 @@ class MaskFieldTextProxy: NSObject { return } - self?.maskedProxy.put(text: value, into: textField) + self?.maskedDelegate.put(text: value, into: textField) }) .addDisposableTo(disposeBag) } @@ -47,7 +53,7 @@ extension MaskFieldTextProxy: MaskedTextFieldDelegateListener { func textField(_ textField: UITextField, didFillMandatoryCharacters complete: Bool, didExtractValue value: String) { text.value = value - isComplete.value = complete + isCompleteHolder.value = complete } } diff --git a/LeadKitAdditions/Sources/Services/ValidationService/ValidationItem.swift b/LeadKitAdditions/Sources/Services/ValidationService/ValidationItem.swift index 1f3574f..902ac93 100644 --- a/LeadKitAdditions/Sources/Services/ValidationService/ValidationItem.swift +++ b/LeadKitAdditions/Sources/Services/ValidationService/ValidationItem.swift @@ -35,7 +35,7 @@ class ValidationItem { private let disposeBag = DisposeBag() - private let validationStateHolder: Variable = Variable(.initial) + private let validationStateHolder = Variable(.initial) var validationState: ValidationItemState { return validationStateHolder.value } @@ -43,19 +43,17 @@ class ValidationItem { return validationStateHolder.asObservable() } - private(set) var rules: [Rule] = [] - private var text: String? + let text = Variable(nil) - init(textObservable: Observable, rules: [Rule]) { + private(set) var rules: [Rule] = [] + + init(rules: [Rule]) { self.rules = rules - bindValue(with: textObservable) + bindText() } - private func bindValue(with textObservable: Observable) { - textObservable - .do(onNext: { [weak self] value in - self?.text = value - }) + private func bindText() { + text.asObservable() .filter { [weak self] _ in !(self?.validationState.isInitial ?? true)} .subscribe(onNext: { [weak self] value in self?.validate(text: value) @@ -65,7 +63,7 @@ class ValidationItem { @discardableResult func manualValidate() -> Bool { - return validate(text: text, isManual: true) + return validate(text: text.value, isManual: true) } @discardableResult diff --git a/LeadKitAdditions/Sources/Services/ValidationService/ValidationService.swift b/LeadKitAdditions/Sources/Services/ValidationService/ValidationService.swift index 5e2366a..37e5fbe 100644 --- a/LeadKitAdditions/Sources/Services/ValidationService/ValidationService.swift +++ b/LeadKitAdditions/Sources/Services/ValidationService/ValidationService.swift @@ -28,7 +28,7 @@ class ValidationService { private(set) var validationItems: [ValidationItem] = [] - private let stateHolder: Variable = Variable(.initial) + private let stateHolder = Variable(.initial) var state: ValidationServiceState { return stateHolder.value } @@ -69,7 +69,7 @@ class ValidationService { @discardableResult func validate() -> Bool { validationStateReactType = .all - let isValid = validationItems.map { $0.manualValidate()}.reduce(true) { $0 && $1 } + let isValid = validationItems.map { $0.manualValidate() }.reduce(true) { $0 && $1 } validationStateReactType = .each return isValid diff --git a/LeadKitAdditions/Sources/Views/CellTextField/CellTextFieldViewModel.swift b/LeadKitAdditions/Sources/Views/CellTextField/CellTextFieldViewModel.swift index 5f819db..c154ede 100644 --- a/LeadKitAdditions/Sources/Views/CellTextField/CellTextFieldViewModel.swift +++ b/LeadKitAdditions/Sources/Views/CellTextField/CellTextFieldViewModel.swift @@ -6,7 +6,7 @@ class CellTextFieldViewModel: CellFieldJumpingProtocol { let text: Variable let placeholder: String - let textFieldSettingsBlock: UIItemSettingsBlock? + let textFieldSettingsBlock: ItemSettingsBlock? // MARK: - CellFieldJumpingProtocol @@ -21,7 +21,7 @@ class CellTextFieldViewModel: CellFieldJumpingProtocol { var isActive: Bool = true - init(initialText: String = "", placeholder: String = "", textFieldSettingsBlock: UIItemSettingsBlock? = nil) { + init(initialText: String = "", placeholder: String = "", textFieldSettingsBlock: ItemSettingsBlock? = nil) { text = Variable(initialText) self.placeholder = placeholder self.textFieldSettingsBlock = textFieldSettingsBlock