Created
January 10, 2017 11:28
-
-
Save laiso/6b1d88cf1d34b9db0dbf382b8df31456 to your computer and use it in GitHub Desktop.
これこれ #CodePiece
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 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