// // ASAddressListViewController.m // Asteria // // Created by iOS on 2024/5/7. // #import "ASAddressListViewController.h" #import "ASAddressViewModel.h" #import "ASMineAddressCell.h" #import "ASEditAddressViewController.h" @interface ASAddressListViewController () @property (nonatomic, strong) UIView *bottomV; @property (nonatomic, strong) UIButton *addNewBt; @property (nonatomic, strong) UITableView *tableV; @property (nonatomic, strong) NSArray *addArr; @end @implementation ASAddressListViewController - (void)viewDidLoad { [super viewDidLoad]; [self loadSubV]; [self setEmptyTip:@"No Addresses"]; self.titleStr = @"SHIPPING ADDRESS"; __weak typeof(self) weak_self = self; self.tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ [weak_self getData]; }]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.tableV.mj_header beginRefreshing]; } - (void)getData { __weak typeof(self) weakSelf = self; [ASUserInfoManager.shared baseInfoNet:^{ [weakSelf.tableV.mj_header endRefreshing]; weakSelf.addArr = ASUserInfoManager.shared.userInfo.addresses; if (weakSelf.addArr.count == 0) { [weakSelf showEmptyV:weakSelf.tableV]; } else { [weakSelf hiddenEmpty]; } [weakSelf.tableV reloadData]; }]; } - (void)loadSubV { [self.view addSubview:self.tableV]; [self.view addSubview:self.bottomV]; [self.bottomV addSubview:self.addNewBt]; [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.customNavBar.mas_bottom); make.left.right.equalTo(self.view); }]; [self.bottomV mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.top.equalTo(self.tableV.mas_bottom); make.height.equalTo(@65); make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom); }]; [self.addNewBt mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.bottomV).offset(10); make.left.equalTo(self.bottomV).offset(20); make.center.equalTo(self.bottomV); make.height.equalTo(@45); }]; } - (UIButton *)addNewBt { if (!_addNewBt) { UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom]; [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; // bt.layer.borderWidth = 1; // bt.layer.borderColor = [UIColor blackColor].CGColor; // bt.layer.cornerRadius = 4; // bt.layer.masksToBounds = true; bt.backgroundColor = UIColor.blackColor; [bt setTitle:@"+ NEW ADDRESS" forState:UIControlStateNormal]; [bt addTarget:self action:@selector(addNewbtAction) forControlEvents:UIControlEventTouchUpInside]; bt.titleLabel.font = [UIFont fontWithName:Rob_Medium size:18]; _addNewBt = bt; } return _addNewBt; } -(UIView *)bottomV { if (!_bottomV) { UIView *v = [UIView new]; v.frame = CGRectMake(0, 0, KScreenWidth, 70); v.backgroundColor = [UIColor whiteColor]; _bottomV = v; } return _bottomV; } - (UITableView *)tableV { IPhoneXHeigh if (!_tableV) { UITableView *tabV = [[UITableView alloc] initWithFrame:CGRectMake(securitytop_Y, 0, KScreenWidth, security_H) style:UITableViewStylePlain]; tabV.backgroundColor = [UIColor whiteColor]; [tabV registerClass:[ASMineAddressCell class] forCellReuseIdentifier:@"ASMineAddressCell"]; tabV.delegate = self; tabV.dataSource = self; tabV.rowHeight = UITableViewAutomaticDimension; tabV.estimatedRowHeight = 100; tabV.separatorStyle = UITableViewCellSeparatorStyleNone; _tableV = tabV; } return _tableV; } - (void)addNewbtAction { ASEditAddressViewController *vc = [ASEditAddressViewController new]; ///当添加地址时,默认地址都改为美国地址 ASAddressModel *model = [ASAddressModel defualtData]; vc.m = model; vc.isEdit = false; vc.isCartEdit = self.isSelMode; __weak typeof(self) weakSelf = self; vc.saveSuccess = ^(ASAddressModel * _Nonnull addressM) { if (weakSelf.selectAddressBlock) { weakSelf.selectAddressBlock(addressM); [weakSelf.navigationController popViewControllerAnimated:true]; } }; [self.navigationController pushViewController:vc animated:true]; } #pragma mark - UITableViewDelegate,UITableViewDataSource - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.addArr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ASMineAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ASMineAddressCell" forIndexPath:indexPath]; ASAddressModel *m = self.addArr[indexPath.row]; [cell setData:m]; if(self.isSelMode){ cell.checkBtn.hidden = NO; if([m.Id isEqualToString:self.sel_Id]){ cell.checkBtn.selected = YES; }else{ cell.checkBtn.selected = NO; } [cell setCheckBack:^{ if (self.selectAddressBlock) { self.selectAddressBlock(m); } [self.navigationController popViewControllerAnimated:YES]; }]; }else{ cell.checkBtn.hidden = YES; } __weak typeof(self) weakSelf = self; [cell setEditBack:^{ // 去编辑地址 ASEditAddressViewController *vc = [ASEditAddressViewController new]; vc.m = m; vc.isEdit = true; vc.isCartEdit = weakSelf.isSelMode; vc.saveSuccess = ^(ASAddressModel * _Nonnull addressM) { if (weakSelf.selectAddressBlock) { weakSelf.selectAddressBlock(addressM); [weakSelf.navigationController popViewControllerAnimated:true]; } }; [weakSelf.navigationController pushViewController:vc animated:true]; }]; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0.1; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 0.1; } @end