KWMEditAddressVC.m 8.95 KB
Newer Older
houweibin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
//
//  KWMEditAddressVC.m
//  iCemarose
//
//  Created by HouWeiBin on 16/9/5.
//  Copyright © 2016年 kollway. All rights reserved.
//

#import "KWMEditAddressVC.h"
#import "KWMStringUtil.h"

@interface KWMEditAddressVC ()

@end

@implementation KWMEditAddressVC{
    BUYAddress *requestAddress;
}

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

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择地址";
    self.client.customerToken = [BUYCustomerToken customerTokenWithJSONDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:@"token"]];
    [self initTitle];
    [self updateAddress];
    [self initTextField];
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickBackgroundView:)];
    [self.view addGestureRecognizer:tapGesture];
}

- (void)onClickBackgroundView:(UITapGestureRecognizer *)tapGesture{
36
    [self.tfCountry resignFirstResponder];
houweibin committed
37 38 39 40 41 42 43 44 45 46 47
    [self.tfProvince resignFirstResponder];
    [self.tfCity resignFirstResponder];
    [self.tfAddress resignFirstResponder];
    [self.tfAddressDetail resignFirstResponder];
    [self.tfZip resignFirstResponder];
    [self.tfName resignFirstResponder];
    [self.tfPhone resignFirstResponder];
}


-(void)initTextField{
48 49 50 51 52 53 54 55 56 57 58
    //http://www.cocoachina.com/bbs/read.php?tid-250044-page-8.html  #78
    //tf为无边框模式时,输入中文会导致文字下沉的问题。
    self.tfName.borderStyle = UITextBorderStyleNone;
    self.tfPhone.borderStyle = UITextBorderStyleNone;
    self.tfCountry.borderStyle = UITextBorderStyleNone;
    self.tfProvince.borderStyle = UITextBorderStyleNone;
    self.tfCity.borderStyle = UITextBorderStyleNone;
    self.tfAddress.borderStyle = UITextBorderStyleNone;
    self.tfAddressDetail.borderStyle = UITextBorderStyleNone;
    self.tfZip.borderStyle = UITextBorderStyleNone;
    
houweibin committed
59 60
    self.tfName.delegate = self;
    self.tfPhone.delegate = self;
61
    self.tfCountry.delegate = self;
houweibin committed
62 63 64 65 66 67 68 69 70 71 72 73
    self.tfProvince.delegate = self;
    self.tfCity.delegate = self;
    self.tfAddress.delegate = self;
    self.tfAddressDetail.delegate = self;
    self.tfZip.delegate = self;
    
    [self.tfProvince setTextFiledCountLimit:10];
    [self.tfCity setTextFiledCountLimit:10];
    [self.tfName setTextFiledCountLimit:10];
    [self.tfAddress setTextFiledCountLimit:50];
    [self.tfAddressDetail setTextFiledCountLimit:50];
    
74
    self.tfCountry.tag = 0;
houweibin committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    self.tfProvince.tag = 1;
    self.tfCity.tag = 2;
    self.tfAddress.tag = 3;
    self.tfAddressDetail.tag = 4;
    self.tfZip.tag = 5;
    self.tfName.tag = 6;
    self.tfPhone.tag = 7;
}

- (void)initTitle{
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"存储" style:UIBarButtonItemStylePlain target:self action:@selector(clickRightButton)];
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(clickLeftButton)];
    self.navigationItem.rightBarButtonItem = rightButton;
    self.navigationItem.leftBarButtonItem = leftButton;
    [self.navigationItem hidesBackButton];
}

- (void)updateAddress{
    if (_address) {
        self.tfName.text = _address.lastName;
        self.tfPhone.text = _address.phone;
96
        self.tfCountry.text = _address.country;
houweibin committed
97 98 99 100 101 102 103 104 105 106
        self.tfProvince.text = _address.province;
        self.tfCity.text = _address.city;
        self.tfAddress.text = _address.address1;
        self.tfAddressDetail.text = _address.address2;
        self.tfZip.text = _address.zip;
    }
}

//点击存储按钮
-(void)clickRightButton{
107 108 109
    if([KWMStringUtil isEmpty:self.tfCountry.text]){
        [self showToast:@"请输入国家"];
    }else if([KWMStringUtil isEmpty:self.tfProvince.text]){
houweibin committed
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
        [self showToast:@"请输入省份"];
    }else if([KWMStringUtil isEmpty:self.tfCity.text]){
        [self showToast:@"请输入城市"];
    }else if([KWMStringUtil isEmpty:self.tfAddress.text]){
        [self showToast:@"请输入地址"];
    }else if([KWMStringUtil isEmpty:self.tfZip.text]){
        [self showToast:@"请输入邮政编码"];
    }else if([KWMStringUtil isEmpty:self.tfName.text]){
        [self showToast:@"请输入姓名"];
    }else if([KWMStringUtil isEmpty:self.tfPhone.text]){
        [self showToast:@"请输入手机号"];
    }else{
        if(self.address == nil){
            [self addAddress];
        }else{
            [self editAddress];
        }
//        [self performSelector:@selector(delayMethod) withObject:nil afterDelay:0.3f];
    }
}

- (void)delayMethod {
     [self showToast:@"存储成功"];
     [[self navigationController] popViewControllerAnimated:YES];
}


//点击取消按钮
-(void)clickLeftButton{
    [[self navigationController] popViewControllerAnimated:YES];
}

-(BUYAddress *)getAddress{
    if(self.address!=nil){
        requestAddress = self.address;
    }else{
        requestAddress = [[BUYAddress alloc] initWithModelManager:self.client.modelManager JSONDictionary:nil];
    }
148
    NSString *country = [self.tfCountry.text trim];
149 150 151
    requestAddress.lastName = [self.tfName.text trim];
    requestAddress.phone = [self.tfPhone.text trim];
    requestAddress.province = [self.tfProvince.text trim];
houweibin committed
152
    requestAddress.provinceCode = 0;
153 154 155
    requestAddress.city = [self.tfCity.text trim];
    requestAddress.address1 = [self.tfAddress.text trim];
    requestAddress.address2 = [self.tfAddressDetail.text trim];
156
    requestAddress.country = [country isEqualToString:@"中国"] ? @"China" : country;
157
    requestAddress.countryCode = @"";
158 159
    requestAddress.zip = [self.tfZip.text trim];
    requestAddress.firstName = [(requestAddress.firstName?requestAddress.firstName:@"") trim];
houweibin committed
160 161 162
    return requestAddress;
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
-(void)showAddressError:(NSError *)error{
    if(error && error.userInfo){
        NSString *errorInfoString = [NSString stringWithFormat:@"%@",error.userInfo];
        if(errorInfoString){
            if([errorInfoString rangeOfString:@"province"].location != NSNotFound &&
               [errorInfoString rangeOfString:@"is not valid"].location != NSNotFound){
                [self showToast:@"请填写正确的省份,例如美国的亚拉巴马州则填写:Alabama"];
                return;
            }
            if([errorInfoString rangeOfString:@"country"].location != NSNotFound &&
               [errorInfoString rangeOfString:@"is not a valid"].location != NSNotFound){
                [self showToast:@"请填写正确的国家,例如美国则填写:United States"];
                return;
            }
        }
    }
    [self showToast:@"更新地址失败"];
}

houweibin committed
182 183 184 185 186 187

-(void)addAddress{
    [self showLoading];
    [self.client createAddress:[self getAddress] callback:^(BUYAddress *address, NSError *error) {
        [self hideLoading];
        if(error!=nil){
188
            [self showAddressError:error];
houweibin committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        }else if(address!=nil){
            self.address = address;
            [self updateCustomerAddress];
            [self showToast:@"更新地址成功"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
    }];
}

-(void)editAddress{
    [self showLoading];
    [self.client updateAddress:[self getAddress] callback:^(BUYAddress *address, NSError *error) {
        [self hideLoading];
        NSLog(@"error  = %@",error);
        if (error != nil) {
206
            [self showAddressError:error];
houweibin committed
207 208 209 210 211 212 213 214 215 216
        }else if(address != nil){
            [self updateCustomerAddress];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
    }];
}


217 218


houweibin committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
- (void)updateCustomerAddress{
    [self showLoading];
    [self.client getCustomerCallback:^(BUYCustomer * _Nullable customer, NSError * _Nullable error) {
        [self showToast:@"更新地址成功"];
        [self hideLoading];
        if (error == nil && customer != nil) {
            [[NSUserDefaults standardUserDefaults] setObject:customer.JSONDictionary forKey:@"customer"];
        }
        if (error != nil) {
            [self showError:error];
        }
    }];
}




- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
238 239
//    return ![string isEqualToString:@" "];
    return YES;
houweibin committed
240 241 242 243 244 245 246 247 248
}

#pragma mark - UITextFieldDelegate
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    NSInteger tag = textField.tag;
    NSInteger removeTag = tag - 2;
    if(removeTag < 0){
        removeTag = 0;
    }
249
    self.marginTopOfCountry.constant = 30 - 57 * removeTag;
houweibin committed
250 251 252 253 254 255 256 257 258
    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
    }];
    
    return YES;
}

//键盘弹出
-(void)keyboardWillHide:(NSNotification *)notification{
259
    self.marginTopOfCountry.constant = 30;
houweibin committed
260 261 262 263 264 265 266 267 268 269
    [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
    }];
    
}




@end