From 56512ca26aa4de1bbc7e3262deb2ef1716b91eb2 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: Sat, 6 Jan 2024 18:22:22 +0300 Subject: [PATCH] d --- Modules/BusX/BusUI/src/BusUI.Value.swift | 11 ++++++-- .../MeetupIdX/src/MeetupId.Component.swift | 21 +++++++++++++++ Modules/MeetupIdX/src/MeetupId.V.swift | 27 +++++++++---------- 3 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 Modules/MeetupIdX/src/MeetupId.Component.swift diff --git a/Modules/BusX/BusUI/src/BusUI.Value.swift b/Modules/BusX/BusUI/src/BusUI.Value.swift index 558e4af..203461e 100644 --- a/Modules/BusX/BusUI/src/BusUI.Value.swift +++ b/Modules/BusX/BusUI/src/BusUI.Value.swift @@ -7,11 +7,18 @@ extension BusUI { public init( _ key: String, - _ defaultValue: T + _ defaultValue: T, + _ id: String? = nil ) { v = defaultValue + + var k = key + if let id { + k = k + id + } + Bus.receiveSync( - [key], + [k], { [weak self] (_, v: T) in self?.v = v }, &subscriptions ) diff --git a/Modules/MeetupIdX/src/MeetupId.Component.swift b/Modules/MeetupIdX/src/MeetupId.Component.swift new file mode 100644 index 0000000..66dcd96 --- /dev/null +++ b/Modules/MeetupIdX/src/MeetupId.Component.swift @@ -0,0 +1,21 @@ +extension MeetupId { + final class Component: ObservableObject { + var isJoinAvailable: BusUI.Value + var isLoading: BusUI.Value + var join: BusUI.Button + var textField: BusUI.TextField + + deinit { + /**/print("ИГР MeetupIC.deinit") + } + + public init(_ id: String) { + /**/print("ИГР MeetupIC.init") + isJoinAvailable = .init(K.isJoinAvailable, false, id) + isLoading = .init(K.isLoading, false, id) + join = .init(K.join) + textField = .init(K.textApp, K.textUI) + } + } +} + diff --git a/Modules/MeetupIdX/src/MeetupId.V.swift b/Modules/MeetupIdX/src/MeetupId.V.swift index 0c76631..2c640e9 100644 --- a/Modules/MeetupIdX/src/MeetupId.V.swift +++ b/Modules/MeetupIdX/src/MeetupId.V.swift @@ -3,39 +3,38 @@ import SwiftUI extension MeetupId { public struct V: View { - @StateObject var isJoinAvailable = BusUI.Value(K.isJoinAvailable, false) - @StateObject var isLoading = BusUI.Value(K.isLoading, false) - @StateObject var join = BusUI.Button(K.join) - @StateObject var textField = BusUI.TextField(K.textApp, K.textUI) + @StateObject private var cmp: Component - public init() { + public init(_ id: String) { + // https://stackoverflow.com/a/62636048 + _cmp = StateObject(wrappedValue: Component(id)) } public var body: some View { VStack(spacing: 8) { HStack { - TextField("Binding-3", value: $textField.v, formatter: BusUI.TextFieldSource()) - .disabled(isLoading.v) + TextField("Binding-3", value: $cmp.textField.v, formatter: BusUI.TextFieldSource()) + .disabled(cmp.isLoading.v) .padding(8) .border( - !isLoading.v ? Color.black : Color.gray, - width: !isLoading.v ? 2 : 1 + !cmp.isLoading.v ? Color.black : Color.gray, + width: !cmp.isLoading.v ? 2 : 1 ) - if isLoading.v { + if cmp.isLoading.v { ProgressView() .padding(8) } } - Button(action: join.v.send) { + Button(action: cmp.join.v.send) { Text("Join") .padding(8) .border( - isJoinAvailable.v ? Color.green : Color.gray, - width: isJoinAvailable.v ? 2 : 1 + cmp.isJoinAvailable.v ? Color.green : Color.gray, + width: cmp.isJoinAvailable.v ? 2 : 1 ) } - .disabled(!isJoinAvailable.v) + .disabled(!cmp.isJoinAvailable.v) } .frame(width: 320) .padding()