// // RQExerciseExamToolBarCell.m // SDJK // // Created by 张嵘 on 2021/8/24. // #import "RQExerciseExamToolBarCell.h" @interface RQExerciseExamToolBarCell () @property (nonatomic, readwrite, strong) RQExerciseExamToolBarItemViewModel *viewModel; @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]; } return cell; } - (void)bindViewModel:(RQExerciseExamToolBarItemViewModel *)viewModel { _viewModel = viewModel; RQCollectionModel *collectionModel = [[RQCollectionModel alloc] init]; collectionModel.questionId = viewModel.exerciseModel._id==0?viewModel.exerciseModel.idYdt:viewModel.exerciseModel._id; collectionModel.km = RQ_COMMON_MANAGER.subjectStr; collectionModel.createTime = [NSDate rq_currentTimeSSSInterval]; collectionModel.userId = RQ_USER_MANAGER.currentUserId; _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]; // [RQ_SDJK_DB_MANAGER getAllCollectionModelsCount]; // } error:^(NSError * _Nullable error) { // [MBProgressHUD rq_showErrorTips:error]; // }]; [RQ_SDJK_DB_MANAGER addCollectionRecordWithRQCollectionModel:collectionModel]; if ([RQ_SDJK_DB_MANAGER isExistWithRQCollectionModel:collectionModel]) { self.collectBtn.selected = YES; [QMUITips showSucceed:@"收藏成功"]; [RQ_SDJK_DB_MANAGER getAllCollectionModelsCount]; // [RQNotificationCenter postNotificationName:RQAddCollectNotification object:nil userInfo:@{RQAddCollectExerciseModelKey:self.viewModel.exerciseModel}]; } else { [QMUITips showError:@"收藏失败"]; } } 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]; // [RQ_SDJK_DB_MANAGER getAllCollectionModelsCount]; // } error:^(NSError * _Nullable error) { // [MBProgressHUD rq_showErrorTips:error]; // }]; [RQ_SDJK_DB_MANAGER deleteCollectionRecordWithQuestionId:collectionModel.questionId]; if (![RQ_SDJK_DB_MANAGER isExistWithRQCollectionModel:collectionModel]) { self.collectBtn.selected = NO; [QMUITips showSucceed:@"取消收藏成功"]; [RQ_SDJK_DB_MANAGER getAllCollectionModelsCount]; [RQNotificationCenter postNotificationName:RQCancelCollectNotification object:nil userInfo:@{RQCancelCollectExerciseModelKey:self.viewModel.exerciseModel}]; } else { [QMUITips showError:@"取消收藏失败"]; } } }]; [_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_VOICE_MANAGER playString:viewModel.exerciseModel.issue]; [RQ_MUSIC_MANAGER rq_resetStreamerWithURLString:viewModel.exerciseModel.issuemp3]; }]; } #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 } @end