From 9a4f181795c5ee1f6d8d1e43dd6f7bac5065af0a 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 14:03:19 +0300 Subject: [PATCH] d --- src/{Aux.swift => Cord.Aux.swift} | 2 +- src/Cord.TextField.swift | 26 ++++++++++++++++++++++++++ src/Cord.swift | 2 ++ src/V.swift | 5 +++-- src/VM.swift | 6 +++++- 5 files changed, 37 insertions(+), 4 deletions(-) rename src/{Aux.swift => Cord.Aux.swift} (81%) create mode 100644 src/Cord.TextField.swift create mode 100644 src/Cord.swift diff --git a/src/Aux.swift b/src/Cord.Aux.swift similarity index 81% rename from src/Aux.swift rename to src/Cord.Aux.swift index 94743b5..e2dc6c3 100644 --- a/src/Aux.swift +++ b/src/Cord.Aux.swift @@ -2,7 +2,7 @@ /// Пропускаем лишь значения от UI /// /// - Returns: Значение без префиксов "a:"/"u:" -func onlyAcceptUIText(_ s: String) -> String? { +func onlyUIText(_ s: String) -> String? { guard s.hasPrefix("u:") else { return nil } return String(s.dropFirst(2)) } diff --git a/src/Cord.TextField.swift b/src/Cord.TextField.swift new file mode 100644 index 0000000..27ec368 --- /dev/null +++ b/src/Cord.TextField.swift @@ -0,0 +1,26 @@ +import Combine +import SwiftUI + +extension Cord { + final class TextField: ObservableObject { + @Published var value = "a:" + var subscriptions = [AnyCancellable]() + + init( + _ textApp: String, + _ textUI: String + ) { + Bus.send( + textUI, + $value.compactMap(onlyUIText).eraseToAnyPublisher(), + sub: &subscriptions + ) + + Bus.receive( + [textApp], + { [weak self] (_, v: String) in self?.value = "a:\(v)" }, + sub: &subscriptions + ) + } + } +} diff --git a/src/Cord.swift b/src/Cord.swift new file mode 100644 index 0000000..f40341f --- /dev/null +++ b/src/Cord.swift @@ -0,0 +1,2 @@ + +public enum Cord { } diff --git a/src/V.swift b/src/V.swift index c19f2bb..f45e3a2 100644 --- a/src/V.swift +++ b/src/V.swift @@ -1,17 +1,18 @@ import SwiftUI struct V: View { + @StateObject var tf = Cord.TextField("text.app", "text.ui") @StateObject var vm = VM() var body: some View { VStack { HStack { Text("Check text field:") - Text("'\(vm.text)'") + Text("'\(tf.value)'") .fontWeight(.bold) } - TextField("Binding-3", value: $vm.text, formatter: TextFieldValueOwner()) + TextField("Binding-3", value: $tf.value, formatter: TextFieldValueOwner()) .padding(8) .border(Color.blue, width: 2) diff --git a/src/VM.swift b/src/VM.swift index 5339891..fb5849e 100644 --- a/src/VM.swift +++ b/src/VM.swift @@ -2,23 +2,26 @@ import Combine import SwiftUI final class VM: ObservableObject { + /* @Published var text = "a:" let format = Bus.Processor( MeetupId.K.meetupIdTextUI.rawValue, MeetupId.K.meetupIdTextApp.rawValue, MeetupId.onlyFormat ) + */ var isJoinAvailable = true let join = PassthroughSubject() var subscriptions = [AnyCancellable]() init() { + /* Bus.send( MeetupId.K.meetupIdTextUI.rawValue, $text // Исключаем конфликты от UI и App путём игнорирования спама. .debounce(for: .seconds(0.3), scheduler: DispatchQueue.main) - .compactMap(onlyAcceptUIText) + .compactMap(onlyUIText) .eraseToAnyPublisher(), sub: &subscriptions ) @@ -29,5 +32,6 @@ final class VM: ObservableObject { opt: [.async], sub: &subscriptions ) + */ } }