RQLearningMaterialsDetailViewController.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // RQLearningMaterialsDetailViewController.m
  3. // SDJK
  4. //
  5. // Created by 张嵘 on 2022/7/1.
  6. //
  7. #import "RQLearningMaterialsDetailViewController.h"
  8. #import "RQLearningMaterialsDetailHeaderView.h"
  9. #import "RQLearningMaterialsDetailFooterView.h"
  10. @interface RQLearningMaterialsDetailViewController ()
  11. /// viewModel
  12. @property (nonatomic, readonly, strong) RQLearningMaterialsDetailViewModel *viewModel;
  13. @property (nonatomic, readwrite, strong) RQLearningMaterialsDetailHeaderView *learningMaterialsDetailHeaderView;
  14. @property (nonatomic, readwrite, strong) RQLearningMaterialsDetailFooterView *learningMaterialsDetailFooterView;
  15. @end
  16. @implementation RQLearningMaterialsDetailViewController
  17. @dynamic viewModel;
  18. #pragma mark - SystemMethod
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. [self.view addSubview:self.learningMaterialsDetailHeaderView];
  22. [self.view bringSubviewToFront:self.learningMaterialsDetailHeaderView];
  23. [self.view addSubview:self.learningMaterialsDetailFooterView];
  24. [self.view bringSubviewToFront:self.learningMaterialsDetailFooterView];
  25. }
  26. - (void)viewDidLayoutSubviews {
  27. [super viewDidLayoutSubviews];
  28. self.learningMaterialsDetailHeaderView.frame = CGRectMake(0, 8 + RQ_APPLICATION_TOP_BAR_HEIGHT, RQ_SCREEN_WIDTH, RQ_FIT_HORIZONTAL(30.f) + 32.f);
  29. 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));
  30. }
  31. - (void)dealloc{
  32. RQDealloc;
  33. /// remove observer ,otherwise will crash
  34. [self.webView stopLoading];
  35. }
  36. #pragma mark - OverrideMethod
  37. - (void)_backItemDidClicked {
  38. /// 判断 是Push还是Present进来的,
  39. if (self.presentingViewController) {
  40. [self.viewModel.services dismissViewModelAnimated:YES completion:NULL];
  41. } else {
  42. [self.viewModel.services popViewModelAnimated:YES];
  43. }
  44. }
  45. #pragma mark - LazyLoad
  46. - (RQLearningMaterialsDetailHeaderView *)learningMaterialsDetailHeaderView {
  47. @weakify(self)
  48. if (!_learningMaterialsDetailHeaderView) {
  49. _learningMaterialsDetailHeaderView = [RQLearningMaterialsDetailHeaderView learningMaterialsDetailHeaderView];
  50. [_learningMaterialsDetailHeaderView.sendBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  51. @strongify(self)
  52. [RQ_ALERT_MANAGER showEmailAlertConfirmAction:^(__kindof QMUIDialogViewController * _Nonnull dialogViewController, __kindof QMUITextField * _Nonnull textFiled) {
  53. [MBProgressHUD rq_showProgressHUD:@"发送中..."];
  54. [[RQ_HTTP_Service postEmailInfoWithAddress:textFiled.text dictCode:self.viewModel.learningMaterialsModel.dictCode] subscribeNext:^(id _Nullable x) {
  55. [MBProgressHUD rq_hideHUD];
  56. } error:^(NSError * _Nullable error) {
  57. [MBProgressHUD rq_hideHUD];
  58. [MBProgressHUD rq_showErrorTips:error];
  59. }];
  60. }];
  61. }];
  62. [_learningMaterialsDetailHeaderView.downloadBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  63. @strongify(self)
  64. [RQ_COMMON_MANAGER downLoadWithFilePath:self.viewModel.learningMaterialsModel.dictValue];
  65. }];
  66. }
  67. return _learningMaterialsDetailHeaderView;
  68. }
  69. - (RQLearningMaterialsDetailFooterView *)learningMaterialsDetailFooterView {
  70. @weakify(self)
  71. if (!_learningMaterialsDetailFooterView) {
  72. _learningMaterialsDetailFooterView = [RQLearningMaterialsDetailFooterView learningMaterialsDetailFooterView];
  73. RAC(_learningMaterialsDetailFooterView.myTitleLabel, text) = RACObserve(self.viewModel, title);
  74. RAC(_learningMaterialsDetailFooterView.lastBtn, enabled) = [RACObserve(self.viewModel, learningMaterialsModel) map:^id _Nullable(id _Nullable value) {
  75. @strongify(self)
  76. return @([self.viewModel.items indexOfObject:self.viewModel.learningMaterialsModel] != 0);
  77. }];
  78. RAC(_learningMaterialsDetailFooterView.nextBtn, enabled) = [RACObserve(self.viewModel, learningMaterialsModel) map:^id _Nullable(id _Nullable value) {
  79. @strongify(self)
  80. return @([self.viewModel.items indexOfObject:self.viewModel.learningMaterialsModel] + 1 != self.viewModel.items.count);
  81. }];
  82. [_learningMaterialsDetailFooterView.lastBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  83. @strongify(self)
  84. NSInteger index = [self.viewModel.items indexOfObject:self.viewModel.learningMaterialsModel];
  85. NSInteger lastIndex = ((index - 1) < 0)? 0 : (index - 1);
  86. if (self.viewModel.items.count > lastIndex) {
  87. RQLearningMaterialsModel *lastLearningMaterialsModel = self.viewModel.items[lastIndex];
  88. NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:lastLearningMaterialsModel.dictValue]];
  89. RQLearningMaterialsDetailViewModel *learningMaterialsDetailViewModel = [[RQLearningMaterialsDetailViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{RQViewModelRequestKey : request, RQViewModelUtilKey : lastLearningMaterialsModel, RQViewCommonValueKey : self.viewModel.items}];
  90. [self.viewModel updateViewModelWith:learningMaterialsDetailViewModel];
  91. [self.webView evaluateJavaScript:@"document.body.innerHTML = '';" completionHandler:^(id result, NSError * _Nullable error) {
  92. }];
  93. [self.webView loadRequest:request];
  94. } else {
  95. }
  96. }];
  97. [_learningMaterialsDetailFooterView.nextBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  98. @strongify(self)
  99. NSInteger index = [self.viewModel.items indexOfObject:self.viewModel.learningMaterialsModel];
  100. NSInteger nextIndex = index + 1;
  101. if (self.viewModel.items.count > nextIndex) {
  102. RQLearningMaterialsModel *nextLearningMaterialsModel = self.viewModel.items[nextIndex];
  103. NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:nextLearningMaterialsModel.dictValue]];
  104. RQLearningMaterialsDetailViewModel *learningMaterialsDetailViewModel = [[RQLearningMaterialsDetailViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{RQViewModelRequestKey : request, RQViewModelUtilKey : nextLearningMaterialsModel, RQViewCommonValueKey : self.viewModel.items}];
  105. [self.viewModel updateViewModelWith:learningMaterialsDetailViewModel];
  106. [self.webView evaluateJavaScript:@"document.body.innerHTML = '';" completionHandler:^(id result, NSError * _Nullable error) {
  107. }];
  108. [self.webView.backForwardList qmui_clearAllBinding];
  109. [self.webView loadRequest:request];
  110. } else {
  111. }
  112. }];
  113. }
  114. return _learningMaterialsDetailFooterView;
  115. }
  116. @end