// // commentTableController.m // ThePaperHD // // Created by scar1900 on 15/1/16. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "commentTableController.h" #import "commentCell.h" #import "loginPopUpController.h" #import "writeCommentAndAskController.h" #import "otherPersonHomepageController.h" #import "AnswerQuoBaseCell.h" @interface commentTableController () { NSMutableArray *rowHeightList; NSMutableArray *rowDataList; BOOL isCommentSuccess; } @property(nonatomic, strong)UILabel *backLabel; //无数据打底 @property(nonatomic, strong)UIButton *writeCommentBtn; @property(nonatomic, strong)writeCommentAndAskController *writeCommentContentVC; @end @implementation commentTableController @synthesize dataList = _dataList; @synthesize nodeId = _nodeId; @synthesize delegate; @synthesize writeCommentContentVC; @synthesize commentOT; static NSString *commentCellID = @"commentCell"; static NSString *AnswerQuoBaseCellID = @"AnswerQuoBaseCell"; - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithHexString:CardBackGroundColor]; self.view.frame = CGRectMake(0, 0, 1164/2, CGRectGetHeight(self.view.bounds)); self.tableView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)); [self.tableView registerClass:[commentCell class] forCellReuseIdentifier:commentCellID]; [self.tableView registerClass:[AnswerQuoBaseCell class] forCellReuseIdentifier:AnswerQuoBaseCellID]; self.tableView.backgroundColor = [UIColor clearColor]; self.tableView.clipsToBounds = YES; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.contentInset = UIEdgeInsetsZero; isCommentSuccess = NO; [self configCanRefresh:YES canLoad:YES]; self.tableView.refreshControl.frame = CGRectMake(0, self.tableView.refreshControl.frame.origin.y, 1164/2, self.tableView.refreshControl.frame.size.height); self.tableView.refreshControl.originalContentInsectY = 0; [self setLoadFooterFrame:CGRectMake(0, CGRectGetHeight(self.tableView.bounds) ,1164/2 , 60)]; [self.view addSubview:self.backLabel]; [self.view insertSubview:self.backLabel belowSubview:self.tableView]; [self.view addSubview:self.writeCommentBtn]; self.writeCommentBtn.frame = CGRectMake(CGRectGetWidth(self.view.bounds)-135/2, CGRectGetHeight(self.view.bounds)-100, 50, 50); } - (void)setNodeId:(NSString *)str { _nodeId = str; } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; self.tableView.refreshControl.frame = CGRectMake(0, self.tableView.refreshControl.frame.origin.y, 1164/2, self.tableView.refreshControl.frame.size.height); self.tableView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)); self.writeCommentBtn.frame = CGRectMake(CGRectGetWidth(self.view.bounds)-135/2, CGRectGetHeight(self.view.bounds)-100, 50, 50); [self.tableView reloadData]; } - (void)setDataList:(NSMutableArray *)list { if (list.count == 1) { [list insertObject:[NSMutableArray array] atIndex:0]; } _dataList = list; if (list.count == 0) { return; } NSMutableArray *commentTempList = [list lastObject]; NSMutableArray *hotCommentTempList = [list firstObject]; if (commentTempList.count == 0 && hotCommentTempList.count == 0) { self.tableView.hidden = YES; self.backLabel.hidden = NO; }else { self.tableView.hidden = NO; self.backLabel.hidden = YES; } rowHeightList = [NSMutableArray array]; rowDataList = [NSMutableArray array]; if (self.dataList.count == 2 && hotCommentTempList.count > 0) { //热评论 NSMutableArray *hotCommentRowHeightList = [NSMutableArray array]; NSMutableArray *hotCommentRowDataList = [NSMutableArray array]; NSArray *hotCommentList = list[0]; [hotCommentList enumerateObjectsUsingBlock:^(commentObjectVO* obj, NSUInteger idx, BOOL *stop) { NSMutableDictionary *dataDic = setDataModelToDic(obj, [commentObjectVO class]); //评论BO commentObjectVO *commentBO = setJsonDicToDataModel(dataDic, [commentObjectVO class]); NSString *content = commentBO.content; //修正1.6.8的RTLable相关Crash(bug:5925) CGFloat contentHeight = heightForAttributeStringWithLabel(getLineSpaceAttributedString(content, 10, appFont(interactionFontSize, NO)), 930/2, appFont(interactionFontSize, NO)); CGFloat rowHeight = 0; rowHeight = 160/2+contentHeight;//修正1.6.8的RTLable相关Crash(bug:5925) [hotCommentRowDataList addObject:commentBO]; [hotCommentRowHeightList addObject:[NSString stringWithFormat:@"%f",rowHeight]]; //盖楼BO if (obj.quoteInfo.count>0 ) { commentObjectVO *quoComment = setJsonDicToDataModel(dataDic, [commentObjectVO class]); if (quoComment.quoteInfo && quoComment.quoteInfo.allKeys.count > 0) { commentObjectVO *quoInfo = setJsonDicToDataModel(quoComment.quoteInfo, [commentObjectVO class]); quoInfo.type = @"1000"; CGFloat quoHeight = 50/2+10+15+15; CGFloat quoLabelHeight = heightForAttributeStringWithLabel(getLineSpaceAttributedString(quoInfo.content, 10, appFont(interactionFontSize, NO)), 930/2, appFont(interactionFontSize, NO)); CGFloat twoLine = twoLineheight; quoInfo.labelHeight = quoLabelHeight; if (quoLabelHeight > twoLine) { quoLabelHeight = twoLine; quoHeight += 15; } quoHeight += quoLabelHeight; [hotCommentRowDataList addObject:quoInfo]; [hotCommentRowHeightList addObject:[NSString stringWithFormat:@"%f",quoHeight]]; } } }]; [rowDataList addObject:hotCommentRowDataList]; [rowHeightList addObject:hotCommentRowHeightList]; } if (commentTempList.count > 0) { NSMutableArray *commentRowHeightList = [NSMutableArray array]; NSMutableArray *commentRowDataList = [NSMutableArray array]; NSArray *commentList = [list lastObject]; [commentList enumerateObjectsUsingBlock:^(commentObjectVO* obj, NSUInteger idx, BOOL *stop) { NSMutableDictionary *dataDic = setDataModelToDic(obj, [commentObjectVO class]); //评论BO commentObjectVO *commentBO = setJsonDicToDataModel(dataDic, [commentObjectVO class]); NSString *content = commentBO.content; //修正1.6.8的RTLable相关Crash(bug:5925) CGFloat contentHeight = heightForAttributeStringWithLabel(getLineSpaceAttributedString(content, 10, appFont(interactionFontSize, NO)), 930/2, appFont(interactionFontSize, NO)); CGFloat rowHeight = 0; rowHeight = 160/2+contentHeight;//修正1.6.8的RTLable相关Crash(bug:5925) [commentRowDataList addObject:commentBO]; [commentRowHeightList addObject:[NSString stringWithFormat:@"%f",rowHeight]]; //盖楼BO if (obj.quoteInfo.count > 0) { commentObjectVO *quoComment = setJsonDicToDataModel(dataDic, [commentObjectVO class]); if (quoComment.quoteInfo && quoComment.quoteInfo.allKeys.count > 0) { commentObjectVO *quoInfo = setJsonDicToDataModel(quoComment.quoteInfo, [commentObjectVO class]); quoInfo.type = @"1000"; CGFloat quoHeight = 50/2+10+15+15; CGFloat quoLabelHeight = heightForAttributeStringWithLabel(getLineSpaceAttributedString(quoInfo.content, 10, appFont(interactionFontSize, NO)), 930/2, appFont(interactionFontSize, NO)); CGFloat twoLine = twoLineheight; quoInfo.labelHeight = quoLabelHeight; if (quoLabelHeight > twoLine) { quoLabelHeight = twoLine; quoHeight += 15; } quoHeight += quoLabelHeight; [commentRowDataList addObject:quoInfo]; [commentRowHeightList addObject:[NSString stringWithFormat:@"%f",quoHeight]]; } } }]; [rowDataList addObject:commentRowDataList]; [rowHeightList addObject:commentRowHeightList]; } [self.tableView reloadData]; } - (UILabel*)backLabel { if (!_backLabel) { _backLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.bounds)/2-230/2, CGRectGetHeight(self.view.bounds)/2-45/2, 230, 45)]; _backLabel.backgroundColor = [UIColor clearColor]; _backLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; _backLabel.textAlignment = NSTextAlignmentCenter; _backLabel.text = @"评论一下吧"; _backLabel.autoresizingMask = AutoresizingAuto; } _backLabel.font = appFont(20, NO); return _backLabel; } - (UIButton*)writeCommentBtn { if (!_writeCommentBtn) { _writeCommentBtn = [UIButton buttonWithType:UIButtonTypeCustom]; if ([[TPUserDefault instance].isNightMode intValue] ==1) {//夜间模式下,进入新闻详情页,左划进入到追问页,评论按钮底色不对(bug:4806) [_writeCommentBtn setImage:Image(@"Button/writeComment_night.png") forState:UIControlStateNormal]; }else{ [_writeCommentBtn setImage:Image(@"Button/writeComment.png") forState:UIControlStateNormal]; } [_writeCommentBtn setImage:Image(@"Button/writeComment_h.png") forState:UIControlStateHighlighted]; [_writeCommentBtn addTarget:self action:@selector(writeComment:) forControlEvents:UIControlEventTouchUpInside]; } return _writeCommentBtn; } - (void)writeComment:(UIButton*)btn { if (![TPUserDefault instance].userBO) { loginPopUpController *loginVC = loginPopUpController.new; [self presentController:loginVC animated:YES presentSize:loginPopUpSize completion:^{ } dismiss:^{ if ([TPUserDefault instance].userBO) { self.writeCommentContentVC = [[writeCommentAndAskController alloc]init]; self.writeCommentContentVC.type = commentType; self.writeCommentContentVC.nodeId = self.nodeId; self.writeCommentContentVC.delegate = self; self.writeCommentContentVC.commentOT = self.commentOT; [self presentController:self.writeCommentContentVC animated:YES presentSize:writeAndAskPopSize completion:^{ } dismiss:^{ self.writeCommentContentVC = nil; } tapHandler:^{ [self.writeCommentContentVC closeKeyBoard]; }]; } }]; }else { self.writeCommentContentVC = [[writeCommentAndAskController alloc]init]; self.writeCommentContentVC.type = commentType; self.writeCommentContentVC.nodeId = self.nodeId; self.writeCommentContentVC.delegate = self; self.writeCommentContentVC.commentOT = self.commentOT; [self presentController:self.writeCommentContentVC animated:YES presentSize:writeAndAskPopSize completion:^{ } dismiss:^{ self.writeCommentContentVC = nil; } tapHandler:^{ [self.writeCommentContentVC closeKeyBoard]; }]; } } #pragma mark UITableViewDelegate and UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { NSMutableArray *commentTempList = [self.dataList lastObject]; NSMutableArray *hotCommentTempList = [self.dataList firstObject]; if (commentTempList.count > 0 && hotCommentTempList.count > 0) { return 2; }else if (commentTempList.count == 0 && hotCommentTempList.count > 0) { return 1; }else if (commentTempList.count > 0 && hotCommentTempList.count == 0) { return 1; }else return 0; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSArray *commentList = rowHeightList[section]; return commentList.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSArray *heightList = rowHeightList[indexPath.section]; return heightList ? [heightList[indexPath.row] floatValue]:0; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (self.dataList.count == 2 && rowHeightList.count == 2) { return 30+15; }else if (self.dataList.count == 2 && rowHeightList.count == 1) { NSMutableArray *commentTempList = [self.dataList lastObject]; if (commentTempList.count == 0) { return 30+15; }else return 0; }else return 0; } - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (self.dataList.count == 2) { UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 45)]; view.backgroundColor = [UIColor colorWithHexString:CardBackGroundColor]; UIView *colorView = [[UIView alloc]initWithFrame:CGRectMake(10, 5+15, 5, 20)]; colorView.backgroundColor = [UIColor colorWithHexString:BLUECOLOR]; [view addSubview:colorView]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(colorView.frame)+10, 15, CGRectGetWidth(self.tableView.frame), 30)]; label.font = appFont(20, NO); label.textColor = [UIColor colorWithHexString:TextBlack]; label.backgroundColor = [UIColor clearColor]; [view addSubview:label]; NSMutableArray *commentTempList = [self.dataList lastObject]; NSMutableArray *hotCommentTempList = [self.dataList firstObject]; if (section == 0) { if (hotCommentTempList.count >0 ) { label.text = @"热评论"; }else if (hotCommentTempList.count == 0 && commentTempList.count >0) { label.text = @"新评论"; }else return nil; }else { if (commentTempList.count == 0) { return nil; } label.text = @"新评论"; } return view; }else { return nil; } } - (commentCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { // NSMutableArray *realDataList = [NSMutableArray array]; // [self.dataList enumerateObjectsUsingBlock:^(NSMutableArray* obj, NSUInteger idx, BOOL *stop) { // if (obj.count > 0) { // [realDataList addObject:obj]; // } // }]; NSArray *commentList = rowDataList[indexPath.section]; commentObjectVO *commentBO = commentList[indexPath.row]; if ([commentBO.type intValue] == 1000) { // return [self getQuoCell:table cellForRowAtIndexPath:indexPath]; return [self getQuoCell:table cellForRowAtIndexPath:indexPath]; }else{ return [self getCommentCell:table cellForRowAtIndexPath:indexPath]; } } - (commentCell *)getCommentCell:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { commentCell* cell = (commentCell*)[table dequeueReusableCellWithIdentifier:commentCellID]; if (nil == cell) { cell = [[commentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:commentCellID]; } // NSMutableArray *realDataList = [NSMutableArray array]; // [self.dataList enumerateObjectsUsingBlock:^(NSMutableArray* obj, NSUInteger idx, BOOL *stop) { // if (obj.count > 0) { // [realDataList addObject:obj]; // } // }]; // NSArray *commentList = realDataList[indexPath.section]; NSArray *commentList = rowDataList[indexPath.section]; commentObjectVO *commentBO = commentList[indexPath.row]; cell.commentBO = commentBO; cell.delegate = self; cell.indexPath = indexPath; return cell; } - (AnswerQuoBaseCell *)getQuoCell:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { AnswerQuoBaseCell* cell = (AnswerQuoBaseCell*)[table dequeueReusableCellWithIdentifier:AnswerQuoBaseCellID]; if (nil == cell) { cell = [[AnswerQuoBaseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AnswerQuoBaseCellID]; } NSArray *commentList = rowDataList[indexPath.section]; commentObjectVO *commentBO = commentList[indexPath.row]; cell.isMe = NO; cell.commentBO = commentBO; cell.delegate = self; __block NSIndexPath *IndexPath = indexPath; __weak typeof(self) Self = self; [cell quoBlock:^(BOOL isQuoExpand, CGFloat quoHeight) { CGFloat backY = [cell.quoContentLabel convertRect:cell.quoContentLabel.bounds toView:KEY_WINDOW].origin.y-200; if (isQuoExpand) { quoHeight = quoHeight+50/2+10+15+15; NSMutableArray *rowHeight = rowHeightList[IndexPath.section]; [rowHeight replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",quoHeight]]; [rowHeightList replaceObjectAtIndex:IndexPath.section withObject:rowHeight]; NSArray *array = @[IndexPath]; [Self.tableView beginUpdates]; [Self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade]; [Self.tableView endUpdates]; }else { CGFloat two = (appFont(TEXT_FOUR_LEVELSIZE, NO).lineHeight+10)*2; quoHeight = two+50/2+10+15+15+15; NSMutableArray *rowHeight = rowHeightList[IndexPath.section]; [rowHeight replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",quoHeight]]; [rowHeightList replaceObjectAtIndex:IndexPath.section withObject:rowHeight]; NSArray *array = @[IndexPath]; [Self.tableView beginUpdates]; [Self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade]; [Self.tableView endUpdates]; if (backY < 0) { [Self.tableView beginUpdates]; [Self.tableView scrollToRowAtIndexPath:IndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; [Self.tableView endUpdates]; } } }]; return cell; } #pragma mark - cell delegate - (void)hasPraisedComment:(NSIndexPath *)indexPath withCommentBO:(commentObjectVO *)commentBO { NSMutableArray *realDataList = [NSMutableArray array]; __block NSInteger replaceIndex = 0; [self.dataList enumerateObjectsUsingBlock:^(NSMutableArray* obj, NSUInteger idx, BOOL *stop) { if (obj.count > 0) { replaceIndex = idx; [realDataList addObject:obj]; } }]; NSMutableArray *array = [NSMutableArray arrayWithArray:realDataList[indexPath.section]]; [array replaceObjectAtIndex:indexPath.row withObject:commentBO]; if (self.dataList.count ==2 ) { [self.dataList replaceObjectAtIndex:indexPath.section withObject:array]; }else { [self.dataList replaceObjectAtIndex:replaceIndex withObject:array]; } } - (void)commentBackForUser:(commentObjectVO *)commentBO { if (![TPUserDefault instance].userBO) { loginPopUpController *loginVC = loginPopUpController.new; [self presentController:loginVC animated:YES presentSize:loginPopUpSize completion:^{ } dismiss:^{ if ([TPUserDefault instance].userBO) { self.writeCommentContentVC = [[writeCommentAndAskController alloc]init]; self.writeCommentContentVC.type = commentForUserType; self.writeCommentContentVC.nodeId = self.nodeId; self.writeCommentContentVC.delegate = self; self.writeCommentContentVC.commentBO = commentBO; self.writeCommentContentVC.commentOT = self.commentOT; [self presentController:self.writeCommentContentVC animated:YES presentSize:writeAndAskPopSize completion:^{ } dismiss:^{ self.writeCommentContentVC = nil; } tapHandler:^{ [self.writeCommentContentVC closeKeyBoard]; }]; } }]; }else { self.writeCommentContentVC = [[writeCommentAndAskController alloc]init]; self.writeCommentContentVC.type = commentForUserType; self.writeCommentContentVC.nodeId = self.nodeId; self.writeCommentContentVC.delegate = self; self.writeCommentContentVC.commentBO = commentBO; self.writeCommentContentVC.commentOT = self.commentOT; [self presentController:self.writeCommentContentVC animated:YES presentSize:writeAndAskPopSize completion:^{ } dismiss:^{ self.writeCommentContentVC = nil; } tapHandler:^{ [self.writeCommentContentVC closeKeyBoard]; }]; } } - (void)deleteCommentSuccess:(NSIndexPath *)indexPath { [self manualRefresh]; } -(void)gotoUserInfo:(commentObjectVO *)commentBO{ otherPersonHomepageController *otherVC = [otherPersonHomepageController new]; otherVC.commentBo = commentBO; [self.navigationController pushViewController:otherVC animated:YES]; } #pragma mark - refresh and load handler - (void)pullRefreshHandler { if (!self.nodeId) { TPLOG(@"数据有问题,无法获取新数据"); [self endRefresh]; if (!self.tableView.userInteractionEnabled) {// 弱网络环境下,新闻详情页的评论和提问提交后有异常(bug:4747) self.tableView.userInteractionEnabled = YES; } return; } NSDictionary *dic = @{@"c":self.nodeId}; [Remote doJsonAction:1 requestUrl:contentCommentURL parameter:dic delegate:self]; } - (void)pullLoadMoreHander { if (!self.nextUrl) { TPLOG(@"数据有问题,无法获取新数据"); [self loadSuccess]; return; } [Remote doJsonAction:2 requestUrl:self.nextUrl parameter:nil delegate:self]; } #pragma mark - remote handler - (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData { if (actionTag == 1) { if (responsData[@"commentList"]) { NSMutableArray *commentList = [NSMutableArray array]; NSMutableArray *tempArray = [NSMutableArray array]; NSMutableArray *hotList = [NSMutableArray array]; NSArray *list = responsData[@"commentList"]; [list enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) { commentObjectVO *commentBO = setJsonDicToDataModel(obj, [commentObjectVO class]); [tempArray addObject:commentBO]; }]; NSArray *hotTempList = responsData[@"hotList"]; [hotTempList enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) { commentObjectVO *commentBO = setJsonDicToDataModel(obj, [commentObjectVO class]); [hotList addObject:commentBO]; }]; if (hotList.count > 0) { [commentList addObject:hotList]; } [commentList addObject:tempArray]; self.dataList = commentList; NSString *url = responsData[@"nextUrl"]; self.nextUrl = url; } [self endRefresh]; if (!self.tableView.userInteractionEnabled) {// 弱网络环境下,新闻详情页的评论和提问提交后有异常(bug:4747) self.tableView.userInteractionEnabled = YES; } if (isCommentSuccess) { NSArray *hotTempList = responsData[@"hotList"]; if (hotTempList.count > 0) { [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:YES]; } isCommentSuccess = NO; } NSString *record = responsData[@"recordTotal"]; if ([self.delegate respondsToSelector:@selector(endCommentRefresh:)]) { [self.delegate endCommentRefresh:record]; } }else { NSMutableArray *tempCommentList= [NSMutableArray arrayWithArray:self.dataList]; NSMutableArray *commentList = [tempCommentList lastObject]; NSMutableArray *tempArray = [NSMutableArray array]; NSArray *list = responsData[@"commentList"]; [list enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) { commentObjectVO *commentBO = setJsonDicToDataModel(obj, [commentObjectVO class]); [tempArray addObject:commentBO]; }]; [commentList addObjectsFromArray:tempArray]; if (tempCommentList.count == 2) { [tempCommentList replaceObjectAtIndex:1 withObject:commentList]; }else { [tempCommentList replaceObjectAtIndex:0 withObject:commentList]; } self.dataList = tempCommentList; // NSMutableArray *indexArray = [NSMutableArray array]; // for (int i = tempCommentList.count;i