Проверка шаблона шины для iOS

95 lines
2.9KB

  1. import Combine
  2. import SwiftUI
  3. final class VM: ObservableObject {
  4. /*
  5. @Published var text = ""
  6. let changeText = PassthroughSubject<String, Never>()
  7. @Published var t2 = "a:value"
  8. var bt2: Binding<String>!
  9. */
  10. @Published var t3 = "a:value"
  11. var subscriptions = Set<AnyCancellable>()
  12. init() {
  13. $t3
  14. .debounce(for: .seconds(0.3), scheduler: DispatchQueue.main)
  15. .sink { v in
  16. /**/print("ИГР VM.init t3: '\(v)'")
  17. // Ignore repoting application initiated strings
  18. // Only report user initiated strings
  19. guard v.hasPrefix("u:") else { return }
  20. Bus.send(MeetupId.Keys.meetupIdTextUI.rawValue, String(v.dropFirst(2)))
  21. }
  22. .store(in: &subscriptions)
  23. Bus.receiveAsync(
  24. &subscriptions,
  25. [MeetupId.Keys.meetupIdTextApp.rawValue],
  26. { [weak self] _, v in self?.applyFormattedValue(v) }
  27. )
  28. /*
  29. bt2 = Binding<String>(
  30. get: { [weak self] in
  31. String(self?.t2.dropFirst(2) ?? "")
  32. },
  33. set: { [weak self] v in
  34. if v.hasPrefix("a:") {
  35. self?.t2 = v
  36. } else {
  37. self?.t2 = "u:\(v)"
  38. }
  39. }
  40. )
  41. $t2
  42. .debounce(for: .seconds(0.3), scheduler: DispatchQueue.main)
  43. .sink { v in
  44. /**/print("ИГР VM.init t2: '\(v)'")
  45. // Ignore repoting application initiated strings
  46. // Only report user initiated strings
  47. guard v.hasPrefix("u:") else { return }
  48. Bus.Service.singleton?.send(MeetupId.Keys.meetupIdTextUI.rawValue, String(v.dropFirst(2)))
  49. }
  50. .store(in: &subscriptions)
  51. Bus.receive(
  52. &subscriptions,
  53. [MeetupId.Keys.meetupIdTextApp.rawValue],
  54. { [weak self] k, v in self?.handleAppValue(k, v) }
  55. )
  56. // Интерпретируем текст с задержкой, потому что:
  57. // 1. смена @Published в ту же секунду игнорируется
  58. // полем ввода, т.е. нужна задержка
  59. // 2. ожидаем окончания ввода (спама), чтобы
  60. // обработать введённое без потерь из-за конфликта
  61. // старых данных и новых.
  62. $text
  63. .debounce(for: .seconds(0.3), scheduler: DispatchQueue.main)
  64. .sink { [weak self] v in
  65. guard
  66. let out = MeetupId.shouldFormat(v),
  67. v != out
  68. else {
  69. return
  70. }
  71. /**/print("ИГР TFCVM.init changeT in/out: '\(v)'/'\(out)'")
  72. self?.text = out
  73. }
  74. .store(in: &subscriptions)
  75. */
  76. }
  77. func applyFormattedValue(_ value: String) {
  78. /**/print("ИГР VM.handleFV value: '\(value)'")
  79. t3 = "a:\(value)"
  80. }
  81. }