@@ -45,13 +45,12 @@ public extension Bus { | |||||
_ subscriptions: inout [AnyCancellable] | _ subscriptions: inout [AnyCancellable] | ||||
) { | ) { | ||||
node | node | ||||
.sink { v in send(key, v) } | |||||
.sink { v in e.send((key, v)) } | |||||
.store(in: &subscriptions) | .store(in: &subscriptions) | ||||
} | } | ||||
/// Единоразово синхронно отправляем событие в шину. | /// Единоразово синхронно отправляем событие в шину. | ||||
static func send(_ key: String, _ value: Any) { | static func send(_ key: String, _ value: Any) { | ||||
/**/print("ИГР Bus.send k/v: '\(key)'/'\(value)'") | |||||
e.send((key, value)) | e.send((key, value)) | ||||
} | } | ||||
} | } |
@@ -5,17 +5,9 @@ extension BusUI { | |||||
public let v = PassthroughSubject<Void, Never>() | public let v = PassthroughSubject<Void, Never>() | ||||
var subscriptions = [AnyCancellable]() | var subscriptions = [AnyCancellable]() | ||||
public init( | |||||
_ key: String, | |||||
_ id: String? = nil | |||||
) { | |||||
var k = key | |||||
if let id { | |||||
k = k + id | |||||
} | |||||
public init(_ key: String) { | |||||
Bus.sendSync( | Bus.sendSync( | ||||
k, | |||||
key, | |||||
v.map { true }.eraseToAnyPublisher(), | v.map { true }.eraseToAnyPublisher(), | ||||
&subscriptions | &subscriptions | ||||
) | ) | ||||
@@ -8,18 +8,10 @@ extension BusUI { | |||||
public init( | public init( | ||||
_ textApp: String, | _ textApp: String, | ||||
_ textUI: String, | |||||
_ id: String? = nil | |||||
_ textUI: String | |||||
) { | ) { | ||||
var ka = textApp | |||||
var ku = textUI | |||||
if let id { | |||||
ka = ka + id | |||||
ku = ku + id | |||||
} | |||||
Bus.sendSync( | Bus.sendSync( | ||||
ku, | |||||
textUI, | |||||
$v | $v | ||||
.removeDuplicates() | .removeDuplicates() | ||||
.compactMap(onlyUIText) | .compactMap(onlyUIText) | ||||
@@ -28,7 +20,7 @@ extension BusUI { | |||||
) | ) | ||||
Bus.receiveSync( | Bus.receiveSync( | ||||
[ka], | |||||
[textApp], | |||||
{ [weak self] (_, v: String) in self?.v = "a:\(v)" }, | { [weak self] (_, v: String) in self?.v = "a:\(v)" }, | ||||
&subscriptions | &subscriptions | ||||
) | ) | ||||
@@ -7,18 +7,11 @@ extension BusUI { | |||||
public init( | public init( | ||||
_ key: String, | _ key: String, | ||||
_ defaultValue: T, | |||||
_ id: String? = nil | |||||
_ defaultValue: T | |||||
) { | ) { | ||||
v = defaultValue | v = defaultValue | ||||
var k = key | |||||
if let id { | |||||
k = k + id | |||||
} | |||||
Bus.receiveSync( | Bus.receiveSync( | ||||
[k], | |||||
[key], | |||||
{ [weak self] (_, v: T) in self?.v = v }, | { [weak self] (_, v: T) in self?.v = v }, | ||||
&subscriptions | &subscriptions | ||||
) | ) | ||||
@@ -1,23 +0,0 @@ | |||||
import BusX | |||||
extension MeetupId { | |||||
final class Component: ObservableObject { | |||||
var isJoinAvailable: BusUI.Value<Bool> | |||||
var isLoading: BusUI.Value<Bool> | |||||
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, id) | |||||
textField = .init(K.textApp, K.textUI, id) | |||||
} | |||||
} | |||||
} | |||||
@@ -3,38 +3,39 @@ import SwiftUI | |||||
extension MeetupId { | extension MeetupId { | ||||
public struct V: View { | public struct V: View { | ||||
@StateObject private var cmp: Component | |||||
@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) | |||||
public init(_ id: String) { | |||||
// https://stackoverflow.com/a/62636048 | |||||
_cmp = StateObject(wrappedValue: Component(id)) | |||||
public init() { | |||||
} | } | ||||
public var body: some View { | public var body: some View { | ||||
VStack(spacing: 8) { | VStack(spacing: 8) { | ||||
HStack { | HStack { | ||||
TextField("Binding-3", value: $cmp.textField.v, formatter: BusUI.TextFieldSource()) | |||||
.disabled(cmp.isLoading.v) | |||||
TextField("Binding-3", value: $textField.v, formatter: BusUI.TextFieldSource()) | |||||
.disabled(isLoading.v) | |||||
.padding(8) | .padding(8) | ||||
.border( | .border( | ||||
!cmp.isLoading.v ? Color.black : Color.gray, | |||||
width: !cmp.isLoading.v ? 2 : 1 | |||||
!isLoading.v ? Color.black : Color.gray, | |||||
width: !isLoading.v ? 2 : 1 | |||||
) | ) | ||||
if cmp.isLoading.v { | |||||
if isLoading.v { | |||||
ProgressView() | ProgressView() | ||||
.padding(8) | .padding(8) | ||||
} | } | ||||
} | } | ||||
Button(action: cmp.join.v.send) { | |||||
Button(action: join.v.send) { | |||||
Text("Join") | Text("Join") | ||||
.padding(8) | .padding(8) | ||||
.border( | .border( | ||||
cmp.isJoinAvailable.v ? Color.green : Color.gray, | |||||
width: cmp.isJoinAvailable.v ? 2 : 1 | |||||
isJoinAvailable.v ? Color.green : Color.gray, | |||||
width: isJoinAvailable.v ? 2 : 1 | |||||
) | ) | ||||
} | } | ||||
.disabled(!cmp.isJoinAvailable.v) | |||||
.disabled(!isJoinAvailable.v) | |||||
} | } | ||||
.frame(width: 320) | .frame(width: 320) | ||||
.padding() | .padding() | ||||
@@ -4,9 +4,9 @@ import UIKit | |||||
struct Content: View { | struct Content: View { | ||||
var body: some View { | var body: some View { | ||||
MeetupId.V("") | |||||
MeetupId.V() | |||||
Divider() | Divider() | ||||
MeetupId.V("abc") | |||||
MeetupId.V() | |||||
} | } | ||||
} | } | ||||