// // FKCirAllCommentController.m // FirstLink // // Created by jack on 16/6/16. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKCirAllCommentController.h" #import "FKCirAllCommentViewModel.h" #import "PdDetailKeyboardInputView.h" #import "PindanDeleteCommentView.h" #import "FKCirAllCommentBottomView.h" #import "FKCirAllCommentReform.h" #import "FKCirAllCommentRequest.h" #define K_REQ_COMMENT_LIST 222 #define K_REQ_COMMENT_LIST_MORE 223 #define K_REQ_COMMENT 224 #define K_REQ_COMMENT_REPLY 225 #define K_REQ_REMOVE_COMMENT 226 static NSString *CIRCLE_COMMENT_CELL_IDENTIFY = @"CIRCLE_COMMENT_CELL_IDENTIFY"; @interface FKCirAllCommentController () @property (nonatomic, strong) FKCirAllCommentViewModel *viewModel; @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) UIView *coverView; @property (nonatomic, strong) PdDetailKeyboardInputView *boardInputView; // 键盘弹出辅助输入视图 @property (nonatomic, strong) PindanDeleteCommentView *deleteCommentView; // 删除自己的评论 @property (nonatomic, strong) FKCirAllCommentBottomView *bottomView; @end @implementation FKCirAllCommentController - (instancetype)initWithCircleID:(NSString *)circleID{ if (self = [super init]) { self.viewModel.circleID = circleID; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.title = @"所有评论"; [self addAllSubviews]; [self addRefreshControl]; [self registerNotifications]; [self reqDataListWithHud:YES]; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:NO animated:YES]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)addAllSubviews{ [self.view addSubview:self.tableView]; [self.view addSubview:self.bottomView]; [self.view addSubview:self.coverView]; [self.view addSubview:self.boardInputView]; self.boardInputView.frame = CGRectMake(0, UISCREENHEIGH, UISCREENWIDTH, 50); [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.right.left.equalTo(self.view); make.bottom.equalTo(self.bottomView.mas_top); }]; [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.equalTo(self.view); make.height.mas_equalTo(49); }]; [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.insets(UIEdgeInsetsZero); }]; } - (void)reqDataListWithHud:(BOOL)hud{ if (hud) [self.hudView show:YES]; [FKCirAllCommentRequest reqCommentListWithID:self.viewModel.circleID startRow:0 identify:K_REQ_COMMENT_LIST delegate:self]; } - (void)reqMoreData{ [FKCirAllCommentRequest reqCommentListWithID:self.viewModel.circleID startRow:self.viewModel.dataArray.count identify:K_REQ_COMMENT_LIST_MORE delegate:self]; } #pragma mark - response - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header{ [self.refreshControl finishRefreshingDirection:RefreshDirectionTop]; [self.refreshControl finishRefreshingDirection:RefreshDirectionBottom]; if (header.code.intValue == RESPONSE_MSG_NORMAL){ if (identify == K_REQ_COMMENT_LIST){ [self.hudView hide:NO]; self.viewModel.dataArray = [FKCirAllCommentReform parserCommentListWithDict:response]; self.viewModel.pageItem = [FKCirAllCommentReform parserPageItemWithDict:response]; self.viewModel.serveTime = [FKCirAllCommentReform parserServeTimeWithDict:response]; }else if (identify == K_REQ_COMMENT_LIST_MORE){ [self.hudView hide:NO]; NSArray *dataArray = [FKCirAllCommentReform parserCommentListWithDict:response]; self.viewModel.pageItem = [FKCirAllCommentReform parserPageItemWithDict:response]; self.viewModel.serveTime = [FKCirAllCommentReform parserServeTimeWithDict:response]; [self.viewModel appendMoreContent:dataArray]; }else if (identify == K_REQ_COMMENT || identify == K_REQ_COMMENT_REPLY || identify == K_REQ_REMOVE_COMMENT){ [self reqDataListWithHud:NO]; return; } [self.tableView reloadData]; [self resetBottomRefresh]; }else{ [FLProgressHUDHelper showText:header.msg inView:self.view]; } } - (void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header{ [self.hudView hide:NO]; [self.refreshControl finishRefreshingDirection:RefreshDirectionTop]; [self.refreshControl finishRefreshingDirection:RefreshDirectionBottom]; [FLProgressHUDHelper showText:header.msg inView:self.view]; } #pragma mark - tableView dataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.viewModel.dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { FKCirDetailCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:CIRCLE_COMMENT_CELL_IDENTIFY]; [cell fk_configWithViewModel:self.viewModel indexPath:indexPath]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ FKCircleCommentItem *item = [self.viewModel commentItemAtIndex:indexPath.row]; return [FKCirDetailCommentCell cellHeightForCommentText:item.content]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { FKCircleCommentItem *item = [self.viewModel commentItemAtIndex:indexPath.row]; if (item) { self.boardInputView.tag = indexPath.row; if ([item isSelfComment]){ // 删除自己的评论 [self.deleteCommentView showInView:self.view animated:YES]; return; } [self.boardInputView.textView becomeFirstResponder]; } } - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction { if (direction == RefreshDirectionTop) { [self reqDataListWithHud:NO]; } else if (direction==RefreshDirectionBottom) { [self reqMoreData]; } } #pragma mark - keyboard observe - (void)keyboardWillChange:(NSNotification *)notification{ // CGFloat duration = [notification.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] floatValue]; if ([notification.name isEqualToString:UIKeyboardWillShowNotification]) { // 键盘弹出 [self showBoardViewWithNotification:notification]; NSInteger row = [self.tableView indexPathForSelectedRow].row; self.boardInputView.placeHolder = [self.viewModel getCommentPlaceHolderWithRow:row]; }else if ([notification.name isEqualToString:UIKeyboardWillHideNotification]){ // 键盘退出 [self dismissBoardViewWithNotification:notification]; }else if ([notification.name isEqualToString:UIKeyboardDidShowNotification]){ }else if ([notification.name isEqualToString:UIKeyboardDidHideNotification]){ self.boardInputView.textView.text = nil; self.boardInputView.placeHolder = nil; } } #pragma mark - cover View show & dismiss - (void)showBoardViewWithNotification:(NSNotification *)notification;{ CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]; CGRect endRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGRect realEnd = [self.view convertRect:endRect fromView:self.view.window]; UIViewAnimationOptions curve = (UIViewAnimationOptions)[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; self.coverView.alpha = 0; WeakSelf(weakSelf); [UIView animateWithDuration:duration delay:0 options:curve animations:^{ weakSelf.boardInputView.frame = CGRectMake(realEnd.origin.x, realEnd.origin.y - 50, CGRectGetWidth(weakSelf.boardInputView.bounds), CGRectGetHeight(weakSelf.boardInputView.bounds)); weakSelf.coverView.alpha = 1.0f; [weakSelf.view bringSubviewToFront:weakSelf.boardInputView]; } completion:^(BOOL finished) { }]; } - (void)dismissBoardViewWithNotification:(NSNotification *)notification{ CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]; CGRect endRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGRect curEndRect = [self.view convertRect:endRect fromView:self.navigationController.view]; UIViewAnimationOptions curve = (UIViewAnimationOptions)[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; self.coverView.alpha = 1; WeakSelf(weakSelf); [UIView animateWithDuration:duration delay:0 options:curve animations:^{ weakSelf.coverView.alpha = 0; weakSelf.boardInputView.frame = CGRectMake(curEndRect.origin.x, curEndRect.origin.y, CGRectGetWidth(self.boardInputView.bounds), CGRectGetHeight(self.boardInputView.bounds)); } completion:^(BOOL finished) { }]; } #pragma mark - method - (void)addRefreshControl{ [self initRefreshControlWithTableView:self.tableView]; } - (void)resetBottomRefresh{ if (self.viewModel.dataArray.count >= self.viewModel.pageItem.total){ [self.refreshControl setBottomEnabled:NO]; return; } [self.refreshControl setBottomEnabled:YES]; } // 注册通知 - (void)registerNotifications { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardDidHideNotification object:nil]; } #pragma mark - action - (void)coverViewTap{ self.boardInputView.textView.text = nil; [self.boardInputView.textView resignFirstResponder]; [self.boardInputView resignFirstResponder]; } - (void)clickInputViewConfirmBtn{ // 发送评论 NSString *commentContent = self.boardInputView.textView.text; NSInteger commentIndex = self.boardInputView.tag; if (!commentContent || commentContent.length == 0){ [FLProgressHUDHelper showText:@"评论内容不能为空" inView:self.view]; return; } if (commentContent.length > 250){ [FLProgressHUDHelper showText:@"评论内容不能超过250个字符" inView:self.view]; return; } FKCircleCommentItem *item = [self.viewModel commentItemAtIndex:commentIndex]; int reqTyep = commentIndex >= 0 ? K_REQ_COMMENT_REPLY : K_REQ_COMMENT; NSString *userID = commentIndex >= 0 ? item.userID : nil; [self.hudView show:YES]; [FKCirAllCommentRequest reqCommentWithID:self.viewModel.circleID replyToUserID:userID content:commentContent identify:reqTyep delegate:self]; self.boardInputView.textView.text = nil; [self.boardInputView.textView resignFirstResponder]; [self.boardInputView resignFirstResponder]; [self.view endEditing:YES]; } - (void)clickDeleteCommentBtn{ // 删除自己的评论 FKCircleCommentItem *item = [self.viewModel commentItemAtIndex:self.boardInputView.tag]; if (!item) return; [self.hudView show:YES]; [FKCirAllCommentRequest reqRmoveCommentWithID:item.itemID identify:K_REQ_REMOVE_COMMENT delegate:self]; [self.deleteCommentView dismissAnimation]; } - (void)clickCommentBtn{ self.boardInputView.tag = - 1; [self.boardInputView.textView becomeFirstResponder]; } #pragma mark - getter & setter - (FKCirAllCommentViewModel *)viewModel{ if (_viewModel == nil) { _viewModel = [[FKCirAllCommentViewModel alloc]init]; } return _viewModel; } - (FKCirAllCommentBottomView *)bottomView{ if (_bottomView == nil) { _bottomView = [[FKCirAllCommentBottomView alloc]init]; [_bottomView.commentBtn addTarget:self action:@selector(clickCommentBtn) forControlEvents:UIControlEventTouchUpInside]; } return _bottomView; } - (UITableView *)tableView{ if (_tableView == nil) { _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.dataSource = self; _tableView.delegate = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.backgroundColor = UIColorFromRGB(0xf4f4f4); [_tableView registerClass:[FKCirDetailCommentCell class] forCellReuseIdentifier:CIRCLE_COMMENT_CELL_IDENTIFY]; } return _tableView; } - (UIView *)coverView{ if (_coverView == nil) { _coverView = [[UIView alloc]init]; _coverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4f]; _coverView.alpha = 0; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(coverViewTap)]; [_coverView addGestureRecognizer:singleTap]; } return _coverView; } - (PdDetailKeyboardInputView *)boardInputView{ if (_boardInputView == nil) { _boardInputView = [[PdDetailKeyboardInputView alloc]init]; _boardInputView.bounds = CGRectMake(0, 0, UISCREENWIDTH, 50); [_boardInputView.confirmButton addTarget:self action:@selector(clickInputViewConfirmBtn) forControlEvents:UIControlEventTouchUpInside]; } return _boardInputView; } - (PindanDeleteCommentView *)deleteCommentView{ if (_deleteCommentView == nil) { _deleteCommentView = [[PindanDeleteCommentView alloc]init]; [_deleteCommentView.deleteButton addTarget:self action:@selector(clickDeleteCommentBtn) forControlEvents:UIControlEventTouchUpInside]; } return _deleteCommentView; } @end