Add dependency injection
This commit is contained in:
parent
151ed6d78b
commit
bf94769573
9 changed files with 140 additions and 51 deletions
|
@ -11,50 +11,51 @@ import SwiftUI
|
|||
|
||||
@objc(CustomButton)
|
||||
class CustomButton: UIView {
|
||||
@Injected private var emitter: EventEmitter
|
||||
|
||||
private let label: UILabel = {
|
||||
let lbl = UILabel()
|
||||
lbl.text = "Hello from native"
|
||||
lbl.textColor = .white
|
||||
lbl.translatesAutoresizingMaskIntoConstraints = false
|
||||
return lbl
|
||||
}()
|
||||
|
||||
private let label: UILabel = {
|
||||
let lbl = UILabel()
|
||||
lbl.text = "Hello from native"
|
||||
lbl.textColor = .white
|
||||
lbl.translatesAutoresizingMaskIntoConstraints = false
|
||||
return lbl
|
||||
}()
|
||||
private let button: UIButton = {
|
||||
let btn = UIButton(type: .system)
|
||||
btn.setTitle("Click Me", for: .normal)
|
||||
btn.setTitleColor(.white, for: .normal)
|
||||
btn.translatesAutoresizingMaskIntoConstraints = false
|
||||
return btn
|
||||
}()
|
||||
|
||||
private let button: UIButton = {
|
||||
let btn = UIButton(type: .system)
|
||||
btn.setTitle("Click Me", for: .normal)
|
||||
btn.setTitleColor(.white, for: .normal)
|
||||
btn.translatesAutoresizingMaskIntoConstraints = false
|
||||
return btn
|
||||
}()
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
self.backgroundColor = .systemBlue
|
||||
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
self.backgroundColor = .systemBlue
|
||||
self.addSubview(label)
|
||||
self.addSubview(button)
|
||||
|
||||
self.addSubview(label)
|
||||
self.addSubview(button)
|
||||
setupConstraints()
|
||||
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
|
||||
}
|
||||
|
||||
setupConstraints()
|
||||
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
|
||||
}
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
private func setupConstraints() {
|
||||
NSLayoutConstraint.activate([
|
||||
label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 16),
|
||||
label.centerYAnchor.constraint(equalTo: self.centerYAnchor),
|
||||
|
||||
private func setupConstraints() {
|
||||
NSLayoutConstraint.activate([
|
||||
label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 16),
|
||||
label.centerYAnchor.constraint(equalTo: self.centerYAnchor),
|
||||
button.leadingAnchor.constraint(equalTo: label.trailingAnchor, constant: 12),
|
||||
button.centerYAnchor.constraint(equalTo: self.centerYAnchor),
|
||||
button.trailingAnchor.constraint(lessThanOrEqualTo: self.trailingAnchor, constant: -16)
|
||||
])
|
||||
}
|
||||
|
||||
button.leadingAnchor.constraint(equalTo: label.trailingAnchor, constant: 12),
|
||||
button.centerYAnchor.constraint(equalTo: self.centerYAnchor),
|
||||
button.trailingAnchor.constraint(lessThanOrEqualTo: self.trailingAnchor, constant: -16)
|
||||
])
|
||||
}
|
||||
|
||||
@objc private func buttonTapped() {
|
||||
|
||||
}
|
||||
@objc private func buttonTapped() {
|
||||
emitter.send(message: "Clicked in UIButton button that was created in RN")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,13 +9,14 @@ import SwiftUI
|
|||
|
||||
struct ToolboxHeader: View {
|
||||
@EnvironmentObject var sharedState: SharedState
|
||||
@Injected var emitter: EventEmitter
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
HStack {
|
||||
Text("Actions")
|
||||
Button("Make it blue") {
|
||||
sharedState.send(message: "hello from Swift!")
|
||||
emitter.send(message: "hello from Swift!")
|
||||
}
|
||||
}
|
||||
Text("Message: \(sharedState.message)")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue