SHARE
TWEET

Untitled

a guest Oct 19th, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import RxSwift
  2. import RxRelay
  3. import RxDataSources
  4.  
  5. class TodoListViewModel {
  6.     private let todoListCellViewModelsBehaviorRelay = BehaviorRelay<[TodoListCellViewModel]>(value: [])
  7.     // 1
  8.     private var notStartedSectionObservable: Observable<TodoListSection> {
  9.         return todoListCellViewModelsBehaviorRelay
  10.             .flatMapLatest { viewModels -> PrimitiveSequence<SingleTrait, [TodoListCellViewModel]> in
  11.                 Observable.from(viewModels)
  12.                     .flatMapLatest { viewModel in
  13.                         viewModel.todoStatusObservable
  14.                             .take(1)
  15.                             .filter { $0 == .notStarted }
  16.                             .map { _ in viewModel }
  17.                     }
  18.                     .toArray()
  19.             }
  20.             .map { TodoListSection(header: "Not Started", items: $0) }
  21.     }
  22.     // 2
  23.     private var startedSectionObserverable: Observable<TodoListSection> {
  24.         return todoListCellViewModelsBehaviorRelay
  25.             .flatMapLatest { viewModels -> PrimitiveSequence<SingleTrait, [TodoListCellViewModel]> in
  26.                 Observable.from(viewModels)
  27.                     .flatMapLatest { viewModel in
  28.                         viewModel.todoStatusObservable
  29.                             .take(1)
  30.                             .filter { $0 == .started }
  31.                             .map { _ in viewModel }
  32.                     }
  33.                     .toArray()
  34.             }
  35.             .map { TodoListSection(header: "Started", items: $0) }
  36.     }
  37.     // 3
  38.     private var finishedSectionObservable: Observable<TodoListSection> {
  39.         return todoListCellViewModelsBehaviorRelay
  40.             .flatMapLatest { viewModels -> PrimitiveSequence<SingleTrait, [TodoListCellViewModel]> in
  41.                 Observable.from(viewModels)
  42.                     .flatMapLatest { viewModel in
  43.                         viewModel.todoStatusObservable
  44.                             .take(1)
  45.                             .filter { $0 == .finished }
  46.                             .map { _ in viewModel }
  47.                     }
  48.                     .toArray()
  49.             }
  50.             .map { TodoListSection(header: "Not Started", items: $0) }
  51.     }
  52.  
  53.     var addButtonObserver: AnyObserver<Void> {
  54.         return AnyObserver { _ in
  55.             let todoListCellViewModel = TodoListCellViewModel(with: Todo(title: "Todo: \(self.todoListCellViewModelsBehaviorRelay.value.count)", todoStatus: .notStarted))
  56.             self.todoListCellViewModelsBehaviorRelay.accept(self.todoListCellViewModelsBehaviorRelay.value + [todoListCellViewModel])
  57.         }
  58.     }
  59.  
  60.     let dataSource = RxTableViewSectionedReloadDataSource<TodoListSection>(configureCell: {dataSource, tableView, indexPath, item in
  61.         guard let cell = tableView.dequeueReusableCell(withIdentifier: "TodoListTableViewCell") as? TodoListTableViewCell else { return UITableViewCell() }
  62.         cell.bind(to: item)
  63.  
  64.         return cell
  65.     })
  66. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top