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

#import "KWMCategoryVC.h"
u  
lee committed
10
#import "KWMNewProductVC.h"
houweibin committed
11
#import "KWMShopCartVC.h"
12
#import "KWMNewProductVC.h"
houweibin committed
13
#import "KWMSearchFeedbackVC.h"
14 15 16 17 18 19
#import "KWMLeftCategoryCell.h"
#import "KWMRightProductCell.h"
#import "KWMBrandsTypeModel.h"
#import "KWMStringUtil.h"
#import "KWMFilterUtil.h"
#import "MJRefresh.h"
houweibin committed
20
#import "KWMShoppingCart.h"
21 22
#import "CacheKeyConstants.h"
#import "KWMPPCacheUtil.h"
23

u  
lee committed
24 25


26 27 28 29 30 31 32 33 34 35 36 37
//#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;

houweibin committed
38
@property (nonatomic) KWMCategoryTitleView *titleView;
39 40 41

@property (nonatomic) NSArray *allBrands;

lee committed
42
//@property(nonatomic) BUYCollectionSort sort;
43

lee committed
44
//@property(nonatomic) NSMutableArray<KWMFilter *> *selectFilters;
45

lee committed
46
//@property(nonatomic) NSString *selectBrands;
47

u  
lee committed
48 49 50 51
@property(nonatomic) NSArray<KWMCategoryModel *>* categories;

@property (nonatomic) KWMCategoryModel *selectedCategory;

52 53 54 55 56 57 58 59 60
@end

@implementation KWMCategoryVC

- (void)awakeFromNib{
    [super awakeFromNib];
    self.title = @"分类";
}

61 62 63 64
+ (NSString *)kwmBoard {
    return @"Main";
}

65 66
- (void)viewDidLoad {
    [super viewDidLoad];
u  
lee committed
67
    [self initData];
68 69
    [self initView];
    [self requestAllBrandsApi];
u  
lee committed
70 71
//    [self.refreshUtil reLoadData];
//    [self.cvRightProduct.mj_header beginRefreshing];
72 73
}

houweibin committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    if(self.titleView){
        NSNumber *count = [[KWMShoppingCart sharedInstance] count];
        self.titleView.count = count;
    }
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

88 89 90 91 92 93 94 95 96 97
-(KWMCollectionRefreshUtil *)refreshUtil{
    if(!_refreshUtil){
        _refreshUtil = [KWMCollectionRefreshUtil new];
        _refreshUtil.collectionView = self.cvRightProduct;
        _refreshUtil.delegate = self;
        _refreshUtil.emptyMsg = @" ";
    }
    return _refreshUtil;
}

u  
lee committed
98 99
-(void) initData {
    self.categories = [@[
houweibin committed
100 101 102 103 104 105 106 107 108 109 110
                         @{@"title":@"上装",@"collection_id":@"192437702",@"image":@"ic_type_tops"},
                         @{@"title":@"连体衣",@"collection_id":@"192438342",@"image":@"ic_type_jumpsuits"},
                         @{@"title":@"外套",@"collection_id":@"192438790",@"image":@"ic_type_coat"},
                         @{@"title":@"裙子",@"collection_id":@"191377286",@"image":@"ic_type_skirt"},
                         @{@"title":@"裤子",@"collection_id":@"333026630",@"image":@"ic_type_trousers"},
                         @{@"title":@"短裙",@"collection_id":@"192434950",@"image":@"ic_type_kilt"},
                         @{@"title":@"套装",@"collection_id":@"192438150",@"image":@"ic_type_suit"},
                         @{@"title":@"泳衣",@"collection_id":@"333026438",@"image":@"ic_type_swimsuit"},
                         @{@"title":@"连裤袜",@"collection_id":@"192437830",@"image":@"ic_type_pantyhose"},
                         @{@"title":@"打底裤",@"collection_id":@"192435334",@"image":@"ic_type_leggings"},
                         @{@"title":@"内衣",@"collection_id":@"333026694",@"image":@"ic_type_underwear"},
111
//                         @{@"title":@"雨衣",@"collection_id":@"423509068",@"image":@"ic_type_raincoat"},
houweibin committed
112 113
                         @{@"title":@"鞋子",@"collection_id":@"171856134",@"image":@"ic_type_shoe"},
                         @{@"title":@"玩具",@"collection_id":@"332990918",@"image":@"ic_type_toy"},
u  
lee committed
114
                         @{@"title":@"配件",@"collection_id":@"191376966",@"image":@"ic_type_Acc"},
u  
lee committed
115
                        ] rx_mapWithBlock:^id(id each) {
houweibin committed
116 117
                            NSString *image = each[@"image"];

u  
lee committed
118 119 120
                            KWMCategoryModel *m = [KWMCategoryModel new];
                            m.title = each[@"title"];
                            m.collection_id = @([each[@"collection_id"] integerValue]);
houweibin committed
121 122
                            m.image = [NSString stringWithFormat:@"%@_nor",image];
                            m.focusImage = [NSString stringWithFormat:@"%@_pre",image];
u  
lee committed
123 124 125 126 127 128
                            m.page = 1;
                            m.products = [NSMutableArray new];
                            m.sort = BUYCollectionSortCollectionDefault;
                            return m;
                        }];
    self.selectedCategory = self.categories.firstObject;
129
    NSIndexPath * path = [NSIndexPath indexPathForItem:0 inSection:0];
houweibin committed
130
    [self.tbvLeftCategory selectRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionTop];
u  
lee committed
131 132
}

133 134 135 136 137
-(void)initView{
    [self initTitleView];
    [self initTbvLeft];
    [self initCvRight];
    
lee committed
138 139
//    self.sort = BUYCollectionSortCollectionDefault;
//    self.selectBrands = @"";
140 141 142
}

-(void)initTbvLeft{
houweibin committed
143
    self.cvRightProduct.mj_footer.hidden = YES;
144 145 146 147 148 149 150 151 152 153 154
    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];
houweibin committed
155 156
    NSNumber *count = [[KWMShoppingCart sharedInstance] count];
    
houweibin committed
157
    self.titleView = [[KWMCategoryTitleView alloc] init];
houweibin committed
158 159 160
    self.titleView.delegate = self;
    self.titleView.count = count;
    [self.view addSubview:self.titleView];
houweibin committed
161
    [self initCustomTitleBarConstarints:self.titleView];
houweibin committed
162 163 164 165 166 167 168 169 170 171 172
}

#pragma mark -- KWMCategoryTitleDelegate
-(void)kwm_onClickShopCart{
    KWMShopCartVC * shopCartVC = (KWMShopCartVC*)[KWMBaseVC findControllerBy:[KWMShopCartVC kwmTag] fromStoryboard:@"ShopCart"];
    [self.navigationController pushViewController:shopCartVC animated:YES];
}

-(void)kwm_onClickSearch{
    KWMSearchFeedbackVC *vc = (KWMSearchFeedbackVC *)[KWMBaseVC findControllerBy:[KWMSearchFeedbackVC kwmTag] fromStoryboard:@"New"];
    [self.navigationController pushViewController:vc animated:YES];
173 174 175 176 177
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KWMLeftCategoryCell *categoryCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([KWMLeftCategoryCell class]) forIndexPath:indexPath];
u  
lee committed
178
    [categoryCell fillData:self.categories[indexPath.item]];
179 180 181 182
    return categoryCell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
u  
lee committed
183
    return self.categories.count;
184 185 186 187 188 189
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

u  
lee committed
190
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
lee committed
191 192
//    [self.cvRightProduct.mj_header endRefreshing];
//    [self.cvRightProduct.mj_footer endRefreshing];
u  
lee committed
193 194 195
    self.selectedCategory = self.categories[indexPath.row];
}

196 197 198 199 200 201 202 203
#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
204
    NSInteger width = ( (UI_SCREEN_WIDTH - 90) - 8*2)/2;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    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{
220
    KWMNewProductVC *productDetailVC = (KWMNewProductVC *)[KWMNewProductVC findControllerBy:[KWMNewProductVC kwmTag] fromStoryboard:@"NewProduct"];
221 222 223 224 225 226 227
    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{
u  
lee committed
228 229 230 231 232
    if (page == 0) {
        [self requestProducts:1];
    }else{
        [self requestProducts:self.selectedCategory.page + 1];
    }
233 234 235 236 237 238 239 240 241
}

#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;
lee committed
242
        brandFilterVC.selectBrand = self.selectedCategory.brand;
243 244 245 246 247
        tragetVC = brandFilterVC;
    }else{
        KWMProductFilterVC *productFilterVC = (KWMProductFilterVC *)[KWMBaseVC findControllerBy:[KWMProductFilterVC kwmTag] fromStoryboard:@"Category"];
        productFilterVC.filterMode = filterMode;
        productFilterVC.delegate = self;
lee committed
248 249
        productFilterVC.selectedSort = self.selectedCategory.sort;
        productFilterVC.selectedTags = self.selectedCategory.filters;
250 251 252 253 254 255 256 257
        tragetVC = productFilterVC;
    }
    CGSize size = [UIScreen mainScreen].bounds.size;
    [self showPresentation:tragetVC size:size tapOutsideClose:YES style:MZFormSheetPresentationTransitionStyleFade];
}

#pragma mark -
-(void)kwm_onCompleProductFilter:(KWMFilterView *)filterView{
lee committed
258 259
//    self.selectFilters = filterView.selectedTags;
    self.selectedCategory.filters = filterView.selectedTags;
u  
lee committed
260
    self.selectedCategory.sort = filterView.selectedSort;
261 262 263 264 265 266 267 268 269 270 271 272
    [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{
lee committed
273 274
//    self.selectBrands = selectBrand;
    self.selectedCategory.brand = selectBrand;
275 276 277 278 279 280 281 282 283 284 285 286 287 288
    [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){
289 290 291 292
        //如果从缓存拿到了数据,不需要提示网络错误,反之提示
        if(!self.allBrands || self.allBrands.count == 0){
            [weakSelf showError:error];
        }
293 294
    };
    void(^success)(NSURLSessionDataTask *,KWMBrandsResult *) = ^(NSURLSessionDataTask *task,KWMBrandsResult *result){
295 296 297 298 299 300 301 302
        if([weakSelf hasCemaroseError:result]){
            return ;
        }
        if(!self.allBrands){
            self.allBrands = result.brands;
        }
    };
    void(^cacheCallback)(KWMBrandsResult *) = ^(KWMBrandsResult *result){
houweibin committed
303 304 305 306 307
//        if([weakSelf hasCemaroseError:result]){
//            return ;
//        }
        if(result && result.brands){
            self.allBrands = result.brands;
308 309
        }
    };
310
    [self.api getAllBrand:nil cacheCallback:cacheCallback success:success failure:failure];
311 312 313
}

#pragma mark - API BUYClient+Storefront.h
u  
lee committed
314
-(void)requestProducts:(NSInteger)cpage{
315 316 317
    __weak KWMCategoryVC *weakSelf = self;
    self.filterTab.userInteractionEnabled = false;
    
u  
lee committed
318
    KWMCategoryModel *cmodel = self.selectedCategory;
lee committed
319
    NSMutableArray *tags = [KWMFilterUtil getTagsByFilterArray:cmodel.filters];
u  
lee committed
320
    if(cmodel.brand){
lee committed
321 322
        [tags addObject:cmodel.brand];
    }
323 324 325 326 327
    NSDictionary *parameters = [KWMPPCacheUtil getProductPageCacheKey:cpage pageSize:self.client.pageSize sortOrder:cmodel.sort tags:tags inCollection:cmodel.collection_id];
    //从缓存中拿数据
    if([self isUseCache:parameters page:cpage cmodel:cmodel]){
        return;
    }
lee committed
328
    [self.client getProductsPage:cpage inCollection:cmodel.collection_id withTags:tags sortOrder:cmodel.sort completion:^(NSArray *products,NSUInteger page, BOOL reachedEnd, NSError *error){
329
        weakSelf.cvRightProduct.mj_footer.hidden = false;
u  
lee committed
330
        if (cmodel == self.selectedCategory) {
lee committed
331 332 333 334 335 336 337
//            [weakSelf.refreshUtil hideLoading];
            if ([weakSelf.cvRightProduct.mj_header isRefreshing]) {
                [weakSelf.cvRightProduct.mj_header endRefreshing];
            }
            if ([weakSelf.cvRightProduct.mj_footer isRefreshing]) {
                [weakSelf.cvRightProduct.mj_footer endRefreshing];
            }
u  
lee committed
338
        }
339
        if(reachedEnd){
lee committed
340
             [weakSelf.cvRightProduct.mj_footer endRefreshingWithNoMoreData];
341
        }
342 343 344 345 346 347
        weakSelf.filterTab.userInteractionEnabled = true;

        if(error){
            [weakSelf showError:error];
        }
        else{
348 349
            //保存缓存
            [KWMPPCacheUtil saveDataToCache:parameters urlKey:CACHE_KEY_SDK_getProductsPage products:products];
u  
lee committed
350 351 352 353 354 355 356 357 358 359 360
//            weakSelf.refreshUtil.page = page;
            cmodel.page = page;
//            weakSelf.refreshUtil.hasNextPage = !reachedEnd;
//            [weakSelf.refreshUtil appendDataList:products];
            if (cpage == 1) {
                [cmodel.products removeAllObjects];
            }
            [cmodel.products addObjectsFromArray:products];
            if (cmodel == weakSelf.selectedCategory) {
                [weakSelf.cvRightProduct reloadData];
            }
361 362 363 364
        }
    }];
}

u  
lee committed
365 366 367
#pragma mark - setter

- (void)setSelectedCategory:(KWMCategoryModel *)selectedCategory {
lee committed
368
    _selectedCategory.offsetY = self.cvRightProduct.contentOffset.y < 0 ? 0 : self.cvRightProduct.contentOffset.y;
u  
lee committed
369
    _selectedCategory = selectedCategory;
lee committed
370 371 372 373
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.cvRightProduct.contentOffset = CGPointMake(0, selectedCategory.offsetY);
    });
//    self.cvRightProduct.contentOffset = CGPointMake(0, selectedCategory.offsetY);
374
    [self.cvRightProduct.mj_footer resetNoMoreData];
u  
lee committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    self.refreshUtil.page = _selectedCategory.page;
    self.refreshUtil.hasNextPage = YES;
    self.refreshUtil.dataList = _selectedCategory.products;
    [self.cvRightProduct reloadData];
    if (_selectedCategory.products.count == 0) {
//        [self requestProducts:1];
//        [self.refreshUtil reLoadData];
        if ([self.cvRightProduct.mj_header isRefreshing]) {
            [self requestProducts:1];
        }else{
            [self.cvRightProduct.mj_header beginRefreshing];
        }
    }else if([self.cvRightProduct.mj_header isRefreshing]) {
        [self.cvRightProduct.mj_header endRefreshing];
    }
//    [self.cvRightProduct reloadData];
}
392

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
#pragma mark -cache
//缓存规则:第一页数据默认先从缓存拿,然后再更新,后面的根据网络判断从缓存拿数据还是从api拿数据
- (BOOL)isUseCache:(NSDictionary *)cacheKeyDictionary page:(NSUInteger)page cmodel:(KWMCategoryModel *)cmodel{
    if(!kIsNetwork || page == 1){
        NSArray *cacheProducts = [KWMPPCacheUtil getProductDataByCache:cacheKeyDictionary urlKey:CACHE_KEY_SDK_getProductsPage];
        if(cacheProducts && cacheProducts.count > 0){
            if ([self.cvRightProduct.mj_header isRefreshing]) {
                [self.cvRightProduct.mj_header endRefreshing];
            }
            if ([self.cvRightProduct.mj_footer isRefreshing]) {
                [self.cvRightProduct.mj_footer endRefreshing];
            }
            cmodel.page = page;
            if (page == 1) {
                [cmodel.products removeAllObjects];
            }
            [cmodel.products addObjectsFromArray:cacheProducts];
            [self.cvRightProduct reloadData];
            self.filterTab.userInteractionEnabled = true;
        }else if(!kIsNetwork){
            [self showToast:CACHE_KEY_FAIL_HINT];
        }
    }
    return !kIsNetwork;
}

419
@end