Skip to content

Instantly share code, notes, and snippets.

@MartinMoizard
Last active April 14, 2023 19:44
Show Gist options
  • Save MartinMoizard/449be0d30920010210988f1773a2ca90 to your computer and use it in GitHub Desktop.
Save MartinMoizard/449be0d30920010210988f1773a2ca90 to your computer and use it in GitHub Desktop.
/// Every view interacting with a `SayHelloViewModel` instance can conform to this.
protocol SayHelloViewModelBindable {
var disposeBag: DisposeBag? { get }
func bind(to viewModel: SayHelloViewModel)
}
/// TableViewCells
final class TextFieldCell: UITableViewCell, SayHelloViewModelBindable {
@IBOutlet weak var nameTextField: UITextField!
var disposeBag: DisposeBag?
override func prepareForReuse() {
super.prepareForReuse()
// Clean Rx subscriptions
disposeBag = nil
}
func bind(to viewModel: SayHelloViewModel) {
let bag = DisposeBag()
nameTextField.rx
.text
.orEmpty
.bind(to: viewModel.input.name)
.disposed(by: bag)
disposeBag = bag
}
}
final class ButtonCell: UITableViewCell, SayHelloViewModelBindable {
@IBOutlet weak var validateButton: UIButton!
var disposeBag: DisposeBag?
override func prepareForReuse() {
super.prepareForReuse()
disposeBag = nil
}
func bind(to viewModel: SayHelloViewModel) {
let bag = DisposeBag()
validateButton.rx
.tap
.bind(to: viewModel.input.validate)
.disposed(by: bag)
disposeBag = bag
}
}
final class GreetingCell: UITableViewCell, SayHelloViewModelBindable {
@IBOutlet weak var greetingLabel: UILabel!
var disposeBag: DisposeBag?
override func prepareForReuse() {
super.prepareForReuse()
disposeBag = nil
}
func bind(to viewModel: SayHelloViewModel) {
let bag = DisposeBag()
viewModel.output.greeting
.drive(greetingLabel.rx.text)
.disposed(by: bag)
disposeBag = bag
}
}
/// View
class TableViewController: UIViewController, UITableViewDataSource {
static let cellIdentifiers = [
"TextFieldCell",
"ButtonCell",
"GreetingCell"
]
@IBOutlet weak var tableView: UITableView!
private let viewModel = SayHelloViewModel()
private let bag = DisposeBag()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return TableViewController.cellIdentifiers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: TableViewController.cellIdentifiers[indexPath.row])
(cell as? SayHelloViewModelBindable)?.bind(to: viewModel)
return cell!
}
}
@georgescumihai
Copy link

georgescumihai commented Feb 17, 2020

The idea is nice, still It has one limitation, you are forced to use a concrete type of the ViewModel inside the View, you can't use ViewModelType.
So you can't reuse the view with other viewModels.
Example a view with a form for CreateAccount, EditAccount, they might look the same, but the viewModels are totally different.
Or you want to mock the ViewModel for UITests.

@phucledien
Copy link

phucledien commented Jun 21, 2020

@georgescumihai, I believe this is just a simple implementation of the view. In his full post here, he has used the viewmodeltype protocol.

So in a real project, I guess we should declare the viewmodel as protocol instead of concreate type, then use DI to inject the viewmodel. With that, we can use different vms for the view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment