Commit 0fc3663b by lee

完结版本

parent 729052aa
...@@ -16,6 +16,8 @@ typedef void(^ShoppingCartCallBack) (NSError *error,KWMCartResult *cart); ...@@ -16,6 +16,8 @@ typedef void(^ShoppingCartCallBack) (NSError *error,KWMCartResult *cart);
@property (nonatomic, strong) NSMutableArray<KWMShopCartModel *> * items; @property (nonatomic, strong) NSMutableArray<KWMShopCartModel *> * items;
@property (nonatomic, strong) NSNumber *count;
+ (instancetype)sharedInstance; + (instancetype)sharedInstance;
- (void)allItemsWithCallback:(ShoppingCartCallBack) callback; - (void)allItemsWithCallback:(ShoppingCartCallBack) callback;
......
...@@ -22,6 +22,10 @@ ...@@ -22,6 +22,10 @@
return (NSMutableArray *)_result.items; return (NSMutableArray *)_result.items;
} }
- (NSNumber *)count {
return _result.item_count;
}
- (void)setResult:(KWMCartResult *)result { - (void)setResult:(KWMCartResult *)result {
_result = result; _result = result;
[[KWMShopCartData alloc] syncCartProducts:result.items]; [[KWMShopCartData alloc] syncCartProducts:result.items];
...@@ -33,6 +37,15 @@ ...@@ -33,6 +37,15 @@
dispatch_once(&onceToken,^{ dispatch_once(&onceToken,^{
instance = [[super allocWithZone:NULL] init]; 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; return instance;
} }
...@@ -46,21 +59,23 @@ ...@@ -46,21 +59,23 @@
} }
- (void)increaseProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity callback:(ShoppingCartCallBack)callback { - (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) { [[KWMAPIManager sharedManager] addProductWithVariantId:variantId quantity:quantity success:^(NSURLSessionDataTask *task, KWMRequestResult *result) {
[[KWMShoppingCart sharedInstance] allItemsWithCallback:^(NSError *error, KWMCartResult *cart) { [[KWMShoppingCart sharedInstance] allItemsWithCallback:^(NSError *error, KWMCartResult *cart) {
callback(error,cart); // callback(error,cart);
}]; }];
} failure:^(NSURLSessionDataTask *task, NSError *error) { } failure:^(NSURLSessionDataTask *task, NSError *error) {
callback(error,nil); // callback(error,nil);
}]; }];
} }
- (void)updateProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity callback:(ShoppingCartCallBack)callback { - (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) { [[KWMAPIManager sharedManager] updateProductWithVariantId:variantId quantity:quantity success:^(NSURLSessionDataTask *task, KWMCartResult *result) {
[[KWMShoppingCart sharedInstance] setResult:result]; [[KWMShoppingCart sharedInstance] setResult:result];
callback(nil,result); // callback(nil,result);
} failure:^(NSURLSessionDataTask *task, NSError *error) { } failure:^(NSURLSessionDataTask *task, NSError *error) {
callback(error,nil); // callback(error,nil);
}]; }];
} }
...@@ -68,6 +83,40 @@ ...@@ -68,6 +83,40 @@
[self updateProductWithVariantId:variantId quantity:0 callback:callback]; [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 { + (id)allocWithZone:(struct _NSZone *)zone {
return [KWMShoppingCart sharedInstance]; return [KWMShoppingCart sharedInstance];
} }
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#import "UIColor+SAMAdditions.h" #import "UIColor+SAMAdditions.h"
#import "KWMFilterVC.h" #import "KWMFilterVC.h"
#import "KWMImageBlurUtil.h" #import "KWMImageBlurUtil.h"
#import "KWMShoppingCart.h"
@interface KWMBrandCaramelVC ()<UITableViewDelegate,UITableViewDataSource,KWMBrandCaramelCellDelegate> @interface KWMBrandCaramelVC ()<UITableViewDelegate,UITableViewDataSource,KWMBrandCaramelCellDelegate>
@property (nonatomic) KWMBarandSelectView *barandSelectView; @property (nonatomic) KWMBarandSelectView *barandSelectView;
...@@ -135,8 +136,7 @@ ...@@ -135,8 +136,7 @@
- (void)initHeaderView{ - (void)initHeaderView{
_barandSelectView = [[KWMBarandSelectView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 64)]; _barandSelectView = [[KWMBarandSelectView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 64)];
NSArray *arr = [[KWMShopCartData alloc] getALLItems]; _barandSelectView.count = [[KWMShoppingCart sharedInstance] count].integerValue;
_barandSelectView.count = arr.count;
_barandSelectView.lbBrand.text = [_brand uppercaseString]; _barandSelectView.lbBrand.text = [_brand uppercaseString];
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#import <SDWebImage/UIImageView+WebCache.h> #import <SDWebImage/UIImageView+WebCache.h>
#import "KWMBrandsTypeModel.h" #import "KWMBrandsTypeModel.h"
#import "UIColor+SAMAdditions.h" #import "UIColor+SAMAdditions.h"
#import "KWMShoppingCart.h"
@interface KWMBrandVC ()<KWMCarCountViewDelegate> @interface KWMBrandVC ()<KWMCarCountViewDelegate>
...@@ -79,8 +80,7 @@ static NSString *cellId = @"KWMBrandCell"; ...@@ -79,8 +80,7 @@ static NSString *cellId = @"KWMBrandCell";
- (void) initHeardView{ - (void) initHeardView{
_vCartCount = [[KWMCarCountView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 64)]; _vCartCount = [[KWMCarCountView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 64)];
NSArray *arr = [[KWMShopCartData alloc] getALLItems]; _vCartCount.count = [[KWMShoppingCart sharedInstance] count].integerValue;
_vCartCount.count = arr.count;
_vCartCount.delegate = self; _vCartCount.delegate = self;
[self.view addSubview:_vCartCount]; [self.view addSubview:_vCartCount];
} }
......
...@@ -73,8 +73,8 @@ ...@@ -73,8 +73,8 @@
[self.navigationController setNavigationBarHidden:YES]; [self.navigationController setNavigationBarHidden:YES];
[self initHeaderView]; [self initHeaderView];
if (_searchBar) { if (_searchBar) {
NSArray *arr = [[KWMShopCartData alloc] getALLItems]; NSInteger count = [[KWMShoppingCart sharedInstance] count].integerValue;
_searchBar.count = arr.count; _searchBar.count = count;
} }
[_searchBar resumeView]; [_searchBar resumeView];
} }
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#import "KWMBrandCaramelVC.h" #import "KWMBrandCaramelVC.h"
#import "UIColor+SAMAdditions.h" #import "UIColor+SAMAdditions.h"
#import "KWMProductDetailVC.h" #import "KWMProductDetailVC.h"
#import "KWMShoppingCart.h"
@interface KWMNewVC () @interface KWMNewVC ()
...@@ -93,8 +94,8 @@ static NSString * idStr = @"KWMNewGoodsCell"; ...@@ -93,8 +94,8 @@ static NSString * idStr = @"KWMNewGoodsCell";
[self.navigationController setNavigationBarHidden:YES]; [self.navigationController setNavigationBarHidden:YES];
[self.tabBarController setHidesBottomBarWhenPushed:NO]; [self.tabBarController setHidesBottomBarWhenPushed:NO];
if (_searchBar) { if (_searchBar) {
NSArray *arr = [[KWMShopCartData alloc] getALLItems]; NSInteger count = [[KWMShoppingCart sharedInstance] count].integerValue;
_searchBar.count = arr.count; _searchBar.count = count;
} }
[_searchBar resumeView]; [_searchBar resumeView];
_vBackground.hidden = YES; _vBackground.hidden = YES;
......
...@@ -126,9 +126,8 @@ ...@@ -126,9 +126,8 @@
} }
- (void)initShopCartCount{ - (void)initShopCartCount{
NSArray *shopCarArray = [[KWMShopCartData alloc] getALLItems]; NSNumber *count = [[KWMShoppingCart sharedInstance] count];
NSInteger count = shopCarArray == 0?0:shopCarArray.count; [self.btnShopCart setTitle:count.stringValue forState:UIControlStateNormal];
[self.btnShopCart setTitle:@(count).stringValue forState:UIControlStateNormal];
} }
- (void)setProductId:(NSNumber *)productId{ - (void)setProductId:(NSNumber *)productId{
......
...@@ -40,9 +40,6 @@ ...@@ -40,9 +40,6 @@
self.title = @"购物车"; self.title = @"购物车";
// self.vBackground.hidden = NO; // self.vBackground.hidden = NO;
_shopCartList = (NSMutableArray *)[[KWMShoppingCart sharedInstance] items]; _shopCartList = (NSMutableArray *)[[KWMShoppingCart sharedInstance] items];
if (!_shopCartList) {
_shopCartList = [NSMutableArray arrayWithArray:[[KWMShopCartData alloc] getALLItems]];
}
if (_shopCartList.count == 0) { if (_shopCartList.count == 0) {
self.vBackground.hidden = NO; self.vBackground.hidden = NO;
}else{ }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