|
//
// trackTableController.m
// ThePaperHD
//
// Created by scar1900 on 15/3/31.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "trackTableController.h"
#import "trackContentCell.h"
#import "trackHeadCell.h"
@interface trackTableController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)UIButton *closeBtn;
@property(nonatomic, strong)UIButton *editBtn;
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UIView *backView;
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *dataList;
@property(nonatomic, strong)UIView *noDataBack;
@property(nonatomic, strong)UIButton *deleteAllBtn;
@end
@implementation trackTableController
@synthesize isNeedHold;
@synthesize dataList=_dataList;
@synthesize delegate;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[MobClick event:@"14"];
self.view.backgroundColor = [UIColor clearColor];
self.view.frame = CGRectMake(0, 0, trackPopSize.width, trackPopSize.height);
[self.view addSubview:self.closeBtn];
[self.view addSubview:self.titleLabel];
[self.view addSubview:self.editBtn];
[self.view addSubview:self.deleteAllBtn];
self.closeBtn.frame = CGRectMake(0, 0, 50, 80);
[self.closeBtn setImageEdgeInsets:UIEdgeInsetsMake(15, 0, 15, 0)];
self.titleLabel.frame = CGRectMake(0,
0,
CGRectGetWidth(self.view.bounds),
80);
self.editBtn.frame = CGRectMake(trackPopSize.width-50, 0, 50, 80);
[self.editBtn setImageEdgeInsets:UIEdgeInsetsMake(15, 0, 15, 0)];
self.deleteAllBtn.frame = CGRectMake(trackPopSize.width-50-100-5, 0, 100, 80);
[self.view addSubview:self.backView];
[self.backView addSubview:self.noDataBack];
[self.backView addSubview:self.tableView];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self enablePopBackTap];
self.isNeedHold = NO;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (!self.dataList || self.dataList.count == 0) {
[self remoteMethod];
}
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:[TPUserDefault instance].msgMark];
[dic setValue:@(0) forKey:@"trackMark"];
[TPUserDefault instance].msgMark = dic;
if ([self.delegate respondsToSelector:@selector(hasAppearTrackCenter)]) {
[self.delegate hasAppearTrackCenter];
}
}
- (UIView*)noDataBack {
if (!_noDataBack) {
_noDataBack = [[UIView alloc]initWithFrame:CGRectMake(0,
0,
trackPopSize.width,
CGRectGetHeight(self.backView.bounds))];
_noDataBack.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
_noDataBack.hidden = YES;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(55, 218/2, 760/2, 110/2)];
label.textColor = [UIColor colorWithHexString:TextBlack];
label.font = appFont(20, NO);
label.textAlignment = NSTextAlignmentLeft;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.numberOfLines = 0;
label.backgroundColor = [UIColor clearColor];
label.text = @"读文章时,点击跟踪按钮,\n这里将集纳事件最新发展,杜绝新闻烂尾。";
[_noDataBack addSubview:label];
UIImageView *imageView = [[UIImageView alloc]initWithImage:Image(@"other/trackNoDataBack.png")];
imageView.frame = CGRectMake(trackPopSize.width/2-486/4, CGRectGetMaxY(label.frame)+35/2, 435/2, 612/2);
[_noDataBack addSubview:imageView];
}
return _noDataBack;
}
- (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;
self.deleteAllBtn.hidden = YES;
return YES;
}
}
- (void)setDataList:(NSMutableArray *)list {
_dataList = list;
if ([self checkIfNoData:list]) {
return;
}
[self.tableView reloadData];
}
- (UIButton*)closeBtn {
if (!_closeBtn) {
_closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_closeBtn setImage:Image(@"login/popUpCloseBtn.png") forState:UIControlStateNormal];
[_closeBtn setImage:Image(@"login/popUpCloseBtn_s.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;
}
- (UIButton*)editBtn {
if (!_editBtn) {
_editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_editBtn setImage:Image(@"Button/editPop.png") forState:UIControlStateNormal];
[_editBtn setImage:Image(@"Button/popConfirmBtn.png") forState:UIControlStateSelected];
[_editBtn addTarget:self action:@selector(clickEditBtn:) forControlEvents:UIControlEventTouchUpInside];
}
return _editBtn;
}
- (UIButton*)deleteAllBtn {
if (!_deleteAllBtn) {
_deleteAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_deleteAllBtn setTitle:@"全部删除" forState:UIControlStateNormal];
[_deleteAllBtn addTarget:self action:@selector(deleteAllTrack:) forControlEvents:UIControlEventTouchUpInside];
_deleteAllBtn.hidden = YES;
}
return _deleteAllBtn;
}
- (UIView*)backView {
if (!_backView) {
_backView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame), trackPopSize.width, trackPopSize.height-80)];
_backView.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
}
return _backView;
}
- (UITableView*)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:self.backView.bounds style:UITableViewStylePlain];
_tableView.backgroundColor = [UIColor clearColor];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSelectionStyleNone;
}
return _tableView;
}
#pragma mark - remote method
- (void)remoteMethod {
__weak typeof(self) Self = self;
[Remote doJsonActionWithBlock:0 requestUrl:myTrackListURL parameter:nil withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
NSArray *trackList = responseData[@"trackerList"];
NSMutableArray *temp = [NSMutableArray array];
[trackList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSMutableArray *trackList = [NSMutableArray array];
trackerInfoBO *trackBO = setJsonDicToDataModel(obj, [trackerInfoBO class]);
trackBO.unNum = obj[@"newNum"];
trackBO.isOpen = @"0";
[trackList addObject:trackBO];
NSArray *contentList = trackBO.contList;
[contentList enumerateObjectsUsingBlock:^(NSDictionary* dic, NSUInteger index, BOOL *stop1) {
listContObjectVO *listBO = setJsonDicToDataModel(dic, [listContObjectVO class]);
[trackList addObject:listBO];
}];
[temp addObject:trackList];
}];
Self.dataList = temp;
}else {
ShowTextMessage(message);
}
}];
}
#pragma mark - button handler
- (void)closeEvent:(UIButton*)btn {
[self dismissControllerAnimated:YES completion:nil];
}
- (void)clickEditBtn:(UIButton*)btn {
btn.selected = !btn.selected;
if (btn.selected) {
[self.tableView setEditing:YES animated:YES];
self.deleteAllBtn.hidden = NO;
}else {
[self.tableView setEditing:NO animated:YES];
self.deleteAllBtn.hidden = YES;
}
}
- (void)deleteAllTrack:(UIButton*)btn {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"是否删除全部跟踪?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != 0) {
__weak typeof(self) Self = self;
[Remote doJsonActionWithBlock:0 requestUrl:cancelTrackURL parameter:nil withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
Self.dataList = [NSMutableArray array];
// Delete the row from the data source.
[Self.tableView reloadData];
[self checkIfNoData:self.dataList];
}else {
ShowMessage(message, NO);
}
}];
}
}
#pragma mark - tableview delegate and datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.dataList?self.dataList.count:0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (!self.dataList || self.dataList.count == 0) {
return 0;
}else {
NSArray *list = self.dataList[section];
return list.count;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (!self.dataList || self.dataList.count == 0) {
return 0;
}else {
NSArray *list = self.dataList[indexPath.section];
trackerInfoBO *trackBO = list[0];
if (indexPath.row == 0) {
return 130/2;
}else {
if ([trackBO.isOpen intValue] == 1) {
return 90;
}else {
return 0;
}
}
}
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *list = self.dataList[indexPath.section];
id data = list[indexPath.row];
if ([data isKindOfClass:[trackerInfoBO class]]) {
return [self getTrackHeadCell:tableView cellForRowAtIndexPath:indexPath];
}else {
return [self getTrackContentCell:tableView cellForRowAtIndexPath:indexPath];
}
}
- (trackHeadCell*)getTrackHeadCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *trakHeadCellID = @"trakHeadCellID";
trackHeadCell *cell = (trackHeadCell*)[tableView dequeueReusableCellWithIdentifier:trakHeadCellID];
if (nil == cell) {
cell = [[trackHeadCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:trakHeadCellID];
}
NSArray *list = self.dataList[indexPath.section];
id data = list[indexPath.row];
cell.trackerInfoBO = data;
return cell;
}
- (trackContentCell*)getTrackContentCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *trackContentCellID = @"trackContentCellID";
trackContentCell *cell = (trackContentCell*)[tableView dequeueReusableCellWithIdentifier:trackContentCellID];
if (nil == cell) {
cell = [[trackContentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:trackContentCellID];
}
NSArray *list = self.dataList[indexPath.section];
listContObjectVO* listBO = list[indexPath.row];
trackerInfoBO* trackBO = list[0];
if ([trackBO.isOpen intValue] == 1) {
[cell setListBO:listBO andTrackBO:trackBO];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSMutableArray *list = [NSMutableArray arrayWithArray:self.dataList[indexPath.section]];
id data = list[indexPath.row];
if ([data isKindOfClass:[trackerInfoBO class]]) {
trackerInfoBO *trackBO = data;
if ([trackBO.isOpen intValue] == 0) {
trackBO.isOpen = @"1";
}else {
trackBO.isOpen = @"0";
}
if ([trackBO.unNum intValue] > 0) {
NSDictionary *dic = @{@"trackId":trackBO.trackerId};
[Remote doJsonActionWithBlock:1 requestUrl:myTrackResultListURL parameter:dic withWaitCursor:NO completion:^(BOOL success, NSString *message, id responseData) {
}];
}
trackBO.unNum = @"0";
[list replaceObjectAtIndex:indexPath.row withObject:trackBO];
[self.dataList replaceObjectAtIndex:indexPath.section withObject:list];
// NSArray *indexPathList = [NSArray arrayWithObject:indexPath];
[self.tableView beginUpdates];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}else {
self.isNeedHold = YES;
if ([self.delegate respondsToSelector:@selector(trackGoToContent:)]) {
[self.delegate trackGoToContent:data];
}
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *list = [NSMutableArray arrayWithArray:self.dataList[indexPath.section]];
id data = list[indexPath.row];
if ([data isKindOfClass:[trackerInfoBO class]]) {
return YES;
}else {
return NO;
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSArray *list = self.dataList[indexPath.section];
trackerInfoBO *trackInfo = list[0];
NSDictionary *dic = @{@"trackIds":trackInfo.trackerId};
__weak typeof(self) Self = self;
[Remote doJsonActionWithBlock:0 requestUrl:cancelTrackURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
[Self.dataList removeObjectAtIndex:indexPath.section];
// Delete the row from the data source.
[Self.tableView beginUpdates];
[Self.tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
[Self.tableView endUpdates];
[self checkIfNoData:self.dataList];
}else {
ShowMessage(message, NO);
}
}];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#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
|