Commit 0fc3663b by lee

完结版本

parent 729052aa
......@@ -16,6 +16,8 @@ typedef void(^ShoppingCartCallBack) (NSError *error,KWMCartResult *cart);
@property (nonatomic, strong) NSMutableArray<KWMShopCartModel *> * items;
@property (nonatomic, strong) NSNumber *count;
+ (instancetype)sharedInstance;
- (void)allItemsWithCallback:(ShoppingCartCallBack) callback;
......
......@@ -22,6 +22,10 @@
return (NSMutableArray *)_result.items;
}
- (NSNumber *)count {
return _result.item_count;
}
- (void)setResult:(KWMCartResult *)result {
_result = result;
[[KWMShopCartData alloc] syncCartProducts:result.items];
......@@ -33,6 +37,15 @@
dispatch_once(&onceToken,^{
instance = [[super allocWithZone:NULL] init];
});
if (!instance.result) {
instance.result = [[KWMCartResult alloc] init];
instance.result.items = [[[KWMShopCartData alloc] getALLItems] mutableCopy];
NSInteger count = 0;
for (KWMShopCartModel *item in instance.items) {
count += item.quantity;
}
instance.result.item_count = @(count);
}
return instance;
}
......@@ -46,21 +59,23 @@
}
- (void)increaseProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity callback:(ShoppingCartCallBack)callback {
callback(nil,[self changeLocalCartWithVariantId:variantId quantity:quantity flag:1]);
[[KWMAPIManager sharedManager] addProductWithVariantId:variantId quantity:quantity success:^(NSURLSessionDataTask *task, KWMRequestResult *result) {
[[KWMShoppingCart sharedInstance] allItemsWithCallback:^(NSError *error, KWMCartResult *cart) {
callback(error,cart);
// callback(error,cart);
}];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
callback(error,nil);
// callback(error,nil);
}];
}
- (void)updateProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity callback:(ShoppingCartCallBack)callback {
callback(nil,[self changeLocalCartWithVariantId:variantId quantity:quantity flag:quantity ? 2 : 0]);
[[KWMAPIManager sharedManager] updateProductWithVariantId:variantId quantity:quantity success:^(NSURLSessionDataTask *task, KWMCartResult *result) {
[[KWMShoppingCart sharedInstance] setResult:result];
callback(nil,result);
// callback(nil,result);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
callback(error,nil);
// callback(error,nil);
}];
}
......@@ -68,6 +83,40 @@
[self updateProductWithVariantId:variantId quantity:0 callback:callback];
}
- (KWMCartResult *) changeLocalCartWithVariantId:(NSNumber *) variantId quantity:(NSInteger) quantity flag:(NSInteger) flag {
NSMutableArray<KWMShopCartModel *> *items= self.items;
NSInteger count = self.result.item_count.integerValue;
BOOL processed = NO;
for (KWMShopCartModel *item in items) {
if (item.identifier.integerValue == variantId.integerValue) {
switch (flag) {
case 0:
count -= item.quantity;
[items removeObject:item];
break;
case 1:
count += quantity;
item.quantity += quantity;
break;
default:
count -= item.quantity - quantity;
item.quantity = quantity;
}
processed = YES;
break;
}
}
if (!processed) {
KWMShopCartModel *model = [[KWMShopCartModel alloc] init];
model.identifier = variantId.stringValue;
model.quantity = quantity;
count += quantity;
[items addObject:model];
}
self.result.item_count = @(count);
return self.result;
}
+ (id)allocWithZone:(struct _NSZone *)zone {
return [KWMShoppingCart sharedInstance];
}
......
......@@ -18,6 +18,7 @@
#import "UIColor+SAMAdditions.h"
#import "KWMFilterVC.h"
#import "KWMImageBlurUtil.h"
#import "KWMShoppingCart.h"
@interface KWMBrandCaramelVC ()<UITableViewDelegate,UITableViewDataSource,KWMBrandCaramelCellDelegate>
@property (nonatomic) KWMBarandSelectView *barandSelectView;
......@@ -135,8 +136,7 @@
- (void)initHeaderView{
_barandSelectView = [[KWMBarandSelectView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 64)];
NSArray *arr = [[KWMShopCartData alloc] getALLItems];
_barandSelectView.count = arr.count;
_barandSelectView.count = [[KWMShoppingCart sharedInstance] count].integerValue;
_barandSelectView.lbBrand.text = [_brand uppercaseString];
......
......@@ -18,6 +18,7 @@
#import <SDWebImage/UIImageView+WebCache.h>
#import "KWMBrandsTypeModel.h"
#import "UIColor+SAMAdditions.h"
#import "KWMShoppingCart.h"
@interface KWMBrandVC ()<KWMCarCountViewDelegate>
......@@ -79,8 +80,7 @@ static NSString *cellId = @"KWMBrandCell";
- (void) initHeardView{
_vCartCount = [[KWMCarCountView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 64)];
NSArray *arr = [[KWMShopCartData alloc] getALLItems];
_vCartCount.count = arr.count;
_vCartCount.count = [[KWMShoppingCart sharedInstance] count].integerValue;
_vCartCount.delegate = self;
[self.view addSubview:_vCartCount];
}
......
......@@ -73,8 +73,8 @@
[self.navigationController setNavigationBarHidden:YES];
[self initHeaderView];
if (_searchBar) {
NSArray *arr = [[KWMShopCartData alloc] getALLItems];
_searchBar.count = arr.count;
NSInteger count = [[KWMShoppingCart sharedInstance] count].integerValue;
_searchBar.count = count;
}
[_searchBar resumeView];
}
......
......@@ -18,6 +18,7 @@
#import "KWMBrandCaramelVC.h"
#import "UIColor+SAMAdditions.h"
#import "KWMProductDetailVC.h"
#import "KWMShoppingCart.h"
@interface KWMNewVC ()
......@@ -93,8 +94,8 @@ static NSString * idStr = @"KWMNewGoodsCell";
[self.navigationController setNavigationBarHidden:YES];
[self.tabBarController setHidesBottomBarWhenPushed:NO];
if (_searchBar) {
NSArray *arr = [[KWMShopCartData alloc] getALLItems];
_searchBar.count = arr.count;
NSInteger count = [[KWMShoppingCart sharedInstance] count].integerValue;
_searchBar.count = count;
}
[_searchBar resumeView];
_vBackground.hidden = YES;
......
......@@ -126,9 +126,8 @@
}
- (void)initShopCartCount{
NSArray *shopCarArray = [[KWMShopCartData alloc] getALLItems];
NSInteger count = shopCarArray == 0?0:shopCarArray.count;
[self.btnShopCart setTitle:@(count).stringValue forState:UIControlStateNormal];
NSNumber *count = [[KWMShoppingCart sharedInstance] count];
[self.btnShopCart setTitle:count.stringValue forState:UIControlStateNormal];
}
- (void)setProductId:(NSNumber *)productId{
......
......@@ -40,9 +40,6 @@
self.title = @"购物车";
// self.vBackground.hidden = NO;
_shopCartList = (NSMutableArray *)[[KWMShoppingCart sharedInstance] items];
if (!_shopCartList) {
_shopCartList = [NSMutableArray arrayWithArray:[[KWMShopCartData alloc] getALLItems]];
}
if (_shopCartList.count == 0) {
self.vBackground.hidden = NO;
}else{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment