// // myAsksController.m // ThePaperHD // // Created by scar1900 on 15/3/30. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "messageAsksController.h" #import "messageAskContentCell.h" #import "messageAnswerCell.h" #import "messageTopCell.h" #import "KGModal.h" #import "messageViewModel.h" @interface messageAsksController () @property(nonatomic, strong)UIButton *closeBtn; @property(nonatomic, strong)UILabel *titleLabel; @property(nonatomic, strong)UIView *backView; @property(nonatomic, strong)NSMutableArray *nextDataList; @property(nonatomic, strong)writeCommentAndAskController *writeVC; @property(nonatomic, strong)messageViewModel *model; @end @implementation messageAsksController @synthesize qaList = _qaList; @synthesize qaDataSource; @synthesize qaHeightArray; static NSString *messageAskCellID = @"messageAskCell"; static NSString *messageAnswerCellID = @"messageAnswerCell"; static NSString *messageTopCellID = @"messageTopCell"; - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor clearColor]; self.navigationController.navigationBar.hidden = YES; [self.view addSubview:self.closeBtn]; [self.view addSubview:self.titleLabel]; self.closeBtn.frame = CGRectMake(hotAskListPopUpSize.width/2 - messagePopSize.width/2, 0, 50, 80); [self.closeBtn setImageEdgeInsets:UIEdgeInsetsMake(15, 0, 15, 0)]; self.titleLabel.frame = CGRectMake(0, 0, hotAskListPopUpSize.width, 80); self.backView.frame = CGRectMake(hotAskListPopUpSize.width/2 - messagePopSize.width/2, 80, messagePopSize.width, messagePopSize.height-80); [self.view addSubview:self.backView]; [self.view addSubview:self.noDataBack]; self.tableView.frame =self.backView.bounds; self.tableView.clipsToBounds = YES; self.tableView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; self.tableView.separatorStyle = UITableViewCellSelectionStyleNone; [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.backView addSubview:self.tableView]; // NSDictionary *dic = @{@"ctype":@(2)}; self.model = [messageViewModel new]; self.model.delegate = self; // self.model.myAskDic = dic; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self manualRefresh]; }); [self.tableView registerClass:[messageAskContentCell class] forCellReuseIdentifier:messageAskCellID]; [self.tableView registerClass:[messageAnswerCell class] forCellReuseIdentifier:messageAnswerCellID]; [self.tableView registerClass:[messageTopCell class] forCellReuseIdentifier:messageTopCellID]; } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [KGModal sharedInstance].tapOutsideToDismiss = NO; } - (UIView*)noDataBack { if (!_noDataBack) { _noDataBack = [[UIView alloc]initWithFrame:CGRectMake(hotAskListPopUpSize.width/2 - messagePopSize.width/2, 80, messagePopSize.width, CGRectGetHeight(self.backView.bounds))]; _noDataBack.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; _noDataBack.hidden = YES; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, messagePopSize.width, 30)]; label.textColor = [UIColor colorWithHexString:TextLightGray]; label.text = @"没提过问题"; label.textAlignment = NSTextAlignmentCenter; label.backgroundColor = [UIColor clearColor]; [_noDataBack addSubview:label]; UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(messagePopSize.width/2 - 357/4, CGRectGetMaxY(label.frame)+20, 357/2, 219/2)]; img.image = Image(@"setting/message_noask.png"); [_noDataBack addSubview:img]; TPCustomButton *gotoAsk = [[TPCustomButton alloc] initWithFrame:CGRectMake(messagePopSize.width/2-300/2,CGRectGetMaxY(img.frame)+50, 300, 40)]; [gotoAsk setTitle:@"去问吧" forState:UIControlStateNormal]; gotoAsk.titleLabel.textColor = [UIColor colorWithHexString:BackGroundColor]; gotoAsk.titleLabel.font = appFont(16, NO); [gotoAsk setBackgroundColor:[UIColor colorWithHexString:BLUECOLOR]]; [gotoAsk addTarget:self action:@selector(gotoAskSelector:) forControlEvents:UIControlEventTouchUpInside]; [_noDataBack addSubview:gotoAsk]; } return _noDataBack; } - (void)setQaList:(NSMutableArray *)list { _qaList = list; if ([self checkIfNoData:list]) { return; } self.qaDataSource = [NSMutableArray array]; self.qaHeightArray = [NSMutableArray array]; [self orgData:self.qaList]; [self.tableView reloadData]; } -(void)setNextDataList:(NSMutableArray *)list{ [self orgData:list]; [self.tableView reloadData]; } -(void) orgData:(NSMutableArray *) list{ [list enumerateObjectsUsingBlock:^(commentObjectVO* obj, NSUInteger idx, BOOL *stop) { NSString *topTitle; if([obj.objectType intValue] == 3){ topTitle = [NSString stringWithFormat:@"【原话题】%@",obj.contName]; }else{ topTitle = [NSString stringWithFormat:@"【原新闻】%@",obj.contName]; } CGFloat topHeight = heightForAttributeStringWithLabel(getLineSpaceAttributedString(topTitle, 2, appFont(TEXT_FIVE_LEVELSIZE, NO)), messagePopSize.width-30, appFont(TEXT_FIVE_LEVELSIZE, NO)); [self.qaDataSource addObject:topTitle]; [self.qaHeightArray addObject:@(topHeight+23)]; CGFloat titleHeight = heightForAttributeStringWithLabel(getLineSpaceAttributedString(obj.content, 10, appFont(interactionFontSize, NO)), messagePopSize.width-30, appFont(interactionFontSize, NO)); obj.labelHeight = titleHeight; [self.qaDataSource addObject:obj]; titleHeight = titleHeight+60; NSArray *qaArray = obj.answerList; if (qaArray.count >0) { [self.qaHeightArray addObject:[NSString stringWithFormat:@"%f",titleHeight]]; }else{ [self.qaHeightArray addObject:[NSString stringWithFormat:@"%f",titleHeight+16]]; } [qaArray enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger index, BOOL *isStop) { commentObjectVO *anserBO = setJsonDicToDataModel(dic, [commentObjectVO class]); anserBO.contId = obj.contId; CGFloat contentHeight = 50; //提问者姓名+空行 CGFloat answerContent = heightForAttributeStringWithLabel(getLineSpaceAttributedString(anserBO.content, 10, appFont(interactionFontSize, NO)), messagePopSize.width-30, appFont(interactionFontSize, NO)); anserBO.labelHeight = answerContent; if (answerContent > interaction4LinesSpace) { answerContent = interaction4LinesSpace + 25; } contentHeight = contentHeight + answerContent+30; [self.qaDataSource addObject:anserBO]; [self.qaHeightArray addObject:[NSString stringWithFormat:@"%f",contentHeight]]; }]; }]; } - (BOOL)checkIfNoData:(NSArray*)list { if (list.count > 0) { self.tableView.hidden =NO; self.noDataBack.hidden = YES; return NO; }else { self.tableView.hidden =YES; self.noDataBack.hidden =NO; return YES; } } #pragma mark - refresh and load handler - (void)pullRefreshHandler { NSDictionary *dic = @{@"ctype":@(2)}; self.model.myAskDic = dic; } - (void)pullLoadMoreHander { if (!self.nextUrl) { TPLOG(@"数据有问题,无法获取新数据"); [self loadSuccess]; return; } self.model.myAskNextUrl = self.nextUrl; // [Remote doJsonAction:2 // requestUrl:self.nextUrl // parameter:nil // delegate:self]; } #pragma mark -- view - (UIButton*)closeBtn { if (!_closeBtn) { _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_closeBtn setImage:Image(@"login/popUpBack.png") forState:UIControlStateNormal]; [_closeBtn setImage:Image(@"login/popUpBack_h.png") forState:UIControlStateHighlighted]; [_closeBtn addTarget:self action:@selector(closeEvent:) forControlEvents:UIControlEventTouchUpInside]; } return _closeBtn; } - (UILabel*)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _titleLabel.backgroundColor = [UIColor clearColor]; _titleLabel.textAlignment = NSTextAlignmentCenter; _titleLabel.text = @"我的提问"; _titleLabel.textColor = [UIColor whiteColor]; _titleLabel.font = appFont(30, NO); } return _titleLabel; } - (UIView*)backView { if (!_backView) { _backView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame),messagePopSize.width, messagePopSize.height-80)]; _backView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; } return _backView; } #pragma mark - btn event - (void)closeEvent:(UIButton*)btn { [(MLNavigationController*)self.navigationController popViewControllerWithFlip]; } -(void)gotoAskSelector:(UIButton*) btn{ if ([self.myAsksDelegate respondsToSelector:@selector(msgToTopicList)]) { [self.myAsksDelegate msgToTopicList]; } } #pragma mark - table view delegate and datasource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.qaDataSource?self.qaDataSource.count:0; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [self.qaHeightArray[indexPath.row] floatValue]; } - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { id data =self.qaDataSource[indexPath.row]; commentObjectVO *commentBO = data; if ([data isKindOfClass:[NSString class]]) { return [self getMessageTopCell:tableView cellForRowAtIndexPath:indexPath]; }else{ if ([commentBO.type intValue] == 2) { return [self getAskContentCell:tableView cellForRowAtIndexPath:indexPath]; //问题(新追问) }else { return [self getAnserContentCell:tableView cellForRowAtIndexPath:indexPath]; //回答(新追问) } } } - (messageTopCell*)getMessageTopCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { messageTopCell *cell = [tableView dequeueReusableCellWithIdentifier:messageTopCellID]; if (nil == cell) { cell = [[messageTopCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageTopCellID]; } cell.topTitle = self.qaDataSource[indexPath.row]; return cell; } //回答详情 - (messageAnswerCell*)getAnserContentCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { messageAnswerCell *cell = (messageAnswerCell*)[tableView dequeueReusableCellWithIdentifier:messageAnswerCellID]; if (nil == cell) { cell = [[messageAnswerCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageAnswerCellID]; } cell.commentBO = self.qaDataSource[indexPath.row]; cell.delegate = self; cell.indexPath = indexPath; cell.isHaveCopyMenu = YES; cell.isNeedTizhu = NO; cell.isNeedAsImg = YES; __block NSIndexPath* IndexPath = indexPath; __weak typeof(self) Self = self; [cell expandCell:^(BOOL isExpand, CGFloat cellHeight) { if (isExpand) { CGFloat offset = cellHeight - 100; NSString *qaAllHeightStr = Self.qaHeightArray[IndexPath.row]; CGFloat qaAllHeight = [qaAllHeightStr floatValue]+offset; [Self.qaHeightArray replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",qaAllHeight]]; NSArray *array = @[IndexPath]; [Self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationAutomatic]; }else { CGFloat offset = cellHeight - 100; NSString *qaAllHeightStr = Self.qaHeightArray[IndexPath.row]; CGFloat qaAllHeight = [qaAllHeightStr floatValue]-offset; [Self.qaHeightArray replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",qaAllHeight]]; NSArray *array = @[IndexPath]; [Self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationAutomatic]; [Self.tableView scrollToRowAtIndexPath:IndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; } }]; return cell; } //问题内容 - (messageAskContentCell*)getAskContentCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { messageAskContentCell *cell = (messageAskContentCell*)[tableView dequeueReusableCellWithIdentifier:messageAskCellID]; if (nil == cell) { cell = [[messageAskContentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageAskCellID]; } cell.commentBO = self.qaDataSource[indexPath.row]; cell.askContentDelegate = self; cell.isHaveMenu = NO; cell.timeHidden = NO; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if ([cell isKindOfClass:[messageAskContentCell class]]) { commentObjectVO* commentData = self.qaDataSource[indexPath.row]; hotAskListHomeController *hotAskVC = [[hotAskListHomeController alloc]init]; hotAskVC.commentBO = commentData; hotAskVC.isPush = YES; hotAskVC.closeOrBack = NO; hotAskVC.delegate = self; [(MLNavigationController*)self.navigationController pushViewControllerWithAnimateFlip:hotAskVC]; commentData.unNums = @"0"; [self.qaDataSource replaceObjectAtIndex:indexPath.row withObject:commentData]; [self.tableView reloadData]; }else if([cell isKindOfClass:[messageTopCell class]]){ commentObjectVO* comment = self.qaDataSource[indexPath.row +1]; if([comment.objectType intValue] ==3 ){ TopicInfoBO *topic = [[TopicInfoBO alloc]init]; topic.topicId = comment.contId; NSDictionary *dic = @{@"data":topic}; if ([self.myAsksDelegate respondsToSelector:@selector(msgAskToTopic:)]) { [self.myAsksDelegate msgAskToTopic:dic]; } }else{ comment.forwordType = comment.objInfo[@"forwordType"]; NSDictionary *dic = @{@"data":comment}; if ([self.myAsksDelegate respondsToSelector:@selector(msgAskToNews:)]) { [self.myAsksDelegate msgAskToNews:dic]; } } } } #pragma mark -- delegate - (void)goToOriginContent:(objInfoBO *)commentBO presentedController:(TPHttpController *)presentedController { NSDictionary *dic = @{@"data":commentBO}; if ([self.myAsksDelegate respondsToSelector:@selector(msgAskToNews:)]) { [self.myAsksDelegate msgAskToNews:dic]; } } -(void)hotAskHomeToTopic:(TopicInfoBO *)topic user:(userBO *)user{ // NSDictionary *dic = @{@"data":topic,@"user":user}; //我的消息:对一个话题自问自答,在话题的问答详情页点击进入原话题,提示此话题已下线(bug:5813) NSDictionary *dic = @{@"data":topic}; if ([self.myAsksDelegate respondsToSelector:@selector(msgAskToTopic:)]) { [self.myAsksDelegate msgAskToTopic:dic]; } } -(void)gotoPersonInfo:(commentObjectVO *)comment{ if ([self.myAsksDelegate respondsToSelector:@selector(gotoUserGambit:)]) { [self.myAsksDelegate gotoUserGambit:comment]; } } -(void)gotoAnswer:(commentObjectVO *)comment{ self.writeVC = [[writeCommentAndAskController alloc]init]; self.writeVC.type = askForUserType; self.writeVC.nodeId = comment.contId; self.writeVC.delegate = self; self.writeVC.commentBO = comment; self.writeVC.isPush = YES; if ([comment.objectType isEqualToString:@"3"]) {//0004269: 个人主页: 通过头像进入个人主页,对列表中的评论、话题的提问进行回复时提示不存在或下线的提示信息(bug:4269) self.writeVC.commentOT = @"3"; }else{ self.writeVC.commentOT = @"1"; } [(MLNavigationController*)self.navigationController pushViewControllerWithAnimateFlip:self.writeVC]; } - (void)commentSuccess:(commentAndAskType)type { [(MLNavigationController*)self.navigationController popViewControllerWithFlip]; self.writeVC = nil; self.closeBtn.hidden = NO; [self manualRefresh]; } -(void)writeConetentPopBack{ [(MLNavigationController*)self.navigationController popViewControllerWithFlip]; self.writeVC = nil; self.closeBtn.hidden = NO; [self enablePopBackTap]; } - (void)answerForUserBack:(commentObjectVO *)commentBO closeHidden:(BOOL)isHidden{ self.writeVC = [[writeCommentAndAskController alloc]init]; self.writeVC.type = askForUserType; self.writeVC.nodeId = commentBO.contId; self.writeVC.delegate = self; self.writeVC.commentBO = commentBO; self.writeVC.isPush = YES; if ([commentBO.objectType isEqualToString:@"3"]) {//0004269: 个人主页: 通过头像进入个人主页,对列表中的评论、话题的提问进行回复时提示不存在或下线的提示信息(bug:4269) self.writeVC.commentOT = @"3"; }else{ self.writeVC.commentOT = @"1"; } [(MLNavigationController*)self.navigationController pushViewControllerWithAnimateFlip:self.writeVC]; } -(void)hasPraisedAnswer:(NSIndexPath *)indexPath withCommentBO:(commentObjectVO *)commentBO{ [self.qaDataSource replaceObjectAtIndex:indexPath.row withObject:commentBO]; } #pragma mark -- view model delegate -(void)returnMyAskList:(NSMutableArray *)data nextUrl:(NSString *)url{ self.qaList = data; self.nextUrl = url; [self endRefresh]; } -(void)returnMyAskNextUrl:(NSMutableArray *)data nextUrl:(NSString *)url{ // NSMutableArray *oldData = [NSMutableArray arrayWithArray:self.qaList]; // [oldData addObjectsFromArray:data]; // self.qaList = oldData; self.nextDataList = data; self.nextUrl = url; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end