Created
March 24, 2022 16:38
-
-
Save eliyap/de6391bfe763e39f354d4bcf9f51d823 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import Combine | |
final class MessageConduit { | |
public let messageSubject = PassthroughSubject<Void, Never>() | |
} | |
/// Represents a view that will send messages, e.g. button presses. | |
final class SenderView: UIView { | |
private let myButton: UIButton | |
private let conduit: MessageConduit | |
@MainActor | |
init(conduit: MessageConduit) { | |
self.myButton = .init() | |
self.conduit = conduit | |
super.init(frame: .zero) | |
let action = UIAction { [weak self] (action) in | |
/// TODO: Send message to `ReceiverView`... | |
self?.conduit.messageSubject.send(Void()) | |
} | |
myButton.addAction(action, for: .touchUpInside) | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
/// Represents a view that responds to events. | |
final class ReceiverView: UIView { | |
private let conduit: MessageConduit | |
@MainActor | |
init(conduit: MessageConduit) { | |
self.conduit = conduit | |
super.init(frame: .zero) | |
} | |
func didReceiveMessage() { | |
print("Got a message from SenderView!") | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
/// Represents some controller coordinating senders and receivers. | |
final class MyViewController : UIViewController { | |
let senderView: SenderView | |
let receiverView: ReceiverView | |
let conduit = MessageConduit() | |
@MainActor | |
init() { | |
senderView = .init(conduit: conduit) | |
receiverView = .init(conduit: conduit) | |
super.init(nibName: nil, bundle: nil) | |
view.addSubview(senderView) | |
view.addSubview(receiverView) | |
/// Layout stuff here... | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment