// // RQLearningMaterialsDetailViewController.m // SDJK // // Created by 张嵘 on 2022/7/1. // #import "RQLearningMaterialsDetailViewController.h" #import "RQLearningMaterialsDetailHeaderView.h" #import "RQLearningMaterialsDetailFooterView.h" @interface RQLearningMaterialsDetailViewController () /// viewModel @property (nonatomic, readonly, strong) RQLearningMaterialsDetailViewModel *viewModel; @property (nonatomic, readwrite, strong) RQLearningMaterialsDetailHeaderView *learningMaterialsDetailHeaderView; @property (nonatomic, readwrite, strong) RQLearningMaterialsDetailFooterView *learningMaterialsDetailFooterView; @end @implementation RQLearningMaterialsDetailViewController @dynamic viewModel; #pragma mark - SystemMethod - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.learningMaterialsDetailHeaderView]; [self.view bringSubviewToFront:self.learningMaterialsDetailHeaderView]; [self.view addSubview:self.learningMaterialsDetailFooterView]; [self.view bringSubviewToFront:self.learningMaterialsDetailFooterView]; } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; self.learningMaterialsDetailHeaderView.frame = CGRectMake(0, 8 + RQ_APPLICATION_TOP_BAR_HEIGHT, RQ_SCREEN_WIDTH, RQ_FIT_HORIZONTAL(30.f) + 32.f); self.learningMaterialsDetailFooterView.frame = CGRectMake(16, RQ_SCREEN_HEIGHT - RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT - RQ_FIT_HORIZONTAL(72.f) - RQ_FIT_HORIZONTAL(54.f), RQ_SCREEN_WIDTH - 32.f, RQ_FIT_HORIZONTAL(54.f)); } - (void)dealloc{ RQDealloc; /// remove observer ,otherwise will crash [self.webView stopLoading]; } #pragma mark - OverrideMethod - (void)_backItemDidClicked { /// 判断 是Push还是Present进来的, if (self.presentingViewController) { [self.viewModel.services dismissViewModelAnimated:YES completion:NULL]; } else { [self.viewModel.services popViewModelAnimated:YES]; } } #pragma mark - LazyLoad - (RQLearningMaterialsDetailHeaderView *)learningMaterialsDetailHeaderView { @weakify(self) if (!_learningMaterialsDetailHeaderView) { _learningMaterialsDetailHeaderView = [RQLearningMaterialsDetailHeaderView learningMaterialsDetailHeaderView]; [_learningMaterialsDetailHeaderView.sendBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) { @strongify(self) [RQ_ALERT_MANAGER showEmailAlertConfirmAction:^(__kindof QMUIDialogViewController * _Nonnull dialogViewController, __kindof QMUITextField * _Nonnull textFiled) { [MBProgressHUD rq_showProgressHUD:@"发送中..."]; [[RQ_HTTP_Service postEmailInfoWithAddress:textFiled.text dictCode:self.viewModel.learningMaterialsModel.dictCode] subscribeNext:^(id _Nullable x) { [MBProgressHUD rq_hideHUD]; } error:^(NSError * _Nullable error) { [MBProgressHUD rq_hideHUD]; [MBProgressHUD rq_showErrorTips:error]; }]; }]; }]; [_learningMaterialsDetailHeaderView.downloadBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) { @strongify(self) [RQ_COMMON_MANAGER downLoadWithFilePath:self.viewModel.learningMaterialsModel.dictValue]; }]; } return _learningMaterialsDetailHeaderView; } - (RQLearningMaterialsDetailFooterView *)learningMaterialsDetailFooterView { @weakify(self) if (!_learningMaterialsDetailFooterView) { _learningMaterialsDetailFooterView = [RQLearningMaterialsDetailFooterView learningMaterialsDetailFooterView]; RAC(_learningMaterialsDetailFooterView.myTitleLabel, text) = RACObserve(self.viewModel, title); RAC(_learningMaterialsDetailFooterView.lastBtn, enabled) = [RACObserve(self.viewModel, learningMaterialsModel) map:^id _Nullable(id _Nullable value) { @strongify(self) return @([self.viewModel.items indexOfObject:self.viewModel.learningMaterialsModel] != 0); }]; RAC(_learningMaterialsDetailFooterView.nextBtn, enabled) = [RACObserve(self.viewModel, learningMaterialsModel) map:^id _Nullable(id _Nullable value) { @strongify(self) return @([self.viewModel.items indexOfObject:self.viewModel.learningMaterialsModel] + 1 != self.viewModel.items.count); }]; [_learningMaterialsDetailFooterView.lastBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) { @strongify(self) NSInteger index = [self.viewModel.items indexOfObject:self.viewModel.learningMaterialsModel]; NSInteger lastIndex = ((index - 1) < 0)? 0 : (index - 1); if (self.viewModel.items.count > lastIndex) { RQLearningMaterialsModel *lastLearningMaterialsModel = self.viewModel.items[lastIndex]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:lastLearningMaterialsModel.dictValue]]; RQLearningMaterialsDetailViewModel *learningMaterialsDetailViewModel = [[RQLearningMaterialsDetailViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{RQViewModelRequestKey : request, RQViewModelUtilKey : lastLearningMaterialsModel, RQViewCommonValueKey : self.viewModel.items}]; [self.viewModel updateViewModelWith:learningMaterialsDetailViewModel]; [self.webView evaluateJavaScript:@"document.body.innerHTML = '';" completionHandler:^(id result, NSError * _Nullable error) { }]; [self.webView loadRequest:request]; } else { } }]; [_learningMaterialsDetailFooterView.nextBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) { @strongify(self) NSInteger index = [self.viewModel.items indexOfObject:self.viewModel.learningMaterialsModel]; NSInteger nextIndex = index + 1; if (self.viewModel.items.count > nextIndex) { RQLearningMaterialsModel *nextLearningMaterialsModel = self.viewModel.items[nextIndex]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:nextLearningMaterialsModel.dictValue]]; RQLearningMaterialsDetailViewModel *learningMaterialsDetailViewModel = [[RQLearningMaterialsDetailViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{RQViewModelRequestKey : request, RQViewModelUtilKey : nextLearningMaterialsModel, RQViewCommonValueKey : self.viewModel.items}]; [self.viewModel updateViewModelWith:learningMaterialsDetailViewModel]; [self.webView evaluateJavaScript:@"document.body.innerHTML = '';" completionHandler:^(id result, NSError * _Nullable error) { }]; [self.webView.backForwardList qmui_clearAllBinding]; [self.webView loadRequest:request]; } else { } }]; } return _learningMaterialsDetailFooterView; } @end