From 36373058a562741afa8b60d0de5d562ddcb82539 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: Thu, 14 Dec 2023 18:11:58 +0300 Subject: [PATCH] d --- src/App.swift | 1 + src/Bus.swift | 1 + src/MeetupId.swift | 14 +++++++------- src/V.swift | 4 ++++ src/VM.swift | 43 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/App.swift b/src/App.swift index 501381d..cfaabc4 100644 --- a/src/App.swift +++ b/src/App.swift @@ -4,6 +4,7 @@ import UIKit class AppDelegate: UIResponder, UIApplicationDelegate { let bus = Bus.Service() + let fmt = MeetupId.MeetupIdFormatter() var window: UIWindow? func application( diff --git a/src/Bus.swift b/src/Bus.swift index 4576fb9..f11194d 100644 --- a/src/Bus.swift +++ b/src/Bus.swift @@ -17,6 +17,7 @@ extension Bus { } func send(_ key: String, _ value: Any) { + /**/print("ИГР BusS.send key/value: '\(key)'/'\(value)'") broadcaster.send((key, value)) } } diff --git a/src/MeetupId.swift b/src/MeetupId.swift index 4bbf0e4..4c4afe7 100644 --- a/src/MeetupId.swift +++ b/src/MeetupId.swift @@ -1,3 +1,4 @@ +import Combine import Foundation enum MeetupId { @@ -9,13 +10,12 @@ enum MeetupId { static func shouldFormat(_ s: String) -> String? { s.components(separatedBy: NSCharacterSet.decimalDigits.inverted).reduce("") { $0 + $1 } } - - /* + final class MeetupIdFormatter { - var subscriptions = [AnyCancellable]() + var subscriptions = Set() deinit { - /**/dbg("ИГР MeetupIF.DEinit") + /**/print("ИГР MeetupIF.DEinit") } init() { @@ -24,16 +24,16 @@ enum MeetupId { [Keys.meetupIdTextUI.rawValue], { [weak self] k, v in self?.handleFormatting(k, v) } ) - /**/dbg("ИГР MeetupIF.init") + /**/print("ИГР MeetupIF.init") } func handleFormatting(_: String, _ value: String) { let out = MeetupId.shouldFormat(value) - /**/dbg("ИГР MeetupIF.handleF out/dt: '\(out)'/'\(Date())'") + /**/print("ИГР MeetupIF.handleF out/dt: '\(out)'/'\(Date())'") Bus.Service.singleton?.send(Keys.meetupIdTextApp.rawValue, out) } } - +/* struct V: View { /*@StateObject*/ var fmt = MeetupIdFormatter() @StateObject var txt = Bus.BindingPipe("", Keys.meetupIdTextApp.rawValue, Keys.meetupIdTextUI.rawValue) diff --git a/src/V.swift b/src/V.swift index 4b6bc5f..4011648 100644 --- a/src/V.swift +++ b/src/V.swift @@ -13,6 +13,10 @@ struct V: View { TextField("Placeholder", text: $vm.text) .padding(8) .border(Color.red, width: 2) + + TextField("Binding-2", text: vm.bt2) + .padding(8) + .border(Color.blue, width: 2) } .frame(width: 320) .padding() diff --git a/src/VM.swift b/src/VM.swift index 74e7fe1..d34d66d 100644 --- a/src/VM.swift +++ b/src/VM.swift @@ -4,9 +4,42 @@ import SwiftUI final class VM: ObservableObject { @Published var text = "" let changeText = PassthroughSubject() + + @Published var t2 = "a:value" + var bt2: Binding! var subscriptions = Set() init() { + bt2 = Binding( + get: { [weak self] in + String(self?.t2.dropFirst(2) ?? "") + }, + set: { [weak self] v in + if v.hasPrefix("a:") { + self?.t2 = v + } else { + self?.t2 = "u:\(v)" + } + } + ) + + $t2 + .debounce(for: .seconds(0.3), scheduler: DispatchQueue.main) + .sink { v in + /**/print("ИГР VM.init t2: '\(v)'") + // Ignore repoting application initiated strings + // Only report user initiated strings + guard v.hasPrefix("u:") else { return } + Bus.Service.singleton?.send(MeetupId.Keys.meetupIdTextUI.rawValue, String(v.dropFirst(2))) + } + .store(in: &subscriptions) + + Bus.receive( + &subscriptions, + [MeetupId.Keys.meetupIdTextApp.rawValue], + { [weak self] k, v in self?.handleAppValue(k, v) } + ) + // Интерпретируем текст с задержкой, потому что: // 1. смена @Published в ту же секунду игнорируется // полем ввода, т.е. нужна задержка @@ -27,4 +60,14 @@ final class VM: ObservableObject { } .store(in: &subscriptions) } + + func handleAppValue(_: String, _ value: String) { + /**/print("ИГР VM.handleAV value: '\(value)'") + /* + DispatchQueue.main.async { [weak self] in + self?.t2 = value + } + */ + } + }