KWMShoppingCart.m 5.87 KB
Newer Older
lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//
//  KWMShoppingCart.m
//  iCemarose
//
//  Created by lee on 2017/5/18.
//  Copyright © 2017年 kollway. All rights reserved.
//

#import "KWMShoppingCart.h"
#import "KWMCartResult.h"
#import "KWMShopCartData.h"

@interface KWMShoppingCart ()

@property (nonatomic, strong) KWMCartResult *result;
16
@property (nonatomic, assign) CGFloat version;
lee committed
17 18 19 20 21 22 23 24 25

@end

@implementation KWMShoppingCart

- (NSMutableArray<KWMShopCartModel *> *)items {
    return (NSMutableArray *)_result.items;
}

lee committed
26 27 28 29
- (NSNumber *)count {
    return _result.item_count;
}

lee committed
30 31 32 33 34
- (void)setResult:(KWMCartResult *)result {
    _result = result;
    [[KWMShopCartData alloc] syncCartProducts:result.items];
}

u  
lee committed
35 36 37 38
- (void)setResultNoSync:(KWMCartResult *)result {
    _result = result;
}

lee committed
39 40 41 42 43 44
+ (instancetype)sharedInstance {
    static KWMShoppingCart *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        instance = [[super allocWithZone:NULL] init];
    });
lee committed
45
    if (!instance.result) {
u  
lee committed
46
        [instance setResultNoSync:[KWMCartResult new]];
lee committed
47 48 49 50 51 52 53
        instance.result.items = [[[KWMShopCartData alloc] getALLItems] mutableCopy];
        NSInteger count = 0;
        for (KWMShopCartModel *item in instance.items) {
            count += item.quantity;
        }
        instance.result.item_count = @(count);
    }
lee committed
54 55 56 57
    return instance;
}

- (void)allItemsWithCallback:(ShoppingCartCallBack)callback {
58 59 60
    CGFloat version = [[NSDate date] timeIntervalSince1970];
    _version = version;
    __weak typeof(self) this = self;
lee committed
61
    [[KWMAPIManager sharedManager] getCartSuccess:^(NSURLSessionDataTask *task, KWMCartResult *result) {
62 63 64 65
        if (version == this.version) {
            [[KWMShoppingCart sharedInstance] setResult:result];
            callback(nil,result);
        }
lee committed
66 67 68 69 70 71
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        callback(error,nil);
    }];
}

- (void)increaseProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity callback:(ShoppingCartCallBack)callback {
72 73 74
    CGFloat version = [[NSDate date] timeIntervalSince1970];
    _version = version;
    __weak typeof(self) this = self;
lee committed
75
    [[KWMAPIManager sharedManager] addProductWithVariantId:variantId quantity:quantity success:^(NSURLSessionDataTask *task, KWMRequestResult *result) {
76 77 78 79 80
        if (version == this.version) {
            [[KWMShoppingCart sharedInstance] allItemsWithCallback:^(NSError *error, KWMCartResult *cart) {
//                callback(error,cart);
            }];
        }
lee committed
81
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
lee committed
82
//        callback(error,nil);
lee committed
83
    }];
lee committed
84
    callback(nil,[self changeLocalCartWithVariantId:variantId quantity:quantity flag:1]);
lee committed
85 86
}

lee committed
87
- (void)changeProductWithVariantId:(NSNumber *)variantId quantity:(NSInteger)quantity callback:(ShoppingCartCallBack)callback {
88 89 90
    CGFloat version = [[NSDate date] timeIntervalSince1970];
    _version = version;
    __weak typeof(self) this = self;
lee committed
91
    [[KWMAPIManager sharedManager] changeProductWithVariantId:variantId quantity:quantity success:^(NSURLSessionDataTask *task, KWMCartResult *result) {
92 93
        if (version == this.version) {
            [[KWMShoppingCart sharedInstance] setResult:result];
n  
lee committed
94
//        callback(nil,result);
95
        }
lee committed
96 97 98
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
//        callback(error,nil);
    }];
lee committed
99
    callback(nil,[self changeLocalCartWithVariantId:variantId quantity:quantity flag:quantity ? 2 : 0]);
lee committed
100 101 102
}

-(void)updateProductWithVariantIds:(NSArray<NSNumber *> *)variantIds quantitties:(NSArray<NSNumber *> *)quantites callback:(ShoppingCartCallBack)callback {
103 104 105
    CGFloat version = [[NSDate date] timeIntervalSince1970];
    _version = version;
    __weak typeof(self) this = self;
lee committed
106
    [[KWMAPIManager sharedManager] updateProductWithVariantIds:variantIds quantities:quantites success:^(NSURLSessionDataTask *task, KWMCartResult *result) {
107 108
        if (version == this.version) {
            [[KWMShoppingCart sharedInstance] setResult:result];
n  
lee committed
109
//        callback(nil,result);
110
        }
lee committed
111
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
lee committed
112
//        callback(error,nil);
lee committed
113
    }];
lee committed
114 115 116 117
    for (int i=0; i<quantites.count; i++) {
        [self changeLocalCartWithVariantId:variantIds[i] quantity:quantites[i].integerValue flag:quantites[i].integerValue ? 2 : 0];
    }
    callback(nil,self.result);
lee committed
118 119 120
}

- (void)deleteProductWithVariantId:(NSNumber *)variantId callback:(ShoppingCartCallBack)callback {
lee committed
121
    [self changeProductWithVariantId:variantId quantity:0 callback:callback];
lee committed
122 123
}

lee committed
124 125 126 127
- (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;
lee committed
128 129 130
    NSUInteger size = items.count;
    for (int i=0;i < size; i++) {
        KWMShopCartModel *item = items[i];
lee committed
131 132 133 134
        if (item.identifier.integerValue == variantId.integerValue) {
            switch (flag) {
                case 0:
                    count -= item.quantity;
lee committed
135
                    [self.items removeObjectAtIndex:i];
lee committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
                    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;
lee committed
154 155
//        [items addObject:model];
        [items insertObject:model atIndex:0];
lee committed
156 157 158 159 160
    }
    self.result.item_count = @(count);
    return self.result;
}

lee committed
161 162 163 164 165 166 167 168 169 170 171 172 173
+ (id)allocWithZone:(struct _NSZone *)zone {
    return [KWMShoppingCart sharedInstance];
}

- (id)copy {
    return self;
}

- (id)mutableCopy {
    return self;
}

@end