// // 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" #import "KWMShoppingCart.h" @interface KWMShopCartVC ()<KWMSelectSizeVCDelegate> @property (nonatomic) NSMutableArray *shopCartList; @end @implementation KWMShopCartVC{ KWMShopCartModel *_removeModel; KWMShopCartModel *_changeModel; } +(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; _shopCartList = (NSMutableArray *)[[KWMShoppingCart sharedInstance] items]; 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]; __weak typeof(self) this = self; [[KWMShoppingCart sharedInstance] allItemsWithCallback:^(NSError *error, KWMCartResult *cart) { [this refresh]; }]; } -(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{ __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; editVC.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0]; editVC.modalPresentationStyle = UIModalPresentationOverCurrentContext; editVC.providesPresentationContextTransitionStyle = YES; editVC.definesPresentationContext = YES; [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]; //下面這個方法在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 - (void)kwm_addShopWithVariantId:(NSNumber *)identifier quantity:(NSInteger)quantity buyNow:(BOOL)buyNow callback:(void (^)(BOOL))callback { __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) { [[KWMShoppingCart sharedInstance] updateProductWithVariantId:identifier quantity:quantity callback:success]; }else{ NSNumber *oldId = @(_changeModel.identifier.integerValue); _changeModel = nil; [[KWMShoppingCart sharedInstance] deleteProductWithVariantId:oldId callback:^(NSError *error, KWMCartResult *cart) { [[KWMShoppingCart sharedInstance] increaseProductWithVariantId:identifier quantity:quantity callback:success]; }]; } } - (void)kwm_addShopCartItem:(KWMShopCartModel *)shopCartModel{ [_shopCartList removeAllObjects]; [_shopCartList addObjectsFromArray:[[KWMShopCartData alloc] getALLItems]]; [self.tbvCart reloadData]; [self setTotalPrice]; } -(void)kwm_fastBuy:(BUYCart *)fastBuyCart{ } -(void)kwm_deleteProduct{ // 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]; [self.tbvCart reloadData]; [self setTotalPrice]; } //计算总价格 - (void) setTotalPrice{ NSInteger price = 0; for (KWMShopCartModel *model in _shopCartList) { price += model.price * model.quantity; } _lbTotalPrice.text = [NSString stringWithFormat: @"¥%@",[KWMStringUtil getEUR2CNYstring:@(price)]]; } @end