diff --git a/src/Aux.swift b/src/Aux.swift new file mode 100644 index 0000000..96c19bf --- /dev/null +++ b/src/Aux.swift @@ -0,0 +1,8 @@ + +/// Пропускаем лишь значения от UI +/// +/// - Returns: Значение без префиксов "a:"/"u:" +static func onlyAcceptUIText(_ s: String) -> String? { + guard s.hasPrefix("u:") else { return nil } + return String(s.dropFirst(2)) +} diff --git a/src/TextFieldValueOwner.swift b/src/TextFieldValueOwner.swift index 2e245cf..fc80728 100644 --- a/src/TextFieldValueOwner.swift +++ b/src/TextFieldValueOwner.swift @@ -1,17 +1,17 @@ import Foundation class TextFieldValueOwner: Formatter { - override func string(for obj: Any?) -> String? { - guard let str = obj as? String else { return nil } - return String(str.dropFirst(2)) - } + override func string(for obj: Any?) -> String? { + guard let str = obj as? String else { return nil } + return String(str.dropFirst(2)) + } - override func getObjectValue( - _ obj: AutoreleasingUnsafeMutablePointer?, - for string: String, - errorDescription error: AutoreleasingUnsafeMutablePointer? - ) -> Bool { - obj?.pointee = "u:\(string)" as AnyObject - return true - } + override func getObjectValue( + _ obj: AutoreleasingUnsafeMutablePointer?, + for string: String, + errorDescription error: AutoreleasingUnsafeMutablePointer? + ) -> Bool { + obj?.pointee = "u:\(string)" as AnyObject + return true + } } diff --git a/src/VM.swift b/src/VM.swift index 9086bfc..f337d33 100644 --- a/src/VM.swift +++ b/src/VM.swift @@ -12,10 +12,7 @@ final class VM: ObservableObject { $text // Исключаем конфликты от UI и App путём игнорирования спама. .debounce(for: .seconds(0.3), scheduler: DispatchQueue.main) - // Нужны лишь значения от UI. - .filter { $0.hasPrefix("u:") } - // Убираем источник. - .map { String($0.dropFirst(2)) } + .compactMap(onlyAcceptUIText) .eraseToAnyPublisher(), sub: &subscriptions )