// // KBFindMainViewController.m // YouHuiProject // // Created by xiaoxi on 2018/1/16. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBFindMainViewController.h" #import "KBFindNavBarView.h" #import "KBCollectionView.h" #import "KBFindTopView.h" #import "KBFindMiddleView.h" #import "KBGoodCollectionCell.h" #import "KBFindRequestViewModel.h" #import "KBGoodDetailViewController.h" #import "KBFindNavBarStaticView.h" #import "KBGoodListViewController.h" #import "KBFindChannelViewController.h" #import "PYSearch.h" #import "KBFindSearchResultViewController.h" #import "KBFindBookWebViewController.h" #define TopViewHeight ((self.hotSearchArr.count-1)/3+1)*FITSIZE(75)+(((self.hotSearchArr.count-1)/3)*FITSIZE(10))+FITSIZE(60) #define MiddleViewHeight ((self.channelArr.count-1)/4+1)*FITSIZE(66)+(((self.channelArr.count-1)/4)*FITSIZE(26))+FITSIZE(70) static NSString *const cellID = @"KBGoodCollectionCell"; static NSString *const collectionViewNavBar = @"navBar"; static NSString *const collectionViewTop = @"top"; static NSString *const collectionViewMiddle = @"middle"; static NSString *const collectionViewBottom = @"bottom"; static NSInteger page = 1; @interface KBFindMainViewController () @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSMutableArray *guessYouLikeArr; @property (nonatomic, strong) NSMutableArray *hotSearchArr; @property (nonatomic, strong) NSMutableArray *channelArr; @property (nonatomic, strong) KBFindNavBarStaticView *navBarStaticView; @property (nonatomic, strong) KBFindNavBarView *navBarView; @property (nonatomic, strong) YYFPSLabel *fpsLabel; @property (nonatomic, strong) NSMutableArray *everyoneSearchArr; @property (nonatomic, strong) NSNumber *couponTotalNum; @end @implementation KBFindMainViewController - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [SVProgressHUD dismiss]; } - (void)viewDidLoad { [super viewDidLoad]; [self initHUD]; [self initNavBar]; [self request]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changedSexRequest) name:ChangeSex object:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)initNavBar { self.navigationBar.hidden = YES; [self.view addSubview:self.navBarStaticView]; } - (void)initSubviews { [self.view addSubview:self.collectionView]; [self.collectionView reloadData]; [self.view bringSubviewToFront:self.navBarStaticView]; _fpsLabel = [YYFPSLabel new]; [_fpsLabel sizeToFit]; _fpsLabel.left = 0; _fpsLabel.bottom = kScreenHeight - TabbarHeight; _fpsLabel.alpha = 0; // [self.view addSubview:_fpsLabel]; } #pragma mark - HUD - (void)initHUD { [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom]; [SVProgressHUD setForegroundColor:[UIColor YHColorWithHex:0xff2420]]; [SVProgressHUD setBackgroundColor:[UIColor YHColorWithHex:0xf5f4f4]]; } #pragma mark - request - (void)request { [SVProgressHUD show]; // [self requestHotSearch]; [self requestChannel]; [self requestEveryoneSearch]; [self requestCouponTotal]; } - (void)changedSexRequest { [self.hotSearchArr removeAllObjects]; [self.channelArr removeAllObjects]; [self.everyoneSearchArr removeAllObjects]; page = 1; [self request]; } - (void)requestHotSearch { [KBFindRequestViewModel requestHotSearchSuccess:^(NSArray *array) { [self.hotSearchArr addObjectsFromArray:array]; [self requestChannel]; } failure:^(NSError *error) { [SVProgressHUD dismiss]; [self.collectionView.mj_header endRefreshing]; }]; } - (void)requestChannel { [KBFindRequestViewModel requestChannelSuccess:^(NSArray *array) { [self.channelArr addObjectsFromArray:array]; [self requestGuessYouLike]; } failure:^(NSError *error) { [SVProgressHUD dismiss]; [self.collectionView.mj_header endRefreshing]; }]; } - (void)requestGuessYouLike { [KBFindRequestViewModel requestGuessYouLikeParamPage:page success:^(NSArray *array) { if ([self.collectionView.mj_header isRefreshing]) { [self.guessYouLikeArr removeAllObjects]; } [SVProgressHUD dismiss]; [self.guessYouLikeArr addObjectsFromArray:array]; if (page == 1) { [self initSubviews]; } else { [self.collectionView reloadData]; } [self.collectionView.mj_header endRefreshing]; [self.collectionView.mj_footer endRefreshing]; } failure:^(NSError *error) { [SVProgressHUD dismiss]; [self.collectionView.mj_header endRefreshing]; [self.collectionView.mj_footer endRefreshing]; }]; } - (void)requestEveryoneSearch { [KBFindRequestViewModel requestEveryoneSearchSuccess:^(NSArray *array) { if (array.count > 0) { for (NSDictionary *dict in array) { [self.everyoneSearchArr addObject:dict]; } } } failure:^(NSError *error) { }]; } - (void)requestCouponTotal { [KBFindRequestViewModel requestCouponTotalSuccess:^(NSNumber *num) { self.couponTotalNum = num; } failure:^(NSError *error) { }]; } #pragma mark - navBarView - (void)yh_FinNavBarViewClickHelp { KBFindBookWebViewController *bookVC = [[KBFindBookWebViewController alloc] init]; [self.navigationController pushViewController:bookVC animated:YES]; } - (void)yh_FindNavBarViewClickSearch { [self createSearchViewControllerWith:nil]; } #pragma mark - navBarStaticView - (void)yh_FindNavBarStaticViewClickSearch { [self createSearchViewControllerWith:nil]; } #pragma mark - topView - (void)yh_FindTopViewDidSelectItemAtIndexPath:(NSIndexPath *)indexPath { KBFindHotSearchModel *model = self.hotSearchArr[indexPath.item]; [self createSearchViewControllerWith:model.name]; } #pragma mark - middleView - (void)yh_FindMiddleViewDidSelectItemAtIndexPath:(NSIndexPath *)indexPath { KBFindChannelModel *model = self.channelArr[indexPath.item]; KBFindChannelViewController *child = [[KBFindChannelViewController alloc] init]; child.model = model; [self.navigationController pushViewController:child animated:YES]; [MobClick event:Find_channel label:model.name]; } #pragma mark - search - (void)createSearchViewControllerWith:(NSString *)text { PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:@[] searchBarPlaceholder:@"输入商品名或粘贴淘宝标题"]; searchViewController.searchHistoryStyle = PYSearchHistoryStyleARCBorderTag; searchViewController.hotSearchStyle = PYHotSearchStyleARCBorderTag; searchViewController.searchTextField.font = [UIFont systemFontOfSize:13]; searchViewController.delegate = self; searchViewController.searchBar.text = text; [self getHotSearchToSearchViewController:searchViewController]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchViewController]; [self presentViewController:nav animated:NO completion:^{ for (int i = 0; i < searchViewController.hotSearchTags.count; i++) { UILabel *tag = searchViewController.hotSearchTags[i]; NSDictionary *dic = self.everyoneSearchArr[i]; if ([dic[@"color"] boolValue]) { tag.layer.borderColor = [UIColor homeRedColor].CGColor; tag.textColor = [UIColor homeRedColor]; } } }]; searchViewController.didSearchBlock = ^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) { [MobClick event:search_count label:Search_Normal]; //处理搜索点击事件 KBFindSearchResultViewController *resultVC = [[KBFindSearchResultViewController alloc] init]; resultVC.searchBar = searchBar; resultVC.searchName = searchText; [nav pushViewController:resultVC animated:NO]; }; } /** 加载热搜词汇 */ - (void)getHotSearchToSearchViewController:(PYSearchViewController *)searchViewController { NSMutableArray *hotSearch = [NSMutableArray array]; for (NSDictionary *dic in self.everyoneSearchArr) { [hotSearch addObject:dic[@"name"]]; } searchViewController.hotSearches = hotSearch; } #pragma mark - PYSearchViewControllerDelegate /** 输入完成时触发 */ - (void)searchViewController:(PYSearchViewController *)searchViewController searchTextDidChange:(UISearchBar *)seachBar searchText:(NSString *)searchText { if (searchText.length) { [KBHttp get:SearchAdvice params:@{@"name":searchText} success:^(id json) { NSArray *list = json[@"data"]; searchViewController.searchSuggestions = list; } failure:^(NSError *error) { }]; } } /** 点击建议 */ - (void)searchViewController:(PYSearchViewController *)searchViewController didSelectSearchSuggestionAtIndex:(NSInteger)index searchText:(NSString *)searchText { KBFindSearchResultViewController *resultVC = [[KBFindSearchResultViewController alloc] init]; resultVC.searchName = searchText; resultVC.searchBar = searchViewController.searchBar; [searchViewController.navigationController pushViewController:resultVC animated:NO]; } /** 点击热搜 */ - (void)searchViewController:(PYSearchViewController *)searchViewController didSelectHotSearchAtIndex:(NSInteger)index searchText:(NSString *)searchText { KBFindSearchResultViewController *resultVC = [[KBFindSearchResultViewController alloc] init]; resultVC.searchName = searchText; resultVC.searchBar = searchViewController.searchBar; [searchViewController.navigationController pushViewController:resultVC animated:NO]; [MobClick event:search_count label:Search_Hot_String]; } /** 点击历史 */ - (void)searchViewController:(PYSearchViewController *)searchViewController didSelectSearchHistoryAtIndex:(NSInteger)index searchText:(NSString *)searchText { KBFindSearchResultViewController *resultVC = [[KBFindSearchResultViewController alloc] init]; resultVC.searchName = searchText; resultVC.searchBar = searchViewController.searchBar; [searchViewController.navigationController pushViewController:resultVC animated:NO]; [MobClick event:search_count label:Search_History_String]; } #pragma mark - scrollView - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat offsetY = scrollView.contentOffset.y; CGFloat standard = FITSIZE(80); // if (offsetY >= standard) { // self.navBarView.hidden = YES; // self.navBarStaticView.hidden = NO; // } // else { // self.navBarView.hidden = NO; // self.navBarStaticView.hidden = YES; // } if (offsetY >= 0) { self.collectionView.backgroundView.backgroundColor = [UIColor clearColor]; self.collectionView.backgroundColor = [UIColor clearColor]; } else { self.collectionView.backgroundView.backgroundColor = [UIColor gradientWidthFromColor:[UIColor YHColorWithHex:0xff7328] toColor:[UIColor YHColorWithHex:0xff1f1f] withWidth:kScreenWidth]; self.collectionView.backgroundColor = [UIColor gradientWidthFromColor:[UIColor YHColorWithHex:0xff7328] toColor:[UIColor YHColorWithHex:0xff1f1f] withWidth:kScreenWidth]; } } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { if (_fpsLabel.alpha == 0) { [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ _fpsLabel.alpha = 1; } completion:NULL]; } } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { if (!decelerate) { if (_fpsLabel.alpha != 0) { [UIView animateWithDuration:1 delay:2 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ _fpsLabel.alpha = 0; } completion:NULL]; } } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if (_fpsLabel.alpha != 0) { [UIView animateWithDuration:1 delay:2 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ _fpsLabel.alpha = 0; } completion:NULL]; } } - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView { if (_fpsLabel.alpha == 0) { [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ _fpsLabel.alpha = 1; } completion:^(BOOL finished) { }]; } } #pragma mark - collectionView - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 3; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (section == 2) { return self.guessYouLikeArr.count; } else { return 0; } } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { KBGoodCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath]; cell.backgroundColor = [UIColor whiteColor]; cell.backgroundView.backgroundColor = [UIColor whiteColor]; cell.contentView.backgroundColor = [UIColor whiteColor]; if (self.guessYouLikeArr.count >= indexPath.item) { KBChildGoodModel *model = self.guessYouLikeArr[indexPath.item]; cell.model = model; } return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *view = nil; if (indexPath.section == 0) { KBFindMiddleView *middleView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewMiddle forIndexPath:indexPath]; middleView.dataSource = self.channelArr; middleView.delegate = self; view = middleView; return view; } else if (indexPath.section == 1) { UICollectionReusableView *bottomView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewBottom forIndexPath:indexPath]; bottomView.backgroundColor = [UIColor YHColorWithHex:0xf5f4f4]; UIView *likeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, FITSIZE(50))]; likeView.backgroundColor = [UIColor clearColor]; [bottomView addSubview:likeView]; UIImageView *likeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, FITSIZE(95), FITSIZE(13))]; likeImageView.center = likeView.center; likeImageView.backgroundColor = [UIColor clearColor]; likeImageView.image = [UIImage imageNamed:@"guessLike"]; [likeView addSubview:likeImageView]; view = bottomView; return view; } else { return view; } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { // if (section == 0) { // return CGSizeMake(kScreenWidth, FITSIZE(160)); // } // else if (section == 1) { // return CGSizeMake(kScreenWidth, TopViewHeight); // } if (section == 0) { return CGSizeMake(kScreenWidth, MiddleViewHeight); } else if (section == 1) { return CGSizeMake(kScreenWidth, FITSIZE(50)); } else { return CGSizeZero; } } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { KBChildGoodModel *model = self.guessYouLikeArr[indexPath.item]; if ([model.type isEqualToString:@"0"]) { //详情 KBGoodDetailViewController *detailVC = [[KBGoodDetailViewController alloc] init]; DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model]; detailVC.requestModel = requestModel; [self.navigationController pushViewController:detailVC animated:YES]; } else { //专场 KBGoodListViewController *listVC = [[KBGoodListViewController alloc] init]; listVC.cate_id = model.goods_id; [self.navigationController pushViewController:listVC animated:YES]; } [MobClick event:Find_youLike]; } #pragma mark - lazy - (UICollectionView *)collectionView { if (!_collectionView) { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical; flowLayout.itemSize = CGSizeMake(FITSIZE(184.9), FITSIZE(287)); flowLayout.minimumLineSpacing = FITSIZE(5); flowLayout.minimumInteritemSpacing = FITSIZE(5); _collectionView = [[KBCollectionView alloc] initWithFrame:CGRectMake(0, FITSIZE(80), kScreenWidth, kScreenHeight-TabbarHeight-FITSIZE(80)) collectionViewLayout:flowLayout]; _collectionView.delegate = self; _collectionView.dataSource = self; [_collectionView registerClass:[KBGoodCollectionCell class] forCellWithReuseIdentifier:cellID]; [_collectionView registerClass:[KBFindNavBarView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewNavBar]; [_collectionView registerClass:[KBFindTopView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewTop]; [_collectionView registerClass:[KBFindMiddleView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewMiddle]; [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewBottom]; if (@available(iOS 11.0, *)) { _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } kWeak(self); MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ [selfWeak changedSexRequest]; }]; header.lastUpdatedTimeLabel.textColor = [UIColor whiteColor]; header.stateLabel.textColor = [UIColor whiteColor]; _collectionView.mj_header = header; _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ page ++; [selfWeak requestGuessYouLike]; }]; } return _collectionView; } - (NSMutableArray *)guessYouLikeArr { if (!_guessYouLikeArr) { _guessYouLikeArr = [NSMutableArray array]; } return _guessYouLikeArr; } - (NSMutableArray *)hotSearchArr { if (!_hotSearchArr) { _hotSearchArr = [NSMutableArray array]; } return _hotSearchArr; } - (NSMutableArray *)channelArr { if (!_channelArr) { _channelArr = [NSMutableArray array]; } return _channelArr; } - (KBFindNavBarStaticView *)navBarStaticView { if (!_navBarStaticView) { _navBarStaticView = [[KBFindNavBarStaticView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, FITSIZE(80))]; _navBarStaticView.delegate = self; // _navBarStaticView.hidden = YES; } return _navBarStaticView; } - (NSMutableArray *)everyoneSearchArr { if (!_everyoneSearchArr) { _everyoneSearchArr = [NSMutableArray array]; } return _everyoneSearchArr; } @end