KWMBrandCaramelVC.m 15.1 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11
//
//  KWMBrandCaramelVC.m
//  iCemarose
//
//  Created by 陈荣科 on 16/8/29.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMBrandCaramelVC.h"
#import "KWMBrandCaramelCell.h"
#import "KWMBarandSelectView.h"
u  
lee committed
12
#import "KWMNewProductVC.h"
houweibin committed
13 14 15 16 17
#import "KWMShopCartVC.h"
#import "KWMShopCartData.h"
#import "UIImageView+WebCache.h"
#import "KWMImageUtil.h"
#import "KWMStringUtil.h"
houweibin committed
18
#import "KWMFilterUtil.h"
houweibin committed
19
#import "UIColor+SAMAdditions.h"
houweibin committed
20
#import "MJRefresh.h"
houweibin committed
21
#import "KWMFilterVC.h"
lee committed
22
#import "KWMShoppingCart.h"
lee committed
23
#import "KWMSearchFeedbackVC.h"
24
#import "KWMPPCacheUtil.h"
houweibin committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

@interface KWMBrandCaramelVC ()<UITableViewDelegate,UITableViewDataSource,KWMBrandCaramelCellDelegate>
@property (nonatomic) KWMBarandSelectView *barandSelectView;
@property (nonatomic) NSArray<BUYProduct *> *productArr;

@end

@implementation KWMBrandCaramelVC{
    UIButton *rightButton;
    BOOL isUp;
    NSInteger backgroundTop;//品牌背景上间距
    NSInteger tvTop;//品牌描述上间距
    BOOL isHasGoods;//是否有商品
    NSInteger mark;//标记展开标尺的pruduct下标
    NSArray *colorArr;
    __weak KWMFilterVC *filterVC;
}

+(NSString *)kwmTag{
    return @"KWMBrandCaramelVC";
}

47 48 49 50
+ (NSString *)kwmBoard {
    return @"Brand";
}

houweibin committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    [self initHeaderView];
}

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

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initView];
houweibin committed
68
    [self initTableViewHeaderHidden];
houweibin committed
69 70 71
    [self getCollection:_handle];
}

houweibin committed
72 73 74 75 76 77 78 79 80 81 82 83 84
- (void)initTableViewHeaderHidden{
    //判断是否有品牌图,有就显示
    if(self.collection &&
       self.collection.image && self.collection.image.sourceURL &&
       ![KWMStringUtil isEmpty:self.collection.image.sourceURL.absoluteString]){
        self.tbvBrandCaramel.tableHeaderView.frame = CGRectMake(0, 0, UI_SCREEN_WIDTH, 210);
        self.tbvBrandCaramel.tableHeaderView.hidden = NO;
    }else{
        self.tbvBrandCaramel.tableHeaderView.frame = CGRectMake(0, 0, 0, 0);
        self.tbvBrandCaramel.tableHeaderView.hidden = YES;
    }
}

houweibin committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
- (void)setBackColor{
       colorArr = [NSArray arrayWithObjects:@"#D8A88D",@"#A4C8C6",@"#E6C995",@"#718473",@"#D7B48B", nil];
    int num = arc4random_uniform(10);
    NSString *colorStr = [colorArr objectAtIndex:num/(colorArr.count)];
    _ivBrand.backgroundColor = [UIColor sam_colorWithHex:colorStr];
}

-(UITableView *)targetTableView{
    return self.tbvBrandCaramel;
}

-(void)loadData{
    if(self.collection!=nil && self.collection.identifier!=nil){
        [self getProcduct:self.collection.identifier];
    }
}

-(void)reLoadData{
    self.currentPage = 0;
    if(self.collection!=nil && self.collection.identifier!=nil){
        [self getProcduct:self.collection.identifier];
106 107
    }
}
houweibin committed
108 109

- (void)initHeaderView{
houweibin committed
110 111
    self.barandSelectView = [[KWMBarandSelectView alloc]init];
    self.barandSelectView.count = [[KWMShoppingCart sharedInstance] count].integerValue;
houweibin committed
112 113
    //self.barandSelectView.lbBrand.text = [_brand uppercaseString];
    self.barandSelectView.lbBrand.text = _brand;
houweibin committed
114
    self.barandSelectView.delegate = self;
houweibin committed
115
    [self.view addSubview:_barandSelectView];
houweibin committed
116
    [self initCustomTitleBarConstarints:self.barandSelectView];
houweibin committed
117 118 119 120 121 122 123 124
}

static NSString *idStr = @"KWMBrandCaramelCell";
- (void)initView{

    isUp = NO;
    backgroundTop = 210;
    tvTop = 255;
houweibin committed
125
    mark = -1;
houweibin committed
126

houweibin committed
127 128
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.hasNextPage = NO;
houweibin committed
129 130 131 132 133 134 135 136
    self.tbvBrandCaramel.delegate = self;
    self.tbvBrandCaramel.showsVerticalScrollIndicator = NO;
    [self.tbvBrandCaramel registerNib:[UINib nibWithNibName:idStr bundle:nil] forCellReuseIdentifier:idStr];
    
    //从新品/折扣跳转过来的非品牌页面
    if(self.goodModel!=nil && self.goodModel.comments!=nil && ![self.goodModel.comments isEqualToString:@"品牌"]){
        self.tbvBrandCaramel.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    }
houweibin committed
137 138 139 140 141 142 143 144 145
    
    if (_backColor) {
        _ivBrand.backgroundColor = _backColor;
    }else{
        [self setBackColor];
    }
    
    self.filterView.delegate = self;
    self.filterTab.delegate = self;
houweibin committed
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
}


- (void)beackLastController:(UIBarButtonItem *)leftBtn{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)onClickBotomBtn:(id)sender {
    
    self.tvDescription.scrollEnabled = NO;
    self.btnBottom.userInteractionEnabled = NO;
    //下划
    if (isUp) {
        _btnBottom.backgroundColor = [UIColor blackColor];
        [self changeFrameBy:+210 WithImageName:@"button_ brand_pull-up" AndShowView:2];
    }else{//上划
        _btnBottom.backgroundColor = [UIColor clearColor];
        [self changeFrameBy:-210 WithImageName:@"button_brand_pull-down" AndShowView:2];
    }
    isUp = !isUp;
}

//动画 筛选显示:0:不显示   1:显示   2:没有
- (void)changeFrameBy:(NSInteger)value WithImageName:(NSString *)imageStr AndShowView:(NSInteger)showNum{
    
    [UIImageView animateWithDuration:1.0 animations:^{
        //heardView动画
        self.vBackground.frame = CGRectMake(0, backgroundTop + value, UI_SCREEN_WIDTH, 210);
        self.tvDescription.frame = CGRectMake(0, tvTop + value, UI_SCREEN_WIDTH, 128);
        
        [self.btnBottom setImage:[UIImage imageNamed:imageStr] forState:UIControlStateNormal];
    } completion:^(BOOL finished) {
       //heardView动画
        backgroundTop = backgroundTop + value;
        tvTop = tvTop + value;
        self.tvDescription.scrollEnabled = YES;
        self.btnBottom.userInteractionEnabled = YES;
    }];
}



#pragma mark -- KWMBarandSelectViewDelegate
//点击返回按钮回调
- (void)kwm_clickBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
//点击购物车回调
- (void)kwm_HitShopCartBtn{

//    self.vNoneGoods.hidden = !self.vNoneGoods.hidden;
    KWMShopCartVC * shopCartVC = (KWMShopCartVC*)[KWMBaseVC findControllerBy:[KWMShopCartVC kwmTag] fromStoryboard:@"ShopCart"];
    [self.navigationController pushViewController:shopCartVC animated:YES];
}

lee committed
201 202 203 204 205 206
- (void)kwm_hitSearchBtn {
    KWMSearchFeedbackVC *vc = (KWMSearchFeedbackVC *)[KWMBaseVC findControllerBy:[KWMSearchFeedbackVC kwmTag] fromStoryboard:@"New"];
    //    vc.searchText = @"";
    [self.navigationController pushViewController:vc animated:YES];
}

houweibin committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
#pragma mark -- KWMFilterDelegate
-(void)kwm_onFilterFinish:(KWMFilterView *)filterView{
    self.filterView.hidden = YES;
    [self.filterTab close];
    [self reLoadData];
    [self.tbvBrandCaramel.mj_header beginRefreshing];
    [self.tbvBrandCaramel setContentOffset:CGPointMake(0,0) animated:YES];
}

-(void)kwm_onClickFilterBlank:(KWMFilterView *)filterView{
    [self.filterTab close];
}

#pragma mark -- KWMNormalFilterTabDelegate
-(void)kwm_onClickTab:(KWMFilterMode)filterMode isExpandView:(BOOL)isExpand{
    self.filterView.filterMode = filterMode;
223 224 225 226
    if(isExpand){
        [self.filterView show];
    }
    self.filterView.hidden = !isExpand;
houweibin committed
227 228
}

houweibin committed
229 230 231 232
#pragma mark -- KWMBrandCaramelCellDelegate

//点击左边商品回调
- (void)kwm_onClickLeftIamge:(KWMBrandCaramelCell *)brandCell{
u  
lee committed
233
    KWMNewProductVC *productDetailVC = [KWMNewProductVC getNewInstance];
houweibin committed
234 235 236 237 238 239
    productDetailVC.product = brandCell.leftProduct;
    [self.navigationController pushViewController:productDetailVC animated:YES];
}

//点击右边商品回调
- (void)kwm_onClickRightIamge:(KWMBrandCaramelCell *)brandCell{
u  
lee committed
240
    KWMNewProductVC *productDetailVC = [KWMNewProductVC getNewInstance];
houweibin committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    productDetailVC.product = brandCell.rightProduct;
    [self.navigationController pushViewController:productDetailVC animated:YES];
}

- (void)kwm_openRule:(BUYProduct *)product Or:(KWMDataProduct *)dataProduct And:(KWMBrandCaramelCell *)brandCell{

    if (product == nil) {
        return;
    }
    [self.dataList enumerateObjectsUsingBlock:^(BUYProduct *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isEqual:product]) {
            if (mark != idx) {
                mark = idx;
                [self.tbvBrandCaramel reloadData];
            }
            *stop = YES;
        }
    }];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (mark % 2 ) {
            [brandCell openRule:NO];
        }else{
            [brandCell openRule:YES];
        }
       
    });
    
}

#pragma mark -- UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataList.count%2 == 0 ? self.dataList.count/2 : self.dataList.count/2 + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KWMBrandCaramelCell *brandCaramelCell = [tableView dequeueReusableCellWithIdentifier:idStr forIndexPath:indexPath];
    
    brandCaramelCell.selectionStyle = UITableViewCellSelectionStyleNone;
    brandCaramelCell.delegate = self;
    NSInteger position = indexPath.row * 2;
    if(self.dataList!=nil && self.dataList.count>0){
        BUYProduct *leftProduct = [self.dataList objectAtIndex:position];
        BUYProduct *rightProduct = self.dataList.count>(position+1)?[self.dataList objectAtIndex:(position+1)]:nil;
        
        //如果该页面来自新品/折扣 且不为品牌页面,则,cell标题显示品牌
        if(self.goodModel!=nil && self.goodModel.comments!=nil && ![self.goodModel.comments isEqualToString:@"品牌"]){
            brandCaramelCell.isShowBrandTitle = YES;
        }
        [brandCaramelCell setData:leftProduct rightProduct:rightProduct];
    }
    return brandCaramelCell;
}

#pragma mark -- UITableViewDataSource
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    [self tableView:tableView didDeselectRowAtIndexPath:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 244;
}

-(void)getCollection:(NSString *)collectionHandle{
    NSString *handle = collectionHandle;
    if(self.goodModel!=nil && ![KWMStringUtil isEmpty:self.goodModel.handle]){
        handle = self.goodModel.handle;
    }else if([KWMStringUtil isEmpty:collectionHandle]){
        return;
    }
310 311
    NSDictionary *parameters = @{@"handle":handle};
    BOOL canUseCacheCollection = [self canUseCacheCollection:parameters];
houweibin committed
312
    [self.client getCollectionByHandle:handle completion:^(BUYCollection * _Nullable collection, NSError * _Nullable error) {
313 314 315
        if(!error){
            [KWMPPCacheUtil saveDataToCache:parameters urlKey:CACHE_KEY_SDK_getCollectionByHandle collection:collection];
        }
houweibin committed
316 317 318
        if(collection && !error){
            self.collection = collection;
            [self setBrandData];
319 320 321
            //如果已经拿到缓存中collection,getProduct则已经调用过了。
            if(!canUseCacheCollection){
                [self getProcduct:collection.identifier];
houweibin committed
322 323 324 325 326 327 328 329
            }
        }else if(error != nil){
            [self showError:error];
        }
    }];
}

- (void)setBrandData{
houweibin committed
330
    [self initTableViewHeaderHidden];
houweibin committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
    //图片
    if(_collection!=nil && _collection.image!=nil){
        NSURL *url =  [_collection.image sourceURL];
        [_ivBrand sd_setImageWithURL:url placeholderImage:nil];
    }

    if(_collection!=nil && ![KWMStringUtil isEmpty:_collection.stringDescription]){
        //文字介绍
        _tvDescription.text = _collection.stringDescription;
        _tvDescription.hidden = NO;
        _btnBottom.hidden = NO;
    }else{
        _tvDescription.hidden = YES;
        _btnBottom.hidden = YES;
    }
}

- (void)getProcduct:(NSNumber *)collectionId{
    
    if(collectionId == nil){
        return;
    }
    __weak KWMBrandCaramelVC *weakSelf = self;
houweibin committed
354 355 356
    //初始化排序及筛选tag
    BUYCollectionSort collectionSort = self.filterView.selectedSort;
    NSMutableArray *tags = [KWMFilterUtil getTagsByFilterArray:self.filterView.selectedTags];
houweibin committed
357
    if(self.isSale){
houweibin committed
358
        [tags addObject:@"sale"];
houweibin committed
359
    }
lee committed
360 361 362
    if (self.currentPage == 0) {
        [self.tbvBrandCaramel.mj_footer resetNoMoreData];
    }
houweibin committed
363
    NSInteger tagetPage = self.currentPage.integerValue + 1;
364 365 366 367
    NSDictionary *parameters = [KWMPPCacheUtil getProductPageCacheKey:tagetPage pageSize:self.client.pageSize sortOrder:collectionSort tags:tags inCollection:collectionId];
    if([self isUseCacheProduct:parameters page:tagetPage]){
        return;
    };
houweibin committed
368
    [self.client getProductsPage:tagetPage inCollection:collectionId withTags:tags sortOrder:collectionSort completion:^(NSArray *products,NSUInteger page, BOOL reachedEnd, NSError *error){
369 370 371
        if (!error){
            [KWMPPCacheUtil saveDataToCache:parameters urlKey:CACHE_KEY_SDK_getProductsPage products:products];
        }
houweibin committed
372
        if (error) {
houweibin committed
373 374
            [self showError:error];
            NSLog(@"Error fetching products: %@", error);
houweibin committed
375 376 377 378
        }else if (products.count == 0 && tagetPage == 1){
            weakSelf.vNoneGoods.hidden = NO;
            [weakSelf.dataList removeAllObjects];
            [self.tbvBrandCaramel reloadData];
houweibin committed
379
        }else if (error == nil && products) {
380 381
            if(tagetPage == 1){
                [weakSelf.dataList removeAllObjects];
houweibin committed
382 383 384
            }
            weakSelf.hasNextPage = !reachedEnd;
            [weakSelf appendDataList:products setPage:page];
houweibin committed
385
            weakSelf.vNoneGoods.hidden = YES;
houweibin committed
386
            [self.tbvBrandCaramel reloadData];
lee committed
387 388 389 390 391
            
//            [weakSelf.dataList addObjectsFromArray:products];
//            [weakSelf.tbvBrandCaramel.mj_header endRefreshing];
//            [weakSelf.tbvBrandCaramel.mj_footer endRefreshing];
//            [weakSelf.tbvBrandCaramel reloadData];
houweibin committed
392 393 394 395 396
        }
        [weakSelf hideLoading];
    }];
}

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
#pragma mark -cache
//缓存规则:第一页数据默认先从缓存拿,然后再更新,后面的根据网络判断从缓存拿数据还是从api拿数据
- (BOOL)isUseCacheProduct:(NSDictionary *)cacheKeyDictionary page:(NSUInteger)page{
    if(!kIsNetwork || page==1){
        NSArray *cacheProducts = [KWMPPCacheUtil getProductDataByCache:cacheKeyDictionary urlKey:CACHE_KEY_SDK_getProductsPage];
        if(cacheProducts && cacheProducts.count > 0){
            self.vNoneGoods.hidden = YES;
            if(page == 1){
                [self.dataList removeAllObjects];
            }
            self.hasNextPage = YES;
            [self appendDataList:cacheProducts setPage:page];
            [self.tbvBrandCaramel reloadData];
        }else if(!kIsNetwork){
            [self showToast:CACHE_KEY_FAIL_HINT];
        }
    }
    return !kIsNetwork;
}

//缓存规则:默认拿缓存数据,api依旧照常执行,不过执行过的getProduct api不会重新执行
-(BOOL)canUseCacheCollection:(NSDictionary *)cacheKeyDictionary{
        BUYCollection *collection = [KWMPPCacheUtil getCollectionByCache:cacheKeyDictionary urlKey:CACHE_KEY_SDK_getCollectionByHandle];
        if(collection){
            self.collection = collection;
            [self setBrandData];
            [self getProcduct:collection.identifier];
            return true;
        }else{
            return false;
        }
}




houweibin committed
433
@end