-
-
Save kakajika/1cb14658237808a570bd01c6cd60150e 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 RxSwift | |
import RxCocoa | |
final class CreateListViewModel { | |
/// 中間Stream for Input | |
//PublishSubject:外からの入力のみを受け付ける | |
private let createBtnDidTapStream = PublishSubject<Void>() | |
private let genreStream = BehaviorSubject<String>(value: "") | |
private let toDoStream = BehaviorSubject<String>(value: "") | |
/// 中間Stream for Output | |
private let navigateToMainStream: Observable<Void> | |
private let toDoListStream: Observable<[[String: Any]]> | |
init() { | |
// ボタンがタップされるたびにtodoを追加したリストを流すストリームの例 | |
self.toDoListStream = self.createBtnDidTapStream | |
.withLatestFrom(Observable.combineLatest( | |
self.toDoStream.asObservable(), | |
self.genreStream.asObservable() | |
)) { _, todoAndGenre -> [String: Any] in | |
let (todo, genre) = todoAndGenre | |
return [ "toDo": todo, "genre": genre, "hasDone": false ] | |
} | |
.scan([]) { todoList, dict in | |
return todoList + [dict] | |
} | |
.share(replay: 1, scope: .whileConnected) // ストリーム分岐のためにshare | |
// todoを追加したあとにmain画面へ遷移するイベントを流すストリームの例 | |
self.navigateToMainStream = self.toDoListStream.map { _ in () } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment