|
//
// myAnswersController.m
// ThePaperHD
//
// Created by scar1900 on 15/3/30.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "messageAnswersController.h"
#import "messageAskContentCell.h"
#import "messageAnswerCell.h"
#import "messageTopCell.h"
#import "messageNoExpandCell.h"
#import "writeCommentAndAskController.h"
#import "KGModal.h"
#import "messageViewModel.h"
#import "messageQuoCell.h"
@interface messageAnswersController ()<hotAskListHomeDelegate,askContentCellDelgate,writeContentDelegate,askFootMoreDelegate,answerContentCellDelegate,messageViewModelDelegate,AnswerQuoDelegate>
@property(nonatomic, strong)UIButton *closeBtn;
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UIView *backView;
@property(nonatomic, strong)messageViewModel *model;
@property(nonatomic, strong)writeCommentAndAskController *writeVC;
@end
@implementation messageAnswersController
@synthesize qaList = _qaList;
@synthesize qaDataSource;
@synthesize qaHeightArray;
static NSString *messageAskCellID = @"messageAskCell";
static NSString *messageAnswerCellID = @"messageAnswerCell";
static NSString *messageTopCellID = @"messageTopCell";
static NSString *messageNoExCellID = @"messageNoExpandCell";
static NSString *messageQuoCellID = @"messageQuoCell";
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor clearColor];
[self.tableView removeFromSuperview];
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];
self.model = [messageViewModel new];
self.model.delegate = self;
// self.model.myAnswerDic = nil;
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];
[self.tableView registerClass:[messageNoExpandCell class] forCellReuseIdentifier:messageNoExCellID];
[self.tableView registerClass:[messageQuoCell class] forCellReuseIdentifier:messageQuoCellID];
}
-(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.view.bounds))];
_noDataBack.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
_noDataBack.hidden = YES;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 190, messagePopSize.width, 36)];
label.textColor = [UIColor colorWithHexString:TextGray];
label.font = appFont(23, NO);
label.textAlignment = NSTextAlignmentCenter;
label.text = @"暂无回复";
[_noDataBack addSubview:label];
}
return _noDataBack;
}
- (void)setQaList:(NSMutableArray *)list {
_qaList = list;
if ([self checkIfNoData:list]) {
return;
}
self.qaDataSource = [NSMutableArray array];
self.qaHeightArray = [NSMutableArray array];
[self.qaList 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 = heightForString(topTitle, appFont(13, NO), messagePopSize.width-30, NSLineBreakByWordWrapping);
[self.qaDataSource addObject:topTitle];
[self.qaHeightArray addObject:@(topHeight+23)];
//提问
if(obj.parentInfo){
commentObjectVO *askBO = setJsonDicToDataModel(obj.parentInfo, [commentObjectVO class]);
askBO.type = @"2";
[self.qaDataSource addObject:askBO];
// CGFloat askHeight = 10+50+returnTextHeightWithRTLabel(askBO.content,messagePopSize.width-30, appFont(interactionFontSize, NO),10); //(姓名+内容高度+空行)
CGFloat askHeight = 60 + heightForAttributeStringWithLabel(getLineSpaceAttributedString(askBO.content, 10, appFont(TEXT_FOUR_LEVELSIZE, NO)), messagePopSize.width-30, appFont(TEXT_FOUR_LEVELSIZE, NO));
[self.qaHeightArray addObject:[NSString stringWithFormat:@"%f",askHeight]];
}
//回答+盖楼
[self.qaDataSource addObject:obj];
//点赞按钮高度45 离原新闻话题 15 与盖楼间距10
// CGFloat answerHeight = 60+10+returnTextHeightWithRTLabel(obj.content,messagePopSize.width-30, appFont(interactionFontSize, NO),10); //(姓名+内容高度+空行)
CGFloat answerHeight = 60+10+heightForAttributeStringWithLabel(getLineSpaceAttributedString(obj.content, 10, appFont(interactionFontSize, NO)), messagePopSize.width-30, appFont(interactionFontSize, NO));
[self.qaHeightArray addObject:[NSString stringWithFormat:@"%f",answerHeight]];
//盖楼BO
if (obj.quoteInfo.count>0 ) {
NSMutableDictionary *dataDic = setDataModelToDic(obj, [commentObjectVO class]);
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(TEXT_FOUR_LEVELSIZE, NO)), messagePopSize.width-46, appFont(TEXT_FOUR_LEVELSIZE, NO));
CGFloat twoLine = twoLineheight;
quoInfo.labelHeight = quoLabelHeight;
if (quoLabelHeight > twoLine) {
quoLabelHeight = twoLine;
quoHeight += 15;
}
quoHeight += quoLabelHeight;
[self.qaDataSource addObject:quoInfo];
[self.qaHeightArray addObject:[NSString stringWithFormat:@"%f",quoHeight]];
}
}
}];
[self.tableView reloadData];
}
- (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 {
self.model.myAnswerDic = nil;
}
- (void)pullLoadMoreHander {
if (!self.nextUrl) {
TPLOG(@"数据有问题,无法获取新数据");
[self loadSuccess];
return;
}
self.model.myAnswerNextUrl = self.nextUrl;
}
#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];
}
#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 if ([commentBO.type intValue] == 1000){
return [self getQuoCell: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;
}
//回答详情
- (messageNoExpandCell*)getAnserContentCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
messageNoExpandCell *cell = (messageNoExpandCell*)[tableView dequeueReusableCellWithIdentifier:messageNoExCellID];
if (nil == cell) {
cell = [[messageNoExpandCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageNoExCellID];
}
cell.commentBO = self.qaDataSource[indexPath.row];
cell.delegate = self;
cell.indexPath = indexPath;
cell.isHaveCopyMenu = YES;
cell.isNeedTizhu = NO;
cell.isNeedAsImg = NO;
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.lineHidden = YES;
return cell;
}
- (messageQuoCell *)getQuoCell:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
messageQuoCell* cell = (messageQuoCell*)[table dequeueReusableCellWithIdentifier:messageQuoCellID];
if (nil == cell) {
cell = [[messageQuoCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:messageQuoCellID];
}
commentObjectVO *commentBO = self.qaDataSource[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-170;
if (isQuoExpand) {
quoHeight = quoHeight+50/2+10+15+15;
[self.qaHeightArray replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",quoHeight]];
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;
[self.qaHeightArray replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",quoHeight]];
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;
}
- (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* oldComment = self.qaDataSource[indexPath.row +1];
commentObjectVO* newComment = [commentObjectVO new];
__block commentObjectVO* newCom = newComment;
__block commentObjectVO *oldCom = oldComment;
[self.qaList enumerateObjectsUsingBlock:^(commentObjectVO *commentBo, NSUInteger idx, BOOL *stop) {
if ([oldCom.contId intValue] == [commentBo.contId intValue]) {
newCom = commentBo;
}
}];
if([newCom.objectType intValue] ==3 ){
TopicInfoBO *topic = [[TopicInfoBO alloc]init];
topic.topicId = newCom.contId;
NSDictionary *dic = @{@"data":topic};
if ([self.myAsksDelegate respondsToSelector:@selector(msgAnswerToTopic:)]) {
[self.myAsksDelegate msgAnswerToTopic:dic];
}
}else{
newCom.forwordType = newCom.objInfo[@"forwordType"];
NSDictionary *dic = @{@"data":newCom};
if ([self.myAsksDelegate respondsToSelector:@selector(msgAnswerToNews:)]) {
[self.myAsksDelegate msgAnswerToNews:dic];
}
}
}
}
#pragma mark -- delegate
- (void)goToOriginContent:(objInfoBO *)commentBO presentedController:(TPHttpController *)presentedController {
NSDictionary *dic = @{@"data":commentBO};
if ([self.myAsksDelegate respondsToSelector:@selector(msgAnswerToNews:)]) {
[self.myAsksDelegate msgAnswerToNews:dic];
}
}
-(void)hotAskHomeToTopic:(TopicInfoBO *)topic user:(userBO *)user{
NSDictionary *dic = nil;
//【倒退】关注的提问和回复我的:进入话题问答详情页,点击进入原话题,闪退(bug:6087)
if (user) {
dic = @{@"data":topic,@"user":user};
}else {
dic = @{@"data":topic};
}
if ([self.myAsksDelegate respondsToSelector:@selector(msgAnswerToTopic:)]) {
[self.myAsksDelegate msgAnswerToTopic:dic];
}
}
-(void)gotoPersonInfo:(commentObjectVO *)comment{
if ([self.myAsksDelegate respondsToSelector:@selector(gotoUserGambit:)]) {
[self.myAsksDelegate gotoUserGambit:comment];
}
}
-(void)gotoAnswer:(commentObjectVO *)comment{
self.writeVC = [[writeCommentAndAskController alloc]init];
if ([comment.type intValue] == 1) {
self.writeVC.type = commentForUserType;
}else {
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];
if ([commentBO.type intValue] == 1) {
self.writeVC.type = commentForUserType;
}else {
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 - remote handler
-(void)returnmyAnswerList:(NSMutableArray *)data nextUrl:(NSString *)url{
self.qaList = data;
self.nextUrl = url;
[self endRefresh];
}
-(void)returnMyAnswerNextUrl:(NSMutableArray *)data nextUrl:(NSString *)url{
NSMutableArray *oldData = [NSMutableArray arrayWithArray:self.qaList];
[oldData addObjectsFromArray:data];
self.qaList = oldData;
self.nextUrl = url;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
|