Last active
July 18, 2021 11:10
-
-
Save ykpoh/7e5800343531c76851340a6d044ab47d to your computer and use it in GitHub Desktop.
This file contains 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
import UIKit | |
import RxSwift | |
class LaunchListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { | |
@IBOutlet var tableView: UITableView! | |
var viewModel: LaunchListViewModelType = LaunchListViewModel() | |
var disposeBag: DisposeBag! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
tableView.delegate = self | |
tableView.dataSource = self | |
setupListeners() | |
} | |
func setupListeners() { | |
disposeBag = DisposeBag() | |
viewModel.launchViewModels | |
.asDriver() | |
.drive(onNext: { [weak self] value in | |
guard let strongSelf = self else { return } | |
strongSelf.tableView.reloadData() | |
}) | |
.disposed(by: disposeBag) | |
viewModel.notifyError | |
.asDriver() | |
.drive(onNext: { [weak self] value in | |
guard let strongSelf = self, let value = value else { return } | |
strongSelf.showAlert(value.localizedDescription) // 1 | |
}) | |
.disposed(by: disposeBag) | |
} | |
func tableView(_ tableView: UITableView, | |
numberOfRowsInSection section: Int) -> Int { | |
return max(viewModel.launchViewModels.value.count, 1) // 2 | |
} | |
func tableView(_ tableView: UITableView, | |
cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
guard viewModel.launchViewModels.value.count > 0 else { | |
return UITableViewCell() | |
} | |
return listingCell(tableView, indexPath) // 3 | |
} | |
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
guard let rocketName = viewModel.launchViewModels.value[indexPath.row].launch.value?.rocket else { return } | |
let rocketDetailVC = RocketDetailViewController.instanceFromStoryboard() | |
rocketDetailVC.viewModel = RocketDetailViewModel(rocketName: rocketName) | |
navigationController?.pushViewController(rocketDetailVC, animated: true) // 4 | |
} | |
private func listingCell(_ tableView: UITableView, _ indexPath: IndexPath) -> LaunchListTableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: | |
"\(LaunchListTableViewCell.self)") as! LaunchListTableViewCell | |
let vm = viewModel.launchViewModels.value[indexPath.row] | |
vm.configure(cell) | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment