KWMSelectAddressVC.m 8.78 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//
//  KWMSelectAddressVC.m
//  iCemarose
//
//  Created by HouWeiBin on 16/9/2.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMSelectAddressVC.h"
#import "KWMAddressCell.h"
#import "KWMEditAddressVC.h"
#import "KWMBeforePayVC.h"
#import "KWMStringUtil.h"
#import "UIViewController+BackButtonHandler.h"
lee committed
15
#import "KWMValidateUtil.h"
houweibin committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181


@interface KWMSelectAddressVC ()
@property (nonatomic) NSMutableArray<BUYAddress *> *listData;

//当前的地址
@property (nonatomic) BUYAddress * address;

@end

@implementation KWMSelectAddressVC{
    UIBarButtonItem *rightButton;
}

+(NSString *)kwmTag{
    return @"KWMSelectAddressVC";
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestGetAddressList];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"收货地址";
    
     [self.tbvAddress setAllowsSelection:YES];
    _vNoAddress.hidden = YES;
    self.needAddressView.delegate = self;
    [self initTitle];
    if(self.checkout && self.checkout.billingAddress){
        self.address = self.checkout.billingAddress;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)initTitle{
    self.isEditing = NO;
    self.vDelete.delegate = self;
    rightButton = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(clickRightButton)];
    self.navigationItem.rightBarButtonItem = rightButton;
}

-(void)onClickAddAddress:(id)sender{
    KWMEditAddressVC *editVC = (KWMEditAddressVC *)[KWMBaseVC findControllerBy:[KWMEditAddressVC kwmTag] fromStoryboard:@"ShopCart"];
    [self.navigationController pushViewController:editVC animated:YES];
}

-(void)clickRightButton{
    self.isEditing = !self.isEditing;
    if(self.isEditing){
        if(self.listData == nil || self.listData.count == 0){
            [self showToast:@"请添加收货地址"];
        }else{
            [rightButton setTitle:@"取消"];
        }
    }else{
        [rightButton setTitle:@"编辑"];
    }
    [self.tbvAddress reloadData];
}

-(UITableView *)targetTableView{
    return self.tbvAddress;
}

-(void)loadData{
    if (_listData == nil) {
        _listData = [NSMutableArray array];
    }
    [self requestGetAddressList];
}

-(void)reLoadData{
    if (_listData == nil) {
        _listData = [NSMutableArray array];
    }
    [self requestGetAddressList];

}

//获取地址API
- (void)requestGetAddressList{
    if(self.loading){
        return;
    }
    [self showLoading];
    self.loading = YES;
    self.client.customerToken = [BUYCustomerToken customerTokenWithJSONDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:@"token"]];
    [self.client getAddressesCallback:^(NSArray<BUYAddress *> * _Nullable addresses, NSError * _Nullable error) {
        self.loading = NO;
        [self hideLoading];
        if (error == nil) {
            [_listData removeAllObjects];
            [_listData addObjectsFromArray:addresses];
            [self.tbvAddress reloadData];
            if (_listData.count == 0) {
                self.vNoAddress.hidden = NO;
            }else{
                self.vNoAddress.hidden = YES;
            }
        }else if (error != nil){
            [self showError:error];
            NSLog(@"error : %@",error);
        }
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _listData.count;
}

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indentifier = @"KWMAddressCell";
    
    KWMAddressCell *addressCell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    
    if (!addressCell) {
        [tableView registerNib:[UINib nibWithNibName:indentifier bundle:nil] forCellReuseIdentifier:indentifier];
        addressCell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    }
    addressCell.address = [_listData objectAtIndex:indexPath.row];
    [addressCell setData:self.isEditing];
    addressCell.delegate = self;
    return addressCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(!self.isEditing){
        _address = [_listData objectAtIndex:indexPath.row];
        [self updateCheckAddress:_address];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -- KWMAddressCell

- (void)kwm_onClickDeleteBtnWith:(BUYAddress *)address{
    self.vDelete.address = address;
    self.vDelete.hidden = NO;
}

-(void)kwm_onClickEditBtnWith:(BUYAddress *)address{
    KWMEditAddressVC *editVC = (KWMEditAddressVC *)[KWMBaseVC findControllerBy:[KWMEditAddressVC kwmTag] fromStoryboard:@"ShopCart"];
    editVC.address = address;
    [self.navigationController pushViewController:editVC animated:YES];
}

#pragma mark -- KWMDeleteDelegate
- (void)kwm_deleteProduct{
    if(self.vDelete.address){
        [self requestDeleteAddressAPIWith:self.vDelete.address];
    }
}

- (void)updateCheckAddress:(BUYAddress *)address{
lee committed
182 183 184
    if (self.delegate == nil) {
        return;
    }
lee committed
185 186 187 188 189 190
    NSString *errorMsg = [KWMValidateUtil validateAddress:address];
    if (!errorMsg) {
        [self.delegate kwm_setAddress:address];
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self showToast:errorMsg];
houweibin committed
191
    }
lee committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    
//    if(self.checkout == nil){
//        if(self.delegate != nil){
//            [self.delegate kwm_getAddress:address];
//        }
//        return;
//    }
//    if(address == nil){
//        self.checkout.billingAddress = nil;
//        self.checkout.shippingAddress = nil;
//        if(self.delegate != nil){
//            [self.delegate kwm_selectAddress:self.checkout];
//        }
//        return;
//    }
//    if(address.identifier.integerValue != self.checkout.billingAddress.identifier.integerValue){
//        if(address!=nil && [KWMStringUtil isEmpty:self.address.firstName]){
//            self.address.firstName = @"";
//        }
//        self.checkout.billingAddress = address;
//        self.checkout.shippingAddress = address;
//        
//        [self showLoading];
//        [self.client updateCheckout:self.checkout completion:^(NSDictionary *dictionary, BUYCheckout *checkout, NSError *error) {
//            [self hideLoading];
//            if (error == nil && checkout) {
//                self.checkout = checkout;
//                if(self.delegate != nil){
//                    [self.delegate kwm_selectAddress:checkout];
//                }
//                [self showToast:@"更新地址成功"];
//                [self.navigationController popViewControllerAnimated:YES];
//            }
//            else {
//                [self showError:error];
////                [self showToast:@"更新地址失败"];
//                NSLog(@"Error applying checkout: %@", error);
//            }
//        }];
//    }
houweibin committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
}

//删除地址API
- (void)requestDeleteAddressAPIWith:(BUYAddress *)address{
    [self showLoading];
    [self.client deleteAddressWithID:address.identifier callback:^(BUYStatus status, NSError * _Nullable error) {
        [self hideLoading];
        if (error == nil || status == BUYStatusComplete || status == BUYStatusProcessing) {
            [_listData removeObject:address];
            [self updateCustomerAddress:address];
            [self.tbvAddress reloadData];
        }else if (error != nil){
            [self showError:error];
        }
    }];
    
}

- (void)updateCustomerAddress:(BUYAddress *)removeAddress{
    [self showLoading];
    self.loading = YES;
    [self.client getCustomerCallback:^(BUYCustomer * _Nullable customer, NSError * _Nullable error) {
        self.loading = NO;
        [self hideLoading];
        if (error == nil && customer != nil) {
            [[NSUserDefaults standardUserDefaults] setObject:customer.JSONDictionary forKey:@"customer"];
        }
        if(error == nil){
            //如果选中的地址被删掉,使用新默认地址
            if(self.address && removeAddress
               && self.address.identifier.integerValue==removeAddress.identifier.integerValue
               ){
                //将地址设置为默认地址
                if(customer && customer.defaultAddress!=nil){
                    [self updateCheckAddress:customer.defaultAddress];
                }else{
                    [self updateCheckAddress:nil];
                }
            }
        }else if (error != nil){
            [self showError:error];
        }
    }];
}

-(void)kwm_cancel{
    [[self navigationController] popViewControllerAnimated:YES];
}

-(BOOL) navigationShouldPopOnBackButton {
lee committed
282 283 284
    if (self.delegate == nil) {
        return YES;
    }
houweibin committed
285 286 287 288 289 290 291
    self.needAddressView.hidden = NO;
    return NO; // Ignore 'Back' button this time
}



@end