// // KWMCategoryVC.m // iCemarose // // Created by HouWeiBin on 2017/6/13. // Copyright © 2017年 kollway. All rights reserved. // #import "KWMCategoryVC.h" #import "KWMProductDetailVC.h" #import "KWMNewProductVC.h" #import "KWMLeftCategoryCell.h" #import "KWMRightProductCell.h" #import "KWMBrandsTypeModel.h" #import "KWMSearchBar.h" #import "KWMStringUtil.h" #import "KWMFilterUtil.h" #import "MJRefresh.h" //#import <RXCollections/RXCollection.h> @interface KWMCategoryVC () @property (nonatomic,weak) IBOutlet KWMCategoryFilterTab *filterTab; @property (nonatomic,weak) IBOutlet UITableView *tbvLeftCategory; @property (nonatomic,weak) IBOutlet UICollectionView *cvRightProduct; @property(nonatomic) KWMCollectionRefreshUtil *refreshUtil; @property (nonatomic) KWMSearchBar *searchBar; @property (nonatomic) NSArray *allBrands; @property(nonatomic) BUYCollectionSort sort; @property(nonatomic) NSMutableArray<KWMFilter *> *selectFilters; @property(nonatomic) NSString *selectBrands; @end @implementation KWMCategoryVC - (void)awakeFromNib{ [super awakeFromNib]; self.title = @"分类"; } - (void)viewDidLoad { [super viewDidLoad]; [self initView]; [self requestAllBrandsApi]; [self.refreshUtil reLoadData]; [self.cvRightProduct.mj_header beginRefreshing]; } -(KWMCollectionRefreshUtil *)refreshUtil{ if(!_refreshUtil){ _refreshUtil = [KWMCollectionRefreshUtil new]; _refreshUtil.collectionView = self.cvRightProduct; _refreshUtil.delegate = self; _refreshUtil.emptyMsg = @" "; } return _refreshUtil; } -(void)initView{ [self initTitleView]; [self initTbvLeft]; [self initCvRight]; self.sort = BUYCollectionSortCreatedDescending; self.selectBrands = @""; } -(void)initTbvLeft{ self.automaticallyAdjustsScrollViewInsets = NO; [self.tbvLeftCategory registerNib:[UINib nibWithNibName:NSStringFromClass([KWMLeftCategoryCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([KWMLeftCategoryCell class])]; } -(void)initCvRight{ [self.cvRightProduct registerNib:[UINib nibWithNibName:NSStringFromClass([KWMRightProductCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([KWMRightProductCell class])]; self.filterTab.delegate = self; } - (void)initTitleView{ [self.navigationController setNavigationBarHidden:YES]; //添加頭部header self.searchBar = [[KWMSearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)]; // self.searchBar.delegate = self; [self.view addSubview:_searchBar]; } #pragma mark - UITableViewDataSource -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ KWMLeftCategoryCell *categoryCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([KWMLeftCategoryCell class]) forIndexPath:indexPath]; return categoryCell; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 20; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 0.01; } #pragma mark - UICollectionViewDataSource -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ KWMRightProductCell *productCell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([KWMRightProductCell class]) forIndexPath:indexPath]; productCell.product = (BUYProduct *)[self.refreshUtil.dataList objectAtIndex:indexPath.row]; return productCell; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { NSInteger width = ( (UI_SCREEN_WIDTH - 90) - 8*2)/2; NSInteger height = width * 160 / 114 ; CGSize size = CGSizeMake(width, height); return size; } -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1; } -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.refreshUtil.dataList.count; } #pragma mark - UICollectionViewDelegate -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ KWMNewProductVC *productDetailVC = (KWMNewProductVC *)[KWMNewProductVC findControllerBy:[KWMNewProductVC kwmTag] fromStoryboard:@"NewProduct"]; BUYProduct *product = (BUYProduct *)[self.refreshUtil.dataList objectAtIndex:indexPath.row]; productDetailVC.product = product; [self.navigationController pushViewController:productDetailVC animated:YES]; } #pragma mark - KWMCollectionRefreshDelegate -(void)refreshUtil:(KWMCollectionRefreshUtil *)util onLoad:(NSInteger)page{ [self requestProducts:page]; } #pragma mark - CategoryFilterTabDelegate -(void)kwm_onClickTab:(KWMFilterMode)filterMode isExpandView:(BOOL)isExpand{ KWMBaseVC *tragetVC; if(filterMode == ModeBrand){ KWMBrandFilterVC *brandFilterVC = (KWMBrandFilterVC *)[KWMBaseVC findControllerBy:[KWMBrandFilterVC kwmTag] fromStoryboard:@"Category"]; brandFilterVC.brandArray = self.allBrands; brandFilterVC.delegate = self; brandFilterVC.selectBrand = self.selectBrands; tragetVC = brandFilterVC; }else{ KWMProductFilterVC *productFilterVC = (KWMProductFilterVC *)[KWMBaseVC findControllerBy:[KWMProductFilterVC kwmTag] fromStoryboard:@"Category"]; productFilterVC.filterMode = filterMode; productFilterVC.delegate = self; productFilterVC.selectedSort = self.sort; productFilterVC.selectedTags = self.selectFilters; tragetVC = productFilterVC; } CGSize size = [UIScreen mainScreen].bounds.size; [self showPresentation:tragetVC size:size tapOutsideClose:YES style:MZFormSheetPresentationTransitionStyleFade]; } #pragma mark - -(void)kwm_onCompleProductFilter:(KWMFilterView *)filterView{ self.selectFilters = filterView.selectedTags; self.sort = filterView.selectedSort; [self.filterTab close]; [self.refreshUtil reLoadData]; [self.cvRightProduct.mj_header beginRefreshing]; [self.cvRightProduct setContentOffset:CGPointMake(0,0) animated:YES]; } -(void)kwm_onCancelProductFilter:(KWMFilterView *)filterView{ [self.filterTab close]; } #pragma mark - -(void)kwm_onCompleteBrandFilter:(NSString *)selectBrand{ self.selectBrands = selectBrand; [self.filterTab close]; [self.refreshUtil reLoadData]; [self.cvRightProduct.mj_header beginRefreshing]; [self.cvRightProduct setContentOffset:CGPointMake(0,0) animated:YES]; } -(void)kwm_onCancelBrandFilter{ [self.filterTab close]; } #pragma mark - API KWMAPIManager.h -(void)requestAllBrandsApi{ __weak KWMCategoryVC *weakSelf = self; void(^failure)(NSURLSessionDataTask *,NSError *) = ^(NSURLSessionDataTask *task,NSError *error){ [weakSelf showError:error]; }; void(^success)(NSURLSessionDataTask *,KWMBrandsResult *) = ^(NSURLSessionDataTask *task,KWMBrandsResult *result){ if([weakSelf hasCemaroseError:result]){ return ; } self.allBrands = result.brands; }; [self.api getAllBrand:nil success:success failure:failure]; } #pragma mark - API BUYClient+Storefront.h -(void)requestProducts:(NSInteger)page{ __weak KWMCategoryVC *weakSelf = self; self.filterTab.userInteractionEnabled = false; NSMutableArray *tags = [KWMFilterUtil getTagsByFilterArray:self.selectFilters]; if(![KWMStringUtil isEmpty:self.selectBrands]){ [tags addObject:self.selectBrands]; } [self.client getProductsPage:page inCollection:@(Collection_All_ID) withTags:tags sortOrder:self.sort completion:^(NSArray *products,NSUInteger page, BOOL reachedEnd, NSError *error){ [weakSelf.refreshUtil hideLoading]; weakSelf.filterTab.userInteractionEnabled = true; if(error){ [weakSelf showError:error]; } else{ weakSelf.refreshUtil.page = page; weakSelf.refreshUtil.hasNextPage = !reachedEnd; [weakSelf.refreshUtil appendDataList:products]; } }]; } @end