KWMBrandVC.m 10.7 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//
//  KWMBrandVC.m
//  iCemarose
//
//  Created by 陈荣科 on 16/8/23.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMBrandVC.h"
#import "KWMBrandCell.h"
#import "KWMBrandCaramelVC.h"
#import "KWMSearchBrandVC.h"
#import "KWMLoginVC.h"
#import "KWMShopCartVC.h"
#import "KWMShopCartData.h"
#import "KWMCarCountView.h"
#import "KWMStringUtil.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "KWMBrandsTypeModel.h"
#import "UIColor+SAMAdditions.h"
lee committed
21
#import "KWMShoppingCart.h"
lee committed
22
#import "KWMSearchFeedbackVC.h"
houweibin committed
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 74 75 76 77 78


@interface KWMBrandVC ()<KWMCarCountViewDelegate>
@property (nonatomic) SFFocusViewLayout *focusViewLayout;
@property (nonatomic) KWMCarCountView *vCartCount;
@end

@implementation KWMBrandVC{
    NSIndexPath *selectPath;
    NSArray *bransType;
    NSMutableDictionary *imageDict;
    NSInteger loadNum;
    NSArray *colorArr;
}

static NSString *cellId = @"KWMBrandCell";

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

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
//    bransType = [NSArray array];
    imageDict = [NSMutableDictionary dictionary];
    colorArr = [NSArray arrayWithObjects:@"#D8A88D",@"#A4C8C6",@"#E6C995",@"#718473",@"#D7B48B", nil];
    // 底部背景高度 = 屏幕高 - 64 nav高度 - 最后一个cell的高度 - tabar高度44
    self.backgroundHeight.constant = UI_SCREEN_HEITHT - 64 - 280 - 44;
    
    [self.ctvBrand registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];
    [self initView];
    [self initBrandNameAndImage];
//    [self requestBrandsType];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    [self.tabBarController setHidesBottomBarWhenPushed:NO];
    [self initHeardView];

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

- (void)initView{
    _focusViewLayout = [[SFFocusViewLayout alloc] init];
    self.ctvBrand.collectionViewLayout = _focusViewLayout;
    self.ctvBrand.delegate = self;
    self.ctvBrand.showsVerticalScrollIndicator = NO;
    [self.ctvBrand registerNib:[UINib nibWithNibName:@"KWMBrandCell" bundle:nil] forCellWithReuseIdentifier:cellId];
79 80 81 82 83
    //collectionview滑动到末尾后,点击底部空白(更多品牌)去往所有品牌页面
    self.ctvBrand.backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
    self.ctvBrand.backgroundView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickMoreBrand:)];
    self.ctvBrand.backgroundView.gestureRecognizers = @[tapRecognizer];
houweibin committed
84 85 86
}

- (void) initHeardView{
houweibin committed
87 88 89 90 91
    self.vCartCount = [[KWMCarCountView alloc] init];
    self.vCartCount.count = [[KWMShoppingCart sharedInstance] count].integerValue;
    self.vCartCount.delegate = self;
    [self.view addSubview:self.vCartCount];
    [self initCustomTitleBarConstarints:self.vCartCount];
houweibin committed
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131
}

- (void)initBrandNameAndImage{
    bransType = [NSArray arrayWithObjects:@"Burberry",@"Caramel",@"Dolce & Gabbana",@"Fendi",@"Kenzo",@"Il Gufo",@"Stella McCartney Kids", nil];
    
    NSDictionary * dict = @{
                               @"Burberry":Burberry,
                               @"Caramel":Caramel,
                               @"Dolce & Gabbana":DolceGabbana,
                               @"Fendi":Fendi,
                               @"Kenzo":Kenzo,
                               @"Il Gufo":IlGufo,
                               @"Stella McCartney Kids":StellaMcCartneyKids,
                           };
    [imageDict addEntriesFromDictionary:dict];
}

- (void)onClickShopCart:(UIBarButtonItem *)btnRight{
    //购物车
    KWMShopCartVC *ShopCartVC = (KWMShopCartVC *)[KWMBaseVC findControllerBy:[KWMShopCartVC kwmTag] fromStoryboard:@"ShopCart"];
    [self.navigationController pushViewController:ShopCartVC animated:YES];
}

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

-(void)onClickMoreBrand:(id)sender{
    KWMSearchBrandVC *searchBrandVC = (KWMSearchBrandVC*)[KWMBaseVC findControllerBy:[KWMSearchBrandVC kwmTag] fromStoryboard:@"Brand"];
    [self.navigationController pushViewController:searchBrandVC animated:YES];
}

#pragma mark - KWMCarCountViewDelegate
- (void)kwm_touchShopCartBtn{
    //购物车
    KWMShopCartVC *ShopCartVC = (KWMShopCartVC *)[KWMBaseVC findControllerBy:[KWMShopCartVC kwmTag] fromStoryboard:@"ShopCart"];
    [self.navigationController pushViewController:ShopCartVC animated:YES];
}

lee committed
132 133 134 135 136 137
- (void)kwm_touchSearchBtn {
    KWMSearchFeedbackVC *vc = (KWMSearchFeedbackVC *)[KWMBaseVC findControllerBy:[KWMSearchFeedbackVC kwmTag] fromStoryboard:@"New"];
//    vc.searchText = @"";
    [self.navigationController pushViewController:vc animated:YES];
}

houweibin committed
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
#pragma mark -- UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (bransType.count > 0) {
        return bransType.count + 1;
    }
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    KWMBrandCell *brandCell = (KWMBrandCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];

    selectPath = [collectionView indexPathsForSelectedItems].lastObject;
    if (indexPath.row == 0) {
        brandCell.lbTitle.text = @"所有品牌";
        brandCell.ivBackground.image = [UIImage imageNamed:@"shore_im_brands"];
    }else{
        NSString *brand = [bransType objectAtIndex:indexPath.row - 1];
        brandCell.lbTitle.text = brand;
//        KWMBrandsTypeModel *brands = [bransType objectAtIndex:indexPath.row - 1];
//        brandCell.lbTitle.text = brands.designers;
        if (imageDict.count > 0) {
            
            brandCell.ivBackground.image = nil;
//            [brandCell.ivBackground sd_setImageWithURL:[imageDict objectForKey:brands.designers] placeholderImage:[UIImage imageNamed:@"ic_loading"]];

            NSString *colorStr = [colorArr objectAtIndex:(indexPath.row)%(colorArr.count)];
            brandCell.ivBackground.backgroundColor = [UIColor sam_colorWithHex:colorStr];
            [brandCell.ivBackground sd_setImageWithURL:[imageDict objectForKey:brand] placeholderImage:nil];
        }
    }
    return brandCell;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    cell = nil;
}
#pragma mark -- UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row) {
//        KWMBrandsTypeModel *brands = [bransType objectAtIndex:indexPath.row - 1];
        //品牌介绍
lee committed
181 182
//        KWMBrandCaramelVC *brandCaramelVC = (KWMBrandCaramelVC *)[KWMBaseVC findControllerBy:[KWMBrandCaramelVC kwmTag] fromStoryboard:@"Brand"];
//        brandCaramelVC.isSale = NO;
houweibin committed
183 184 185 186
//        brandCaramelVC.handle = [KWMStringUtil deleteOtherCharExceptLetterWithLine:brands.designers];
//        brandCaramelVC.brand = brands.designers;
        
        NSString *brand = [bransType objectAtIndex:indexPath.row - 1];
lee committed
187 188 189 190 191 192 193 194 195
//        brandCaramelVC.handle = [KWMStringUtil deleteOtherCharExceptLetterWithLine:brand];
//        brandCaramelVC.brand = brand;
//
//        NSString *colorStr = [colorArr objectAtIndex:(indexPath.row)%(colorArr.count)];
//        brandCaramelVC.backColor = [UIColor sam_colorWithHex:colorStr];
//
//        [self.navigationController pushViewController:brandCaramelVC animated:YES];
        brand = [[KWMStringUtil deleteOtherCharExceptLetterWithLine:brand] lowercaseString];
        [self openURLWithString:[NSString stringWithFormat:@"https://www.cemarose.cn/collections/%@",brand]];
houweibin committed
196 197 198 199 200 201 202 203 204 205 206 207 208 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 258 259 260 261 262
    }else{
        //品牌搜索
        KWMSearchBrandVC *searchBrandVC = (KWMSearchBrandVC*)[KWMBaseVC findControllerBy:[KWMSearchBrandVC kwmTag] fromStoryboard:@"Brand"];
        [self.navigationController pushViewController:searchBrandVC animated:YES];
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusableView = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        reusableView = header;
    }
    reusableView.backgroundColor = [UIColor greenColor];
    if (kind == UICollectionElementKindSectionFooter)
    {
        UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
        footerview.backgroundColor = [UIColor purpleColor];
        reusableView = footerview;
    }
    return reusableView;
}

- (void)requestBrandsType{
    __weak KWMBrandVC *weakSelf = self;
    [weakSelf.api getBrandsType:nil success:^(NSURLSessionDataTask *task, KWMBrandsTypeResult *result) {
        NSString *handle;
        bransType = result.brandsType;
        loadNum = 0;
        for (int i = 0; i < bransType.count; ++i) {
            KWMBrandsTypeModel *brandsType = [bransType objectAtIndex:i];
            handle = [KWMStringUtil deleteOtherCharExceptLetterWithLine:brandsType.designers];
            [weakSelf requestBrandsdetail:handle With:brandsType.designers];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [weakSelf hideLoading];
        [self showError:error];
    }];
    [self showLoading];
}

- (void)requestBrandsdetail:(NSString *)handle With:(NSString *)key{
    [self.client getCollectionByHandle:handle completion:^(BUYCollection * _Nullable collection, NSError * _Nullable error) {
        if (error == nil && collection) {
             [self hideLoading];
            loadNum++;
            [imageDict setObject:collection.image.sourceURL forKey:key];
            [self hideLoading];
            if (loadNum == bransType.count) {
                [self.ctvBrand reloadData];
            }
        }else{
             [self hideLoading];
            NSLog(@"error : %@",error);
        }
    }];
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSIndexPath *path =  [self.ctvBrand indexPathForItemAtPoint:CGPointMake(scrollView.contentOffset.x, scrollView.contentOffset.y)];
}


@end