refactored code

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-08-18 15:11:44 -05:00
parent 231fa9c486
commit dccd42cb5e

View File

@ -20,31 +20,20 @@ struct User {
} }
} }
public protocol Bindable { class TextFieldBindingCell: UIControl {
associatedtype ModelType
var model: ModelType { get set }
var subject: CurrentValueSubject<ModelType, Never>? { get set }
func createBinding(with subject: CurrentValueSubject<ModelType, Never>, storeIn subscriptions: inout Set<AnyCancellable>)
}
extension Bindable { private let firstNameTextField = TextField()
public func send() { private let lastNameTextField = TextField()
subject?.send(model) private var subscriptions = Set<AnyCancellable>()
var user: User {
User(firstName: firstNameTextField.text ?? "", lastName: lastNameTextField.text ?? "" )
} }
}
class TextFieldBindingCell: UIView, Bindable { init(user: User) {
//bindable
var model: User
var subject: CurrentValueSubject<User, Never>?
let firstNameTextField = TextField()
let lastNameTextField = TextField()
init(model: User) {
self.model = model
super.init(frame: .zero) super.init(frame: .zero)
firstNameTextField.text = user.firstName
lastNameTextField.text = user.lastName
translatesAutoresizingMaskIntoConstraints = false translatesAutoresizingMaskIntoConstraints = false
firstNameTextField.translatesAutoresizingMaskIntoConstraints = false firstNameTextField.translatesAutoresizingMaskIntoConstraints = false
@ -52,7 +41,6 @@ class TextFieldBindingCell: UIView, Bindable {
addSubview(firstNameTextField) addSubview(firstNameTextField)
addSubview(lastNameTextField) addSubview(lastNameTextField)
print("\(model.firstName) \(model.lastName)")
firstNameTextField.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true firstNameTextField.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
firstNameTextField.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true firstNameTextField.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
@ -65,35 +53,23 @@ class TextFieldBindingCell: UIView, Bindable {
lastNameTextField.heightAnchor.constraint(equalToConstant: 50).isActive = true lastNameTextField.heightAnchor.constraint(equalToConstant: 50).isActive = true
lastNameTextField.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true lastNameTextField.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
firstNameTextField
.textPublisher
.sink { [weak self] _ in
self?.sendActions(for: .valueChanged)
}.store(in: &subscriptions)
lastNameTextField
.textPublisher
.sink { [weak self] _ in
self?.sendActions(for: .valueChanged)
}.store(in: &subscriptions)
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
func createBinding(with subject: CurrentValueSubject<User, Never>, storeIn subscriptions: inout Set<AnyCancellable>) {
self.model = subject.value
self.subject = subject
//add the logic
let firstNameSubject = CurrentValueSubject<String, Never>(model.firstName)
firstNameTextField.createBinding(with: firstNameSubject, storeIn: &subscriptions)
firstNameSubject
.sink {[weak self] value in
guard let self = self else { return }
self.model.firstName = value
self.send()
}.store(in: &subscriptions)
let lastNameSubject = CurrentValueSubject<String, Never>(model.lastName)
lastNameTextField.createBinding(with: lastNameSubject, storeIn: &subscriptions)
lastNameSubject
.sink {[weak self] value in
guard let self = self else { return }
self.model.lastName = value
self.send()
}.store(in: &subscriptions)
}
} }
@objcMembers class TestViewController: UIViewController, Initable { @objcMembers class TestViewController: UIViewController, Initable {
@ -101,7 +77,13 @@ class TextFieldBindingCell: UIView, Bindable {
print("\(Self.self) deinit") print("\(Self.self) deinit")
} }
var user = User(firstName: "Joe", lastName: "User") var user = User(firstName: "Joe", lastName: "User") {
didSet {
print("User oldValue: \(oldValue)")
print("User newValue: \(user)")
}
}
private var subscriptions = Set<AnyCancellable>() private var subscriptions = Set<AnyCancellable>()
required init() { required init() {
@ -116,16 +98,7 @@ class TextFieldBindingCell: UIView, Bindable {
super.viewDidLoad() super.viewDidLoad()
view.backgroundColor = .white view.backgroundColor = .white
let subject = CurrentValueSubject<User, Never>(user) let nameTextField = TextFieldBindingCell(user: user)
subject
.sink{ [weak self] updatedModel in
self?.user = updatedModel
}
.store(in: &subscriptions)
let nameTextField = TextFieldBindingCell(model: user)
nameTextField.createBinding(with: subject, storeIn: &subscriptions)
let button = UIButton() let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false button.translatesAutoresizingMaskIntoConstraints = false
@ -142,38 +115,22 @@ class TextFieldBindingCell: UIView, Bindable {
button.backgroundColor = .blue button.backgroundColor = .blue
button.setTitle("Print", for: .normal) button.setTitle("Print", for: .normal)
button.addAction(UIAction(title: "", handler: { [weak self] action in //publishers
if let user = self?.user { nameTextField
print("\(user.firstName) \(user.lastName)") .publisher(for: .valueChanged)
} .sink(receiveValue: { [weak self] control in
}), for: .touchUpInside) guard let self else { return }
} self.user = control.user
} })
.store(in: &subscriptions)
button
extension UITextField { .tapPublisher
.sink { [weak self] _ in
public func textPublisher() -> AnyPublisher<String, Never> { if let user = self?.user {
NotificationCenter.default print("\(user.firstName) \(user.lastName)")
.publisher(for: UITextField.textDidChangeNotification, object: self) }
.compactMap({ ($0.object as? UITextField)?.text }) }.store(in: &subscriptions)
.eraseToAnyPublisher()
}
public func createBinding(with subject: CurrentValueSubject<String, Never>,
storeIn subscriptions: inout Set<AnyCancellable>) {
subject.sink { [weak self] (value) in
if value != self?.text {
self?.text = value
}
}.store(in: &subscriptions)
self.textPublisher().sink { (value) in
if value != subject.value {
subject.send(value)
}
}.store(in: &subscriptions)
} }
} }