Skip to content

Instantly share code, notes, and snippets.

@igorkulman
Last active August 7, 2017 10:57
Show Gist options
  • Save igorkulman/a2c5b63f835e8bd01df89566a5627d6b to your computer and use it in GitHub Desktop.
Save igorkulman/a2c5b63f835e8bd01df89566a5627d6b to your computer and use it in GitHub Desktop.
import Foundation
import UIKit
import RxSwift
class SyncStepCell: UITableViewCell {
static let reuseIdentifier = "SyncStepCell"
@IBOutlet private weak var titleLabel: UILabel!
@IBOutlet private weak var activityIndicator: UIActivityIndicatorView!
@IBOutlet private weak var checkmarkImage: UIImageView!
var disposeBag = DisposeBag()
var viewModel: SyncStepViewModel? {
didSet {
if let vm = viewModel {
vm.isRunning.asObservable().map({!$0}).bindTo(activityIndicator.rx.isHidden).disposed(by: disposeBag)
vm.isRunning.asObservable().map({$0 ? UIColor.black : UIColor.gray}).bindTo(titleLabel.rx.textColor).disposed(by: disposeBag)
vm.stepTitle.bindTo(titleLabel.rx.text).disposed(by: disposeBag)
vm.isRunning.asObservable().bindTo(checkmarkImage.rx.isHidden).disposed(by: disposeBag)
}
}
}
}
import Foundation
import RxSwift
class SyncStepViewModel {
let isRunning = Variable(true)
let percentComplete = Variable<Int>(0)
let stepTitle: Observable<String>
private let title: String
init(title: String) {
self.title = title
stepTitle = Observable.combineLatest(isRunning.asObservable(), percentComplete.asObservable()) {
(running: Bool, percent: Int) -> String in if (running && percent>0) {
return "\(title) \(percent)%"
} else {
return title
}
}
}
}
viewModel.syncSteps.asObservable()
.bindTo(syncStepsTableView.rx.items(cellIdentifier: SyncStepCell.reuseIdentifier, cellType: UITableViewCell.self)) { (row, element, cell) in
if let cell = cell as? SyncStepCell {
cell.viewModel = element
}
}
.disposed(by: disposeBag)
import Foundation
import RxSwift
class SyncViewModel {
let syncSteps = Variable<[SyncStepViewModel]>([])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment