Created
January 8, 2018 19:47
-
-
Save mecid/a9c0026dd5ebdc68e383e075e37ac1db to your computer and use it in GitHub Desktop.
Mastering MVVM
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
class ItemsViewController: UIViewController { | |
@IBOutlet private weak var tableView: UITableView! | |
private let activityIndicator = ActivityIndicatorView() | |
private var viewModel: ItemsViewModel | |
init(viewModel: ItemsViewModel) { | |
self.viewModel = viewModel | |
super.init(nibName: nil, bundle: nil) | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setupUI() | |
bindViewModel() | |
viewModel.fetch() | |
} | |
func bindViewModel() { | |
viewModel.refreshing.bind(to: activityIndicator.reactive.isAnimating) | |
viewModel.items.bind(to: self) { strongSelf, _ in | |
strongSelf.tableView.reloadData() | |
} | |
} | |
func setupUI() { | |
view.addSubview(activityIndicator) | |
} | |
} | |
extension ItemsViewController: UITableViewDataSource { | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return viewModel.items.value.count | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment