Last active
March 23, 2021 14:01
-
-
Save wongzigii/a42099ce11b7ceb97260bd1dddb3d306 to your computer and use it in GitHub Desktop.
Demo.swift
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 | |
import RxSwiftExt | |
class AccquiredItemViewModel: PaginationViewModel<EggProduct> { | |
var selection = PublishSubject<EggProduct>() | |
var preselectedProduct = PublishSubject<EggProduct>() | |
// 选中的状态 | |
var state = BehaviorSubject<Set<EggProduct>>(value: Set()) | |
override init() { | |
super.init() | |
self.selection | |
.asObservable() | |
.scan(Set<EggProduct>()) { (acc: Set<EggProduct>, item: EggProduct) in | |
// 选中的逻辑,什么时候应该选中:点击对应蛋壳collectionview cell 的时候 | |
// 不选中的逻辑,什么时候应该不选中:(1)点击已选中的蛋壳 colelctionview 的时候 | |
// (2) 点进去详情把所有商品取消选中的时候 | |
var acc = acc | |
if acc.isEmpty { | |
acc.insert(item) | |
} else { | |
acc.removeAll() | |
acc.insert(item) | |
} | |
return acc | |
} | |
.startWith(Set()) | |
.bind(to: self.state) | |
.disposed(by: disposeBag) | |
let response = request | |
.flatMapLatest { page in | |
AppEnvironment.current.apiService.getEggProductList(type: 0, page: page).materialize() | |
} | |
.share(replay: 1) | |
response.elements() | |
.map { $0.products.first } | |
.filterNil() | |
.bind(to: selection) | |
.disposed(by: disposeBag) | |
Observable | |
.combineLatest(request, response.elements(), items.asObservable()) { req, res, array in | |
let responseMachines = res.products | |
self.isEnd.accept(responseMachines.count < Constants.kDefaultPageLimit) | |
return self.pageIndex == Constants.kDefaultPageIndex ? responseMachines : array + responseMachines | |
} | |
.sample(response.elements()) | |
.bind(to: items) | |
.disposed(by: disposeBag) | |
Observable | |
.merge(request.map { _ in true }, | |
response.map { _ in false }, | |
error.map { _ in false }) | |
.bind(to: loading) | |
.disposed(by: disposeBag) | |
response | |
.errors() | |
.requestErrors() | |
.bind(to: error) | |
.disposed(by: disposeBag) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment