热更新demo

topicListController.m 16KB

    // // topicListController.m // ThePaperBase // // Created by YoungLee on 15/8/10. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "topicListController.h" #import "channelTopicContentCell.h" #import "topicListCell.h" #define ALLDATATAG 1001 #define NEXTURLTAG 1002 @interface topicListController (){ BOOL needManualRefresh; //需要接受通知刷新标志位 NSMutableArray *heightList; NSMutableArray *dataSourceList; NSMutableArray *cateList; BOOL isFirstInto; } @property(nonatomic, strong) NSMutableArray *dataList; @property(nonatomic, strong)NSString *cateId; @property(nonatomic, strong)NSString *oldCate; @property(nonatomic, strong)UIView *hiddenView; @end @implementation topicListController @synthesize cateBO = _cateBO; @synthesize hotOrLastr = _hotOrLastr; @synthesize cateId = _cateId; //static NSString *topicContentCellID = @"channelTopicContentCell"; static NSString *topicContentCellID = @"topicListCell"; - (void)pageChangeBegin:(NSInteger)index { self.currentIndex = [NSString stringWithFormat:@"%ld",(long)index]; [self firstAutoRefreshHandler]; if (needManualRefresh) {//切换到页面检查是否需要刷新 [self manualRefresh]; needManualRefresh = NO; } if (self.dataList.count == 0) { self.cateId = self.cateBO.category; } } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; isFirstInto = YES; needManualRefresh = NO; self.tableView.clipsToBounds = YES; self.tableView.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; [self configCanRefresh:YES canLoad:YES]; [self.view addSubview:self.hiddenView]; self.hiddenView.frame = self.tableView.bounds; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // [self.tableView registerClass:[channelTopicContentCell class] forCellReuseIdentifier:topicContentCellID]; [self.tableView registerClass:[topicListCell class] forCellReuseIdentifier:topicContentCellID]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshFrontPage) name:NEEDREFRESHENTERFORGROUND object:nil]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //进入后台后回到前台的间隔时间大于一小时 - (void)refreshFrontPage { needManualRefresh = YES; if (self.currentIndex && [self.currentIndex intValue] == [TPUserDefault instance].currentPageIndex) { needManualRefresh = NO; [self manualRefresh]; } } -(void)pullRefreshHandler { if (isFirstInto) { self.hiddenView.hidden = NO; isFirstInto = NO; }else{ self.hiddenView.hidden = YES; } [self remoteAction]; } -(void)pullLoadMoreHander { if (isBlankString(self.nextUrl)) { //TPLOG(@"数据有问题,无法获取新数据 "); [self loadFailed]; return; } NSString *url = [self.nextUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [Remote doJsonAction:NEXTURLTAG requestUrl:url parameter:nil delegate:self]; } - (void)remoteAction { if (isBlankString(_hotOrLastr)) { _hotOrLastr = @"1"; } //_hotOrLastr==0 新 _hotOrLastr== 1 热 NSDictionary *dic = @{@"sort":_hotOrLastr,@"category":_cateBO.category}; [Remote doJsonAction:ALLDATATAG requestUrl:topicListURL parameter:dic delegate:self]; } -(void)setCateBO:(categoryBO *)bo { _cateBO = bo; } -(void)setHotOrLastr:(NSString *)str { if (isBlankString(str)) { return; } self.oldCate = self.hotOrLastr; if (![_hotOrLastr isEqualToString:str]) { _hotOrLastr = str; self.cateId = self.cateBO.category; // [self manualRefresh]; } } - (void)changeHotOrLast:(NSString*)hotOrLast { if (isBlankString(hotOrLast)) { return; } if (![_hotOrLastr isEqualToString:hotOrLast]) { _hotOrLastr = hotOrLast; [self manualRefresh]; } } -(UIView *)hiddenView{ if (!_hiddenView) { _hiddenView = [UIView new]; _hiddenView.backgroundColor = [UIColor clearColor]; _hiddenView.hidden = YES; } return _hiddenView; } #pragma mark -- click top - (void)scrollTableViewToTop { [self.tableView setContentOffset:CGPointZero animated:YES]; } #pragma mark - tableView delegate and dataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return dataSourceList.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // if (heightArray.count > 0) { // return [heightArray[indexPath.row] floatValue]; // }else return 0.f; return [heightList[indexPath.row] floatValue]; } - (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { return [self getContentCell:table cellForRowAtIndexPath:indexPath]; } //问题内容 - (topicListCell *)getContentCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { topicListCell *cell = [tableView dequeueReusableCellWithIdentifier:topicContentCellID]; if (nil == cell) { cell = [[topicListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:topicContentCellID]; } cell.indexPath = indexPath; TopicInfoBO *topicInfo = dataSourceList[indexPath.row]; cell.topic = topicInfo; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // id data = dataSourceList[indexPath.row]; [tableView deselectRowAtIndexPath:indexPath animated:NO]; if ([[tableView cellForRowAtIndexPath:indexPath] isKindOfClass:[channelTopicContentCell class]]) { channelTopicContentCell *cell = (channelTopicContentCell*)[tableView cellForRowAtIndexPath:indexPath]; [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); } if ([self.delegate respondsToSelector:@selector(gotoInfo:)]) { [self.delegate gotoInfo:self.dataList[indexPath.row]]; } }]; } // topicContentController *topic = [topicContentController new]; // topic.preTopicInfo = data; // [self.navigationController pushViewController:topic animated:YES]; /** * bug:5232(问吧,点击问吧卡片需要有点击效果) */ } #pragma mark -- set data - (void)setDataList:(NSMutableArray *)data { _dataList = data; heightList = [NSMutableArray array]; dataSourceList = [NSMutableArray array]; [data enumerateObjectsUsingBlock:^(TopicInfoBO* obj, NSUInteger idx, BOOL *stop) { CGFloat scale = 0.75; obj.isFirstCard = @"0"; [dataSourceList addObject:obj]; CGFloat height = (rect_screen.size.width-20)*scale+10; [heightList addObject:[NSString stringWithFormat:@"%f",height]]; }]; [self.tableView reloadData]; } - (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData { if (ALLDATATAG== actionTag) { NSArray *list = responsData[@"topicList"]; NSMutableArray *temp = [NSMutableArray array]; [list enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL *stop) { if (![dic[@"forwordType"] isEqualToString:@"4"]) { TopicInfoBO *bo = setJsonDicToDataModel(dic, [TopicInfoBO class]); [temp addObject:bo]; } }]; self.dataList = temp; // if([self.hotOrLastr isEqualToString:@"1"]){ [self saveRemoteDataToCoreData:responsData]; // } [self endRefresh]; self.nextUrl = responsData[@"nextUrl"]; self.hiddenView.hidden = YES; }else if(NEXTURLTAG == actionTag){ NSMutableArray *oldArray = [NSMutableArray arrayWithArray:self.dataList]; NSMutableArray *tempList = responsData[@"topicList"]; [tempList enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL *stop) { if (![dic[@"forwordType"] isEqualToString:@"4"]) { TopicInfoBO *bo = setJsonDicToDataModel(dic, [TopicInfoBO class]); [oldArray addObject:bo]; } }]; self.dataList = oldArray; [self loadSuccess]; NSString *url = responsData[@"nextUrl"]; self.nextUrl = url; } } - (void)saveRemoteDataToCoreData:(id)resData { NSError *error; NSFetchRequest *request = [[NSFetchRequest alloc]init]; //为已创建好的实体利用检索到的上下文创建一个实体描述 NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"ListTopicDB" inManagedObjectContext:[CoreDataManager shareInstance].managedObjectContext]; [request setEntity:entityDescription]; //确定持久库中是否存在与此字段相对应的托管对象,所以穿件一个谓词来确定字段的正确对象: NSPredicate *pred = [NSPredicate predicateWithFormat:@"category == %@ and cateStr == %@",self.cateBO.category,self.hotOrLastr]; [request setPredicate:pred]; ListTopicDB *listContPageDB = nil; NSArray *objs = [[CoreDataManager shareInstance].managedObjectContext executeFetchRequest:request error:&error]; if (objs == nil) { TPLOG(@"there was an error!!"); } if (objs.count > 0) { listContPageDB = [objs objectAtIndex:0]; [[CoreDataManager shareInstance].managedObjectContext deleteObject:listContPageDB]; } ListTopicDB *listDB = nil; listDB = [NSEntityDescription insertNewObjectForEntityForName:@"ListTopicDB" inManagedObjectContext:[CoreDataManager shareInstance].managedObjectContext]; listDB = makeDicToCoreDataModel(resData, listDB); listDB.date = getLocalDate(); listDB.category = self.cateBO.category; listDB.cateStr = self.hotOrLastr; [[CoreDataManager shareInstance] saveContext]; } - (void)getRefreshDataFromDic:(NSDictionary*)sourceDic sourceKey:(NSString*)sourceKey contentClass:(Class)contentClass{ NSArray *contList = sourceDic[sourceKey]; if (contList && contList.count > 0) { NSMutableArray *tempArray = [NSMutableArray array]; [contList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { id contentBO = setJsonDicToDataModel(obj, contentClass); 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]; } }]; self.dataList = tempArray; NSString *url = sourceDic[@"nextUrl"]; self.nextUrl = url; }else { self.dataList = [NSMutableArray array]; TPLOG(@"话题列表为空"); } } -(void)setCateId:(NSString *)cateID{ _cateId = cateID; // if (self.dataList.count == 0) { NSError *error; NSFetchRequest *request = [[NSFetchRequest alloc]init]; //为已创建好的实体利用检索到的上下文创建一个实体描述 NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"ListTopicDB" inManagedObjectContext:[CoreDataManager shareInstance].managedObjectContext]; [request setEntity:entityDescription]; //确定持久库中是否存在与此字段相对应的托管对象,所以穿件一个谓词来确定字段的正确对象: if (isBlankString(_hotOrLastr)) { _hotOrLastr = @"1"; } NSPredicate *pred = [NSPredicate predicateWithFormat:@"category == %@ and cateStr == %@",_cateId,_hotOrLastr]; [request setPredicate:pred]; ListTopicDB *listContPageDB = nil; NSArray *objs = [[CoreDataManager shareInstance].managedObjectContext executeFetchRequest:request error:&error]; if (objs == nil) { TPLOG(@"there was an error!!"); } if (objs.count > 0) { listContPageDB = [objs objectAtIndex:0]; NSDictionary *coreDataDic = makeCoreDataModelToDic(listContPageDB, [ListTopicDB class]); [self getRefreshDataFromDic:coreDataDic sourceKey:@"topicList" contentClass:[TopicInfoBO class]]; } else { if (![self.oldCate isEqualToString:self.hotOrLastr]) { [self manualRefresh]; } } // } } @end