// // RQExerciseExamToolBarCell.m // SDJK // // Created by 张嵘 on 2021/8/24. // #import "RQExerciseExamToolBarCell.h" @interface RQExerciseExamToolBarCell () @property (nonatomic, readwrite, strong) RQExerciseExamToolBarItemViewModel *viewModel; @property (weak, nonatomic) IBOutlet UILabel *numLabel; @property (nonatomic, readwrite, assign) BOOL readingQuestionsAndAnswersBtnisRead; @end @implementation RQExerciseExamToolBarCell #pragma mark - PublicMethods + (instancetype)cellWithTableView:(UITableView *)tableView { static NSString *ID = @"RQExerciseExamToolBarCell"; RQExerciseExamToolBarCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { cell = [self rq_viewFromXib]; cell.selectionStyle = UITableViewCellSelectionStyleNone; [cell.readingQuestionsAndAnswersBtn layoutButtonWithEdgeInsetsStyle:RQButtonEdgeInsetsStyleTop imageTitleSpace:5.f]; [cell.collectBtn layoutButtonWithEdgeInsetsStyle:RQButtonEdgeInsetsStyleTop imageTitleSpace:5.f]; [cell.readingQuestionsBtn layoutButtonWithEdgeInsetsStyle:RQButtonEdgeInsetsStyleTop imageTitleSpace:5.f]; [cell.lastQuestionBtn layoutButtonWithEdgeInsetsStyle:RQButtonEdgeInsetsStyleTop imageTitleSpace:5.f]; [cell.nextQuestionBtn layoutButtonWithEdgeInsetsStyle:RQButtonEdgeInsetsStyleTop imageTitleSpace:5.f]; } return cell; } - (void)bindViewModel:(RQExerciseExamToolBarItemViewModel *)viewModel { _viewModel = viewModel; RQCollectionModel *collectionModel = [[RQCollectionModel alloc] init]; collectionModel.questionId = viewModel.exerciseModel._id; _collectBtn.selected = [RQ_SDJK_DB_MANAGER isExistWithRQCollectionModel:collectionModel]; [_collectBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) { if (!_collectBtn.selected) { [[RQ_HTTP_Service queryAddCollectionRecordWithQuestionId:viewModel.exerciseModel._id carType:viewModel.homePageCarType subject:viewModel.homePageSubjectType] subscribeNext:^(id _Nullable x) { [RQ_SDJK_DB_MANAGER addCollectionRecordWithRQCollectionModel:collectionModel]; _collectBtn.selected = [RQ_SDJK_DB_MANAGER isExistWithRQCollectionModel:collectionModel]; } error:^(NSError * _Nullable error) { [MBProgressHUD rq_showErrorTips:error]; }]; } else { [[RQ_HTTP_Service queryCancleCollectionRecordWithQuestionIdArr:@[@(viewModel.exerciseModel._id)]] subscribeNext:^(id _Nullable x) { [RQ_SDJK_DB_MANAGER deleteCollectionRecordWithQuestionId:collectionModel.questionId]; _collectBtn.selected = [RQ_SDJK_DB_MANAGER isExistWithRQCollectionModel:collectionModel]; } error:^(NSError * _Nullable error) { [MBProgressHUD rq_showErrorTips:error]; }]; } }]; [_readingQuestionsAndAnswersBtn addBlockForControlEvents:UIControlEventTouchUpInside block:^(UIButton *btn) { if (_readingQuestionsAndAnswersBtn.selected) { _readingQuestionsAndAnswersBtn.selected = NO; [RQ_MUSIC_MANAGER rq_cancelStreamer]; } else { _readingQuestionsAndAnswersBtn.selected = YES; [[viewModel.readingQuestionsAndAnswerCommand execute:nil] subscribeNext:^(id _Nullable x) { if ([x isEqualToString:@"DOUAudioStreamerFinished"]) { _readingQuestionsAndAnswersBtnisRead = NO; } else if ([x isEqualToString:@"DOUAudioStreamerPlaying"]) { _readingQuestionsAndAnswersBtnisRead = YES; } }]; } }]; RAC(_readingQuestionsAndAnswersBtn, selected) = [RACObserve(self, readingQuestionsAndAnswersBtnisRead) takeUntil:self.rac_prepareForReuseSignal]; [_readingQuestionsBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) { [RQ_MUSIC_MANAGER rq_resetStreamerWithURLString:viewModel.exerciseModel.issuemp3]; }]; _numLabel.text = [NSString stringWithFormat:@"%ld/%ld",_viewModel.exerciseModel.num + 1,(long)_viewModel.exerciseModel.allNum]; } #pragma mark - SystemMethods - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (IBAction)lastQuestionBtnAction:(id)sender { [_viewModel.lastQuestionCommand execute:nil]; } - (IBAction)nextQuestionBtnAction:(id)sender { [_viewModel.nextQuestionCommand execute:nil]; } - (IBAction)showCatalogueBtnAction:(id)sender { [_viewModel.showCatalogueCommand execute:nil]; } @end