// // KDPSearchResultViewController.m // KuDianProject // // Created by admin on 2019/7/10. // Copyright © 2019 KDP. All rights reserved. // #import "KDPSearchResultViewController.h" #import "KDPSupplyContentReusableView.h" #import "KDPRecommendGoodCell.h" #import "KDPScreenViewController.h" @interface KDPSearchResultViewController () @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) NSMutableArray *dataSource; @property (nonatomic, assign) NSInteger page; @property (nonatomic, strong) NSMutableDictionary *params; @end @implementation KDPSearchResultViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self setupNav]; [self.view addSubview:self.collectionView]; [self requestGoodData]; } - (void)setupNav{ self.navBar.hidden = YES; UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, KDNavBarHeight)]; navView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:navView]; self.view.backgroundColor = [UIColor colorWithHex:0xf4f4f4]; UIButton *returnBtn =[[UIButton alloc]initWithFrame:CGRectMake(15, KDNavBarHeight - 13-18, 11, 18)]; [returnBtn addTarget:self action:@selector(returnClickBtn) forControlEvents:UIControlEventTouchUpInside]; [returnBtn setImage:[UIImage imageNamed:@"return_black"] forState:UIControlStateNormal]; returnBtn.adjustsImageWhenHighlighted = NO; [navView addSubview:returnBtn]; UIButton *searchBtn =[[UIButton alloc]initWithFrame:CGRectMake(41, KDStatusHeight+5, SCREEN_WIDTH-55, 31)]; searchBtn.layer.cornerRadius=15.5; searchBtn.backgroundColor=[UIColor colorWithHex:0xEEEEEE]; [searchBtn addTarget:self action:@selector(returnClickBtn) forControlEvents:UIControlEventTouchUpInside]; [navView addSubview:searchBtn]; UIImageView *iconimg=[[UIImageView alloc]initWithFrame:CGRectMake(12, 8, 15, 15)]; iconimg.image=[UIImage imageNamed:@"search_icon"]; [searchBtn addSubview:iconimg]; UITextField *textField=[[UITextField alloc]init]; textField.textColor=[UIColor whiteColor]; textField.backgroundColor=[UIColor colorWithHex:0x929292]; textField.font=[UIFont systemFontOfSize:14]; textField.userInteractionEnabled=NO; [searchBtn addSubview:textField]; textField.layer.cornerRadius=2; textField.layer.masksToBounds=YES; [textField mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(iconimg.right+5); make.top.mas_equalTo(3); make.height.mas_equalTo(24); }]; textField.text = [NSString stringWithFormat:@" %@ × ",self.keyWordStr]; } - (void)requestGoodData{ if (![KDPAccountTool isLogin]) { return; } [self.params setValue:[NSString stringWithFormat:@"%ld",(long)self.page] forKey:@"page"]; [LoadingView showInView:self.view]; NSString *getGoodUrl = [NSString stringWithFormat:@"%@api/goods/search",KDURL]; [KDPNetworkRequestHTTP postURL:getGoodUrl params:self.params success:^(id _Nonnull json) { [LoadingView dismiss]; NSArray *dataArr = [NSArray yy_modelArrayWithClass:[KDPGoodsModel class] json:json[@"data"]]; if (self.page == 1) { [self.dataSource removeAllObjects]; } if (dataArr.count > 0) { [self.dataSource addObjectsFromArray:dataArr]; } else{ [self.collectionView.mj_footer endRefreshingWithNoMoreData]; } [self.collectionView.mj_footer endRefreshing]; [self.collectionView.mj_header endRefreshing]; [self.collectionView reloadData]; } failure:^(NSError * _Nonnull error) { [LoadingView dismiss]; }]; } - (void)returnClickBtn{ [self.navigationController popViewControllerAnimated:NO]; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault; } - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent; } - (UICollectionView *)collectionView{ if (!_collectionView) { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, 130); flowLayout.minimumLineSpacing = 0; flowLayout.minimumInteritemSpacing = 0; flowLayout.sectionHeadersPinToVisibleBounds = YES; _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, KDNavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-KDNavBarHeight) collectionViewLayout:flowLayout]; _collectionView.showsVerticalScrollIndicator = NO; _collectionView.showsHorizontalScrollIndicator = NO; _collectionView.emptyDataSetSource = self; _collectionView.emptyDataSetDelegate = self; [_collectionView registerClass:[KDPRecommendGoodCell class] forCellWithReuseIdentifier:NSStringFromClass([KDPRecommendGoodCell class])]; [_collectionView registerClass:[KDPSupplyContentReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([KDPSupplyContentReusableView class])]; _collectionView.backgroundColor = [UIColor whiteColor]; _collectionView.delegate = self; _collectionView.dataSource = self; _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ self.page = 1; [self requestGoodData]; }]; if (@available(iOS 11.0, *)) { _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { self.automaticallyAdjustsScrollViewInsets = NO; } _collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ self.page ++; [self requestGoodData]; }]; } return _collectionView; } - (NSMutableArray *)dataSource{ if (!_dataSource) { _dataSource = [NSMutableArray array]; } return _dataSource; } - (NSMutableDictionary *)params{ if (!_params) { _params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"",@"page",@"",@"sort",self.keyWordStr,@"keyword",@"",@"start_price",@"",@"end_price",@"",@"start_tk_rate",@"0",@"is_has_coupon", nil]; } return _params; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ KDPSupplyContentReusableView *reusAbleView; if (kind == UICollectionElementKindSectionHeader) { reusAbleView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([KDPSupplyContentReusableView class]) forIndexPath:indexPath]; reusAbleView.layer.borderColor = [UIColor colorWithHex:0xf4f4f4].CGColor; reusAbleView.layer.borderWidth = 0.5; WeakSelf(weakSelf); __weak KDPSupplyContentReusableView *weakReusAbleView = reusAbleView; reusAbleView.changeBlock = ^(BOOL isPrice) { KDPScreenViewController *screenVC = [[KDPScreenViewController alloc] initWithPrice:isPrice]; screenVC.params = weakSelf.params; screenVC.resetBlock = ^(BOOL isPrice) { if (isPrice == YES) { [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStylePrice Color:[UIColor colorWithHex:0x333333] text:@"价格筛选"]; [self.params setValue:@"" forKey:@"start_price"]; [self.params setValue:@"" forKey:@"end_price"]; } else{ [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStyleProfit Color:[UIColor colorWithHex:0x333333] text:@"利润筛选"]; [self.params setValue:@"" forKey:@"start_tk_rate"]; } [weakSelf.collectionView.mj_header beginRefreshing]; }; screenVC.profitBlock = ^(NSString *baseScale) { if (!baseScale.length) { [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStyleProfit Color:[UIColor colorWithHex:0x333333] text:[NSString stringWithFormat:@"利润筛选"]]; [self.params setValue:@"" forKey:@"start_tk_rate"]; } else{ [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStyleProfit Color:[UIColor baseColor] text:[NSString stringWithFormat:@"佣金%@%%以上",baseScale]]; [self.params setValue:baseScale forKey:@"start_tk_rate"]; } [weakSelf.collectionView.mj_header beginRefreshing]; }; screenVC.priceBlock = ^(NSString *minPrice, NSString *maxPrice) { if (minPrice.length != 0 || maxPrice.length != 0) { if (minPrice.length == 0) { minPrice = @"0"; } NSString *price=[NSString stringWithFormat:@"价格%@-%@元",minPrice,maxPrice]; if (maxPrice.integerValue == 0 || [maxPrice intValue] <= [minPrice intValue]) { price=[NSString stringWithFormat:@"价格%@元以上",minPrice]; maxPrice = @""; } [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStylePrice Color:[UIColor baseColor] text:price]; } else{ [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStylePrice Color:[UIColor colorWithHex:0x333333] text:@"价格筛选"]; } [weakSelf.params setValue:minPrice forKey:@"start_price"]; [weakSelf.params setValue:maxPrice forKey:@"end_price"]; [weakSelf.collectionView.mj_header beginRefreshing]; }; CWLateralSlideConfiguration *conf = [CWLateralSlideConfiguration configurationWithDistance:0 maskAlpha:0.7 scaleY:1 direction:CWDrawerTransitionFromRight backImage:nil]; conf.distance=SCREEN_WIDTH/3*2; [weakSelf cw_showDrawerViewController:screenVC animationType:0 configuration:conf]; }; return reusAbleView; } return nil; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ if (section == 0) { return CGSizeMake(SCREEN_WIDTH, 33); } return CGSizeZero; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.dataSource.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ KDPRecommendGoodCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([KDPRecommendGoodCell class]) forIndexPath:indexPath]; cell.model = self.dataSource[indexPath.row]; return cell; } - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{ return [UIImage imageNamed:@"no_order"]; } - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{ return YES; } - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView{ return 50; } - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{ return [[NSAttributedString alloc] initWithString:@"还没有记录" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x333333],NSFontAttributeName:FONT_SYS(12)}]; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ KDPGoodDetailVC *detailVC = [[KDPGoodDetailVC alloc] init]; detailVC.model = self.dataSource[indexPath.row]; [self.navigationController pushViewController:detailVC animated:YES]; } @end