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

#import "KWMSelectedGoodsVC.h"
u  
lee committed
10
#import "KWMNewProductVC.h"
houweibin committed
11 12
#import "KWMBeforePayVC.h"
#import "KWMMineTitleView.h"
houweibin committed
13 14
#import "MJRefresh.h"
#import "KWMFilterUtil.h"
houweibin committed
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
#import "KWMStringUtil.h"
#import "KWMImageBlurUtil.h"

@interface KWMSelectedGoodsVC ()
@end

@implementation KWMSelectedGoodsVC{
    UIButton *rightButton;
    BOOL isSelect;
    UIView *vStopTap;
    NSInteger openPosition;//标记展开标尺的pruduct下标
}

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

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    [_vTitle removeFromSuperview];
}

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


static NSString *idStr = @"KWMBrandCaramelCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
lee committed
48 49
    
//    [self reLoadData];
houweibin committed
50 51 52
}

- (void)initView{
houweibin committed
53 54
    
    self.hasNextPage = NO;
houweibin committed
55
    self.collectionSort = BUYCollectionSortCreatedDescending;
u  
lee committed
56
    if (self.handle && !self.title) {
lee committed
57
        self.title = self.handle.capitalizedString;
lee committed
58
    }else if(self.productType != nil){
houweibin committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        //初始化双行标题栏
        NSString *mTitle;
        if(self.tagType == 0){
            mTitle = @"婴儿";
        }else if(self.tagType == 1){
            mTitle = @"女孩";
        }else if(self.tagType == 2){
            mTitle = @"男孩";
        }else if(self.tagType == 3){
            mTitle = @"鞋子";
        }
        KWMDoubleTitleView *doubleTitleView = [[KWMDoubleTitleView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
        doubleTitleView.lbTitle.text = mTitle;
        doubleTitleView.lbSecondTitle.text = self.productType.value;
        self.navigationItem.titleView=doubleTitleView;
    }
    openPosition = -1;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    isSelect = NO;
    vStopTap = [[UIView alloc] initWithFrame:CGRectMake(0, 20, UI_SCREEN_WIDTH, 44)];
    vStopTap.backgroundColor = [UIColor clearColor];
    _vHeard.backgroundColor = [UIColor sam_colorWithHex:@"F4F5F7"];
houweibin committed
82 83 84 85
    
    self.filterView.delegate = self;
    self.filterTab.delegate = self;
    
houweibin committed
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
    [self initCacheData];
}

-(void)initCacheData{
    NSNumber *collectionId;
    NSArray *tags;
    if(self.collection!=nil){
        collectionId = self.collection.identifier;
    }
    if(self.productType!=nil){
        if(![KWMStringUtil isEmpty:self.productType.collectionId]){
            NSInteger num = [self.productType.collectionId integerValue];
            collectionId = @(num);
        }
        NSString *tag;
        if(self.tagType == 0){
            tag = @"baby";
        }else if(self.tagType == 1){
            tag = @"girls";
        }else if(self.tagType == 2){
            tag = @"boys";
        }else if(self.tagType == 3){
            tag = @"shoes";
        }
        tags = [NSArray arrayWithObjects:tag, nil];
    }
lee committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    NSMutableDictionary *sf = [NSMutableDictionary new];
    
    for (NSString *tag in self.ctags) {
        for (KWMFilter *kf in self.filterView.allTags) {
            NSString * key = nil;
            for (NSString *it in kf.tags) {
                if ([[KWMFilterUtil transformTag:nil tag:it] isEqualToString:tag]) {
                    key = it;
                    break;
                }
            }
            if (key) {
                if (sf[kf.group]) {
                    [sf[kf.group] addObject:key];
                }else{
                    [sf setValue:[[NSMutableArray alloc] initWithObjects:key, nil] forKey:kf.group];
                }
                break;
            }
        }
    }
    
    if ([sf count]) {
        [self.filterView setSelectedTags:[[[sf allKeys] rx_mapWithBlock:^id(id each) {
            KWMFilter *r = [[KWMFilter alloc] init];
            r.group = each;
            r.tags = sf[each];
            return r;
        }] mutableCopy]];
    }
houweibin committed
142 143 144 145 146 147 148 149 150 151 152 153 154

    NSArray *products =  [self.userDao getCollectionCache:collectionId tags:tags];
    if(products && products.count>0){
        [self appendDataList:products setPage:1];
        self.currentPage = 0;
    }
}

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

-(void)loadData{
houweibin committed
155
    [self requestProductList];
houweibin committed
156 157 158 159
}

-(void)reLoadData{
    self.currentPage = 0;
houweibin committed
160
    [self requestProductList];
houweibin committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
}


- (void)setAnimatedWith:(CGFloat)moveHeight{
    [UIImageView animateWithDuration:1.0 animations:^{
        self.vGoodsStyle.frame = CGRectMake(0, moveHeight, UI_SCREEN_WIDTH, 340);
    } completion:^(BOOL finished) {
        [vStopTap removeFromSuperview];
        if (moveHeight>0) {
            self.vBackground.hidden = NO;
        }else{
            self.vBackground.hidden = YES;
        }
    }];
}

houweibin committed
177 178 179 180
#pragma mark -- KWMFilterDelegate
-(void)kwm_onFilterFinish:(KWMFilterView *)filterView{
    self.filterView.hidden = YES;
    [self.filterTab close];
lee committed
181
//    [self reLoadData];
houweibin committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    [self.tbvSelectedGoods.mj_header beginRefreshing];
    [self.tbvSelectedGoods 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.hidden = !isExpand;
    self.filterView.filterMode = filterMode;
}


houweibin committed
197 198 199 200
#pragma mark -- KWMBrandCaramelCellDelegate

//点击左边商品回调
- (void)kwm_onClickLeftIamge:(KWMBrandCaramelCell *)brandCell{
u  
lee committed
201
    KWMNewProductVC *productDetailVC = [KWMNewProductVC getNewInstance];
houweibin committed
202 203 204 205 206 207
    productDetailVC.product = brandCell.leftProduct;
    [self.navigationController pushViewController:productDetailVC animated:YES];
}

//点击右边商品回调
- (void)kwm_onClickRightIamge:(KWMBrandCaramelCell *)brandCell{
u  
lee committed
208
    KWMNewProductVC *productDetailVC = [KWMNewProductVC getNewInstance];
houweibin committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    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 (openPosition != idx) {
                openPosition = idx;
                [self.tbvSelectedGoods reloadData];
            }
            *stop = YES;
        }
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (openPosition % 2 ) {
            [brandCell openRule:NO];
        }else{
            [brandCell openRule:YES];
        }
        
    });
}

#pragma mark -- UITableViewDelegateU
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(self.dataList == nil){
        return 0;
    }
    NSInteger count = self.dataList.count;
    if(count%2 == 0){
        return count/2;
    }else{
        return count/2+1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    KWMBrandCaramelCell *brandCaramelCell = [tableView dequeueReusableCellWithIdentifier:idStr];
    if(!brandCaramelCell){
        [tableView registerNib:[UINib nibWithNibName:idStr bundle:nil] forCellReuseIdentifier:idStr];
        brandCaramelCell = [tableView dequeueReusableCellWithIdentifier:idStr];
    }
lee committed
258
    brandCaramelCell.isShowBrandTitle = YES;
houweibin committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    brandCaramelCell.delegate = self;
    brandCaramelCell.selectionStyle = UITableViewCellSelectionStyleNone;
    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;
        [brandCaramelCell setData:leftProduct rightProduct:rightProduct];
    }    
    return brandCaramelCell;
}

#pragma mark -- ITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return (UI_SCREEN_WIDTH/2 - 20 + 64);
}

houweibin committed
275
- (void)requestProductList{
lee committed
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
    __weak KWMSelectedGoodsVC *weakSelf = self;
    NSInteger tagetPage = self.currentPage.integerValue + 1;
    void(^completion)(NSArray<BUYProduct *> *,NSUInteger,BOOL,NSError*) = ^(NSArray *products,NSUInteger page, BOOL reachedEnd, NSError *error){
        [weakSelf hideLoading];
        
        if(tagetPage == 1){
            if(!products || products.count == 0){
                self.vNoData.hidden = NO;
                [weakSelf.dataList removeAllObjects];
                [weakSelf.tbvSelectedGoods reloadData];
                return;
            }
        }
        
        weakSelf.isLoading = NO;
        if (error == nil && products) {
            self.vNoData.hidden = YES;
            weakSelf.hasNextPage = !reachedEnd;
            [weakSelf appendDataList:products setPage:page];
        }
        else {
            [self showError:error];
            NSLog(@"Error fetching products: %@", error);
        }
    };
lee committed
301 302
    if (self.handle) {
        [self.client getCollectionByHandle:self.handle completion:^(BUYCollection * _Nullable collection, NSError * _Nullable error) {
lee committed
303 304 305 306 307
            NSArray * tags = [KWMFilterUtil getTagsByFilterArray:self.filterView.selectedTags];
            if ([tags count] <= 0) {
                tags = self.ctags;
            }
            [self.client getProductsPage:tagetPage inCollection:collection.identifier withTags:tags sortOrder:self.filterView.selectedSort completion:completion];
lee committed
308 309 310 311
        }];
//        [self.client getProductsPage:tagetPage inCollection:nil withTags:self.ctags sortOrder:self.filterView.selectedSort completion:completion];
        return;
    }
houweibin committed
312 313 314 315
    NSNumber *collectionId;
    if(self.collection!=nil){
        collectionId = self.collection.identifier;
    }
houweibin committed
316 317 318 319 320
    
    //初始化排序及筛选tag
    BUYCollectionSort collectionSort = self.filterView.selectedSort;
    NSMutableArray *tags = [KWMFilterUtil getTagsByFilterArray:self.filterView.selectedTags];
    
houweibin committed
321 322 323 324 325 326
    if(self.productType!=nil){
        if(![KWMStringUtil isEmpty:self.productType.collectionId]){
            NSInteger num = [self.productType.collectionId integerValue];
            collectionId = @(num);
        }
        if(self.tagType == 0){
houweibin committed
327
            [tags addObject:@"baby"];
houweibin committed
328
        }else if(self.tagType == 1){
houweibin committed
329
            [tags addObject:@"girls"];
houweibin committed
330
        }else if(self.tagType == 2){
houweibin committed
331
            [tags addObject:@"boys"];
houweibin committed
332
        }else if(self.tagType == 3){
houweibin committed
333
            [tags addObject:@"shoes"];
houweibin committed
334 335 336 337 338 339 340 341 342 343
        }
    }
    
    if(collectionId == nil){
        return;
    }
    
    self.isLoading = YES;
    
    
lee committed
344
    [self.client getProductsPage:tagetPage inCollection:collectionId withTags:tags sortOrder:collectionSort completion:completion];
houweibin committed
345 346 347
}

@end