Няма описание

PhotoDetailItemViewModel.swift 3.4KB

    // // PhotoDetailViewModel.swift // PaiaiDataKit // // Created by FFIB on 2017/1/4. // Copyright © 2017年 FFIB. All rights reserved. // import Foundation import RxSwift import RxCocoa import RxDataSources public final class PhotoDetailItemViewModel { var item: BehaviorRelay<PhotoItem> var thumbupItems = BehaviorRelay<[PhotoThumbupUserItem]>(value: []) var commentItems = BehaviorRelay<[PhotoCommentItem]>(value: []) private var repository: PhotoDetailRepository private var disposeBag = DisposeBag() public init(item: PhotoItem) { self.item = BehaviorRelay<PhotoItem>(value: item) self.repository = PhotoDetailRepository(photoId: item.photo_id, groupId: item.group_id) self.item.subscribe(onNext: {[unowned self] (photoItem) in self.repository = PhotoDetailRepository(photoId: photoItem.photo_id, groupId: photoItem.group_id) self.loadThumbupAndComment() // Observable.zip(self.loadCommentItems(), self.loadThumbupUserItems()).subscribe(onNext: { _ in // <#code#> // }, onError: <#T##((Error) -> Void)?##((Error) -> Void)?##(Error) -> Void#>, onCompleted: <#T##(() -> Void)?##(() -> Void)?##() -> Void#>, onDisposed: <#T##(() -> Void)?##(() -> Void)?##() -> Void#>) // .subscribe(onCompleted: { // // var val = self.item.value // val.comment_num = self.commentItems.value.count // val.thumbup_num = self.thumbupItems.value.count // self.item.accept(val) // // let userInfo = [PhotoItemsOperator.key: PhotoItemsOperator.update(val.photo_id, val)] // NotificationCenter.default.post(name: .PhotoItemsChanged, // object: nil, // userInfo: userInfo) // }).disposed(by: self.disposeBag) }).disposed(by: disposeBag) } private func loadThumbupAndComment() { Single.zip(repository.loadComments(), repository.loadThumbups()).subscribe(onSuccess: {[unowned self] (commentItems, thumbupItems) in self.commentItems.accept(commentItems) self.thumbupItems.accept(thumbupItems) self.update() }).disposed(by: disposeBag) } private func loadThumbupUserItems() -> Single<[PhotoThumbupUserItem]> { return repository.loadThumbups() } private func loadCommentItems() -> Single<[PhotoCommentItem]> { return repository.loadComments() } private func update() { var val = self.item.value val.comment_num = self.commentItems.value.count val.thumbup_num = self.thumbupItems.value.count let userInfo = [PhotoItemsOperator.key: PhotoItemsOperator.update(val.photo_id, val)] NotificationCenter.default.post(name: .PhotoItemsChanged, object: nil, userInfo: userInfo) } public func submitThumbup() { repository.submitThumbup().subscribe(onSuccess: { items in self.thumbupItems.accept(items) self.update() }).disposed(by: disposeBag) } public func submitComment(text: String) { repository.submitComment(text: text).subscribe(onSuccess: { (items) in self.commentItems.accept(items) self.update() }).disposed(by: disposeBag) } }