// // KBNineNineMainViewController.m // YouHuiProject // // Created by xiaoxi on 2018/1/16. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBNineNineMainViewController.h" #import "KBTableView.h" #import "KBNineNineHeaderView.h" #import "KBNineNineScrollViewController.h" #import "KBNineNineTableViewCell.h" #import "KBNineNineRequestViewModel.h" #import "KBNineTableView.h" #import "KBGoodListViewController.h" #import "KBGoodDetailViewController.h" #import "PYSearch.h" #import "KBFindSearchResultViewController.h" #import "KBFindRequestViewModel.h" #import "KBGoodListCidViewController.h" #import "KBNineNineScrollChildViewController.h" static NSString *const cellID = @"KBNineNineTableViewCell"; @interface KBNineNineMainViewController () @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) KBNineNineHeaderView *headerView; @property (nonatomic, strong) KBNineNineScrollViewController *scrollViewController; @property (nonatomic, strong) KBNineNineModel *nineNineModel; @property (nonatomic, assign) BOOL canScroll; @property (nonatomic, strong) YYFPSLabel *fpsLabel; @property (nonatomic, strong) NSMutableArray *everyoneSearchArr; @end @implementation KBNineNineMainViewController - (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.canScroll = YES; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeScrollStatus) name:@"leaveTop" object:nil]; [self.navigationBar setNavightionBarBackgroundColor:[UIColor clearColor]]; // NSInteger sex = [[[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey] integerValue]; // if (sex == 0) { // [self.navigationBar setNavightionBarBackgroundColor:[UIColor YHColorWithHex:0xff8daa]]; // } else { // [self.navigationBar setNavightionBarBackgroundColor:[UIColor YHColorWithHex:0x98befd]]; // } UIButton *search = [UIButton buttonWithType:UIButtonTypeCustom]; search.frame = CGRectMake(0, 0, FITSIZE(22), FITSIZE(22)); search.backgroundColor = [UIColor clearColor]; [search setBackgroundImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal]; [search addTarget:self action:@selector(searchButtonAction) forControlEvents:UIControlEventTouchUpInside]; [self.navigationBar setCustomRightButtons:@[search]]; UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.navigationBar.width/2-FITSIZE(116)/2, KStatusBarHeight+22-FITSIZE(25)/2, FITSIZE(116), FITSIZE(25))]; titleImageView.backgroundColor = [UIColor clearColor]; titleImageView.image = [UIImage imageNamed:@"99navBar"]; [self.navigationBar addSubview:titleImageView]; UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal]; [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside]; [self.navigationBar setCustomLeftButtons:@[leftBtn]]; leftBtn.hidden = !self.showBackButton; } - (void)backAction { [self.navigationController popViewControllerAnimated:YES]; } - (void)searchButtonAction { [self createSearchViewControllerWith:nil]; } #pragma mark notify - (void)changeScrollStatus//改变主视图的状态 { self.canScroll = YES; self.scrollViewController.childCanScroll = NO; } - (void)initSubviews { [SVProgressHUD dismiss]; [self.view addSubview:self.tableView]; self.tableView.tableHeaderView = self.headerView; [self.tableView reloadData]; [self.view bringSubviewToFront:self.navigationBar]; _fpsLabel = [YYFPSLabel new]; [_fpsLabel sizeToFit]; _fpsLabel.bottom = kScreenHeight - TabbarHeight; _fpsLabel.left = 0; _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 requestGoodList]; [self requestEveryoneSearch]; } - (void)changedSexRequest { NSInteger sex = [[[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey] integerValue]; if (sex == 0) { self.headerView.backgroundColor = [UIColor YHColorWithHex:0xff8daa]; [self.navigationBar setNavightionBarBackgroundColor:[UIColor YHColorWithHex:0xff8daa]]; } else { self.headerView.backgroundColor = [UIColor YHColorWithHex:0x98befd]; [self.navigationBar setNavightionBarBackgroundColor:[UIColor YHColorWithHex:0x98befd]]; } [self.everyoneSearchArr removeAllObjects]; [self.scrollViewController removeFromParentViewController]; self.scrollViewController = nil; [self request]; } - (void)requestGoodList { [KBNineNineRequestViewModel requestNineNineGoodListParamGid:1 success:^(KBNineNineModel *model) { if (model) { self.nineNineModel = model; self.headerView.headerModel = model.hot; [self initSubviews]; } [self.tableView.mj_header endRefreshing]; } failure:^(NSError *error) { [SVProgressHUD dismiss]; [self.tableView.mj_header endRefreshing]; }]; } - (void)requestEveryoneSearch { [KBFindRequestViewModel requestEveryoneSearchSuccess:^(NSArray *array) { if (array.count > 0) { for (NSDictionary *dict in array) { [self.everyoneSearchArr addObject:dict]; } } } failure:^(NSError *error) { }]; } #pragma mark - nineNineHeaderViewDelegate - (void)yh_NineNineHeaderViewDidSelectItem:(YHNineNineHeaderCollectionModel *)model { if (model.cid.length) { KBGoodListCidViewController *listVC = [[KBGoodListCidViewController alloc] init]; listVC.goods_id = model.Id.stringValue; listVC.cid = model.cid; listVC.topRequest = 2; [self.navigationController pushViewController:listVC animated:YES]; } else { KBGoodListViewController *listVC = [[KBGoodListViewController alloc] init]; listVC.cate_id = model.Id.stringValue; [self.navigationController pushViewController:listVC animated:YES]; } [MobClick event:nine_top_pic]; } #pragma mark - nineNineTableViewCellDelegate - (void)yh_NineNineTableViewCellDidSelectItem:(YHNineNineMiddleModel *)model { if (model.title.cid.length) { KBGoodListCidViewController *listVC = [[KBGoodListCidViewController alloc] init]; listVC.goods_id = model.title.Id.stringValue; listVC.cid = model.title.cid; listVC.topRequest = 2; [self.navigationController pushViewController:listVC animated:YES]; } else { KBGoodListViewController *listVC = [[KBGoodListViewController alloc] init]; listVC.cate_id = model.title.Id.stringValue; [self.navigationController pushViewController:listVC animated:YES]; } [MobClick event:nine_better]; } #pragma mark - nineNineScrollViewControllerDelegate - (void)yh_NineNineScrollViewControllerDidSelectItem:(KBChildGoodModel *)model { 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 { if (model.cid.length) { KBGoodListCidViewController *listVC = [[KBGoodListCidViewController alloc] init]; listVC.goods_id = model.goods_id; listVC.cid = model.cid; listVC.topRequest = 1; [self.navigationController pushViewController:listVC animated:YES]; } else { //专场 KBGoodListViewController *listVC = [[KBGoodListViewController alloc] init]; listVC.cate_id = model.goods_id; [self.navigationController pushViewController:listVC animated:YES]; } } } #pragma mark - search - (void)createSearchViewControllerWith:(NSString *)text { PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:@[] searchBarPlaceholder:@"输入商品名或粘贴淘宝标题"]; searchViewController.swapHotSeachWithSearchHistory = YES; 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.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; [searchViewController.navigationController pushViewController:resultVC animated:NO]; } /** 点击热搜 */ - (void)searchViewController:(PYSearchViewController *)searchViewController didSelectHotSearchAtIndex:(NSInteger)index searchText:(NSString *)searchText { KBFindSearchResultViewController *resultVC = [[KBFindSearchResultViewController alloc] init]; resultVC.searchName = searchText; [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; [searchViewController.navigationController pushViewController:resultVC animated:NO]; [MobClick event:search_count label:Search_History_String]; } #pragma mark - tableView - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) { return self.nineNineModel.recommend.count; } else { return 0; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { KBNineNineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; cell.delegate = self; YHNineNineMiddleModel *model = self.nineNineModel.recommend[indexPath.row]; cell.model = model; return cell; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (section == 0) { return [[UIView alloc] init]; } else { return self.scrollViewController.view; } } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (section == 0) { return 0.1; } else { return kScreenHeight-NavBarHeight; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { return FITSIZE(240); } else { return 0.1; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { YHNineNineMiddleModel *model = self.nineNineModel.recommend[indexPath.row]; if (model.title.cid.length) { KBGoodListCidViewController *listVC = [[KBGoodListCidViewController alloc] init]; listVC.goods_id = model.title.Id.stringValue; listVC.cid = model.title.cid; listVC.topRequest = 2; [self.navigationController pushViewController:listVC animated:YES]; } else { KBGoodListViewController *listVC = [[KBGoodListViewController alloc] init]; listVC.cate_id = model.title.Id.stringValue; [self.navigationController pushViewController:listVC animated:YES]; } } #pragma mark - scrollView - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.contentOffset.y >= 0) { NSInteger sex = [[[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey] integerValue]; if (sex == 0) { [self.navigationBar setNavightionBarBackgroundColor:[UIColor YHColorWithHex:0xff8daa]]; } else { [self.navigationBar setNavightionBarBackgroundColor:[UIColor YHColorWithHex:0x98befd]]; } self.tableView.backgroundView.backgroundColor = [UIColor clearColor]; self.tableView.backgroundColor = [UIColor clearColor]; } else { [self.navigationBar setNavightionBarBackgroundColor:[UIColor clearColor]]; NSInteger sex = [[[NSUserDefaults standardUserDefaults] objectForKey:UserSexKey] integerValue]; if (sex == 0) { self.tableView.backgroundView.backgroundColor = [UIColor YHColorWithHex:0xff8daa]; self.tableView.backgroundColor = [UIColor YHColorWithHex:0xff8daa]; } else { self.tableView.backgroundView.backgroundColor = [UIColor YHColorWithHex:0x98befd]; self.tableView.backgroundColor = [UIColor YHColorWithHex:0x98befd]; } } CGFloat offY = scrollView.contentOffset.y; CGFloat height = [self.tableView rectForSection:1].origin.y - NavBarHeight; if (offY >= height) { [self.tableView setContentOffset:CGPointMake(0, height)]; if (self.canScroll) { self.canScroll = NO; self.scrollViewController.childCanScroll = YES; } }else { if (!self.canScroll) {//子视图没到顶部 scrollView.contentOffset = CGPointMake(0, height); } } } - (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 - lazy - (UITableView *)tableView { if (!_tableView) { // CGFloat height = self.showBackButton?SCREEN_HEIGHT:SCREEN_HEIGHT-TabbarHeight; _tableView = [[KBNineTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain]; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.backgroundView.backgroundColor = [UIColor clearColor]; _tableView.backgroundColor = [UIColor clearColor]; _tableView.showsVerticalScrollIndicator = NO; _tableView.estimatedRowHeight = 0; _tableView.estimatedSectionHeaderHeight = 0; _tableView.estimatedSectionFooterHeight = 0; _tableView.delegate = self; _tableView.dataSource = self; [_tableView registerClass:[KBNineNineTableViewCell class] forCellReuseIdentifier:cellID]; if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } kWeak(self); MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ [selfWeak changedSexRequest]; }]; header.lastUpdatedTimeLabel.textColor = [UIColor whiteColor]; header.stateLabel.textColor = [UIColor whiteColor]; _tableView.mj_header = header; } return _tableView; } - (KBNineNineHeaderView *)headerView { if (!_headerView) { _headerView = [[KBNineNineHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, FITSIZE((197+NavBarHeight)))]; _headerView.delegate = self; } return _headerView; } - (KBNineNineScrollViewController *)scrollViewController { if (!_scrollViewController) { _scrollViewController = [[KBNineNineScrollViewController alloc] init]; _scrollViewController.delegate = self; [self addChildViewController:_scrollViewController]; } return _scrollViewController; } - (NSMutableArray *)everyoneSearchArr { if (!_everyoneSearchArr) { _everyoneSearchArr = [NSMutableArray array]; } return _everyoneSearchArr; } @end