KWMNewHomeVC.m 16.3 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11 12
//
//  KWMNewHomeVC.m
//  iCemarose
//
//  Created by HouWeiBin on 2017/7/10.
//  Copyright © 2017年 kollway. All rights reserved.
//

#import "KWMNewHomeVC.h"
#import "KWMShopCartVC.h"
#import "KWMNewHomeCell.h"
#import "KWMSearchBar.h"
houweibin committed
13
#import "KWMHomeData.h"
houweibin committed
14
#import "KWMShoppingCart.h"
lee committed
15 16
#import <RegexKitLite/RegexKitLite.h>
#import <RXCollections/RXCollection.h>
lee committed
17
#import "KWMHomeVC.h"
lee committed
18 19
#import "KWMWishListVC.h"
#import <MJRefresh.h>
houweibin committed
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

typedef enum{
    
    HeaderAd=0,
    
    HeaderMenu=1,
    
    HeaderRecommend=2,
    
    HeaderClothingSets=3,

    HeaderHotSales=4,
    
}Header;


@interface KWMNewHomeVC ()

@property(nonatomic,weak) IBOutlet UICollectionView *cvHome;

@property(nonatomic,strong) KWMAdHeader *adHeader;

@property(nonatomic,strong) KWMMenuHeader *menuHeader;

@property(nonatomic,strong) KWMRecommendHeader *recommendHeader;

@property(nonatomic,strong) KWMClothingSetsHeader *clothingSetsHeader;

@property(nonatomic,strong) KWMHotSalesHeader *hotSalesHeader;

@property(nonatomic,strong) KWMSearchBar *titleView;

houweibin committed
52 53
@property(nonatomic) KWMHomeData *homeData;

lee committed
54 55
@property(nonatomic) NSInteger page;

lee committed
56 57 58
@property(nonatomic) NSMutableArray<BUYProduct *>* productArray;

@property(nonatomic) NSMutableDictionary *dataMapping;
houweibin committed
59

r  
lee committed
60
//@property(nonatomic) NSString *clotingSetsHandle;
houweibin committed
61

houweibin committed
62 63 64 65 66 67 68
@end

@implementation KWMNewHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
lee committed
69
    self.page = 1;
lee committed
70 71 72
//    [self requestProduct:@""];
    [self loadHomeData];
    
houweibin committed
73 74 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
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if(self.adHeader){
        [self.adHeader startBannerTimer];
    }
    if(self.hotSalesHeader){
        [self.hotSalesHeader startBannerTimer];
    }
    [self.navigationController setNavigationBarHidden:YES];
    if (self.titleView) {
        NSInteger count = [[KWMShoppingCart sharedInstance] count].integerValue;
        self.titleView.count = count;
        [self.titleView resumeView];
    }
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    if(self.adHeader){
        [self.adHeader endBannerTimer];
    }
    if(self.hotSalesHeader){
        [self.hotSalesHeader endBannerTimer];
    }
}

houweibin committed
101

houweibin committed
102
- (void)initView{
lee committed
103
//    self.title = @"首页";
houweibin committed
104 105 106
    self.adHeader = [[KWMAdHeader alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 275)];
    self.menuHeader = [[KWMMenuHeader alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 100)];
    self.recommendHeader = [[KWMRecommendHeader alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 230)];
houweibin committed
107
    self.clothingSetsHeader = [[KWMClothingSetsHeader alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 317)];
houweibin committed
108
    self.hotSalesHeader = [[KWMHotSalesHeader alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 250)];
houweibin committed
109 110 111 112 113 114 115
    
    self.adHeader.delegate = self;
    self.menuHeader.delegate = self;
    self.recommendHeader.delegate = self;
    self.clothingSetsHeader.delegate = self;
    self.hotSalesHeader.delegate = self;
    
houweibin committed
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 142
    [self initTitleView];
    [self initCollectionView];
}

-(void)initTitleView{
    self.titleView = [[KWMSearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
    self.titleView.delegate = self;
    self.titleView.btnSearch.hidden = YES;
    [self.view addSubview:self.titleView];
}

- (void)initCollectionView{
    
    CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.minimumColumnSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.columnCount = 2;
    self.cvHome.collectionViewLayout = layout;
    
    [self.cvHome registerNib:[UINib nibWithNibName:NSStringFromClass([KWMNewHomeCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([KWMNewHomeCell class])];
    
    [self.cvHome registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader withReuseIdentifier:[KWMAdHeader kwmTag]];
    [self.cvHome registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader withReuseIdentifier:[KWMHotSalesHeader kwmTag]];
    [self.cvHome registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader withReuseIdentifier:[KWMRecommendHeader kwmTag]];
    [self.cvHome registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader withReuseIdentifier:[KWMClothingSetsHeader kwmTag]];
    [self.cvHome registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader withReuseIdentifier:[KWMMenuHeader kwmTag]];
lee committed
143 144 145 146 147 148
    
    self.cvHome.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
}

- (void) loadMore {
    [self requestProductWithHandle:@"products-list-app-homepage" tags:nil valueKeyPath:@"productArray"];
houweibin committed
149 150
}

houweibin committed
151 152
#pragma mark - KWMAdDelegate
-(void)kwm_gotoWebView:(NSString *)url{
lee committed
153 154
//    NSLog(@"click webview");
    [self openURLWithString:url];
houweibin committed
155 156 157 158
}

#pragma mark - KWMMenuDelegate
-(void)kwm_onClickHotMenu{
u  
lee committed
159
    [self openURLWithString:@"https://cemarose.myshopify.com/collections/new?title=最新单品"];
houweibin committed
160 161 162
}

-(void)kwm_onClickNewMenu{
lee committed
163 164
    KWMHomeVC* selectedGoodsVC = (KWMHomeVC *)[KWMBaseVC findControllerBy:[KWMHomeVC kwmTag] fromStoryboard:@"Main"];
    [self.navigationController pushViewController:selectedGoodsVC animated:YES];
houweibin committed
165 166 167
}

-(void)kwm_onClickWishMenu{
lee committed
168
    NSLog(@"wishmenu");
houweibin committed
169 170 171
    if(![self checkLogin]){
        return;
    }
lee committed
172 173
    KWMWishListVC* vc= (KWMWishListVC *)[KWMBaseVC findControllerBy:[KWMWishListVC kwmTag] fromStoryboard:@"NewProduct"];
    [self.navigationController pushViewController:vc animated:YES];
houweibin committed
174 175 176
}

-(void)kwm_onClickDeliveryMenu{
lee committed
177
    NSLog(@"deliverymenu");
u  
lee committed
178
    [self openURLWithString:@"https://www.cemarose.com/pages/service-description/?title=服务说明"];
houweibin committed
179 180
}

houweibin committed
181 182
#pragma mark - KWMRecommendDelegate
-(void)kwm_gotoRecommendPage{
183
    [self openURLWithString:@"https://cemarose.myshopify.com/collections/hot-sell-app?title=主推单品"];
houweibin committed
184 185 186
}

-(void)kwm_gotoClothingSetsPage{
r  
lee committed
187 188
//    [self openURLWithString:[NSString stringWithFormat:@"https://cemarose.myshopify.com/collections/%@",self.clotingSetsHandle]];
    [self openURLWithString:self.homeData.clothing_sets.url];
houweibin committed
189 190 191
}

#pragma mark - KWMRecommendDelegate KWMClothingSetsDelegate KWMHotSalesDelegate
houweibin committed
192
-(void)kwm_onClickProduct:(BUYProduct *)product{
lee committed
193 194
//    NSLog(@"product");
    [self openURLWithString:[NSString stringWithFormat:@"https://cemarose.myshopify.com/products/%@",product.handle]];
houweibin committed
195 196
}

houweibin committed
197 198 199 200 201 202 203
#pragma mark - UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 5;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if(section == HeaderHotSales){
houweibin committed
204
        return self.productArray ? self.productArray.count:0;
houweibin committed
205 206 207 208 209 210
    }
    return 0;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    KWMNewHomeCell *homeCell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([KWMNewHomeCell class]) forIndexPath:indexPath];
houweibin committed
211 212
    BUYProduct *product = [self.productArray objectAtIndex:indexPath.row];
    homeCell.product = product;
houweibin committed
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 263 264 265 266 267 268 269
    return homeCell;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *headerView;
    if (kind == CHTCollectionElementKindSectionHeader) {
        switch (indexPath.section) {
            case HeaderAd:
                headerView = [collectionView dequeueReusableSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader withReuseIdentifier:[KWMAdHeader kwmTag] forIndexPath:indexPath];
                if(headerView.subviews.count == 0){
                    [headerView addSubview:self.adHeader];
                }
                break;
            case HeaderMenu:
                headerView = [collectionView dequeueReusableSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader withReuseIdentifier:[KWMMenuHeader kwmTag] forIndexPath:indexPath];
                if(headerView.subviews.count == 0){
                    [headerView addSubview:self.menuHeader];
                }
                break;
            case HeaderRecommend:
                headerView = [collectionView dequeueReusableSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader withReuseIdentifier:[KWMRecommendHeader kwmTag] forIndexPath:indexPath];
                if(headerView.subviews.count == 0){
                    [headerView addSubview:self.recommendHeader];
                }
                break;
            case HeaderClothingSets:
                headerView = [collectionView dequeueReusableSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader withReuseIdentifier:[KWMClothingSetsHeader kwmTag] forIndexPath:indexPath];
                if(headerView.subviews.count == 0){
                    [headerView addSubview:self.clothingSetsHeader];
                }
                break;
            case HeaderHotSales:
                headerView = [collectionView dequeueReusableSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader withReuseIdentifier:[KWMHotSalesHeader kwmTag] forIndexPath:indexPath];
                if(headerView.subviews.count == 0){
                    [headerView addSubview:self.hotSalesHeader];
                }
                break;
            default:
                break;
        }
    }
    return headerView;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout heightForHeaderInSection:(NSInteger)section{
    NSInteger height = 0;
    switch (section) {
        case HeaderAd:
            height = 275;
            break;
        case HeaderMenu:
            height = 100;
            break;
        case HeaderRecommend:
            height = 230;
            break;
        case HeaderClothingSets:
houweibin committed
270
            height = 317;
houweibin committed
271 272
            break;
        case HeaderHotSales:
houweibin committed
273
            height = self.hotSalesHeader.actrualHeight;
houweibin committed
274 275 276 277 278 279 280 281 282
            break;
        default:
            break;
    }
    return height;
}

#pragma mark - CHTCollectionViewDelegateWaterfallLayout
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
lee committed
283
    [self kwm_onClickProduct:self.productArray[indexPath.item]];
houweibin committed
284 285 286
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
houweibin committed
287 288 289 290
    BUYProduct *product = [self.productArray objectAtIndex:indexPath.row];
    NSInteger width = (UI_SCREEN_WIDTH-30)/2;
    NSInteger height = [KWMNewHomeCell acturlHeight:product width:width];
    CGSize size = CGSizeMake(width, height);
houweibin committed
291 292 293 294 295 296 297 298 299 300
    return size;
}

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

houweibin committed
301
#pragma makr - api
lee committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
//- (void)requestProduct:(NSString *)tag{
//    NSMutableArray *tags = nil;
//    if(tag){
//        tags = [NSMutableArray array];
//        [tags addObject:tag];
//    }
//    
//    __weak KWMNewHomeVC *weakSelf = self;
//    [self showLoading];
//    [self.client getProductsPage:1 inCollection:@(Collection_All_ID) withTags:tags sortOrder:BUYCollectionSortBestSelling completion:^(NSArray *products,NSUInteger page, BOOL reachedEnd, NSError *error){
//        [weakSelf hideLoading];
//        if(error){
//            [weakSelf showError:error];
//        }
//        else if(products && products.count>0){
//            weakSelf.productArray = products;
//            [weakSelf.cvHome reloadData];
////            [weakSelf initTestData];
//        }
//    }];    
//}

- (void) requestProductWithHandle:(NSString *) handle tags:(NSArray *) tags valueKeyPath:(NSString *) valueKeyPath {
houweibin committed
325
    [self showLoading];
lee committed
326 327
    BOOL isHome = [handle isEqualToString:@"products-list-app-homepage"];
    NSInteger cpage = isHome ? self.page : 1;
lee committed
328 329
    [self.client getCollectionByHandle:handle completion:^(BUYCollection * _Nullable collection, NSError * _Nullable error) {
        if (!error) {
lee committed
330
            [self.client getProductsPage:cpage inCollection:collection.identifier withTags:tags sortOrder:BUYCollectionSortCollectionDefault completion:^(NSArray<BUYProduct *> * _Nullable products, NSUInteger page, BOOL reachedEnd, NSError * _Nullable error) {
lee committed
331
                [self hideLoading];
lee committed
332
                
lee committed
333 334 335 336 337 338
                if (!error) {
                    [self setValue:products forKeyPath:valueKeyPath];
                    [self.cvHome reloadData];
                }else{
                    [self showError:error];
                }
lee committed
339 340 341 342
                if (isHome) {
                    self.page = self.page + 1;
                    products.count ? [self.cvHome.mj_footer endRefreshing] : [self.cvHome.mj_footer endRefreshingWithNoMoreData];
                }
lee committed
343 344 345 346
            }];
        }else{
            [self hideLoading];
            [self showError:error];
houweibin committed
347
        }
lee committed
348
    }];
houweibin committed
349 350
}

u  
lee committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
//- (void) requestProductWithId:(NSNumber *) identifier tags:(NSArray *) tags valueKeyPath:(NSString *) valueKeyPath home:(BOOL) isHome page:(NSInteger) cpage{
//    [self showLoading];
//    [self.client getProductsPage:cpage inCollection:identifier withTags:tags sortOrder:BUYCollectionSortCollectionDefault completion:^(NSArray<BUYProduct *> * _Nullable products, NSUInteger page, BOOL reachedEnd, NSError * _Nullable error) {
//        [self hideLoading];
//        
//        if (!error) {
//            [self setValue:products forKeyPath:valueKeyPath];
//            [self.cvHome reloadData];
//        }else{
//            [self showError:error];
//        }
//        if (isHome) {
//            self.page = self.page + 1;
//            products.count ? [self.cvHome.mj_footer endRefreshing] : [self.cvHome.mj_footer endRefreshingWithNoMoreData];
//        }
//    }];
//}

lee committed
369 370 371 372 373 374 375 376 377
- (void) loadHomeData {
    [self requestProductWithHandle:@"hot-sell-app" tags:nil valueKeyPath:@"recommendHeader.productArray"];
    [self.api getHomeDataWithSuccess:^(NSURLSessionDataTask *task, KWMHomeDataResult *result) {
        self.homeData = result.data;
        [self.cvHome reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [self showError:error];
    }];
}
houweibin committed
378

lee committed
379 380 381
- (void)setHomeData:(KWMHomeData *)homeData {
    _homeData = homeData;
    self.adHeader.adArray = self.homeData.ad_banner;
382
//    self.homeData.hot_sales.single_show_count = @(1);
lee committed
383
    self.clothingSetsHeader.headerImage = self.homeData.clothing_sets.image;
u  
lee committed
384 385 386
//    self.homeData.clothing_sets.url =@"https://www.cemarose.cn/collections/skirts/girls+spring-summer-2016?title=ml";
    NSURL *url = [[NSURL alloc] initWithString:self.homeData.clothing_sets.url];
    NSArray *clothingParam = [url.path arrayOfCaptureComponentsMatchedByRegex:@"collections\\/([^/]*+)\\/?(.*)$"];
lee committed
387 388 389 390 391
    if (clothingParam.count) {
        clothingParam = clothingParam.firstObject;
        NSString *handle = clothingParam.count > 1 ? clothingParam[1] : nil;
        NSArray *tags = clothingParam.count > 2 ? [clothingParam[2] componentsSeparatedByString:@"+"] : nil;
        if (handle) {
r  
lee committed
392
//            self.clotingSetsHandle = handle;
lee committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
            [self requestProductWithHandle:handle tags:tags valueKeyPath:@"clothingSetsHeader.productArray"];
        }
    }
    [self requestProductWithHandle:@"products-list-app-homepage" tags:nil valueKeyPath:@"productArray"];
    NSLog(@"path:%@",clothingParam);
}

- (void)setProductArray:(NSArray<BUYProduct *> *)productArray {
    if (!_productArray) {
        NSInteger ssize = self.homeData.hot_sales.single_show_count.integerValue;
        NSInteger count = productArray.count;
        self.hotSalesHeader.singleShowArray = count <= ssize ? productArray : [productArray subarrayWithRange:NSMakeRange(0, ssize)];
        _productArray = [[NSMutableArray alloc] init];
//        [_productArray addObjectsFromArray:[productArray subarrayWithRange:NSMakeRange(self.homeData.hot_sales.single_show_count.integerValue, 99)]];
        if (count > ssize) {
        [_productArray addObjectsFromArray:[productArray subarrayWithRange:NSMakeRange(ssize, count - ssize)]];
        }
    }else{
        [_productArray addObjectsFromArray:productArray];
    }
//    _productArray = productArray;
    
}
houweibin committed
416 417

@end