|
//
// myAttentionController.m
// ThePaperHD
//
// Created by liyuan on 15/4/21.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "myAttentionController.h"
#import "TopicInfoBO.h"
#import "myAttentionCell.h"
#import "topicContentController.h"
#import "topicModel.h"
@interface myAttentionController ()<topicModelDelegate> {
BOOL canEdit;
}
@property(nonatomic ,strong)topicModel *model;
@end
@implementation myAttentionController
@synthesize myAttentionList;
@synthesize heightArray;
@synthesize editDelegate;
- (void)viewDidLoad {
[super viewDidLoad];
canEdit = NO;
// self.topic = @"编辑";
[self.view addSubview:self.noDataBack];
self.tableView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
self.tableView.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
// Do any additional setup after loading the view.
[self configCanRefresh:YES canLoad:YES];
self.tableView.refreshControl.originalContentInsectY = 0;
[self manualRefresh];
self.model = [topicModel new];
self.model.delegate = self;
// Do any additional setup after loading the view.
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self manualRefresh];
}
- (BOOL)checkIfNoData:(NSArray*)list {
if (list.count > 0) {
self.topic = @"编辑";
if ([self.editDelegate respondsToSelector:@selector(editHidden:)]) {//我的话题-我创建的,如下操作后右上角会显示“编辑”(bug:4277)
[self.editDelegate editHidden:NO];
}
self.tableView.hidden =NO;
self.noDataBack.hidden = YES;
heightArray = [NSMutableArray array];
for (int i =0 ; i < myAttentionList.count; i++) {
TopicInfoBO *topicBO = myAttentionList[i];
CGFloat titleHeight;
CGFloat labelWidth = widthForString(topicBO.title, appFont(18, NO), 25, NSLineBreakByWordWrapping);
if (labelWidth <= gambitCenterPopSize.width-20) {//单行
titleHeight = 25;
}else{//多行
labelWidth = gambitCenterPopSize.width-30;
titleHeight = heightForString(topicBO.title, appFont(18, NO), gambitCenterPopSize.width-30, NSLineBreakByWordWrapping);
}
// CGFloat titleHeight = heightForString(topicBO.title, appFont(18, NO), gambitCenterPopSize.width-30, NSLineBreakByWordWrapping);
[heightArray addObject:@(titleHeight+71)];
}
return NO;
}else {
if ([self.editDelegate respondsToSelector:@selector(editHidden:)]) {//我的话题-我创建的,如下操作后右上角会显示“编辑”(bug:4277)
[self.editDelegate editHidden:YES];
}
self.tableView.hidden =YES;
self.noDataBack.hidden =NO;
return YES;
}
}
#pragma mark - button evetn
-(void) gotoAskSelector:(UIButton *)btn{
if ([self.editDelegate respondsToSelector:@selector(goAsk)]) {
[self.editDelegate goAsk];
}
}
#pragma mark - refresh and load handler
- (void)pullRefreshHandler {
// NSDictionary *dic = @{@"sort":@(1)};
self.model.myAttenDic = nil;
// [Remote doJsonAction:1
// requestUrl:myAttentionTopicListURL
// parameter:nil
// delegate:self];
}
- (void)pullLoadMoreHander {
if (!self.nextUrl) {
TPLOG(@"数据有问题,无法获取新数据");
[self loadSuccess];
return;
}
self.model.myAttenNextUrl = self.nextUrl;
// [Remote doJsonAction:2
// requestUrl:self.nextUrl
// parameter:nil
// delegate:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIView*)noDataBack {
if (!_noDataBack) {
_noDataBack = [[UIView alloc]initWithFrame:CGRectMake(0,
0,
gambitCenterPopSize.width,
CGRectGetHeight(self.view.bounds))];
_noDataBack.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
_noDataBack.hidden = YES;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(80, 350/2, gambitCenterPopSize.width-160, 20)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithHexString:TextGray];
label.font = appFont(20, NO);
label.textAlignment = NSTextAlignmentCenter;
label.text = @"还没有关注的话题哦,快去问吧逛逛吧!";
[_noDataBack addSubview:label];
TPCustomButton *gotoAsk = [[TPCustomButton alloc] initWithFrame:CGRectMake((gambitCenterPopSize.width-300)/2, CGRectGetMaxY(label.frame)+65, 300, 40)];
[gotoAsk setTitle:@"去问吧" forState:UIControlStateNormal];
gotoAsk.titleLabel.textColor = [UIColor colorWithHexString:BackGroundColor];
gotoAsk.titleLabel.font = appFont(18, NO);
[gotoAsk setBackgroundColor:[UIColor colorWithHexString:BLUECOLOR]];
[gotoAsk addTarget:self action:@selector(gotoAskSelector:) forControlEvents:UIControlEventTouchUpInside];
[_noDataBack addSubview:gotoAsk];
}
return _noDataBack;
}
-(void)setMyAttentionList:(NSMutableArray *)list{
myAttentionList = list;
[self checkIfNoData:myAttentionList];
}
#pragma mark - table view delegate and datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return myAttentionList.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [heightArray[indexPath.row] floatValue];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIndetifier = @"myAttentionCell";
myAttentionCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier];
if (nil == cell) {
cell = [[myAttentionCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellIndetifier];
}
[cell setData:myAttentionList[indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([self.editDelegate respondsToSelector:@selector(attentionToTopic:)]) {
[self.editDelegate attentionToTopic:self.myAttentionList[indexPath.row]];
TopicInfoBO *topic = myAttentionList[indexPath.row];
topic.unNums = @"0";
[myAttentionList replaceObjectAtIndex:indexPath.row withObject:topic];
[self.tableView reloadData];
}
// topicContentController *vc = [[topicContentController alloc]init];
// TopicInfoBO *topic = self.myAttentionList[indexPath.row];
// vc.preTopicInfo = topic;
// userBO *user = setJsonDicToDataModel(topic.userInfo, [userBO class]);
// vc.creatUser = user;
// [self.navigationController pushViewController:vc animated:YES];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return canEdit;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
TopicInfoBO *topicInfoBO = myAttentionList[indexPath.row];
NSDictionary *dict = @{@"commentId":topicInfoBO.topicId,@"ot":@"1"};
// [Remote doJsonAction:3 requestUrl:cancelAttentionURL parameter:dict delegate:self];
[Remote doJsonActionWithBlock:3 requestUrl:cancelAttentionURL parameter:dict withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
// UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"是否确认删除关注?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"删除",nil];
// [alert show];
[self.tableView beginUpdates];
[myAttentionList removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[heightArray removeObjectAtIndex:indexPath.row];
if (myAttentionList.count <= 0) {
// self.noDataBack.hidden = NO;
// self.tableView.hidden = YES;
[self manualRefresh];
}
[self.tableView endUpdates];
// [self.tableView reloadData];
// TPLOG(message);
}else{
ShowTextMessage(message);
// TPLOG(message);
}
}];
// listContObjectVO *list = self.dataList[indexPath.row];
// NSDictionary *dic = @{@"cids":list.contId};
// __weak typeof(self) Self = self;
// [Remote doJsonActionWithBlock:0 requestUrl:deleteFavoriteURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
// if (success) {
// [Self.dataList removeObjectAtIndex:indexPath.row];
// [Self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
// [self checkIfNoData:self.dataList];
// }else {
// ShowMessage(message, NO);
// }
// }];
}
}
-(void)returnMyAttenList:(NSMutableArray *)data url:(NSString *)url{
self.myAttentionList = data;
self.nextUrl = url;
[self endRefresh];
[self.tableView reloadData];
}
-(void)returnMyAttenNextList:(NSMutableArray *)data url:(NSString *)url{
NSMutableArray *oldArry = [NSMutableArray arrayWithArray:self.myAttentionList];
[oldArry addObjectsFromArray:data];
self.myAttentionList = oldArry;
self.nextUrl= url;
[self.tableView reloadData];
}
//#pragma mark - remote handler
//- (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData {
// if (actionTag == 1) {
// NSArray *topicTempList = responsData[@"topicList"];
// NSMutableArray *topicArray = [NSMutableArray array];
// [topicTempList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
// TopicInfoBO *topicInfo = setJsonDicToDataModel(obj, [TopicInfoBO class]);
// topicInfo.desc = obj[@"description"];
// NSString *newNums = obj[@"newNums"];
// if (newNums) {
// topicInfo.unNums = newNums;
// }
// [topicArray addObject:topicInfo];
// }];
// self.myAttentionList = topicArray;
// NSString *url = responsData[@"nextUrl"];
// self.nextUrl = url;
// [self.tableView reloadData];
// [self endRefresh];
// }else if(actionTag == 2){
// NSArray *topicTempList = responsData[@"topicList"];
// NSMutableArray *qaArray = [NSMutableArray arrayWithArray:myAttentionList];
// NSMutableArray *newArray = [NSMutableArray array];
//// [myAttentionList removeAllObjects];
// [topicTempList enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) {
// TopicInfoBO *topicBo = setJsonDicToDataModel(obj, [TopicInfoBO class]);
// topicBo.desc = obj[@"description"];
// NSString *newNums = obj[@"newNums"];
// if (newNums) {
// topicBo.unNums = newNums;
// }
// [newArray addObject:topicBo];
// }];
// [qaArray addObjectsFromArray:newArray];
// self.myAttentionList = [NSMutableArray arrayWithArray:qaArray];
// [self loadSuccess];
// NSString *url = responsData[@"nextUrl"];
// self.nextUrl = url;
// [self.tableView reloadData];
// }
//
//}
-(void) editAttentionTopic:(BOOL)isEdit{
if (isEdit) {
canEdit = YES;
[self.tableView setEditing:YES animated:YES];
self.topic = @"完成";
}else{
canEdit = NO;
[self.tableView setEditing:NO animated:YES];
self.topic = @"编辑";
}
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
|