pull request fix

This commit is contained in:
Grigory 2017-06-20 17:32:18 +03:00
parent 36200eaeb9
commit c79c740d79
6 changed files with 36 additions and 48 deletions

View File

@ -2,7 +2,7 @@ import RxSwift
import RxCocoa
import UIKit
typealias UIItemSettingsBlock<UIItem> = (UIItem) -> Void where UIItem: UIView
typealias ItemSettingsBlock<UIItem> = (UIItem) -> Void where UIItem: UIView
protocol CellFieldJumpingProtocol: FormCellViewModelProtocol {

View File

@ -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
}
}

View File

@ -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<Bool> {
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
}
}

View File

@ -35,7 +35,7 @@ class ValidationItem {
private let disposeBag = DisposeBag()
private let validationStateHolder: Variable<ValidationItemState> = Variable(.initial)
private let validationStateHolder = Variable<ValidationItemState>(.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<String?>(nil)
init(textObservable: Observable<String?>, rules: [Rule]) {
private(set) var rules: [Rule] = []
init(rules: [Rule]) {
self.rules = rules
bindValue(with: textObservable)
bindText()
}
private func bindValue(with textObservable: Observable<String?>) {
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

View File

@ -28,7 +28,7 @@ class ValidationService {
private(set) var validationItems: [ValidationItem] = []
private let stateHolder: Variable<ValidationServiceState> = Variable(.initial)
private let stateHolder = Variable<ValidationServiceState>(.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

View File

@ -6,7 +6,7 @@ class CellTextFieldViewModel: CellFieldJumpingProtocol {
let text: Variable<String?>
let placeholder: String
let textFieldSettingsBlock: UIItemSettingsBlock<UITextField>?
let textFieldSettingsBlock: ItemSettingsBlock<UITextField>?
// MARK: - CellFieldJumpingProtocol
@ -21,7 +21,7 @@ class CellTextFieldViewModel: CellFieldJumpingProtocol {
var isActive: Bool = true
init(initialText: String = "", placeholder: String = "", textFieldSettingsBlock: UIItemSettingsBlock<UITextField>? = nil) {
init(initialText: String = "", placeholder: String = "", textFieldSettingsBlock: ItemSettingsBlock<UITextField>? = nil) {
text = Variable(initialText)
self.placeholder = placeholder
self.textFieldSettingsBlock = textFieldSettingsBlock