热更新demo

channalMyPaperController.m 13KB

    // // UITestViewController.m // ThePaperDemo // // Created by Scar on 14-9-3. // Copyright (c) 2014年 scar1900. All rights reserved. // #import "channalMyPaperController.h" #import "loginHomeController.h" #import "orderCenterController.h" #import "AppDelegate.h" #import "TPMainPageViewController.h" @interface channalMyPaperController () { BOOL isLogin; } @end @implementation channalMyPaperController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.noneLoginBack]; [self.view addSubview:self.noOrderedBack]; self.dataKey = @"contList"; // self.nodeId = @"-1"; /** * bug:6024(【需求】无缓存数据时,瀑布流自动刷新机制) */ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(login) name:HAVELOGIN object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginOut) name:HAVELOGINOUT object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshOrder) name:CLEARALLORDER object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshOrder) name:NEEDREFRESHORDER object:nil]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // [self manualRefresh]; } - (UIView *)noneLoginBack { if (!_noneLoginBack) { _noneLoginBack = [[UIView alloc]initWithFrame:self.view.bounds]; _noneLoginBack.backgroundColor = [UIColor clearColor]; UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom]; [loginButton setImage:Image(@"Button/orderButton.png") forState:UIControlStateNormal]; loginButton.frame = CGRectMake(CGRectGetWidth(_noneLoginBack.bounds)/2-50/2, 45, 50, 50); [loginButton addTarget:self action:@selector(loginHandler:) forControlEvents:UIControlEventTouchUpInside]; [_noneLoginBack addSubview:loginButton]; UILabel *noneLoginLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(loginButton.frame)+40, CGRectGetWidth(_noneLoginBack.bounds), 20)]; noneLoginLabel.textAlignment = NSTextAlignmentCenter; noneLoginLabel.textColor = [UIColor colorWithHexString:LINECOLOR]; noneLoginLabel.backgroundColor = [UIColor clearColor]; noneLoginLabel.text = @"登录后马上订阅你感兴趣的栏目"; noneLoginLabel.tag = 20; [_noneLoginBack addSubview:noneLoginLabel]; } UILabel *noneLoginLabel = (UILabel*)[_noneLoginBack viewWithTag:20]; noneLoginLabel.font = appFont(TEXT_FOUR_LEVELSIZE, NO); return _noneLoginBack; } - (UIView *)noOrderedBack { if (!_noOrderedBack) { _noOrderedBack = [[UIView alloc]initWithFrame:self.view.bounds]; _noOrderedBack.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; _noOrderedBack.hidden = YES; UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom]; [loginButton setImage:Image(@"Button/orderButton.png") forState:UIControlStateNormal]; loginButton.frame = CGRectMake(CGRectGetWidth(_noOrderedBack.bounds)/2-50/2, 45, 50, 50); [loginButton addTarget:self action:@selector(orderHandler:) forControlEvents:UIControlEventTouchUpInside]; [_noOrderedBack addSubview:loginButton]; UILabel *noneLoginLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(loginButton.frame)+40, CGRectGetWidth(_noOrderedBack.bounds), 20)]; noneLoginLabel.textAlignment = NSTextAlignmentCenter; noneLoginLabel.textColor = [UIColor colorWithHexString:LINECOLOR]; noneLoginLabel.backgroundColor = [UIColor clearColor]; noneLoginLabel.text = @"马上添加感兴趣的栏目到这里"; noneLoginLabel.tag = 20; [_noOrderedBack addSubview:noneLoginLabel]; } UILabel *noneLoginLabel = (UILabel*)[_noOrderedBack viewWithTag:20]; noneLoginLabel.font = appFont(TEXT_FOUR_LEVELSIZE, NO); return _noOrderedBack; } - (void)loginHandler:(UIButton*)btn { [[NSNotificationCenter defaultCenter] postNotificationName:PUSHTOLOGIN object:nil]; } - (void)refreshOrder { if (self.tableView.contentOffset.y == 0) { [self manualRefresh]; /** * bug:5604( 我的订阅频道:加载过内容,进入栏目导航页,返回,不要刷新,如果在顶部就需要刷新) */ } } - (void)orderHandler:(UIButton*)btn { if ([TPUserDefault instance].userBO) { orderCenterController *vc = orderCenterController.new; [self.navigationController pushViewController:vc animated:YES]; }else { loginHomeController *vc = [loginHomeController new]; [self.navigationController pushViewController:vc animated:YES]; } } - (void)login { isLogin = YES; self.noneLoginBack.hidden = YES;; self.tableView.hidden = NO; [self manualRefresh]; } - (void)loginOut { isLogin = NO; // AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate]; // TPMainPageViewController *mainVC=[delegate mainVC]; // [mainVC resetNaviBar]; // [mainVC disableFollowScroll]; self.noneLoginBack.hidden = NO; self.tableView.hidden = YES; [self cancelLoadMore]; [self cancelRefresh]; } - (void)pullRefreshHandler { [self remoteAction]; } - (void)pullLoadMoreHander { if (!isLogin) { return; } [Remote doJsonAction:2 requestUrl:self.nextUrl parameter:nil delegate:self]; } - (void)remoteAction { if (!isLogin) { [self endRefresh]; return; } if (isBlankString(self.nodeId)) { self.nodeId = @"-1"; } /** * bug:6157(【倒退】登录:登出后再登录其他账号,有一定概率会闪退) */ /** * bug:6168(【倒退】频道放在倒数第二个位置(默认为生活频道),进行登录必闪退) */ NSDictionary *dic = @{@"n":self.nodeId}; [Remote doJsonAction:1 requestUrl:channelNodeListURL parameter:dic delegate:self]; } - (void)pageChangeBegin:(NSInteger)index { self.currentIndex = [NSString stringWithFormat:@"%ld",(long)index]; if ([TPUserDefault instance].userBO) { self.noneLoginBack.hidden = YES; self.tableView.hidden = NO; isLogin = YES; if (isBlankString(self.nodeId)) { self.nodeId = @"-1"; } /** * bug:6024(【需求】无缓存数据时,瀑布流自动刷新机制) */ [self firstAutoRefreshHandler]; }else { self.noneLoginBack.hidden = NO; self.tableView.hidden = YES; isLogin = NO; if (isBlankString(self.nodeId)) { self.nodeId = @"-1"; } /** * bug:6024(【需求】无缓存数据时,瀑布流自动刷新机制) */ [self needNoFirstAutoRefresh]; } } - (void)startWaitCursor:(int)actionTag { } - (void)stopWaitCursor:(int)actionTag { } - (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData { if (actionTag == 1) { AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate]; TPMainPageViewController *mainVC=[delegate mainVC]; [mainVC enableFollowScroll]; self.noOrderedBack.hidden = YES; [self getRefreshDataFromDic:responsData sourceKey:self.dataKey contentClass:self.dataClass]; [self saveRemoteDataToCoreData:responsData]; [self endRefresh]; }else if (actionTag == 2){ 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 *contentBO = setJsonDicToDataModel(obj, [listContObjectVO class]); if ([contentBO isKindOfClass:[listContObjectVO class]]) { listContObjectVO *list = contentBO; if ([list.cardMode intValue] != ChildListCardType) { if ([list.cardMode intValue] == HeadCardType || [list.cardMode intValue] == BigCardType || [list.cardMode intValue] == SmallCardType || [list.cardMode intValue] == HotAskCardType || [list.cardMode intValue] == AdCardType){ // if ([list.cardMode intValue] == AdCardType) { // list.adUrl = @"http://ad.thepaper.cn/s?z=paper&c=1104&op=1"; // } 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]; } } } }else if ([list.cardMode intValue] == HotTopicListType){ [tempArray addObject:list.title?list.title:@""]; [list.childList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { TopicInfoBO *topicInfoBO = setJsonDicToDataModel(obj, [TopicInfoBO class]); [tempArray addObject:topicInfoBO]; }]; } }else if ([list.cardMode intValue] == ChildListCardType){ [tempArray addObject:list.title?list.title:@""]; [list.childList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { listContObjectVO *childListBO = setJsonDicToDataModel(obj, [listContObjectVO class]); childListBO.isChildList = @"1"; if ([[TPUserDefault instance].readModeStr intValue] == imageMode) { [tempArray addObject:childListBO]; }else if ([[TPUserDefault instance].readModeStr intValue] == textMode) { if (![list.forwordType intValue] == imageNewsForwardType) { [tempArray addObject:childListBO]; } }else { if ([Remote IsEnableWIFI]) { [tempArray addObject:childListBO]; }else { if (![list.forwordType intValue] == imageNewsForwardType) { [tempArray addObject:childListBO]; } } } }]; } }else { [tempArray addObject:contentBO]; } }]; 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)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code{ if ([code intValue] == 6) { // AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate]; // TPMainPageViewController *mainVC=[delegate mainVC]; // [mainVC resetNaviBar]; // [mainVC disableFollowScroll]; self.noOrderedBack.hidden = NO; }else { ShowTextMessage(message); } [self endRefresh]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:HAVELOGIN object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:HAVELOGINOUT object:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end