Slide 155
Slide 155 text
class DetailViewModel {
let episodeDetail: Observable
let recommendList: Property<[Episode]>
let readyForInitializingViews: Observable
let stackAxis: Observable
let addThumbnailToRoot: Observable
let sectionModels: Observable<[SectionModel]>
init(...,
traitCollectionDidChange: Observable,
toggleContentInfo: Observable,
didFinishInitializingViews: Observable) {
let _recommendList = BehaviorRelay<[Episode]>(value: [])
let _isContentInfoHidden = BehaviorRelay(value: true)
let _isPortrait = BehaviorRelay(value: true)
let _sectionModels = BehaviorRelay<[SectionModel]>(value: [])
self.sectionModels = Observable.combineLatest(didFinishInitializingViews.take(1), _sectionModels.asObservable()) { $1 }
self.recommendList = Property(_recommendList)
...
Observable.combineLatest(_isPortrait, _isContentInfoHidden, _recommendList)
.map { isPortrait, isContentInfoHidden, recommendList -> [SectionModel] in
let contentItems: [SectionModel.Item] = [
isPortrait ? .thumbnail : nil,
.summary,
isContentInfoHidden ? nil : .contentInfo,
.other,
.episodeList
].compactMap { $0 }
let recommendSections: [SectionModel]
if recommendList.isEmpty {
recommendSections = []
} else {
recommendSections = [.recommend(
recommendList.enumerated().map { args in
.recommend(IndexPath(row: args.offset, section: 0))
}
)]
}
return [SectionModel.content(contentItems)] + recommendSections
}
.bind(to: _sectionModels)
.disposed(by: disposeBag)
...
}
}