From 848f658327daabf4690a4003c6a8db4888c51739 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: Wed, 13 Dec 2023 19:27:57 +0300 Subject: [PATCH] d --- src/App.swift | 60 +--------------------------------------------- src/MeetupId.swift | 54 +++++++++++++++++++++++++++++++++++++++++ src/VM.swift | 10 +------- 3 files changed, 56 insertions(+), 68 deletions(-) create mode 100644 src/MeetupId.swift diff --git a/src/App.swift b/src/App.swift index 185ce53..501381d 100644 --- a/src/App.swift +++ b/src/App.swift @@ -3,6 +3,7 @@ import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { + let bus = Bus.Service() var window: UIWindow? func application( @@ -89,63 +90,4 @@ public extension Bus { } } } - -// BusSlot.swift -var busHolder = Bus.Service() - -// ======================================== -// MARK: - MeetupX module -// ======================================== - -enum MeetupId { - enum Keys: String, RawRepresentable { - case meetupIdTextApp - case meetupIdTextUI - } - - static func shouldFormat(_ s: String) -> String? { - return s.components(separatedBy: NSCharacterSet.decimalDigits.inverted).reduce("") { $0 + $1 } - } - - final class MeetupIdFormatter { - var subscriptions = [AnyCancellable]() - - deinit { - /**/dbg("ИГР MeetupIF.DEinit") - } - - init() { - Bus.receive( - &subscriptions, - [Keys.meetupIdTextUI.rawValue], - { [weak self] k, v in self?.handleFormatting(k, v) } - ) - /**/dbg("ИГР MeetupIF.init") - } - - func handleFormatting(_: String, _ value: String) { - let out = MeetupId.shouldFormat(value) - /**/dbg("ИГР 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) - - var body: some View { - VStack { - Text("Hi, the text is: '\(txt.value)'") - .fontWeight(.bold) - TextField("Placeholder", text: $txt.value) - .padding(8) - .border(Color.blue, width: 2) - } - .frame(width: 320) - .padding() - } - } -} - */ diff --git a/src/MeetupId.swift b/src/MeetupId.swift new file mode 100644 index 0000000..4bbf0e4 --- /dev/null +++ b/src/MeetupId.swift @@ -0,0 +1,54 @@ +import Foundation + +enum MeetupId { + enum Keys: String, RawRepresentable { + case meetupIdTextApp + case meetupIdTextUI + } + + static func shouldFormat(_ s: String) -> String? { + s.components(separatedBy: NSCharacterSet.decimalDigits.inverted).reduce("") { $0 + $1 } + } + + /* + final class MeetupIdFormatter { + var subscriptions = [AnyCancellable]() + + deinit { + /**/dbg("ИГР MeetupIF.DEinit") + } + + init() { + Bus.receive( + &subscriptions, + [Keys.meetupIdTextUI.rawValue], + { [weak self] k, v in self?.handleFormatting(k, v) } + ) + /**/dbg("ИГР MeetupIF.init") + } + + func handleFormatting(_: String, _ value: String) { + let out = MeetupId.shouldFormat(value) + /**/dbg("ИГР 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) + + var body: some View { + VStack { + Text("Hi, the text is: '\(txt.value)'") + .fontWeight(.bold) + TextField("Placeholder", text: $txt.value) + .padding(8) + .border(Color.blue, width: 2) + } + .frame(width: 320) + .padding() + } + } + */ +} diff --git a/src/VM.swift b/src/VM.swift index fd9730f..74e7fe1 100644 --- a/src/VM.swift +++ b/src/VM.swift @@ -7,29 +7,21 @@ final class VM: ObservableObject { var subscriptions = Set() init() { - // Уведомляем об изменении текста. - $text - .sink { [weak self] v in self?.changeText.send(v) } - .store(in: &subscriptions) - // Интерпретируем текст с задержкой, потому что: // 1. смена @Published в ту же секунду игнорируется // полем ввода, т.е. нужна задержка // 2. ожидаем окончания ввода (спама), чтобы // обработать введённое без потерь из-за конфликта // старых данных и новых. - changeText + $text .debounce(for: .seconds(0.3), scheduler: DispatchQueue.main) .sink { [weak self] v in - /* guard let out = MeetupId.shouldFormat(v), v != out else { return } - */ - let out = v /**/print("ИГР TFCVM.init changeT in/out: '\(v)'/'\(out)'") self?.text = out }