KWMCategoryVC.m 8.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
//
//  KWMCategoryVC.m
//  iCemarose
//
//  Created by HouWeiBin on 2017/6/13.
//  Copyright © 2017年 kollway. All rights reserved.
//

#import "KWMCategoryVC.h"
#import "KWMProductDetailVC.h"
11
#import "KWMNewProductVC.h"
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#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];
    
houweibin committed
74
    self.sort = BUYCollectionSortCreatedDescending;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    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 {
houweibin committed
118
    NSInteger width = ( (UI_SCREEN_WIDTH - 90) - 8*2)/2;
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    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{
134
    KWMNewProductVC *productDetailVC = (KWMNewProductVC *)[KWMNewProductVC findControllerBy:[KWMNewProductVC kwmTag] fromStoryboard:@"NewProduct"];
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    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];
    }
    
houweibin committed
218
    [self.client getProductsPage:page inCollection:@(Collection_All_ID) withTags:tags sortOrder:self.sort completion:^(NSArray *products,NSUInteger page, BOOL reachedEnd, NSError *error){
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        [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