Last active
October 25, 2017 02:01
-
-
Save magi82/10dbf0c38c09f976755f4a5550a1bd38 to your computer and use it in GitHub Desktop.
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
// command | |
plusBtn.tap | |
.map(ViewModel.Command.plus) | |
.bind(to: viewModel.command) | |
.disposed(by: disposeBag) | |
minusBtn.tap | |
.map(ViewModel.Command.minus) | |
.bind(to: viewModel.command) | |
.disposed(by: disposeBag) | |
// state | |
viewModel.state | |
.numValue | |
.drive(label.rx.text) | |
.disposed(by: disposeBag) |
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
enum Command { | |
case plus | |
case minus | |
} | |
// MARK: Action | |
struct Action { | |
let numValue: Variable<Int> = Variable(0) | |
} | |
// MARK: State | |
struct State { | |
let numValue: Driver<Int> | |
init(action: Action) { | |
numValue = action.numValue.asDriver() | |
} | |
} | |
// MARK: Properties | |
var disposeBag = DisposeBag() | |
var command: PublishSubject<Command> | |
var action: Action | |
var state: State | |
init() { | |
command = PublishSubject() | |
action = Action() | |
state = State(action: action) | |
commandBind() | |
} | |
func commandBind() { | |
command.asObservable() | |
.subscribe(onNext: { [weak self] command in | |
switch command { | |
case .plus: | |
self?.action.numValue.value = self?.action.numValue.value + 1 | |
case .minus: | |
self?.action.numValue.value = self?.action.numValue.value - 1 | |
default: | |
break | |
} | |
}) | |
.disposed(by: self.disposeBag) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment