From 50a94c4b8568babc7d6a20271803039cdfe96235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Tue, 26 Dec 2023 12:28:00 +0300 Subject: [PATCH] d --- src/Aux.swift | 8 ++++++++ src/TextFieldValueOwner.swift | 24 ++++++++++++------------ src/VM.swift | 5 +---- 3 files changed, 21 insertions(+), 16 deletions(-) create mode 100644 src/Aux.swift 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 )