update access modifiers of ValidationService
This commit is contained in:
parent
9208d41301
commit
5c6423d901
|
|
@ -1,5 +1,8 @@
|
|||
# Changelog
|
||||
|
||||
## 0.1.2
|
||||
- **Update**: Access modifiers of `ValidationService`
|
||||
|
||||
## 0.1.1
|
||||
|
||||
- **Add**: `acceptableStatusCodes` property in `DefaultNetworkService`.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
Pod::Spec.new do |s|
|
||||
s.name = "LeadKitAdditions"
|
||||
s.version = "0.1.1"
|
||||
s.version = "0.1.2"
|
||||
s.summary = "iOS framework with a bunch of tools for rapid development"
|
||||
s.homepage = "https://github.com/TouchInstinct/LeadKitAdditions"
|
||||
s.license = "Apache License, Version 2.0"
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
import SwiftValidator
|
||||
|
||||
struct ValidationError: Error {
|
||||
public struct ValidationError: Error {
|
||||
|
||||
let failedRule: Rule
|
||||
let errorMessage: String?
|
||||
let errorHint: String?
|
||||
public let failedRule: Rule
|
||||
public let errorMessage: String?
|
||||
public let errorHint: String?
|
||||
|
||||
init(failedRule: Rule, errorMessage: String?, errorHint: String? = nil) {
|
||||
public init(failedRule: Rule, errorMessage: String?, errorHint: String? = nil) {
|
||||
self.failedRule = failedRule
|
||||
self.errorMessage = errorMessage
|
||||
self.errorHint = errorHint
|
||||
|
|
|
|||
|
|
@ -2,14 +2,14 @@ import SwiftValidator
|
|||
import RxSwift
|
||||
import RxCocoa
|
||||
|
||||
enum ValidationItemState {
|
||||
public enum ValidationItemState {
|
||||
case initial
|
||||
case correction(ValidationError)
|
||||
case error(ValidationError)
|
||||
case valid
|
||||
}
|
||||
|
||||
extension ValidationItemState {
|
||||
public extension ValidationItemState {
|
||||
|
||||
var isInitial: Bool {
|
||||
switch self {
|
||||
|
|
@ -31,15 +31,15 @@ extension ValidationItemState {
|
|||
|
||||
}
|
||||
|
||||
class ValidationItem {
|
||||
public final class ValidationItem {
|
||||
|
||||
private let disposeBag = DisposeBag()
|
||||
|
||||
private let validationStateHolder = Variable<ValidationItemState>(.initial)
|
||||
var validationState: ValidationItemState {
|
||||
public var validationState: ValidationItemState {
|
||||
return validationStateHolder.value
|
||||
}
|
||||
var validationStateObservable: Observable<ValidationItemState> {
|
||||
public var validationStateObservable: Observable<ValidationItemState> {
|
||||
return validationStateHolder.asObservable()
|
||||
}
|
||||
|
||||
|
|
@ -47,27 +47,33 @@ class ValidationItem {
|
|||
|
||||
private(set) var rules: [Rule] = []
|
||||
|
||||
init(rules: [Rule]) {
|
||||
public init(rules: [Rule], textDriver: Driver<String?>) {
|
||||
self.rules = rules
|
||||
bindText()
|
||||
|
||||
bindText(textDriver: textDriver)
|
||||
}
|
||||
|
||||
private func bindText() {
|
||||
text.asObservable()
|
||||
.filter { [weak self] _ in !(self?.validationState.isInitial ?? true)}
|
||||
.subscribe(onNext: { [weak self] value in
|
||||
self?.validate(text: value)
|
||||
private func bindText(textDriver: Driver<String?>) {
|
||||
textDriver
|
||||
.drive(text)
|
||||
.disposed(by: disposeBag)
|
||||
|
||||
textDriver.asObservable().withLatestFrom(validationStateHolder.asObservable()) { (text: $0, validationState: $1) }
|
||||
.filter { !$0.validationState.isInitial }
|
||||
.map { $0.text }
|
||||
.subscribe(onNext: { [weak self] text in
|
||||
self?.validate(text: text)
|
||||
})
|
||||
.disposed(by: disposeBag)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func manualValidate() -> Bool {
|
||||
return validate(text: text.value, isManual: true)
|
||||
public func manualValidate() -> Bool {
|
||||
validate(text: text.value, isManual: true)
|
||||
|
||||
return validationStateHolder.value.isValid
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
private func validate(text: String?, isManual: Bool = false) -> Bool {
|
||||
private func validate(text: String?, isManual: Bool = false) {
|
||||
let error = rules.filter {
|
||||
return !$0.validate(text ?? "")
|
||||
}
|
||||
|
|
@ -89,8 +95,6 @@ class ValidationItem {
|
|||
} else {
|
||||
validationStateHolder.value = .valid
|
||||
}
|
||||
|
||||
return validationStateHolder.value.isValid
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,13 +8,13 @@ private enum ValidationServiceStateReactType {
|
|||
case each
|
||||
}
|
||||
|
||||
enum ValidationServiceState {
|
||||
public enum ValidationServiceState {
|
||||
case initial
|
||||
case valid
|
||||
case invalid
|
||||
}
|
||||
|
||||
extension ValidationServiceState {
|
||||
public extension ValidationServiceState {
|
||||
|
||||
var isValid: Bool {
|
||||
return self == .valid
|
||||
|
|
@ -22,41 +22,45 @@ extension ValidationServiceState {
|
|||
|
||||
}
|
||||
|
||||
class ValidationService {
|
||||
public final class ValidationService {
|
||||
|
||||
private var disposeBag = DisposeBag()
|
||||
|
||||
private(set) var validationItems: [ValidationItem] = []
|
||||
|
||||
private let stateHolder = Variable<ValidationServiceState>(.initial)
|
||||
var state: ValidationServiceState {
|
||||
public var state: ValidationServiceState {
|
||||
return stateHolder.value
|
||||
}
|
||||
var stateObservable: Observable<ValidationServiceState> {
|
||||
public var stateObservable: Observable<ValidationServiceState> {
|
||||
return stateHolder.asObservable()
|
||||
}
|
||||
|
||||
private var validationStateReactType: ValidationServiceStateReactType = .none
|
||||
|
||||
func register(item: ValidationItem) {
|
||||
public init() {
|
||||
// just to be accessible
|
||||
}
|
||||
|
||||
public func register(item: ValidationItem) {
|
||||
register(items: [item])
|
||||
}
|
||||
|
||||
func register(items: [ValidationItem]) {
|
||||
public func register(items: [ValidationItem]) {
|
||||
validationItems += items
|
||||
bindItems()
|
||||
}
|
||||
|
||||
func unregisterAll() {
|
||||
public func unregisterAll() {
|
||||
validationItems.removeAll()
|
||||
bindItems()
|
||||
}
|
||||
|
||||
func unregister(item: ValidationItem) {
|
||||
public func unregister(item: ValidationItem) {
|
||||
unregister(items: [item])
|
||||
}
|
||||
|
||||
func unregister(items: [ValidationItem]) {
|
||||
public func unregister(items: [ValidationItem]) {
|
||||
items.forEach { item in
|
||||
if let removeIndex = validationItems.index(where: { $0 === item }) {
|
||||
validationItems.remove(at: removeIndex)
|
||||
|
|
@ -66,8 +70,7 @@ class ValidationService {
|
|||
bindItems()
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func validate() -> Bool {
|
||||
public func validate() -> Bool {
|
||||
validationStateReactType = .all
|
||||
let isValid = validationItems.map { $0.manualValidate() }.reduce(true) { $0 && $1 }
|
||||
validationStateReactType = .each
|
||||
|
|
|
|||
Loading…
Reference in New Issue