// // myFocusController.m // ThePaperHD // // Created by scar1900 on 15/3/30. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "messageFocusController.h" #import "messageTopCell.h" #import "messageAskContentCell.h" #import "messageFocusCell.h" #import "KGModal.h" #import "messageViewModel.h" @interface messageFocusController () { UIView *_noDataBack; } @property(nonatomic, strong)UIButton *closeBtn; @property(nonatomic, strong)UILabel *titleLabel; @property(nonatomic, strong)UIView *backView; @property(nonatomic, strong)UIButton *editBtn; @property(nonatomic, strong)messageViewModel *model; @end @implementation messageFocusController @synthesize qaList = _qaList; @synthesize qaDataSource; @synthesize qaHeightArray; static NSString *messageAskCellID = @"messageAskCell"; static NSString *messageTopCellID = @"messageTopCell"; static NSString *messageFocusCellID = @"mesageFocusCell"; - (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.view addSubview:self.editBtn]; self.closeBtn.frame = CGRectMake(hotAskListPopUpSize.width/2 - messagePopSize.width/2, 0, 50, 80); [self.closeBtn setImageEdgeInsets:UIEdgeInsetsMake(15, 0, 15, 0)]; self.editBtn.frame = CGRectMake(messagePopSize.width/2+hotAskListPopUpSize.width/2-50, 0, 50, 80); [self.editBtn 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.editBtn.selected = NO; 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:[messageTopCell class] forCellReuseIdentifier:messageTopCellID]; [self.tableView registerClass:[messageFocusCell class] forCellReuseIdentifier:messageFocusCellID]; } -(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, messagePopSize.height-80)]; _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.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)]; [self.qaDataSource addObject:obj]; CGFloat titleHeight = 50+returnTextHeightWithRTLabel(obj.content,messagePopSize.width-30, appFont(interactionFontSize, NO),10); //(姓名+内容高度+空行) [self.qaHeightArray addObject:[NSString stringWithFormat:@"%f",titleHeight]]; }]; [self.tableView reloadData]; } - (BOOL)checkIfNoData:(NSArray*)list { if (list.count > 0) { self.tableView.hidden =NO; self.noDataBack.hidden = YES; self.editBtn.hidden = NO; return NO; }else { self.tableView.hidden =YES; self.noDataBack.hidden =NO; self.editBtn.hidden = YES; return YES; } } #pragma mark - refresh and load handler - (void)pullRefreshHandler { self.model.myFocusDic = nil; } - (void)pullLoadMoreHander { if (!self.nextUrl) { TPLOG(@"数据有问题,无法获取新数据"); [self loadSuccess]; return; } self.model.myFocusNextUrl = 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; } - (UIButton*)editBtn { if (!_editBtn) { _editBtn = [UIButton buttonWithType:UIButtonTypeCustom]; // [_editBtn setTitle:@"编辑" forState:UIControlStateNormal]; // [_editBtn setTitle:@"编辑" forState:UIControlStateHighlighted]; // [_editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; // [_editBtn setTitleColor:[UIColor colorWithHexString:BLUECOLOR] forState:UIControlStateHighlighted]; [_editBtn setImage:Image(@"Button/editPop.png") forState:UIControlStateNormal]; [_editBtn setImage:Image(@"Button/popConfirmBtn.png") forState:UIControlStateSelected]; _editBtn.titleLabel.font = appFont(18, NO); [_editBtn addTarget:self action:@selector(editSelector:) forControlEvents:UIControlEventTouchUpInside]; } return _editBtn; } #pragma mark - btn event - (void)closeEvent:(UIButton*)btn { [(MLNavigationController*)self.navigationController popViewControllerWithFlip]; } - (void)editSelector:(UIButton*)btn { btn.selected = !btn.selected; if (btn.selected) { [self.tableView setEditing:YES animated:YES]; }else { [self.tableView setEditing:NO animated:YES]; } } -(void)gotoAskSelector:(UIButton*) btn{ if ([self.delegate respondsToSelector:@selector(foucsToTopicList) ]) { [self.delegate foucsToTopicList]; } // [[NSNotificationCenter defaultCenter] postNotificationName:MESSAGETOTOPICLIST object:nil userInfo:nil]; } #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{ // return [self getAskContentCell:tableView cellForRowAtIndexPath:indexPath]; //问题(新追问) return [self getFocusContentCell: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; } - (messageAskContentCell*)getAskContentCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { messageAskContentCell *cell = (messageAskContentCell*)[tableView dequeueReusableCellWithIdentifier:messageAskCellID]; if (nil == cell) { cell = [[messageAskContentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageAskCellID]; cell.dashedLineView.hidden = YES; UIView *line = [[UIView alloc]init]; line.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; line.tag = 20; [cell addSubview:line]; } cell.commentBO = self.qaDataSource[indexPath.row]; cell.isHaveMenu = NO; cell.askContentDelegate =self; UIView *line = [cell viewWithTag:20]; CGFloat height = [self.qaHeightArray[indexPath.row] floatValue]; line.frame = CGRectMake(15,height-1, myAskCenterPopSize.width-30, 1); return cell; } //问题内容 - (messageFocusCell*)getFocusContentCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { messageFocusCell *cell = (messageFocusCell*)[tableView dequeueReusableCellWithIdentifier:messageFocusCellID]; if (nil == cell) { cell = [[messageFocusCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageFocusCellID]; } cell.commentBO = self.qaDataSource[indexPath.row]; cell.isHaveMenu = NO; cell.delegate = self; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if ([cell isKindOfClass:[messageFocusCell 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.delegate respondsToSelector:@selector(msgFocusToTopic:)]) { [self.delegate msgFocusToTopic:dic]; } // [[NSNotificationCenter defaultCenter] postNotificationName:MESSAGETOTOIPICINFO object:nil userInfo:dic]; }else{ comment.forwordType = comment.objInfo[@"forwordType"]; NSDictionary *dic = @{@"data":comment}; if ([self.delegate respondsToSelector:@selector(msgFocusToNews:)]) { [self.delegate msgFocusToNews:dic]; } // [[NSNotificationCenter defaultCenter] postNotificationName:MESSAGETODETAIL object:nil userInfo:dic]; } } } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { if([self.qaDataSource[indexPath.row] isKindOfClass:[NSString class]]){ return NO; }else{ return YES; } } - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { commentObjectVO *comment = self.qaDataSource[indexPath.row]; NSDictionary *dic = @{@"commentId":comment.commentId}; __weak typeof(self) Self = self; [Remote doJsonActionWithBlock:0 requestUrl:cancelAttentionURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { if (success) { [Self.qaDataSource removeObjectAtIndex:indexPath.row]; [Self.qaDataSource removeObjectAtIndex:indexPath.row-1]; [Self.qaHeightArray removeObjectAtIndex:indexPath.row]; [Self.qaHeightArray removeObjectAtIndex:indexPath.row-1]; // Delete the row from the data source. [Self.tableView beginUpdates]; NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:0]; [Self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,lastIndex,nil] withRowAnimation:UITableViewRowAnimationFade]; if (self.qaDataSource.count ==0) { if ([self.delegate respondsToSelector:@selector(deleteAllFocus)]) { [self.delegate deleteAllFocus]; } } [Self checkIfNoData:self.qaDataSource]; [Self.tableView endUpdates]; }else { ShowMessage(message, NO); } }]; } } -(void)gotoPersonInfo:(commentObjectVO *)comment{ if ([self.delegate respondsToSelector:@selector(gotoUserGambit:)]) { [self.delegate gotoUserGambit:comment]; } } - (void)goToOriginContent:(objInfoBO *)commentBO presentedController:(TPHttpController *)presentedController { NSDictionary *dic = @{@"data":commentBO}; if ([self.delegate respondsToSelector:@selector(msgFocusToNews:)]) { [self.delegate msgFocusToNews:dic]; } } -(void)hotAskHomeToTopic:(TopicInfoBO *)topic user:(userBO *)user{ //【倒退】关注的提问和回复我的:进入话题问答详情页,点击进入原话题,闪退(bug:6087) NSDictionary *dic = nil; if (user) { dic = @{@"data":topic,@"user":user}; }else{ dic = @{@"data":topic}; } if ([self.delegate respondsToSelector:@selector(msgFocusToTopic:)]) { [self.delegate msgFocusToTopic:dic]; } } -(void)returnMyFocusList:(NSMutableArray *)data nextUrl:(NSString *)url{ self.qaList = data; self.nextUrl = url; [self endRefresh]; } -(void)returnMyFocusNextUrl:(NSMutableArray *)data nextUrl:(NSString *)url{ NSMutableArray *oldData = [NSMutableArray arrayWithArray:self.qaList]; [oldData addObjectsFromArray:data]; self.qaList = oldData; self.nextUrl = url; } //#pragma mark - remote handler //- (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData { // if (actionTag == 1) { // NSArray *qaTempList = responsData[@"qaList"]; // NSMutableArray *qaArray = [NSMutableArray array]; // // [qaTempList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { // commentObjectVO *commentBO = setJsonDicToDataModel(obj, [commentObjectVO class]); // NSString *newNums = obj[@"newNums"]; // if (newNums) { // commentBO.unNums = newNums; // } // [qaArray addObject:commentBO]; // }]; // // self.qaList = qaArray; // // NSString *url = responsData[@"nextUrl"]; // self.nextUrl = url; // [self endRefresh]; // }else { // NSArray *qaTempList = responsData[@"qaList"]; // NSMutableArray *qaArray = [NSMutableArray arrayWithArray:self.qaList]; // [qaTempList enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) { // commentObjectVO *commentBO = setJsonDicToDataModel(obj, [commentObjectVO class]); // [qaArray addObject:commentBO]; // }]; // // self.qaList = [NSMutableArray arrayWithArray:qaArray]; // [self loadSuccess]; // // NSString *url = responsData[@"nextUrl"]; // self.nextUrl = url; // } // //} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end