RQExerciseExamToolBarCell.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // RQExerciseExamToolBarCell.m
  3. // SDJK
  4. //
  5. // Created by 张嵘 on 2021/8/24.
  6. //
  7. #import "RQExerciseExamToolBarCell.h"
  8. @interface RQExerciseExamToolBarCell ()
  9. @property (nonatomic, readwrite, strong) RQExerciseExamToolBarItemViewModel *viewModel;
  10. @property (weak, nonatomic) IBOutlet UILabel *numLabel;
  11. @property (nonatomic, readwrite, assign) BOOL readingQuestionsAndAnswersBtnisRead;
  12. @end
  13. @implementation RQExerciseExamToolBarCell
  14. #pragma mark - PublicMethods
  15. + (instancetype)cellWithTableView:(UITableView *)tableView {
  16. static NSString *ID = @"RQExerciseExamToolBarCell";
  17. RQExerciseExamToolBarCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  18. if (!cell) {
  19. cell = [self rq_viewFromXib];
  20. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  21. [cell.readingQuestionsAndAnswersBtn layoutButtonWithEdgeInsetsStyle:RQButtonEdgeInsetsStyleTop imageTitleSpace:5.f];
  22. [cell.collectBtn layoutButtonWithEdgeInsetsStyle:RQButtonEdgeInsetsStyleTop imageTitleSpace:5.f];
  23. [cell.readingQuestionsBtn layoutButtonWithEdgeInsetsStyle:RQButtonEdgeInsetsStyleTop imageTitleSpace:5.f];
  24. [cell.lastQuestionBtn layoutButtonWithEdgeInsetsStyle:RQButtonEdgeInsetsStyleTop imageTitleSpace:5.f];
  25. [cell.nextQuestionBtn layoutButtonWithEdgeInsetsStyle:RQButtonEdgeInsetsStyleTop imageTitleSpace:5.f];
  26. }
  27. return cell;
  28. }
  29. - (void)bindViewModel:(RQExerciseExamToolBarItemViewModel *)viewModel {
  30. _viewModel = viewModel;
  31. RQCollectionModel *collectionModel = [[RQCollectionModel alloc] init];
  32. collectionModel.questionId = viewModel.exerciseModel._id;
  33. _collectBtn.selected = [RQ_SDJK_DB_MANAGER isExistWithRQCollectionModel:collectionModel];
  34. [_collectBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  35. if (!_collectBtn.selected) {
  36. [[RQ_HTTP_Service queryAddCollectionRecordWithQuestionId:viewModel.exerciseModel._id carType:viewModel.homePageCarType subject:viewModel.homePageSubjectType] subscribeNext:^(id _Nullable x) {
  37. [RQ_SDJK_DB_MANAGER addCollectionRecordWithRQCollectionModel:collectionModel];
  38. _collectBtn.selected = [RQ_SDJK_DB_MANAGER isExistWithRQCollectionModel:collectionModel];
  39. } error:^(NSError * _Nullable error) {
  40. [MBProgressHUD rq_showErrorTips:error];
  41. }];
  42. } else {
  43. [[RQ_HTTP_Service queryCancleCollectionRecordWithQuestionIdArr:@[@(viewModel.exerciseModel._id)]] subscribeNext:^(id _Nullable x) {
  44. [RQ_SDJK_DB_MANAGER deleteCollectionRecordWithQuestionId:collectionModel.questionId];
  45. _collectBtn.selected = [RQ_SDJK_DB_MANAGER isExistWithRQCollectionModel:collectionModel];
  46. } error:^(NSError * _Nullable error) {
  47. [MBProgressHUD rq_showErrorTips:error];
  48. }];
  49. }
  50. }];
  51. [_readingQuestionsAndAnswersBtn addBlockForControlEvents:UIControlEventTouchUpInside block:^(UIButton *btn) {
  52. if (_readingQuestionsAndAnswersBtn.selected) {
  53. _readingQuestionsAndAnswersBtn.selected = NO;
  54. [RQ_MUSIC_MANAGER rq_cancelStreamer];
  55. } else {
  56. _readingQuestionsAndAnswersBtn.selected = YES;
  57. [[viewModel.readingQuestionsAndAnswerCommand execute:nil] subscribeNext:^(id _Nullable x) {
  58. if ([x isEqualToString:@"DOUAudioStreamerFinished"]) {
  59. _readingQuestionsAndAnswersBtnisRead = NO;
  60. } else if ([x isEqualToString:@"DOUAudioStreamerPlaying"]) {
  61. _readingQuestionsAndAnswersBtnisRead = YES;
  62. }
  63. }];
  64. }
  65. }];
  66. RAC(_readingQuestionsAndAnswersBtn, selected) = [RACObserve(self, readingQuestionsAndAnswersBtnisRead) takeUntil:self.rac_prepareForReuseSignal];
  67. [_readingQuestionsBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  68. [RQ_MUSIC_MANAGER rq_resetStreamerWithURLString:viewModel.exerciseModel.issuemp3];
  69. }];
  70. _numLabel.text = [NSString stringWithFormat:@"%ld/%ld",_viewModel.exerciseModel.num + 1,(long)_viewModel.exerciseModel.allNum];
  71. }
  72. #pragma mark - SystemMethods
  73. - (void)awakeFromNib {
  74. [super awakeFromNib];
  75. // Initialization code
  76. }
  77. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  78. [super setSelected:selected animated:animated];
  79. // Configure the view for the selected state
  80. }
  81. - (IBAction)lastQuestionBtnAction:(id)sender {
  82. [_viewModel.lastQuestionCommand execute:nil];
  83. }
  84. - (IBAction)nextQuestionBtnAction:(id)sender {
  85. [_viewModel.nextQuestionCommand execute:nil];
  86. }
  87. - (IBAction)showCatalogueBtnAction:(id)sender {
  88. [_viewModel.showCatalogueCommand execute:nil];
  89. }
  90. @end