Skip to content

Instantly share code, notes, and snippets.

@laiso
Created January 10, 2017 11:28
Show Gist options
  • Save laiso/6b1d88cf1d34b9db0dbf382b8df31456 to your computer and use it in GitHub Desktop.
Save laiso/6b1d88cf1d34b9db0dbf382b8df31456 to your computer and use it in GitHub Desktop.
これこれ #CodePiece
class HomeViewModel {
let charges = Variable<[Charge]>([])
let didAppear = PublishSubject<Void>()
let disposeBag = DisposeBag()
init(with state: AppState) {
guard let api = state.webAPI else { fatalError("error") }
let _ = didAppear.flatMapLatest { api.getCharges() }
.bindTo(charges)
}
}
class HomeViewController: UIViewController {
@IBOutlet weak var chargesView: UITableView!
let viewModel = HomeViewModel(with: AppState.shared)
let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
let nib = UINib(nibName: "ChargeCell", bundle: nil)
chargesView.register(nib, forCellReuseIdentifier: "ChargeCell")
viewModel.charges.asObservable()
.bindTo(chargesView.rx.items(cellIdentifier: "ChargeCell", cellType: ChargeCell.self)) { row, charge, cell in
cell.configure(with: charge)
}
.addDisposableTo(disposeBag)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
viewModel.didAppear.onNext()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment