// // LDClassifyViewController.m // YouHuiProject // // Created by 小花 on 2018/4/28. // Copyright © 2018年 kuxuan. All rights reserved. // #import "LDClassifyViewController.h" #import "LDClassifyLeftView.h" #import "LDClassifyRightView.h" #import "LDCategoryModel.h" #import "LDSecondCategoryModel.h" #import "LDGoodListViewController.h" #import "LDClassifyListController.h" #import "LDFindNavBarStaticView.h" #import "PYSearchViewController.h" #import "LDFindSearchResultViewController.h" #import "LDFindRequestViewModel.h" @interface LDClassifyViewController () { } @property (nonatomic, strong) LDClassifyLeftView *leftView; //类别 @property (nonatomic, strong) LDClassifyRightView *rightView;//详单 @property (nonatomic, strong) NSMutableArray *dataArr; @property (nonatomic, strong) NSArray *secondDataArr; @property (nonatomic, strong) LDFindNavBarStaticView *navBarStaticView; @property (nonatomic, strong) NSMutableArray *everyoneSearchArr; @property (nonatomic, strong) NSString *classifyName; @property (nonatomic, strong) NSString *firstCategoryId; @end @implementation LDClassifyViewController - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault]; // [SVProgressHUD dismiss]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault]; [SVProgressHUD dismiss]; } - (void)viewDidLoad { [super viewDidLoad]; [self creatNavBar]; [self createView]; [self requestData]; [self requestEveryoneSearch]; } - (void)creatNavBar { [self.navigationBar addSubview:self.navBarStaticView]; 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]]; [self.navigationBar setShowNavigationBarBottomLine:YES]; self.view.backgroundColor = [UIColor whiteColor]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeSex) name:ChangeSex object:nil]; } - (void)backAction { [self.navigationController popViewControllerAnimated:YES]; } - (void)createView { [self createLeftView]; [self createRightView]; } - (void)changeSex { [self createLeftView]; [self createRightView]; [self refreshData]; } - (void)createLeftView{ self.leftView = [[LDClassifyLeftView alloc]initWithFrame:CGRectMake(0, NavBarHeight, 92*SCREEN_MUTI, SCREEN_HEIGHT - NavBarHeight)]; __weak typeof(self) weakSelf = self; self.leftView.selectRowBlock = ^(NSInteger index) { LDCategoryModel *model = weakSelf.dataArr[index]; weakSelf.secondDataArr = model.itemList; weakSelf.rightView.dataSource = model.itemList; weakSelf.classifyName = model.name; weakSelf.firstCategoryId = model.Id; }; [self.view addSubview:self.leftView]; } - (void)createRightView{ self.rightView = [[LDClassifyRightView alloc]initWithFrame:CGRectMake(92*SCREEN_MUTI, NavBarHeight, SCREEN_WIDTH - 92*SCREEN_MUTI, SCREEN_HEIGHT -NavBarHeight)]; __weak typeof(self) weakSelf = self; weakSelf.rightView.selectItemBlock = ^(NSInteger index) { LDClassifyListController *list = [[LDClassifyListController alloc] init]; LDSecondCategoryModel *model = weakSelf.secondDataArr[index]; list.firstCategoryId = weakSelf.firstCategoryId; list.cate_id = model.Id; list.name = model.name; [self.navigationController pushViewController:list animated:YES]; NSString *tagStr = [NSString stringWithFormat:@"%@-%@",weakSelf.classifyName,model.name]; [MobClick event:Classify label:tagStr]; }; [self.view addSubview:self.rightView]; } - (void)requestData { [SVProgressHUD show]; NSString *urlString = [NSString stringWithFormat:@"%@/api/v2/category/list", BaseURL]; [LDCacheHttp get:urlString params:nil success:^(id json, BOOL isCache) { [self.dataArr removeAllObjects]; NSArray *arr = [NSArray yy_modelArrayWithClass:[LDCategoryModel class] json:json[@"data"]]; [self.dataArr addObjectsFromArray:arr]; self.leftView.dataArray = arr; LDCategoryModel *model = arr.firstObject; self.classifyName = model.name; self.secondDataArr = model.itemList; self.rightView.dataSource = model.itemList; self.firstCategoryId = model.Id; [SVProgressHUD dismiss]; } failure:^(NSError *error) { [SVProgressHUD dismiss]; }]; } - (void)requestEveryoneSearch { [LDFindRequestViewModel requestEveryoneSearchSuccess:^(NSArray *array) { if (array.count > 0) { for (NSDictionary *dict in array) { [self.everyoneSearchArr addObject:dict]; } } } failure:^(NSError *error) { }]; } - (void)refreshData { [self.dataArr removeAllObjects]; [self requestData]; } #pragma mark - navBarView - (void)yh_FindNavBarStaticViewClickSearch { [self createSearchViewControllerWith:nil]; } #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]; nav.modalPresentationStyle = UIModalPresentationFullScreen; [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]; //处理搜索点击事件 LDFindSearchResultViewController *resultVC = [[LDFindSearchResultViewController 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) { [LDHttp 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 { LDFindSearchResultViewController *resultVC = [[LDFindSearchResultViewController 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 { LDFindSearchResultViewController *resultVC = [[LDFindSearchResultViewController 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 { LDFindSearchResultViewController *resultVC = [[LDFindSearchResultViewController 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 ----- - (LDFindNavBarStaticView *)navBarStaticView { if (!_navBarStaticView) { _navBarStaticView = [[LDFindNavBarStaticView alloc] initWithFrame:CGRectMake(60, 0, kScreenWidth-60, NavBarHeight-2)]; _navBarStaticView.backgroundColor = [UIColor whiteColor]; _navBarStaticView.delegate = self; } return _navBarStaticView; } - (NSMutableArray *)dataArr { if (!_dataArr) { _dataArr = [NSMutableArray array]; } return _dataArr; } - (NSMutableArray *)everyoneSearchArr { if (!_everyoneSearchArr) { _everyoneSearchArr = [NSMutableArray array]; } return _everyoneSearchArr; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end