// // ToppicListNewController.m // ThePaperHD // // Created by liyuan on 15/7/3. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "topicListHomeController.h" #import "channalView.h" #import "topicListTopView.h" #import "customFlowLayout.h" #import "topicListController.h" #import "loginPopUpController.h" #import "gambitCenterHomeController.h" #import "topicSearchController.h" #import "topicContentController.h" #import "topicListViewModel.h" #define CATEGORYTAG 1001 #define ALLDATATAG 1002 #define PADDING 35 @interface topicListHomeController (){ UIInterfaceOrientation lastOrientation; bool isCateInto; NSString *cateName; BOOL firstInto; } @property(nonatomic, strong)UILabel *topicView; @property(nonatomic, strong)channalView *titleView; @property(nonatomic, strong) topicListTopView *listTopView; @property(nonatomic, strong)gambitCenterHomeController *gambitVC; @property(nonatomic, strong)topicSearchController *topicSearchVC; @property(nonatomic, strong)topicListViewModel *listViewModel; @end @implementation topicListHomeController @synthesize index; - (void)viewDidLoad { [super viewDidLoad]; //【需求】统计-iPad(BUG:4571) [MobClick event:@"29"]; isCateInto = YES; self.sort = @"2"; cateName = @"精选"; firstInto = YES; [self leftIconButton:@"Button/backBarBtn.png" highlited:nil selector:@selector(backButtonHandler:)]; // self.navigationController.navigationBarHidden = NO; if (isIOS7) { self.automaticallyAdjustsScrollViewInsets = YES; } self.navigationController.navigationBar.translucent = YES; NSString *barColor; for (int i = 0 ; i < [TPUserDefault instance].channldataList.count; i++) { if ([[TPUserDefault instance].channldataList[i][@"nodeId"] isEqualToString:@"-3"]) { barColor =[TPUserDefault instance].channldataList[i][@"color"]; self.titleView.titleLabel.text = [TPUserDefault instance].channldataList[i][@"name"]; self.titleView.assistLabel.text = [TPUserDefault instance].channldataList[i][@"enname"]; } } if (isIOS7) { self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:barColor]; }else { self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:barColor]; } self.listViewModel.cateDic = nil; [self.view addSubview:self.listTopView]; [self.listTopView makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view.left); make.right.equalTo(self.view.right); make.top.equalTo(self.view.top).offset(64); make.height.equalTo(@55); }]; [self.collectionView makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view.left); make.right.equalTo(self.view.right); make.top.equalTo(self.view.top); make.bottom.equalTo(self.view.bottom); }]; self.collectionView.refreshControl.originalContentInsectY = 64+55; self.collectionView.refreshControl.topicFrangePageInsectY = 64+55; [self setLayoutBaseWithOrientation]; self.listTopView.topicTopDelegate = self; self.listViewModel = topicListViewModel.new; self.listViewModel.delegate = self; [self manualRefresh]; } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ NSString *barColor; for (int i = 0 ; i < [TPUserDefault instance].channldataList.count; i++) { if ([[TPUserDefault instance].channldataList[i][@"nodeId"] isEqualToString:@"-3"]) { barColor =[TPUserDefault instance].channldataList[i][@"color"]; // self.titleView.titleLabel.text = [TPUserDefault instance].channldataList[i][@"name"]; // self.titleView.assistLabel.text = [TPUserDefault instance].channldataList[i][@"enname"]; } } if (isIOS7) { self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:barColor]; }else { self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:barColor]; } }); // //在精选界面手指稍微向左滑一点等右上角的“我的话题”字样出现后,迅速向右滑至首页,则再返回精选页面右上角会出现“我的话题”字样,出现后一直存在(bug:4910) // for (int i = 0; i < self.navigationController.navigationBar.subviews.count; i++) { // if ([self.navigationController.navigationBar.subviews[i] isKindOfClass:[UILabel class]]) { // [self.navigationController.navigationBar.subviews[i] removeFromSuperview]; // } // } self.topicView.hidden = NO; self.navigationController.navigationBarHidden = NO;//(bug:5720) self.navigationItem.titleView = self.titleView; if (_topicView) { [self.topicView removeFromSuperview]; } [self.navigationController.navigationBar addSubview:self.topicView]; __weak typeof(self) Self = self; if (self.gambitVC && self.gambitVC.isNeedHold) { [self presentController:self.gambitVC animated:YES presentSize:gambitCenterPopSize completion:^{ } dismiss:^{ if (!Self.gambitVC.isNeedHold) { Self.gambitVC = nil; } }]; } if (self.topicSearchVC && self.topicSearchVC.isNeedHold) { [self presentController:self.topicSearchVC animated:YES presentSize:searchPopSize completion:^{ } dismiss:^{ if (!Self.topicSearchVC.isNeedHold) { Self.topicSearchVC = nil; } }]; } } -(void)viewWillDisappear:(BOOL)animated{ [self.topicView removeFromSuperview]; self.topicView = nil; self.topicView.hidden = YES; //在精选界面手指稍微向左滑一点等右上角的“我的话题”字样出现后,迅速向右滑至首页,则再返回精选页面右上角会出现“我的话题”字样,出现后一直存在(bug:4910) for (int i = 0; i < self.navigationController.navigationBar.subviews.count; i++) { if ([self.navigationController.navigationBar.subviews[i] isKindOfClass:[UILabel class]]) { [self.navigationController.navigationBar.subviews[i] removeFromSuperview]; } } [super viewWillDisappear:animated]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (UILabel*)topicView { if (!_topicView) { _topicView = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.navigationController.navigationBar.bounds)-150, 0, 150, CGRectGetHeight(self.navigationController.navigationBar.bounds))]; _topicView.backgroundColor = [UIColor clearColor]; _topicView.userInteractionEnabled = YES; CGFloat width = 1; if (ISNotRETINA) { width = 2; } UIButton *topicBtn = [UIButton buttonWithType:UIButtonTypeCustom]; topicBtn.backgroundColor = [UIColor clearColor]; [topicBtn setTitle:@"我的话题" forState:UIControlStateNormal]; [topicBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [topicBtn setTitleColor:[UIColor colorWithHexString:TextLightGray] forState:UIControlStateHighlighted]; topicBtn.titleLabel.font = appFont(18, NO); CGFloat btnWidth = widthForString(@"我的话题", appFont(18,0), CGRectGetHeight(self.navigationController.navigationBar.bounds), NSLineBreakByWordWrapping); topicBtn.frame = CGRectMake(CGRectGetMaxX(_topicView.bounds)-btnWidth-20, 0,btnWidth , CGRectGetHeight(self.navigationController.navigationBar.bounds)); [topicBtn addTarget:self action:@selector(goToMyTopic:) forControlEvents:UIControlEventTouchUpInside]; [_topicView addSubview:topicBtn]; UIView *topicLine = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMinX(topicBtn.frame)-10, CGRectGetHeight(_topicView.bounds)/2-10, width, 20)]; topicLine.backgroundColor = [UIColor whiteColor]; [_topicView addSubview:topicLine]; UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom]; searchBtn.frame = CGRectMake(CGRectGetMinX(topicLine.frame)-10 - 33/2, CGRectGetHeight(self.navigationController.navigationBar.bounds)/2 - 35/4, 33/2, 35/2); [searchBtn setImage:Image(@"topic/topicSearch.png") forState:UIControlStateNormal]; [searchBtn addTarget:self action:@selector(searchSelector:) forControlEvents:UIControlEventTouchUpInside]; [_topicView addSubview:searchBtn]; } return _topicView; } - (channalView*)titleView { if (!_titleView) { _titleView = [[channalView alloc]initWithFrame:CGRectMake(0, 0, 80, 43)]; _titleView.pageControl.hidden = YES; } return _titleView; } -(topicListTopView *)listTopView{ if (!_listTopView) { _listTopView = [[topicListTopView alloc]init]; } return _listTopView; } - (void)backButtonHandler:(id)sender { if (!self.isPresent) { [self.navigationController popViewControllerAnimated:YES]; }else { [self dismissViewControllerAnimated:YES completion:nil]; } } -(void)searchSelector:(UIButton *)btn{//【需求】搜索:增加话题的搜索功能(bug:4314) if (!self.topicSearchVC) { self.topicSearchVC = [[topicSearchController alloc] init]; self.topicSearchVC.delegate = self; } [self presentController:self.topicSearchVC animated:YES presentSize:searchPopSize completion:^{ } dismiss:^{ if (!self.topicSearchVC.isNeedHold) { self.topicSearchVC = nil; } }]; } #pragma nark--delegate //弹出我的话题 - (void)goToMyTopic:(UIButton*)btn { [MobClick event:@"51"]; if (![TPUserDefault instance].userBO) { loginPopUpController *loginVC = loginPopUpController.new; __weak typeof(self) Self = self; [self presentController:loginVC animated:YES presentSize:loginPopUpSize completion:^{ } dismiss:^{ if ([TPUserDefault instance].userBO) { if (!Self.gambitVC) { Self.gambitVC = gambitCenterHomeController.new; Self.gambitVC.delegate = self; } [Self presentController:Self.gambitVC animated:YES presentSize:gambitCenterPopSize completion:^{ } dismiss:^{ if (!Self.gambitVC.isNeedHold) { Self.gambitVC = nil; } }]; } }]; }else { if (!self.gambitVC) { self.gambitVC = gambitCenterHomeController.new; self.gambitVC.delegate = self; } __weak typeof(self) Self = self; [Self presentController:self.gambitVC animated:YES presentSize:gambitCenterPopSize completion:^{ } dismiss:^{ if (!Self.gambitVC.isNeedHold) { Self.gambitVC = nil; } }]; } } -(void)goToTopicList{ __weak typeof(self) Self = self; [self.gambitVC dismissControllerAnimated:YES completion:^{ if (!Self.gambitVC.isNeedHold) { Self.gambitVC = nil; } customFlowLayout *layout = nil; UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; CGSize itemSize = CGSizeZero; layout= [[customFlowLayout alloc]init]; layout.scrollDirection = UICollectionViewScrollDirectionVertical; layout.minimumLineSpacing = 35; layout.minimumInteritemSpacing = 35; CGFloat width = 0; if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { width = (self.view.bounds.size.width-35*3)/2; }else { width = (self.view.bounds.size.width-35*4)/3; } CGFloat viewScale = width/600; itemSize = CGSizeMake(width, 450*viewScale); layout.itemSize = itemSize; topicListHomeController *topicController = [[topicListHomeController alloc]initWithCollectionViewLayout:layout]; [Self.navigationController pushViewController:topicController animated:YES]; }]; } - (void)statusBarOrientationChange:(NSNotification *)notification{ [self setLayoutBaseWithOrientation]; } - (void)setRefreshControlInsectY:(CGFloat)insectY { self.collectionView.refreshControl.originalContentInsectY = 200; } - (void)setLayoutBaseWithOrientation { UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (lastOrientation != orientation && lastOrientation != UIInterfaceOrientationUnknown) { if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { self.topicView.frame = CGRectMake(618, 0, 150, 44); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.collectionLayout = [self VericalLayout]; self.collectionView.refreshControl.frame = CGRectMake(0, self.collectionView.refreshControl.frame.origin.y, CGRectGetWidth(self.collectionView.frame), self.collectionView.refreshControl.frame.size.height); }); }else { self.topicView.frame = CGRectMake(874, 0, 150,44); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.collectionLayout = [self HorizontalLayout]; self.collectionView.refreshControl.frame = CGRectMake(0, self.collectionView.refreshControl.frame.origin.y, CGRectGetWidth(self.collectionView.frame), self.collectionView.refreshControl.frame.size.height); }); }; } lastOrientation = orientation; // if (firstInto) { // self.orientation = UIInterfaceOrientationUnknown; // firstInto = NO; // }else{ // self.orientation = orientation; // // } } - (customFlowLayout*)HorizontalLayout { CGFloat width = (1024-PADDING*4)/3; CGFloat viewScale = width/600; CGSize itemSize = CGSizeMake(width, 450*viewScale); customFlowLayout *layout= [[customFlowLayout alloc]init]; layout.scrollDirection = UICollectionViewScrollDirectionVertical; layout.minimumLineSpacing = PADDING; layout.minimumInteritemSpacing = PADDING; layout.itemSize = itemSize; return layout; } - (customFlowLayout*)VericalLayout { CGFloat width = (768-PADDING*3)/2; CGFloat viewScale = width/600; CGSize itemSize = CGSizeMake(width, 450*viewScale); customFlowLayout *layout= [[customFlowLayout alloc]init]; layout.scrollDirection = UICollectionViewScrollDirectionVertical; layout.minimumLineSpacing = PADDING; layout.minimumInteritemSpacing = PADDING; layout.itemSize = itemSize; return layout; } - (UIEdgeInsets)HorizontalInsets { UIEdgeInsets top = {PADDING,PADDING,PADDING,PADDING}; return top; } - (UIEdgeInsets)VercalInsets { UIEdgeInsets top = {PADDING,PADDING,PADDING,PADDING}; return top; } #pragma mark -- get data -(void)categoryName:(categoryBO *)bo viewList:(NSMutableArray *)viewList sort:(NSString *)sort{ self.catagoryInfo = bo; cateName = bo.name; NSMutableDictionary *dic = [NSMutableDictionary dictionary]; if ([bo.name isEqualToString:@"精选"]) { self.sort = @"2"; [dic setObject:@"2" forKey:@"sort"]; [dic setObject:bo.category forKey:@"category"]; // if(firstInto){ // self.listViewModel.firstDict= dic; // firstInto = NO; // }else{ self.listViewModel.dict = dic; // } }else{ self.sort = sort; [dic setObject:sort forKey:@"sort"]; [dic setObject:bo.category forKey:@"category"]; self.listViewModel.dict = dic; } } #pragma mark -- delegate -(void)hotAndLaster:(NSDictionary *)dict{ [MobClick event:@"30"];//【需求】统计-iPad(BUG:4571) self.sort = dict[@"sort"]; self.listViewModel.dict = dict; } -(void)searchToFilter:(TopicInfoBO *)topic{//【需求】搜索:增加话题的搜索功能(bug:4314) [self.topicSearchVC dismissControllerAnimated:YES completion:^{ topicContentController *vc = [[topicContentController alloc]init]; vc.preTopicInfo = topic; userBO *user = setJsonDicToDataModel(topic.userInfo, [userBO class]); vc.creatUser = user; [self.navigationController pushViewController:vc animated:YES]; [[UIApplication sharedApplication] endIgnoringInteractionEvents]; }]; } #pragma mark -- viewModel delegate -(void)returnCategory:(NSMutableArray *)list{ self.listTopView.dataList = list; } -(void)returnAlldata:(NSMutableArray *)allDataList nextUrl:(NSString *)url{ if (allDataList &&allDataList.count >0) { [self endRefresh:^{ if ([cateName isEqualToString:@"精选"]) { NSMutableArray *noRepeatArray = [NSMutableArray array]; NSMutableArray *topicIdArray = [NSMutableArray array]; [allDataList enumerateObjectsUsingBlock:^(TopicInfoBO *obj, NSUInteger idx, BOOL *stop) { if (topicIdArray.count >0) { if (![topicIdArray containsObject:obj.topicId]) { [noRepeatArray addObject:obj]; [topicIdArray addObject:obj.topicId]; } }else{ [noRepeatArray addObject:obj]; [topicIdArray addObject:obj.topicId]; } }]; self.dataList = noRepeatArray; // [self manualRefresh]; }else{ self.dataList = allDataList; } self.nextUrl = url; } completion:^{ }]; }else{ [self endRefresh:^{ self.nextUrl = url; self.dataList = [NSMutableArray array]; } completion:^{ TPLOG(@"列表为空"); }]; } } -(void)returnBannerData:(NSMutableArray *)bannerDataList{ // self.bannerList = [NSMutableArray array]; NSMutableArray *banner = [NSMutableArray array]; [bannerDataList enumerateObjectsUsingBlock:^(NSMutableDictionary *dic, NSUInteger idx, BOOL *stop) { listContObjectVO *listBo = [listContObjectVO new]; listBo.forwordNodeId = dic[@"nodeId"]; listBo.name = dic[@"name"]; listBo.pic = dic[@"pic"]; [banner addObject:listBo]; }]; self.bannerList = banner; } -(void)returnCateData:(NSMutableArray *)cateDataList nextUrl:(NSString *)url{ isCateInto = YES; [self manualRefresh]; } -(void)returnNextUrlData:(NSMutableArray *)nextUrlData nextUrl:(NSString *)url{ if (nextUrlData && nextUrlData.count > 0) { @try { [self.collectionView performBatchUpdates:^{ NSMutableArray *oriListData = [NSMutableArray arrayWithArray:self.dataList]; [self.dataList addObjectsFromArray:nextUrlData]; NSMutableArray *indexList = [NSMutableArray array]; for (NSInteger i=oriListData.count;i