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

#import "KWMShopCartVC.h"
#import "KWMSelectSizeVC.h"
#import "KWMShopCarCell.h"
#import "KWMBeforePayVC.h"
#import "KWMUserModel.h"
#import "KWMLoginVC.h"
#import "KWMShopCartData.h"
#import "KWMNewVC.h"
#import "KWMStringUtil.h"
lee committed
18
#import "KWMShoppingCart.h"
houweibin committed
19 20 21 22 23 24 25

@interface KWMShopCartVC ()<KWMSelectSizeVCDelegate>
@property (nonatomic) NSMutableArray *shopCartList;
@end

@implementation KWMShopCartVC{
    KWMShopCartModel *_removeModel;
lee committed
26
    KWMShopCartModel *_changeModel;
houweibin committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
}

+(NSString *)kwmTag{
    return @"KWMShopCartVC";
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"购物车";
//    self.vBackground.hidden = NO;
lee committed
42
    _shopCartList = (NSMutableArray *)[[KWMShoppingCart sharedInstance] items];
houweibin committed
43 44 45 46 47 48 49 50 51
    if (_shopCartList.count == 0) {
        self.vBackground.hidden = NO;
    }else{
        self.vBackground.hidden = YES;
    }
    self.btnRandom.layer.borderWidth = 0.5;
    self.btnRandom.layer.borderColor = [UIColor colorWithRed:78.0/255 green:78.0/255 blue:78.0/255 alpha:1.0].CGColor;
    
    [self setTotalPrice];
lee committed
52 53 54 55
    __weak typeof(self) this = self;
    [[KWMShoppingCart sharedInstance] allItemsWithCallback:^(NSError *error, KWMCartResult *cart) {
        [this refresh];
    }];
houweibin committed
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
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (_shopCartList.count == 0) {
        self.vBackground.hidden = NO;
    }else{
        self.vBackground.hidden = YES;
    }
    return _shopCartList.count;
}

#pragma  mark UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 184;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indentifier = @"KWMShopCarCell";
    
    KWMShopCarCell *shopCartCell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    KWMShopCartModel *shopCartModel = [_shopCartList objectAtIndex:indexPath.row];
    if (!shopCartCell) {
        [tableView registerNib:[UINib nibWithNibName:indentifier bundle:nil] forCellReuseIdentifier:indentifier];
        shopCartCell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    }
    shopCartCell.shopCartModel = shopCartModel;
    shopCartCell.delegate = self;
    return shopCartCell;
}

//点击随意逛逛
- (IBAction)onClickRandomBtn:(id)sender {
    NSLog(@"点击了随意逛逛");
    KWMNewVC *newVC = (KWMNewVC *)[KWMBaseVC findControllerBy:[KWMNewVC kwmTag] fromStoryboard:@"Main"];
    self.hidesBottomBarWhenPushed = NO;
    [self.navigationController pushViewController:newVC animated:YES];
}

- (void)onClickComplete:(id)sender{
    
    KWMUserModel *useModel = [KWMUserModel shareUser];
    if (useModel.status) {
        KWMBeforePayVC *beforePayVC = (KWMBeforePayVC *)[KWMBaseVC findControllerBy:[KWMBeforePayVC kwmTag] fromStoryboard:@"ShopCart"];
        beforePayVC.shopcartArray = self.shopCartList;
        [self.navigationController pushViewController:beforePayVC animated:YES];
    }else{
        //登录
        KWMLoginVC *loginVc = (KWMLoginVC *)[KWMBaseVC findControllerBy:[KWMLoginVC kwmTag] fromStoryboard:@"Login"];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVc];
        [self presentViewController:nav animated:YES completion:nil];
    }
}

- (void)kwm_onClickDelete:(KWMShopCartModel *)shopCartModel{
    self.vDelete.hidden = NO;
    self.vDelete.delegate = self;
    _removeModel = shopCartModel;
}

- (void)kwm_onClickEdit:(KWMShopCartModel *)shopCartModel{
lee committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130
    __block KWMShopCartVC *this = self;
    _changeModel = shopCartModel;
    [self showLoading];
    [self.client getProductById:shopCartModel.product_id completion:^(BUYProduct * _Nullable product, NSError * _Nullable error) {
        [this hideLoading];
        this.definesPresentationContext = YES;
        KWMSelectSizeVC *editVC = (KWMSelectSizeVC *)[KWMBaseVC findControllerBy:[KWMSelectSizeVC kwmTag] fromStoryboard:@"ShopCart"];
        editVC.delegate = this;
//        BUYProduct * product = [[BUYProduct alloc] initWithModelManager:self.client.modelManager JSONDictionary:shopCartModel.shopCartDict];
        
        editVC.isShopcart = YES;
        editVC.product = product;
        editVC.shopCartModel = shopCartModel;
        
houweibin committed
131 132 133 134
        editVC.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        editVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        editVC.providesPresentationContextTransitionStyle = YES;
        editVC.definesPresentationContext = YES;
lee committed
135 136 137 138 139 140 141 142 143 144 145 146
        [this presentViewController:editVC animated:YES completion:nil];
    }];
  
    
    
    //設置背景為透明
//    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//        editVC.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
//        editVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
//        editVC.providesPresentationContextTransitionStyle = YES;
//        editVC.definesPresentationContext = YES;
//        [self presentViewController:editVC animated:YES completion:nil];
houweibin committed
147 148 149 150 151 152 153 154 155 156 157

//下面這個方法在9.0的模擬器上嘗試發現失敗,8.0以下沒試過,不過官方模擬器都沒有8.0以下的可以下載了,也沒有支持的必要。
//    }else{
//        editVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
//        self.view.window.rootViewController.modalPresentationStyle = UIModalPresentationCurrentContext;
//        [self presentViewController:editVC animated:NO completion:nil];
//        self.view.window.rootViewController.modalPresentationStyle = UIModalPresentationFullScreen;
//    }
}

#pragma mark -- KWMSelectSizeVCDelegate
lee committed
158

lee committed
159
- (void)kwm_addShopWithVariantId:(NSNumber *)identifier quantity:(NSInteger)quantity variant:(BUYProductVariant *) variant buyNow:(BOOL)buyNow callback:(void (^)(BOOL))callback {
lee committed
160 161 162 163 164 165 166 167 168 169 170
    __weak KWMShopCartVC *this = self;
    void(^success)(NSError*,KWMCartResult*) = ^(NSError *error,KWMCartResult *cart){
        if (!error) {
            [this refresh];
            callback(YES);
        }else{
            [this showError:error];
            callback(NO);
        }
    };
    if (_changeModel.identifier.integerValue == identifier.integerValue) {
lee committed
171
        [[KWMShoppingCart sharedInstance] changeProductWithVariantId:identifier quantity:quantity callback:success];
lee committed
172 173
    }else{
        NSNumber *oldId = @(_changeModel.identifier.integerValue);
lee committed
174
        KWMShopCartModel *model  = _changeModel;
lee committed
175
        _changeModel = nil;
lee committed
176 177 178 179 180 181 182 183 184 185 186 187 188
        NSArray<NSNumber *> *variantIds = @[oldId,identifier];
        NSArray<NSNumber *> *quantities = @[@0,@(quantity)];
        [[KWMShoppingCart sharedInstance] updateProductWithVariantIds:variantIds quantitties:quantities callback:^(NSError *error, KWMCartResult *cart) {
            if (!cart.items.firstObject.product_id) {
                KWMShopCartModel *tmp = cart.items.firstObject;
                tmp.imageStr = model.imageStr;
                tmp.size = variant.title;
                tmp.brand = model.brand;
                tmp.name = model.name;
                tmp.price = variant.price.floatValue;
                tmp.product_id = model.product_id;
            }
            success(error,cart);
lee committed
189
        }];
lee committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203
//        [[KWMShoppingCart sharedInstance] deleteProductWithVariantId:oldId callback:^(NSError *error, KWMCartResult *cart) {
//            [[KWMShoppingCart sharedInstance] increaseProductWithVariantId:identifier quantity:quantity callback:^(NSError *error, KWMCartResult *cart) {
//                if (!cart.items.lastObject.product_id) {
//                    KWMShopCartModel *tmp = cart.items.lastObject;
//                    tmp.imageStr = model.imageStr;
//                    tmp.size = variant.title;
//                    tmp.brand = model.brand;
//                    tmp.name = model.name;
//                    tmp.price = variant.price.floatValue;
//                    tmp.product_id = model.product_id;
//                }
//                success(error,cart);
//            }];
//        }];
lee committed
204 205 206
    }
}

lee committed
207 208 209 210 211 212 213 214 215 216 217
//- (void)kwm_addShopCartItem:(KWMShopCartModel *)shopCartModel{
//    
//    [_shopCartList removeAllObjects];
//    [_shopCartList addObjectsFromArray:[[KWMShopCartData alloc] getALLItems]];
//    [self.tbvCart reloadData];
//    [self setTotalPrice];
//}
//
//-(void)kwm_fastBuy:(BUYCart *)fastBuyCart{
//    
//}
houweibin committed
218 219

-(void)kwm_deleteProduct{
lee committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
//    BOOL isHas = NO;
//    for (KWMShopCartModel *item in _shopCartList) {
//        if ([item isEqual:_removeModel]) {
//            isHas = YES;
//            break;
//        }
//    }
//    if (isHas) {
//        [_shopCartList removeObjectIdenticalTo:_removeModel];
//    }
//    [self.tbvCart reloadData];
//    [self setTotalPrice];
//    [[KWMShopCartData alloc] removeItem:_removeModel];
    NSNumber *identifier = @(_removeModel.identifier.integerValue);
    __weak KWMShopCartVC *this = self;
    [self showLoading];
    [[KWMShoppingCart sharedInstance] deleteProductWithVariantId:identifier callback:^(NSError *error, KWMCartResult *cart) {
        [this hideLoading];
        [this refresh];
    }];
}

- (void)refresh {
    self.shopCartList = [[KWMShoppingCart sharedInstance] items];
houweibin committed
244 245 246 247 248 249
    [self.tbvCart reloadData];
    [self setTotalPrice];
}

//计算总价格
- (void) setTotalPrice{
lee committed
250
    NSInteger price = 0;
houweibin committed
251 252 253 254 255 256 257
    for (KWMShopCartModel *model in _shopCartList) {
        price += model.price * model.quantity;
    }
    _lbTotalPrice.text = [NSString stringWithFormat: @"¥%@",[KWMStringUtil getEUR2CNYstring:@(price)]];
}

@end