Михаил Капелько 10 months ago
parent
commit
50a94c4b85
3 changed files with 21 additions and 16 deletions
  1. +8
    -0
      src/Aux.swift
  2. +12
    -12
      src/TextFieldValueOwner.swift
  3. +1
    -4
      src/VM.swift

+ 8
- 0
src/Aux.swift View File

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

+ 12
- 12
src/TextFieldValueOwner.swift View File

@@ -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<AnyObject?>?,
for string: String,
errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?
) -> Bool {
obj?.pointee = "u:\(string)" as AnyObject
return true
}
override func getObjectValue(
_ obj: AutoreleasingUnsafeMutablePointer<AnyObject?>?,
for string: String,
errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?
) -> Bool {
obj?.pointee = "u:\(string)" as AnyObject
return true
}
}

+ 1
- 4
src/VM.swift View File

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


Loading…
Cancel
Save