Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
94 lines (76 sloc) 3.24 KB
//
// ListViewController.swift
// RxSwiftMVVM
//
// Created by Tsubasa Hayashi on 2019/04/29.
// Copyright © 2019 Tsubasa Hayashi. All rights reserved.
//
import UIKit
import RxCocoa
import RxSwift
final class ListViewController: UIViewController {
static func make(with viewModel: ListViewModel) -> ListViewController {
let view = ListViewController.instantiate()
view.viewModel = viewModel
return view
}
@IBOutlet private weak var tableView: UITableView!
@IBOutlet private weak var indicatorView: UIActivityIndicatorView!
private var viewModel: ListViewModelType!
private let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
// Bind ViewModel Outputs
viewModel.outputs.navigationBarTitle
.observeOn(MainScheduler.instance)
.bind(to: navigationItem.rx.title)
.disposed(by: disposeBag)
viewModel.outputs.gitHubRepositories
.observeOn(MainScheduler.instance)
.bind(to: tableView.rx.items) { tableView, row, githubRepository in
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "subtitle")
cell.textLabel?.text = "\(githubRepository.fullName)"
cell.detailTextLabel?.textColor = UIColor.lightGray
cell.detailTextLabel?.text = "\(githubRepository.description)"
return cell
}
.disposed(by: disposeBag)
tableView.rx.modelSelected(GitHubRepository.self)
.observeOn(MainScheduler.instance)
.subscribe(onNext: { [weak self] in
let vc = DetailViewController.make(with: DetailViewModel(repository: $0))
self?.navigationController?.pushViewController(vc, animated: true)
})
.disposed(by: disposeBag)
viewModel.outputs.isLoading
.observeOn(MainScheduler.instance)
.bind(to: indicatorView.rx.isAnimating)
.disposed(by: disposeBag)
viewModel.outputs.isLoading
.observeOn(MainScheduler.instance)
.subscribe(onNext: { [weak self] in
self?.tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: $0 ? 50 : 0, right: 0)
})
.disposed(by: disposeBag)
viewModel.outputs.error
.observeOn(MainScheduler.instance)
.subscribe(onNext: { [weak self] in
let ac = UIAlertController(title: "Error \($0)", message: nil, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self?.present(ac, animated: true)
})
.disposed(by: disposeBag)
// Bind ViewModel Inputs
tableView.rx.reachedBottom.asObservable()
.bind(to: viewModel.inputs.reachedBottomTrigger)
.disposed(by: disposeBag)
viewModel.inputs.fetchTrigger.onNext(())
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.indexPathsForSelectedRows?.forEach { [weak self] in
self?.tableView.deselectRow(at: $0, animated: true)
}
}
}
extension ListViewController: StoryboardInstantiable {}
You can’t perform that action at this time.