|
//
// columnManagerController.m
// ThePaperDemo
//
// Created by scar1900 on 14/10/31.
// Copyright (c) 2014年 scar1900. All rights reserved.
//
#import "columnManagerController.h"
#import "columnHeaderView.h"
#import "channelListCell.h"
#import "detailContentHomeController.h"
#import "CoreAnimationEffect.h"
#import "ImageContentController.h"
#import "TPLiveHomeController.h"
#import "loginHomeController.h"
#define backBtnTag 500
@interface columnManagerController () <columnHeadDelegate>{
CGRect headRect;
}
@property(nonatomic, strong)columnHeaderView *columnHeader;
@property(nonatomic, strong)UIButton *orderButton;
@end
@implementation columnManagerController
@synthesize nodeBO = _nodeBO;
@synthesize dataList = _dataList;
- (void)viewDidLoad {
[super viewDidLoad];
[MobClick event:@"50"];
// Do any additional setup after loading the view.
self.tableView.clipsToBounds = YES;
[self.view addSubview:self.columnHeader];
self.dataKey = @"contList";
self.nodeId = self.nodeBO.nodeId;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
if (isIOS7) {
backBtn.frame = CGRectMake(0, 18, 40, 63-18);
}else {
backBtn.frame = CGRectMake(0, 0, 40, 45);
}
backBtn.backgroundColor = [UIColor clearColor];
if ([[TPUserDefault instance].isNightMode intValue] == 0) {
[backBtn setImage:Image(@"detailPage/backBtn.png") forState:UIControlStateNormal];
[backBtn setImage:Image(@"detailPage/backBtn_s.png") forState:UIControlStateHighlighted];
}else {
[backBtn setImage:Image(@"detailPage/backBtn_night.png") forState:UIControlStateNormal];
[backBtn setImage:Image(@"detailPage/backBtn_night_s.png") forState:UIControlStateHighlighted];
}
[backBtn addTarget:self action:@selector(backHandler) forControlEvents:UIControlEventTouchUpInside];
backBtn.tag = backBtnTag;
[self.view addSubview:backBtn];
[self.view addSubview:self.orderButton];
[self firstAutoRefreshHandler];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapNaviBar:)];
[self.columnHeader addGestureRecognizer:tapGesture];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode:) name:REFRESHAFTERNIGHTMODE object:nil];
}
- (void)setNodeBO:(nodeObjectBO *)obj{
_nodeBO = obj;
if ([obj.isOrder intValue] == 1 && [TPUserDefault instance].userBO) {
self.orderButton.selected = YES;
self.orderButton.backgroundColor = [UIColor colorWithHexString:BLUECOLOR];
}else {
self.orderButton.selected = NO;
self.orderButton.backgroundColor = [UIColor colorWithHexString:@"0xbababa"];
}
}
- (columnHeaderView*)columnHeader {
if (!_columnHeader) {
if (isIOS7) {
headRect = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 290/2);
_columnHeader = [[columnHeaderView alloc]initWithFrame:headRect withNodeObject:self.nodeBO];
self.contentInsetTop = CGRectGetHeight(_columnHeader.frame)-63;
self.tableView.frame = CGRectMake(0,
63,
CGRectGetWidth(self.view.bounds),
CGRectGetHeight(self.view.bounds)-63);
}else {
headRect = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 290/2-18);
_columnHeader = [[columnHeaderView alloc]initWithFrame:headRect withNodeObject:self.nodeBO];
self.contentInsetTop = CGRectGetHeight(_columnHeader.frame)-45;
self.tableView.frame = CGRectMake(0,
45,
CGRectGetWidth(self.view.bounds),
CGRectGetHeight(self.view.bounds)-45);
}
_columnHeader.delegate = self;
}
return _columnHeader;
}
- (UIButton*)orderButton {
if (!_orderButton) {
_orderButton = [UIButton buttonWithType:UIButtonTypeCustom];
_orderButton.layer.cornerRadius = 3;
if (isIOS7) {
_orderButton.frame = CGRectMake(CGRectGetWidth(self.view.bounds)-60-10, 18+14, 60, 20);
}else {
_orderButton.frame = CGRectMake(CGRectGetWidth(self.view.bounds)-60-10, 0+14, 60, 20);
}
[_orderButton setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:LIGHTGRAY]) forState:UIControlStateNormal];
[_orderButton setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:BUTTONDISABLEBACK]) forState:UIControlStateSelected];
[_orderButton addTarget:self action:@selector(orderHandler:) forControlEvents:UIControlEventTouchUpInside];
[_orderButton setImage:Image(@"detailPage/addPic.png") forState:UIControlStateNormal];
[_orderButton setImage:Image(@"detailPage/ordered.png") forState:UIControlStateSelected];
[_orderButton setTitle:@"订阅" forState:UIControlStateNormal];
[_orderButton setTitle:@"已订阅" forState:UIControlStateSelected];
_orderButton.titleLabel.textAlignment = NSTextAlignmentLeft;
_orderButton.titleLabel.textColor = [UIColor whiteColor];
}
_orderButton.titleLabel.font = appFont(TEXT_SIX_LEVELSIZE, NO);
return _orderButton;
}
- (void)setDataList:(NSMutableArray *)list {
if (!_dataList || _dataList.count == 0) {
if (isIOS7) {
self.tableView.frame = CGRectMake(0,
63,
CGRectGetWidth(self.view.bounds),
CGRectGetHeight(self.view.bounds)-63);
self.contentInsetTop = CGRectGetHeight(self.columnHeader.frame)-63;
}else {
self.tableView.frame = CGRectMake(0,
45,
CGRectGetWidth(self.view.bounds),
CGRectGetHeight(self.view.bounds)-45);
self.contentInsetTop = CGRectGetHeight(self.columnHeader.frame)-45;
}
}
_dataList = list;
if ([[TPUserDefault instance].readModeStr intValue] == imageMode) {
[self setTableDataSource:list];
}else if ([[TPUserDefault instance].readModeStr intValue] == textMode) {
[self setTableDataSourceWithoutImage:list];
}else {
if ([Remote IsEnableWIFI]) {
[self setTableDataSource:list];
}else {
[self setTableDataSourceWithoutImage:list];
}
}}
- (void)getContentOffset:(CGPoint)contentOffset {
if (contentOffset.y > 0) {
CGFloat offset = self.contentInsetTop;
CGRect rect = headRect;
rect.origin.y = rect.origin.y -offset;
self.columnHeader.frame = rect;
return;
}
if (contentOffset.y < 0-self.contentInsetTop) {
self.columnHeader.frame = headRect;
return;
}
// NSLog(@"contentOffsetY:%f",contentOffset.y);
CGFloat offset = self.contentInsetTop + contentOffset.y;
CGRect rect = headRect;
rect.origin.y = rect.origin.y -offset;
self.columnHeader.frame = rect;
}
- (void)tapNaviBar:(id)sender {
[self scrollTableViewToTop];
}
- (void)backHandler {
((MLNavigationController *)self.navigationController).canDragBack = YES;
[self.navigationController popViewControllerAnimated:YES];
}
- (void)orderHandler:(UIButton*)btn {
if (btn.selected) {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"n":self.nodeId,@"oType":@"2"}];
[Remote doJsonActionWithBlock:1 requestUrl:orderNodeURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
btn.selected = NO;
btn.backgroundColor = [UIColor colorWithHexString:@"0xbababa"];
ShowMessage(@"取消订阅", YES);
[[NSNotificationCenter defaultCenter] postNotificationName:NEEDREFRESHORDER object:nil];
}
}];
}else {
if ([TPUserDefault instance].userBO) {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{@"n":self.nodeId,@"oType":@"1"}];
[Remote doJsonActionWithBlock:1 requestUrl:orderNodeURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
btn.selected = YES;
btn.backgroundColor = [UIColor colorWithHexString:BLUECOLOR];
ShowMessage(@"订阅成功", YES);
[[NSNotificationCenter defaultCenter] postNotificationName:NEEDREFRESHORDER object:nil];
}
}];
}else {
loginHomeController *vc = [loginHomeController new];
[self.navigationController pushViewController:vc animated:YES];
}
}
}
#pragma mark - remtote action
- (void)remoteAction {
NSDictionary *dic = @{@"n":self.nodeId};
[Remote doJsonAction:1
requestUrl:nodeContentListURL
parameter:dic
delegate:self];
}
#pragma mark - reimplementation table View delegate and datasource(some)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
/**
* bug:5231(首页推荐条,回来不要点击效果,包括夜间模式)
*/
if ([[tableView cellForRowAtIndexPath:indexPath] isKindOfClass:[channelListCell class]]) {
channelListCell *cell = (channelListCell*)[tableView cellForRowAtIndexPath:indexPath];
listContObjectVO* listOBJ = self.dataList[indexPath.row];
[UIView animateWithDuration:0.15 animations:^{
if (isNotIOS8) {
cell.transform = CGAffineTransformMakeScale(0.97, 0.97);
}else {
cell.backView.transform = CGAffineTransformMakeScale(0.97, 0.97);
}
} completion:^(BOOL finished) {
if (isNotIOS8) {
cell.transform = CGAffineTransformMakeScale(1.00, 1.00);
}else {
cell.backView.transform = CGAffineTransformMakeScale(1.00, 1.00);
}
pushContentWithListContentObject(self.navigationController, listOBJ);
NSArray *array = [NSArray arrayWithObject:indexPath];
[self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationAutomatic];
}];
}
}
#pragma mark - TPTableView delegate
- (void)pullRefreshHandler {
[self remoteAction];
}
- (void)pullLoadMoreHander {
[Remote doJsonAction:2
requestUrl:self.nextUrl
parameter:nil
delegate:self];
}
#pragma mark - remote call back
- (void)startWaitCursor:(int)actionTag {
}
- (void)stopWaitCursor:(int)actionTag {
}
- (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData {
if (actionTag == 1) {
[self getRefreshDataFromDic:responsData sourceKey:self.dataKey contentClass:self.dataClass];
[self saveRemoteDataToCoreData:responsData];
[self endRefresh];
nodeObjectBO *nodeInfo = setJsonDicToDataModel(responsData[@"nodeInfo"], [nodeObjectBO class]);
self.nodeBO = nodeInfo;
/**
* bug:5005&&5022( 订阅导航页,点击一个已订阅的栏目,栏目页中依然是未订阅状态)
*/
if (nodeInfo.sponsor) {
self.columnHeader.spansorData = setJsonDicToDataModel(nodeInfo.sponsor, [sponsorBO class]);
}
self.loadFooter.hidden = NO;
NSString *url = responsData[@"nextUrl"];
self.nextUrl = url;
}else {
NSArray *contList = responsData[@"contList"];
if (contList && contList.count > 0) {
NSMutableArray *oriListData = [NSMutableArray arrayWithArray:self.dataList];
NSMutableArray *tempArray = [NSMutableArray array];
[contList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
listContObjectVO *list = setJsonDicToDataModel(obj, [listContObjectVO class]);
if (!list.title) {
if ([[TPUserDefault instance].readModeStr intValue] == imageMode) {
[tempArray addObject:list];
}else if ([[TPUserDefault instance].readModeStr intValue] == textMode) {
if ([list.forwordType intValue] != imageNewsForwardType) {
[tempArray addObject:list];
}
}else {
if ([Remote IsEnableWIFI]) {
[tempArray addObject:list];
}else {
if ([list.forwordType intValue] != imageNewsForwardType) {
[tempArray addObject:list];
}
}
}
}
/**
* bug:5598(无图模式:栏目页,加载出来的图集新闻没有过滤掉)
*/
}];
NSInteger firstIndex = self.dataList.count;
[oriListData addObjectsFromArray:tempArray];
self.needReload = NO;
self.dataList = oriListData;
NSMutableArray *indexList = [NSMutableArray array];
for (NSInteger i = firstIndex;i<self.dataList.count;i++) {
[indexList addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:indexList withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
[self loadSuccess];
NSString *url = responsData[@"nextUrl"];
self.nextUrl = url;
}else {
TPLOG(@"加载数据为空");
[self loadFailed];
}
}
}
- (void)GotoSponsorAd:(sponsorBO *)sponsor {
if (isBlankString(sponsor.link)) {
return;
}
TPWebViewContoller *webVC = [[TPWebViewContoller alloc]init];
[self.navigationController pushViewController:webVC animated:YES];
webVC.url = sponsor.link;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)needrefreshNightMode:(id)sender{
UIButton *backBtn = (UIButton*)[self.view viewWithTag:backBtnTag];
if ([[TPUserDefault instance].isNightMode intValue] == 0) {
[backBtn setImage:Image(@"detailPage/backBtn.png") forState:UIControlStateNormal];
[backBtn setImage:Image(@"detailPage/backBtn_s.png") forState:UIControlStateHighlighted];
}else {
[backBtn setImage:Image(@"detailPage/backBtn_night.png") forState:UIControlStateNormal];
[backBtn setImage:Image(@"detailPage/backBtn_night_s.png") forState:UIControlStateHighlighted];
}
[self.orderButton setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:LIGHTGRAY]) forState:UIControlStateNormal];
[self.orderButton setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:BUTTONDISABLEBACK]) forState:UIControlStateSelected];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
/*
#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
|