KWMSelectSizeVC.m 14 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//
//  KWMSelectSizeViewController.m
//  iCemarose
//
//  Created by HouWeiBin on 16/9/1.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMSelectSizeVC.h"
#import "KWMSizeCell.h"
#import "KWMUserModel.h"
#import "KWMImageUtil.h"
#import "KWMBeforePayVC.h"
#import "KWMStringUtil.h"
#import "KWMLoginVC.h"
#import "KWMLoadingView.h"
#import "UIColor+SAMAdditions.h"
lee committed
18 19
#import "BUYProductVariant+Currency.h"
#import "KWMCurrencyUtil.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 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 79 80 81 82 83 84 85 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 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 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

@interface KWMSelectSizeVC ()

@end

@implementation KWMSelectSizeVC{
    NSMutableArray *shopCartList;
    BUYProductVariant *remindVariant;
    BUYProductVariant *defalutSize;
    NSInteger defalutCount;
    KWMLoadingView *loadingView;
    UIView *vLoadingContainer;
}

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

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.btnBackground.hidden = YES;
    [self startPageAnim];
}

-(void)startPageAnim{
    
    // 透明度动画
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    // 设定动画选项
    animation.duration = 1; // 持续时间
    animation.repeatCount = 1; // 重复次数
    
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    
    animation.fromValue = [NSNumber numberWithFloat:0.0]; // 起始透明度
    animation.toValue = [NSNumber numberWithFloat:1.0]; // 终止透明度
    
    [self.btnBackground.layer addAnimation:animation forKey:@"trans-anim"];
    self.btnBackground.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initContentHeight];
    self.cvSize.delegate = self;
    [self.cvSize registerNib:[UINib nibWithNibName:@"KWMSizeCell" bundle:nil] forCellWithReuseIdentifier:@"KWMSizeCell"];
    [self refreshView:YES];
    if(self.product!=nil){
        [self requestAdminProduct:self.product.identifier];
    }
    self.vShopCar.hidden = self.isShopcart;
    self.btnComplete.hidden = !self.isShopcart;
    self.vShopCar.layer.cornerRadius = 1; //圆角(圆形)
    self.vShopCar.layer.borderColor  = [UIColor sam_colorWithHex:@"494949"].CGColor; //要设置的颜色
    self.vShopCar.layer.borderWidth = 1; //要设置的描边宽
    
    CGFloat screenWidth =  UIScreen.mainScreen.bounds.size.width;

    self.vRemindMarginLeft.constant = -screenWidth;
}

//初始化佈局高度
-(void)initContentHeight{
    if(self.product != nil && self.product.variantsArray!=nil){
        NSInteger cellCount =  self.product.variantsArray.count;
        if(cellCount <=0){
            return;
        }
        NSInteger cellWidth = (UI_SCREEN_WIDTH - (30 + 30) - (10*3)) / 3;
        NSInteger cellHeight = cellWidth * 0.4;
        NSInteger lineCount = (cellCount +2) / 3;
        self.vContentHeight.constant = 220 + lineCount * cellHeight + 10 *(lineCount - 1);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)setShopCartModel:(KWMShopCartModel *)shopCartModel{
    _shopCartModel = shopCartModel;
    if (shopCartModel != nil) {
        NSArray<BUYProductVariant *> *sizeArray = self.product.variantsArray;
        for (int i = 0; i < sizeArray.count; ++i) {
            BUYProductVariant *productSize = [sizeArray objectAtIndex:i];
            if ([shopCartModel.size isEqualToString:productSize.title]) {
                self.selectSize = productSize;
                self.count = shopCartModel.quantity;
                defalutSize = productSize;
                defalutCount = shopCartModel.quantity;
            }
        }
    }
}

#pragma mark -- UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if(self.product != nil && self.product.variantsArray!=nil){
        return self.product.variantsArray.count;
    }else{
        return 0;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *indentifier = @"KWMSizeCell";
    KWMSizeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:indentifier forIndexPath:indexPath];
    if(self.product!=nil){
        NSArray<BUYProductVariant *> *sizeArray = self.product.variantsArray;
        if(sizeArray!=nil && sizeArray.count > 0){
            
            BUYProductVariant *productSize = [sizeArray objectAtIndex:indexPath.row];
            KWMVariants *adminVariant = [self checkAdminVariants:self.adminVariants targetVariant:productSize];
            
            BOOL isSelect = self.selectSize && productSize.identifier.integerValue == self.selectSize.identifier.integerValue;
            BOOL isRemind = remindVariant && productSize.identifier.integerValue == remindVariant.identifier.integerValue;
            [cell setData:productSize adminVariant:adminVariant isSelect:isSelect isRemind:isRemind];
        }
    }
    cell.delegate = self;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    //返回每个item的大小
    NSInteger width = (UI_SCREEN_WIDTH - (30 + 30) - (10*3)) / 3;
    NSInteger height = width * 0.4;
    return (CGSize){width,height};
}

-(CGFloat )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;{
    if(self.product!=nil){
        return self.product.variantsArray.count;
    }else{
        return 0;
    }
}

-(void)onClickBg:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)initModelByPrudct{
    
    _shopCartModel = [[KWMShopCartModel alloc] init];
    
    _shopCartModel.name = _product.title;
    _shopCartModel.brand = _product.vendor;
    
houweibin committed
174
    NSString *imageUrl = [KWMImageUtil getProductImageUrl:_product ImageSize:NormalImage];
houweibin committed
175 176 177 178 179 180 181
    
    _shopCartModel.imageStr = imageUrl;
    _shopCartModel.shopCartDict = _product.JSONDictionary;
}

- (void)initBySelectSize{
    _shopCartModel.size = _selectSize.title;
lee committed
182 183
//    _shopCartModel.price = _selectSize.price.integerValue;
    _shopCartModel.price = _selectSize.price;
houweibin committed
184 185 186
    _shopCartModel.identifier = [NSString stringWithFormat:@"%@",_selectSize.identifier];
    _shopCartModel.quantity = _count;
}
lee committed
187 188 189 190 191 192
/**
 *  点击加入购物车
 *
 *  @param sender <#sender description#>
 */
-(void)onClickComplete:(UIButton *)sender{
houweibin committed
193 194 195 196 197 198 199 200 201
    if (self.selectSize == nil || self.count==0) {
        [self showToast:@"请选择您想要的规格"];
        return;
    }
    if (self.product && !self.product.JSONDictionary){
        [self showToast:@"该商品信息缺失"];
        return;
    }
    
lee committed
202 203 204 205
    BOOL buyNow = sender.tag == 1;
    __weak id this = self;
    [self showLoading];
    if (self.delegate) {
lee committed
206
        [self.delegate kwm_addShopWithVariantId:_selectSize.identifier quantity:_count variant:_selectSize buyNow:buyNow callback:^(BOOL success) {
lee committed
207 208 209 210 211
            [self hideLoading];
            if(success) {
                [this dismissViewControllerAnimated:YES completion:nil];
            }
        }];
houweibin committed
212
    }
lee 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
//    KWMShopCartData *shopCartData = [[KWMShopCartData alloc] init];
//    if (_shopCartModel == nil) {
//        [self initModelByPrudct];
//        [self initBySelectSize];
//        [shopCartData inocreaseItem:_shopCartModel];
//    }else{
//        NSString *seletStr = [NSString stringWithFormat:@"%@",_selectSize.identifier];
//        _shopCartModel.quantity = _count;
//        //判断规格是否相同
//        if ([_shopCartModel.identifier isEqualToString:seletStr]) {
//            [self initBySelectSize];
//            [shopCartData changeNumberShopcart:_shopCartModel];
//        }else{
//            [shopCartData removeItem:_shopCartModel];
//            [self initBySelectSize];
//            //变为选择的规格
//            [shopCartData inocreaseItem:_shopCartModel];
//        }
//    }
//    self.btnComplete.userInteractionEnabled =NO;
//    if (_isShopcart) {
//        [self showToast:@"修改成功"];
//    }else{
//        [self showToast:@"加入成功"];
//    }
//    if (self.delegate != nil) {
//        [self.delegate kwm_addShopCartItem:_shopCartModel];
//    }
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [self dismissViewControllerAnimated:YES completion:nil];
//    });
houweibin committed
244 245
}

lee committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
////点击了快速购买按钮
//-(void)onClickFastBuy:(id)sender{
//    
//    if (self.selectSize == nil || self.count == 0) {
//        [self showToast:@"请选择您想要的规格"];
//        return;
//    }
//    if (self.product && !self.product.JSONDictionary){
//        [self showToast:@"该商品信息缺失"];
//        return;
//    }
//    BUYCart *cart = [self.client.modelManager insertCartWithJSONDictionary:nil];
//    [cart setVariant:self.selectSize withTotalQuantity:self.count];
//    [self dismissViewControllerAnimated:YES completion:nil];
//    if(self.delegate != nil){
//        [self.delegate kwm_fastBuy:cart];
//    }
//}

houweibin committed
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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
-(void)initData{
    if(self.product == nil){
        return;
    }
    if(self.product.variantsArray!=nil && self.product.variantsArray.count>0){
        self.selectSize = [self.product.variantsArray objectAtIndex:0];
    }
    [self refreshView:NO];
}

-(void)kwm_onClickSize:(BUYProductVariant *)productVariant adminVariant:(KWMVariants *)adminVariant setSelect:(BOOL)isSelect{
    if(self.selectSize == nil && isSelect){
        self.count = 1;
    }
    if(isSelect){
        self.selectSize = productVariant;
        self.selectAdminSize = adminVariant;
    }else{
        self.selectSize = nil;
    }
    if (defalutSize.identifier.integerValue == productVariant.identifier.integerValue) {
        self.count = defalutCount;
    }else{
        self.count = 1;
    }
    [self refreshInventory:adminVariant];
    [self.cvSize reloadData];
    [self refreshView:NO];
}

-(void)kwm_onClickRemind:(BUYProductVariant *)productVariant{
    self.vRemind.hidden = NO;
    self.vRemindMarginLeft.constant = 0;
    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
    }];
}

-(void)onClickAdd:(id)sender{
    if(self.selectSize == nil){
        [self showToast:@"请选择您想要的规格"];
    }
    if(self.selectAdminSize!=nil && self.selectAdminSize.inventoryQuantity!=nil){
        if(self.count + 1 > self.selectAdminSize.inventoryQuantity.integerValue){
            [self showToast:@"商品剩余数量不足"];
            return;
        }
    }
    
    self.count ++;
    [self refreshView:NO];
}

-(void)onClickReduce:(id)sender{
    if(self.selectSize == nil){
        [self showToast:@"请选择您想要的规格"];
        return;
    }
    if(self.count != 1){
        self.count --;
        [self refreshView:NO];
    }
}

-(void)onClickRemind:(id)sender{
        CGFloat screenWidth =  UIScreen.mainScreen.bounds.size.width;
        self.vRemindMarginLeft.constant = -screenWidth;
        self.vRemind.hidden = YES;
}

//更新庫存
-(void)refreshInventory:(KWMVariants *)adminVariant{
    if(adminVariant!=nil && adminVariant.inventoryQuantity!=nil && adminVariant.inventoryQuantity.integerValue<5){
        self.lbInventory.hidden = NO;
        self.lbInventory.text = [NSString stringWithFormat:@"(仅剩%@件)",adminVariant.inventoryQuantity.stringValue];
    }else{
        self.lbInventory.hidden = YES;
    }
}

-(KWMVariants *)checkAdminVariants:(NSArray<KWMVariants *> *)adminVariants targetVariant:(BUYProductVariant *)targetVariant{
    if(adminVariants!=nil && targetVariant!=nil){
        for(KWMVariants *variant in adminVariants){
            if(variant!=nil && variant.id!=nil
               && targetVariant.identifier!=nil
               && variant.id.integerValue ==  targetVariant.identifier.integerValue){
                return variant;
            }
        }
    }
    return nil;
}

//更新数量,总价,以及按钮状态
-(void)refreshView:(BOOL)isDefalut{
    if(self.selectSize != nil ){
        self.lbCount.text = [NSString stringWithFormat: @"%ld", (long)self.count];
        if (defalutSize.identifier.integerValue == self.selectSize.identifier.integerValue && isDefalut) {
            self.count = defalutCount;
        }
lee committed
365 366 367 368
//        double price =  self.selectSize.price.doubleValue * self.count;
        NSDecimalNumber *totalPrice = [self.selectSize.priceValue decimalNumberByMultiplyingBy:[NSDecimalNumber decimalNumberWithDecimal:@(self.count).decimalValue]];
//        self.lbPrice.text = [NSString stringWithFormat: @"¥ %@", [KWMStringUtil getEUR2CNYstring:@(price)]];
        self.lbPrice.text = [[KWMCurrencyUtil sharedInstance] priceFormatted:totalPrice];
houweibin committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    }else{
        self.lbCount.text = @"0";
        self.lbPrice.text = @"¥ 0.00";
        self.count = 0;
    }
}

-(void)requestAdminProduct:(NSNumber *)productId{
    if(productId == nil){
        return;
    }
    __weak KWMSelectSizeVC *weakSelf = self;
    void(^failure)(NSURLSessionDataTask *,NSError *) = ^(NSURLSessionDataTask *task,NSError *error){
        //        [weakSelf hideLoading];
        [weakSelf showError:error];
    };
    void(^success)(NSURLSessionDataTask *,KWMProductResult *) = ^(NSURLSessionDataTask *task,KWMProductResult *result){
        //        [weakSelf hideLoading];
        if(result!=nil && result.product!=nil){
            KWMProduct* adminProduct = (KWMProduct *)result.product;
            if(adminProduct!=nil){
                NSArray<KWMVariants *> *adminVariants = adminProduct.variants;
                weakSelf.adminVariants = adminVariants;
                [weakSelf.cvSize reloadData];
                
                //如果已選中了某一項,更新標題
                KWMVariants* adminVariant = [self checkAdminVariants:adminVariants targetVariant:weakSelf.selectSize];
                self.selectAdminSize = adminVariant;
                [weakSelf refreshInventory:adminVariant];
            }
        }
    };
    [self.api getAdminProduct:nil productId:productId success:success failure:failure];
    //    [self showLoading];
}

@end