Last active
February 3, 2017 18:15
-
-
Save desmondmc/9958c3ac5468cb853017bfa2d01b927d to your computer and use it in GitHub Desktop.
Micro ViewModel+View Recipe RxSwift
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 TestViewModel { | |
struct Inputs { | |
let tap = PublishSubject<Void>() | |
} | |
struct Outputs { | |
let someString: Observable<String?> | |
} | |
let inputs: TestViewModel.Inputs | |
let outputs: TestViewModel.Outputs | |
init() { | |
// Inputs | |
inputs = Inputs() | |
// Outputs | |
let stringThing: Observable<String?> = inputs.tap.map { "Tapped!" } | |
outputs = Outputs(someString: stringThing) | |
} | |
} | |
class TestVC: UIViewController { | |
@IBOutlet weak var button: UIButton! | |
@IBOutlet weak var someLabel: UILabel! | |
private let viewModel: TestViewModel | |
private let disposeBag = DisposeBag() | |
init(viewModel: TestViewModel) { | |
self.viewModel = viewModel | |
super.init(nibName: "TestVC", bundle: Bundle.main) | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Bind Model Inputs | |
button.rx.tap | |
.bindTo(viewModel.inputs.tap) | |
.addDisposableTo(disposeBag) | |
// Bind Model Outputs | |
viewModel.outputs.someString | |
.bindTo(someLabel.rx.text) | |
.addDisposableTo(disposeBag) | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment