// // FLFindController.m // FirstLink // // Created by jack on 15/8/21. // Copyright (c) 2015年 FirstLink. All rights reserved. // #import "FLExploreController.h" #import "FLControllerHelper.h" #import "FLProgressHUDHelper.h" #import "FKTopicController.h" #import "PindanDetailController.h" #import "FKProDetailController.h" #import "FKExploreListController.h" #import "FKDownloadRemotefile.h" #import "FKDownloadStore.h" #import "FKExploreTopicListItem.h" #import "FKExploreRequest.h" #import "FKExploreReform.h" #import "FKExploreViewModel.h" #import "FKHistoryKeywordView.h" #import "FKExploreCollectionCell.h" #import "FKExploreTableViewCell.h" #import static NSString *const FKExploreCollectionCellIdentify = @"FKExploreCollectionCellIdentify"; static NSString *const FKExploreTableViewCellIdentify = @"FKExploreTableViewCellIdentify"; @interface FLExploreController () @property (nonatomic, strong) HMSegmentedControl *segmentedControl; @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) FKExploreViewModel *viewModel; @end @implementation FLExploreController - (void)viewDidLoad{ [super viewDidLoad]; [self addAllSubviews]; [self requestExploreTopicTags]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationItem.title = @"专题"; [self.navigationController setNavigationBarHidden:NO animated:YES]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } #pragma mark - Request - (void)requestExploreTopicTags { [FKExploreRequest requestExploreTopicTags:FKExploreRequestTopicTags delegate:self]; } - (void)requestExploreTopicList:(int)identify index:(NSInteger)index startRow:(NSString *)startRow { NSString *tagID = [self.viewModel topicTagIDAtIndex:index]; [self.hudView show:YES]; [FKExploreRequest requestExploreTopicDetail:identify tagID:tagID startRow:startRow pageSize:[NSString stringWithFormat:@"%d", FKExploreRequestPageSize] delegate:self userInfo:@{FKExploreTopicTagKey: tagID}]; } - (void)checkAndRequestTopicList { NSString *tagID = [self.viewModel topicTagIDAtIndex:self.segmentedControl.selectedSegmentIndex]; if (tagID.length > 0) { NSMutableArray *cacheArray = [self.viewModel.topicListCache valueForKey:tagID]; if (cacheArray.count == 0) { [self requestExploreTopicList:FKExploreRequestTopicList index:self.segmentedControl.selectedSegmentIndex startRow:@"0"]; } } } #pragma mark - Response - (void)networkDidReceiveError:(NSError*)error identify:(int)identify header:(MSGHeader*)header { [self.hudView hide:NO]; [self finishLoadingData]; [FLProgressHUDHelper showText:header.msg inView:self.view]; } - (void)networkDidSuccessResponse:(NSDictionary*)response identify:(int)identify header:(MSGHeader*)header userInfo:(NSDictionary *)userInfo { [self.hudView hide:NO]; self.refreshControl.bottomEnabled = YES; if (header.code.intValue == RESPONSE_MSG_NORMAL) { if (identify == FKExploreRequestTopicTags) { self.viewModel.topicTagArray = [FKExploreReform parserExploreTopicTags:response]; self.segmentedControl.sectionTitles = self.viewModel.sectionTitlesArray; [self.segmentedControl setNeedsDisplay]; [self requestExploreTopicList:FKExploreRequestTopicList index:0 startRow:@"0"]; } else { [self finishLoadingData]; if (identify == FKExploreRequestTopicList) { NSMutableArray *items = [FKExploreReform parserExploreTopicList:response]; NSString *tagID = [userInfo valueForKey:FKExploreTopicTagKey]; if (tagID && items.count > 0) { [self.viewModel.topicListCache setValue:items forKey:tagID]; [self.collectionView reloadData]; } } else if (identify == FKExploreRequestTopicListNextPage) { NSMutableArray *items = [FKExploreReform parserExploreTopicList:response]; if (items.count > 0) { NSString *tagID = [userInfo valueForKey:FKExploreTopicTagKey]; if (tagID) { NSMutableArray *mutableArray = [self.viewModel.topicListCache valueForKey:tagID]; [mutableArray addObjectsFromArray:items]; [self.collectionView reloadData]; } } else { self.refreshControl.bottomEnabled = NO; } } } } else { [self finishLoadingData]; [FLProgressHUDHelper showText:header.msg inView:self.view]; } } #pragma mark - FKExploreCellDelegate - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction { self.refreshControl = refreshControl; if (direction == RefreshDirectionTop) { [self.viewModel.contensOffsetArray removeAllObjects]; if (self.segmentedControl.selectedSegmentIndex == 0) { [self requestExploreTopicTags]; } else { [self requestExploreTopicList:FKExploreRequestTopicList index:self.segmentedControl.selectedSegmentIndex startRow:@"0"]; } } else if (direction == RefreshDirectionBottom) { NSString *tagID = [self.viewModel topicTagIDAtIndex:self.segmentedControl.selectedSegmentIndex]; [self requestExploreTopicList:FKExploreRequestTopicListNextPage index:self.segmentedControl.selectedSegmentIndex startRow:[self.viewModel startRowWithTagID:tagID]]; } } // 专题大图点击事件 - (void)exploreCellClickItem:(NSInteger)index { NSString *tagID = [self.viewModel topicTagIDAtIndex:self.segmentedControl.selectedSegmentIndex]; NSArray *topicList = [self.viewModel validTopicListWithTagID:tagID]; if (index < topicList.count) { FKExploreTopicListItem *item = topicList[index]; FKTopicController *controller = [[FKTopicController alloc]init]; controller.topicId = item.topicID; [self pushViewController:controller]; } } #pragma mark - UICollectionViewDataSource - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.viewModel.topicTagArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { FKExploreCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:FKExploreCollectionCellIdentify forIndexPath:indexPath]; [cell.tableView registerClass:[FKExploreTableViewCell class] forCellReuseIdentifier:FKExploreTableViewCellIdentify]; cell.delegate = self; cell.viewModel = self.viewModel; cell.tableView.dataSource = self; cell.tableView.tag = indexPath.row; [cell.tableView reloadData]; return cell; } #pragma mark - UICollectionViewDelegate - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if (scrollView == self.collectionView) { CGFloat offsetX = scrollView.contentOffset.x; int index = (int)((offsetX + 1)/UISCREENWIDTH); [self.segmentedControl setSelectedSegmentIndex:index animated:YES]; [self checkAndRequestTopicList]; } } #pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSInteger index = tableView.tag; NSString *tagID = [self.viewModel topicTagIDAtIndex:index]; return [self.viewModel validTopicListWithTagID:tagID].count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { FKExploreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FKExploreTableViewCellIdentify]; NSInteger index = tableView.tag; NSString *tagID = [self.viewModel topicTagIDAtIndex:index]; [cell configCell:[self.viewModel validTopicListItemWithTagID:tagID atIndex:indexPath.row]]; cell.collectionView.delegate = self; cell.collectionView.tag = indexPath.row; NSString *tag = [NSString stringWithFormat:@"%ld", (long)tableView.tag]; CGFloat offset = [self.viewModel contentOffsetForKey:tag.hash^indexPath.hash]; [cell.collectionView setContentOffset:CGPointMake(offset, 0) animated:NO]; return cell; } #pragma mark - UITableViewDelegate /** * 单个专题下商品列表点击事件 */ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (collectionView != self.collectionView) { NSInteger segmentIndex = self.segmentedControl.selectedSegmentIndex; NSString *tagID = [self.viewModel topicTagIDAtIndex:segmentIndex]; NSArray *topicArray = [self.viewModel validTopicListWithTagID:tagID]; if (collectionView.tag < topicArray.count) { FKExploreTopicListItem *item = topicArray[collectionView.tag]; if (indexPath.row < item.subItemArray.count) { UIViewController *controller; FKExploreTopicListSubItem *subItem = item.subItemArray[indexPath.row]; if ([item.objectType isEqualToString:@"10"]) { controller = [[FKProDetailController alloc] initWithProductID:subItem.itemID]; } else { controller = [[PindanDetailController alloc] initWithUserID:subItem.userID postID:subItem.itemID]; } [self pushViewController:controller]; } if (indexPath.row == item.subItemArray.count) { if ([self respondsToSelector:@selector(exploreCellClickItem:)]) { [self exploreCellClickItem:collectionView.tag]; } } } } } #pragma mark - Property - (HMSegmentedControl *)segmentedControl { if (!_segmentedControl) { _segmentedControl = [HMSegmentedControl new]; _segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown; _segmentedControl.selectionIndicatorHeight = 2; _segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 15, -8, 15); _segmentedControl.selectionIndicatorEdgeInsets = UIEdgeInsetsMake(0, 40, -8, 40); _segmentedControl.selectionIndicatorColor = UIColorFromRGB(0xff6362); _segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe; _segmentedControl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : [UIFont systemFontOfSize:14]}; _segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor grayColor], NSFontAttributeName : [UIFont systemFontOfSize:14]}; _segmentedControl.borderType = HMSegmentedControlBorderTypeBottom; _segmentedControl.borderColor = UIColorFromRGB(0xe5e5e5); _segmentedControl.borderWidth = 0.5; _segmentedControl.type = HMSegmentedControlTypeText; _segmentedControl.segmentWidthStyle = HMSegmentedControlSegmentWidthStyleDynamic; WeakSelf(weakSelf); _segmentedControl.indexChangeBlock = ^(NSInteger index) { if (index < (weakSelf.viewModel.topicTagArray.count)) { [weakSelf checkAndRequestTopicList]; [weakSelf.collectionView setContentOffset:CGPointMake(UISCREENWIDTH*index, 0) animated:YES]; } }; } return _segmentedControl; } - (UICollectionView *)collectionView { if (!_collectionView) { CGFloat height = (UISCREENHEIGH - 44 - CGRectGetHeight([UIApplication sharedApplication].statusBarFrame) - CGRectGetHeight(self.navigationController.navigationBar.frame)); UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new]; [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal]; layout.minimumLineSpacing = 0; layout.minimumInteritemSpacing = 0; layout.itemSize = CGSizeMake(UISCREENWIDTH, height); layout.sectionInset = UIEdgeInsetsZero; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _collectionView.showsVerticalScrollIndicator = NO; _collectionView.pagingEnabled = YES; _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.backgroundColor = [UIColor whiteColor]; _collectionView.contentInset = UIEdgeInsetsZero; [_collectionView registerClass:[FKExploreCollectionCell class] forCellWithReuseIdentifier:FKExploreCollectionCellIdentify]; } return _collectionView; } - (FKExploreViewModel *)viewModel { if (!_viewModel) { _viewModel = [FKExploreViewModel new]; } return _viewModel; } #pragma mark - Layout - (void)addAllSubviews { [self.view addSubview:self.segmentedControl]; [self.segmentedControl mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.right.equalTo(self.view); make.height.mas_equalTo(44); }]; [self.view addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.bottom.right.equalTo(self.view); make.top.equalTo(self.segmentedControl.mas_bottom); }]; } - (void)pushViewController:(UIViewController*)controller { controller.hidesBottomBarWhenPushed = YES; dispatch_async(dispatch_get_main_queue(), ^{ [[FLControllerHelper currentController].navigationController pushViewController:controller animated:YES]; }); } @end