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,39 +20,27 @@ struct User {
}
}
public protocol Bindable {
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>)
}
class TextFieldBindingCell: UIControl {
private let firstNameTextField = TextField()
private let lastNameTextField = TextField()
private var subscriptions = Set<AnyCancellable>()
extension Bindable {
public func send() {
subject?.send(model)
var user: User {
User(firstName: firstNameTextField.text ?? "", lastName: lastNameTextField.text ?? "" )
}
}
class TextFieldBindingCell: UIView, Bindable {
//bindable
var model: User
var subject: CurrentValueSubject<User, Never>?
let firstNameTextField = TextField()
let lastNameTextField = TextField()
init(model: User) {
self.model = model
init(user: User) {
super.init(frame: .zero)
firstNameTextField.text = user.firstName
lastNameTextField.text = user.lastName
translatesAutoresizingMaskIntoConstraints = false
firstNameTextField.translatesAutoresizingMaskIntoConstraints = false
lastNameTextField.translatesAutoresizingMaskIntoConstraints = false
addSubview(firstNameTextField)
addSubview(lastNameTextField)
print("\(model.firstName) \(model.lastName)")
firstNameTextField.leadingAnchor.constraint(equalTo: leadingAnchor).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.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) {
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 {
@ -101,7 +77,13 @@ class TextFieldBindingCell: UIView, Bindable {
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>()
required init() {
@ -116,16 +98,7 @@ class TextFieldBindingCell: UIView, Bindable {
super.viewDidLoad()
view.backgroundColor = .white
let subject = CurrentValueSubject<User, Never>(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 nameTextField = TextFieldBindingCell(user: user)
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
@ -142,38 +115,22 @@ class TextFieldBindingCell: UIView, Bindable {
button.backgroundColor = .blue
button.setTitle("Print", for: .normal)
button.addAction(UIAction(title: "", handler: { [weak self] action in
if let user = self?.user {
print("\(user.firstName) \(user.lastName)")
}
}), for: .touchUpInside)
}
}
extension UITextField {
public func textPublisher() -> AnyPublisher<String, Never> {
NotificationCenter.default
.publisher(for: UITextField.textDidChangeNotification, object: self)
.compactMap({ ($0.object as? UITextField)?.text })
.eraseToAnyPublisher()
}
public func createBinding(with subject: CurrentValueSubject<String, Never>,
storeIn subscriptions: inout Set<AnyCancellable>) {
//publishers
nameTextField
.publisher(for: .valueChanged)
.sink(receiveValue: { [weak self] control in
guard let self else { return }
self.user = control.user
})
.store(in: &subscriptions)
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)
button
.tapPublisher
.sink { [weak self] _ in
if let user = self?.user {
print("\(user.firstName) \(user.lastName)")
}
}.store(in: &subscriptions)
}
}