// // KBShopDetailViewController.m // YouHuiProject // // Created by xiaoxi on 2018/1/29. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBShopDetailViewController.h" #import "KBGoodDetailModel.h" #import "KBCollectionView.h" #import "KBGoodCollectionCell.h" #import "KBGoodDetailRequestViewModel.h" #import "KBGoodDetailViewController.h" static NSString *const cellID = @"KBGoodCollectionCell"; static NSString *const collectionViewHeader = @"collectionViewHeader"; static NSInteger page = 1; @interface KBShopDetailViewController () @property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) UIImageView *shopImageView; @property (nonatomic, strong) UILabel *shopTitleLabel; @property (nonatomic, strong) CALayer *lineLayer; @property (nonatomic, strong) UIButton *goodIntro; @property (nonatomic, strong) UIButton *shopService; @property (nonatomic, strong) UIButton *postService; @property (nonatomic, strong) NSMutableArray *goodsArr; @end @implementation KBShopDetailViewController - (void)viewDidLoad { [super viewDidLoad]; [self initNavBar]; [self initSubviews]; [self request]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)initNavBar { [self.navigationBar setBackButtonWithTarget:self selector:@selector(backAction)]; [self.navigationBar setNavTitle:@"商家店铺"]; [self.navigationBar setShowNavigationBarBottomLine:YES]; } - (void)backAction { [self.navigationController popViewControllerAnimated:YES]; } - (void)initSubviews { [self.view addSubview:self.collectionView]; } - (void)setGoodModel:(KBGoodDetailModel *)goodModel { _goodModel = goodModel; [self.shopImageView yy_setImageWithURL:[NSURL URLWithString:goodModel.shop_pict_url] options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation]; self.shopTitleLabel.text = goodModel.shop_title; [self.goodIntro setTitle:[NSString stringWithFormat:@"宝贝描述:%@", goodModel.item_score] forState:UIControlStateNormal]; [self.shopService setTitle:[NSString stringWithFormat:@"卖家服务:%@", goodModel.service_score] forState:UIControlStateNormal]; [self.postService setTitle:[NSString stringWithFormat:@"物流服务:%@", goodModel.delivery_score] forState:UIControlStateNormal]; } #pragma mark - request - (void)request { [KBGoodDetailRequestViewModel requestShopGoodParamSellerId:self.goodModel.sellerId Page:page success:^(NSArray *array) { if (array.count > 0) { [self.goodsArr addObjectsFromArray:array]; [self.collectionView reloadData]; } [self noMoreDataWithArray:array]; [self.collectionView.mj_header endRefreshing]; [self.collectionView.mj_footer endRefreshing]; } failure:^(NSError *error) { [self.collectionView.mj_header endRefreshing]; [self.collectionView.mj_footer endRefreshing]; }]; } - (void)noMoreDataWithArray:(NSArray *)array { if (array.count <= 0) { MJRefreshBackNormalFooter *foot = (MJRefreshBackNormalFooter *)self.collectionView.mj_footer; [foot setTitle:@"到底啦" forState:MJRefreshStateIdle]; } } #pragma mark - collectionView - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.goodsArr.count; } - (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]; KBChildGoodModel *model = self.goodsArr[indexPath.item]; cell.model = model; return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewHeader forIndexPath:indexPath]; view.backgroundColor = [UIColor whiteColor]; [view.layer addSublayer:self.lineLayer]; for (NSInteger i=0; i<3; i++) { CALayer *layer = [CALayer layer]; layer.frame = CGRectMake(kScreenWidth/3*i, FITSIZE(105)+FITSIZE(40)/2-FITSIZE(5)/2, 1, FITSIZE(5)); layer.backgroundColor = [UIColor YHColorWithHex:0x999999].CGColor; [view.layer addSublayer:layer]; } [view addSubview:self.shopImageView]; [view addSubview:self.shopTitleLabel]; [view addSubview:self.goodIntro]; [view addSubview:self.shopService]; [view addSubview:self.postService]; [self.shopImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(view).offset(FITSIZE(20)); make.top.equalTo(view).offset(FITSIZE(22)); make.size.mas_equalTo(CGSizeMake(FITSIZE(60), FITSIZE(60))); }]; [self.shopTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.shopImageView.mas_right).offset(FITSIZE(14)); make.centerY.equalTo(self.shopImageView); }]; [self.goodIntro mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(view); make.top.equalTo(view).offset(FITSIZE(105)); make.width.mas_equalTo(kScreenWidth/3); make.bottom.equalTo(view); }]; [self.shopService mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.goodIntro.mas_right); make.top.equalTo(self.goodIntro); make.width.equalTo(self.goodIntro); make.bottom.equalTo(self.goodIntro); }]; [self.postService mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.shopService.mas_right); make.top.equalTo(self.shopService); make.width.equalTo(self.shopService); make.bottom.equalTo(self.shopService); }]; return view; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { return CGSizeMake(kScreenWidth, FITSIZE(145)); } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { KBChildGoodModel *model = self.goodsArr[indexPath.item]; //详情 KBGoodDetailViewController *detailVC = [[KBGoodDetailViewController alloc] init]; DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model]; detailVC.requestModel = requestModel; KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:@"0" category_id:@"0" source:merchantShopAction]; detailVC.eventModel = evevtModel; [self.navigationController pushViewController:detailVC animated:YES]; } #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, NavBarHeight, kScreenWidth, kScreenHeight-NavBarHeight) collectionViewLayout:flowLayout]; _collectionView.delegate = self; _collectionView.dataSource = self; [_collectionView registerClass:[KBGoodCollectionCell class] forCellWithReuseIdentifier:cellID]; [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:collectionViewHeader]; if (@available(iOS 11.0, *)) { _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } kWeak(self); _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ [self.goodsArr removeAllObjects]; page = 1; [selfWeak request]; }]; _collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ page ++; [selfWeak request]; }]; } return _collectionView; } - (CALayer *)lineLayer { if (!_lineLayer) { _lineLayer = [CALayer layer]; _lineLayer.frame = CGRectMake(0, FITSIZE(100), kScreenWidth, FITSIZE(5)); _lineLayer.backgroundColor = [UIColor YHColorWithHex:0xf5f4f4].CGColor; } return _lineLayer; } - (UIImageView *)shopImageView { if (!_shopImageView) { _shopImageView = [[UIImageView alloc] init]; _shopImageView.backgroundColor = [UIColor clearColor]; _shopImageView.layer.borderColor = [UIColor YHColorWithHex:0xdddddd].CGColor; _shopImageView.layer.borderWidth = 0.5f; } return _shopImageView; } - (UILabel *)shopTitleLabel { if (!_shopTitleLabel) { _shopTitleLabel = [[UILabel alloc] init]; _shopTitleLabel.backgroundColor = [UIColor clearColor]; _shopTitleLabel.textColor = [UIColor YHColorWithHex:0x222222]; _shopTitleLabel.font = [UIFont systemFontOfSize:FITSIZE(14)]; } return _shopTitleLabel; } - (UIButton *)goodIntro { if (!_goodIntro) { _goodIntro = [UIButton buttonWithType:UIButtonTypeCustom]; _goodIntro.backgroundColor = [UIColor clearColor]; [_goodIntro setTitleColor:[UIColor YHColorWithHex:0x333333] forState:UIControlStateNormal]; _goodIntro.titleLabel.font = [UIFont systemFontOfSize:FITSIZE(11)]; } return _goodIntro; } - (UIButton *)shopService { if (!_shopService) { _shopService = [UIButton buttonWithType:UIButtonTypeCustom]; _shopService.backgroundColor = [UIColor clearColor]; [_shopService setTitleColor:[UIColor YHColorWithHex:0x333333] forState:UIControlStateNormal]; _shopService.titleLabel.font = [UIFont systemFontOfSize:FITSIZE(11)]; } return _shopService; } - (UIButton *)postService { if (!_postService) { _postService = [UIButton buttonWithType:UIButtonTypeCustom]; _postService.backgroundColor = [UIColor clearColor]; [_postService setTitleColor:[UIColor YHColorWithHex:0x333333] forState:UIControlStateNormal]; _postService.titleLabel.font = [UIFont systemFontOfSize:FITSIZE(11)]; } return _postService; } - (NSMutableArray *)goodsArr { if (!_goodsArr) { _goodsArr = [NSMutableArray array]; } return _goodsArr; } @end