// // TFCommentsViewController.m // TFReader // // Created by 谢腾飞 on 2020/12/13. // Copyright © 2020 xtfei_2011@126.com. All rights reserved. // #import "TFCommentsViewController.h" #import "TFCommentsViewCell.h" #import "CXTextView.h" @interface TFCommentsViewController () @property (nonatomic ,strong) CXTextView *commentTextView; @property (nonatomic ,strong) UIView *commentBottomView; @property (nonatomic ,strong) TFCommentsModel *commentModel; @property (nonatomic ,assign) CGFloat keyboardHeight; @property (nonatomic ,assign) CGFloat commentViewHeight; @end @implementation TFCommentsViewController - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self setStatusBarDefaultStyle]; } - (void)viewDidLoad { [super viewDidLoad]; [self initialize]; [self createSubviews]; } - (void)initialize { if (self.chapter_id > 0) { if (self.productionType == TFProductionTypeComic) { [self setNavigationBarTitle:TFLocalizedString(@"评论")]; } else { [self setNavigationBarTitle:[NSString stringWithFormat:@"%@ (0)", TFLocalizedString(@"本章评论")]]; } } else { [self setNavigationBarTitle:[NSString stringWithFormat:@"%@ (0)", TFLocalizedString(@"书评")]]; } self.commentViewHeight = 30; NSArray *viewControllers = self.navigationController.viewControllers; if (viewControllers.count <= 1) { UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom]; closeButton.frame = CGRectMake(kHalfMargin, PUB_NAVBAR_OFFSET + 20, 44, 44); closeButton.imageEdgeInsets = UIEdgeInsetsMake(12, 12, 12, 12); [closeButton setImage:[UIImage imageNamed:@"public_close"] forState:UIControlStateNormal]; [closeButton addTarget:self action:@selector(popViewController) forControlEvents:UIControlEventTouchUpInside]; [self setNavigationBarLeftButton:closeButton]; } if (self.pushFromReader) { id target = self.navigationController.interactivePopGestureRecognizer.delegate; UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:target action:NSSelectorFromString(@"handleNavigationTransition:")]; panGesture.delegate = self; [self.view addGestureRecognizer:panGesture]; self.navigationController.interactivePopGestureRecognizer.enabled = NO; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netRequest) name:Notification_Reload_BookDetail object:nil]; // 增加监听,当键盘出现或改变时收出消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotification object:nil]; // 增加监听,当键退出时收出消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotification object:nil]; } - (void)createSubviews { self.mainTableView.delegate = self; self.mainTableView.dataSource = self; [self.view addSubview:self.mainTableView]; [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.top.mas_equalTo(PUB_NAVBAR_HEIGHT); make.width.mas_equalTo(self.view.mas_width); make.height.mas_equalTo(self.view.mas_height).with.offset(- PUB_NAVBAR_HEIGHT - PUB_TABBAR_HEIGHT); }]; self.commentBottomView = [[UIView alloc] init]; self.commentBottomView.backgroundColor = kGrayViewColor; [self.view addSubview:self.commentBottomView]; [self.commentBottomView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.bottom.mas_equalTo(self.view.mas_bottom); make.width.mas_equalTo(self.view.mas_width); make.height.mas_equalTo(PUB_TABBAR_HEIGHT); }]; WS(weakSelf) self.commentTextView = [[CXTextView alloc] initWithFrame:CGRectMake(kHalfMargin, kQuarterMargin, SCREEN_WIDTH - kMargin, 30)]; self.commentTextView.placeholder = TFLocalizedString(@"说点什么吧"); self.commentTextView.maxLine = 5; self.commentTextView.maxLength = 200; self.commentTextView.font = kMainFont; self.commentTextView.backgroundColor = [UIColor whiteColor]; self.commentTextView.layer.cornerRadius = 15; self.commentTextView.textHeightChangeBlock = ^(CGFloat height) { SS(strongSelf) strongSelf.commentViewHeight = height; [weakSelf.commentBottomView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(strongSelf.keyboardHeight + height + kHalfMargin); }]; }; self.commentTextView.returnHandlerBlock = ^{ [weakSelf sendCommentNetRequest]; [[[UIApplication sharedApplication] keyWindow] endEditing:YES]; }; [self.commentBottomView addSubview:self.commentTextView]; self.mainTableView.mj_header = [TFRefreshHeader headerWithRefreshingBlock:^{ weakSelf.currentPageNumber = 1; [weakSelf netRequest]; }]; [self.mainTableView.mj_header beginRefreshing]; self.mainTableView.mj_footer = [TFRefreshFooter footerWithRefreshingBlock:^{ weakSelf.currentPageNumber ++; [weakSelf netRequest]; }]; [self setEmptyOnView:self.mainTableView title:TFLocalizedString(@"暂无评论内容") tapBlock:^{ }]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataSourceArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"TFCommentsViewCell"; TFCommentsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (!cell) { cell = [[TFCommentsViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } cell.commentModel = [self.dataSourceArray objectOrNilAtIndex:indexPath.row]; cell.hiddenEndLine = indexPath.row == self.dataSourceArray.count - 1; if (self.chapter_id > 0) { if (self.productionType == TFProductionTypeComic) { [self setNavigationBarTitle:TFLocalizedString(@"评论")]; } else { [self setNavigationBarTitle:[NSString stringWithFormat:@"%@ (%@)", TFLocalizedString(@"本章评论"), [TFUtilsHelper formatStringWithInteger:self.commentModel.total_count]]]; } } else { [self setNavigationBarTitle:[NSString stringWithFormat:@"%@ (%@)", TFLocalizedString(@"书评"), [TFUtilsHelper formatStringWithInteger:self.commentModel.total_count]]]; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { TFCommentsListModel *t_model = [self.dataSourceArray objectOrNilAtIndex:indexPath.row]; [self.commentTextView textViewBecomeFirstResponder]; self.comment_id = t_model.comment_id; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return CGFLOAT_MIN; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGFLOAT_MIN)]; view.backgroundColor = [UIColor clearColor]; return view; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return CGFLOAT_MIN; } - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGFLOAT_MIN)]; view.backgroundColor = [UIColor clearColor]; return view; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView == self.mainTableView) { [[[UIApplication sharedApplication] keyWindow] endEditing:YES]; } } - (void)setComment_id:(NSInteger)comment_id { _comment_id = comment_id; [self resetCommentTextViewPlaceHolder]; } - (void)resetCommentTextViewPlaceHolder { if (!self.dataSourceArray || self.dataSourceArray.count == 0) { return; } for (TFCommentsListModel *t_model in self.dataSourceArray) { if (t_model.comment_id == self.comment_id) { self.commentTextView.placeholder = [NSString stringWithFormat:@"%@ @%@", TFLocalizedString(@"回复"), [TFUtilsHelper formatStringWithObject:t_model.nickname]]; break; } } } // 当键盘出现或改变时调用 - (void)keyboardWillShow:(NSNotification *)notification { // 获取键盘的高度 self.keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height; [self.commentBottomView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(self.keyboardHeight + self.commentViewHeight + kHalfMargin + kQuarterMargin); }]; [self.commentBottomView.superview layoutIfNeeded]; } // 当键退出时调用 - (void)keyboardWillHide:(NSNotification *)notification { self.keyboardHeight = PUB_TABBAR_OFFSET; [self.commentBottomView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(self.commentViewHeight + PUB_TABBAR_OFFSET + (PUB_TABBAR_HEIGHT - PUB_TABBAR_OFFSET - 30)); }]; [self.commentBottomView.superview layoutIfNeeded]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if (![self.commentBottomView pointInside:[self.commentBottomView convertPoint:[[touches anyObject] locationInView:self.view] fromView:self.view] withEvent:event]) { [[[UIApplication sharedApplication] keyWindow] endEditing:YES]; } } - (void)netRequest { NSString *url = @""; NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; switch (self.productionType) { case TFProductionTypeNovel: url = Book_Comment_List; parameters[@"book_id"] = [TFUtilsHelper formatStringWithInteger:self.production_id] ? : @""; parameters[@"page_num"] = [TFUtilsHelper formatStringWithInteger:self.currentPageNumber]; break; case TFProductionTypeComic: url = Comic_Comment_List; parameters[@"comic_id"] = [TFUtilsHelper formatStringWithInteger:self.production_id] ? : @""; parameters[@"page_num"] = [TFUtilsHelper formatStringWithInteger:self.currentPageNumber]; break; case TFProductionTypeAudio: url = Audio_Comment_List; parameters[@"audio_id"] = [TFUtilsHelper formatStringWithInteger:self.production_id] ? : @""; parameters[@"page_num"] = [TFUtilsHelper formatStringWithInteger:self.currentPageNumber]; break; default: break; } if (self.chapter_id > 0) { [parameters setObject:[TFUtilsHelper formatStringWithInteger:self.chapter_id] ? : @"" forKey:@"chapter_id"]; } WS(weakSelf) [TFNetworkTools POST:url parameters:parameters model:TFCommentsModel.class success:^(BOOL isSuccess, TFCommentsModel *_Nullable t_model, TFNetworkRequestModel *requestModel) { [weakSelf.mainTableView endRefreshing]; if (isSuccess) { if (weakSelf.currentPageNumber == 1) { [weakSelf.mainTableView showRefreshFooter]; [weakSelf.dataSourceArray removeAllObjects]; weakSelf.dataSourceArray = [NSMutableArray arrayWithArray:t_model.list]; } else { [weakSelf.dataSourceArray addObjectsFromArray:t_model.list]; } if (t_model.total_page <= t_model.current_page) { [weakSelf.mainTableView hideRefreshFooter]; } [weakSelf resetCommentTextViewPlaceHolder]; weakSelf.commentModel = t_model; } else { [weakSelf.mainTableView hideRefreshFooter]; } [weakSelf.mainTableView reloadData]; [weakSelf.mainTableView xtfei_endLoading]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { [weakSelf.mainTableView endRefreshing]; [weakSelf.mainTableView xtfei_endLoading]; [weakSelf.mainTableView hideRefreshFooter]; [TFPromptManager showPromptWithError:error defaultText:nil]; }]; } - (void)sendCommentNetRequest { if (!TFUserInfoManager.isLogin) { TFAlertView *alert = [[TFAlertView alloc] init]; alert.alertDetailContent = TFLocalizedString(@"登录后才可以进行评论"); alert.confirmTitle = TFLocalizedString(@"去登录"); alert.cancelTitle = TFLocalizedString(@"暂不"); alert.confirmButtonClickBlock = ^{ [TFLoginOptionsViewController presentLoginView:nil]; }; [alert showAlertView]; return; } if ([self.commentTextView.text isEqualToString:@""]) { return; } NSString *t_text = self.commentTextView.text; self.commentTextView.text = @""; NSString *url = @""; NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; switch (self.productionType) { case TFProductionTypeNovel: url = Book_Comment_Post; parameters = [NSMutableDictionary dictionaryWithDictionary:@{@"book_id":[TFUtilsHelper formatStringWithInteger:self.production_id] ? : @"", @"content":t_text}]; break; case TFProductionTypeComic: url = Comic_Comment_Post; parameters = [NSMutableDictionary dictionaryWithDictionary:@{@"comic_id":[TFUtilsHelper formatStringWithInteger:self.production_id] ? : @"", @"content":t_text}]; break; case TFProductionTypeAudio: url = Audio_Comment_Post; parameters = [NSMutableDictionary dictionaryWithDictionary:@{@"audio_id":[TFUtilsHelper formatStringWithInteger:self.production_id] ? : @"", @"content":t_text}]; break; default: break; } if (self.chapter_id > 0) { [parameters setObject:[TFUtilsHelper formatStringWithInteger:self.chapter_id] forKey:@"chapter_id"]; } if (self.comment_id && self.comment_id != 0) { [parameters setObject:[TFUtilsHelper formatStringWithInteger:self.comment_id] forKey:@"comment_id"]; } WS(weakSelf) [TFNetworkTools POST:url parameters:parameters model:TFCommentsListModel.class success:^(BOOL isSuccess, TFCommentsListModel *_Nullable t_model, TFNetworkRequestModel *requestModel) { if (isSuccess) { weakSelf.comment_id = 0; weakSelf.commentTextView.placeholder = TFLocalizedString(@"说点什么吧"); [TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"评论成功")]; !weakSelf.commentsSuccessBlock ?: weakSelf.commentsSuccessBlock(t_model); if (t_model) { [weakSelf.dataSourceArray insertObject:t_model atIndex:0]; } weakSelf.commentModel.total_count ++; [[NSNotificationCenter defaultCenter] postNotificationName:@"changeComment" object:[NSString stringWithFormat:@"%zd", weakSelf.commentModel.total_count]]; [weakSelf.mainTableView reloadData]; [weakSelf.mainTableView xtfei_endLoading]; } else if (Compare_Json_isEqualTo(requestModel.code, 315)) { weakSelf.comment_id = 0; weakSelf.commentTextView.placeholder = TFLocalizedString(@"说点什么吧"); [TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:requestModel.msg]; } else if (Compare_Json_isEqualTo(requestModel.code, 318)) { weakSelf.commentTextView.text = @""; [TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:requestModel.msg]; } else { weakSelf.commentTextView.text = t_text; [TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:requestModel.msg]; } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { [TFPromptManager showPromptWithError:error defaultText:TFLocalizedString(@"评论失败")]; }]; } - (void)popViewController { [super popViewController]; [self dismissViewControllerAnimated:YES completion:nil]; } @end