Last active
August 7, 2017 10:57
-
-
Save igorkulman/a2c5b63f835e8bd01df89566a5627d6b 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 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) | |
} | |
} | |
} | |
} |
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 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 | |
} | |
} | |
} | |
} |
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
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) |
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 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