// // NotificationHistoryPresenter.swift // GME Remit // // Created by InKwon James Kim on 2020/02/11. //Copyright © 2020 Gobal Money Express Co. Ltd. All rights reserved. // import RxSwift import RxCocoa class NotificationHistoryPresenter: ViewModelType { var interactor: NotificationHistoryInteractorInput? var wireframe: NotificationHistoryWireframeInput? struct Input { let viewWillAppear: Driver let selectNoticeIndex: Driver } struct Output { let isError: Driver let isProgress: Driver let models: Driver<[NoticeModel]> } private let disposeBag = DisposeBag() private let progressLinker = PublishSubject() private let errorLinker = PublishSubject() private let model = PublishSubject<[NoticeModel]>() func transform(input: Input) -> Output { input.viewWillAppear.drive(onNext: {[weak self] in self?.progressLinker.onNext(true) self?.interactor?.fetchNotices() }).disposed(by: disposeBag) input.selectNoticeIndex .withLatestFrom(model.asDriverOnErrorJustComplete()) { (indexPath, notices) -> NoticeModel in return notices[indexPath] } .drive(onNext: {[weak self] in self?.wireframe?.detail(index: $0.key ?? "") }).disposed(by: disposeBag) return Output( isError: errorLinker.asDriverOnErrorJustComplete(), isProgress: progressLinker.asDriverOnErrorJustComplete(), models: model.asDriverOnErrorJustComplete() ) } } // MARK: NotificationHistory interactor output interface extension NotificationHistoryPresenter: NotificationHistoryInteractorOutput { func setNotices(with model: [NoticeModel]) { progressLinker.onNext(false) self.model.onNext(model) } func setError(with error: Error) { progressLinker.onNext(false) errorLinker.onNext(error) } }