// // AddressListController.m // FirstLink // // Created by ascii on 15/5/28. // Copyright (c) 2015年 FirstLink. All rights reserved. // #import "FKAddressListController.h" #import "FKAddressListViewModel.h" #import "FKAddressListRequest.h" #import "FKAddressDetailRequest.h" #import "FKAddressListReform.h" #import "ShipAddress.h" #import "FKAddressListManageCell.h" #import "FKAddressListChoiceCell.h" #import "UIExtendAlertView.h" #import "FKAddressListCellDelegate.h" #import "FKAddressDetailController.h" static NSString * const FKAddressListManageCellIdentifier = @"FKAddressListManageCellIdentifier"; static NSString * const FKAddressListChoiceCellIdentifier = @"FKAddressListChoiceCellIdentifier"; static NSInteger FKAddressListControllerDeleteTag = 1; @interface FKAddressListController () @property (nonatomic, assign) FKAddressListControllerType controllerType; @property (nonatomic, strong) FKAddressListViewModel *viewModel; @property (nonatomic, strong) UITableView *tableView; @end @implementation FKAddressListController - (instancetype)initWithType:(FKAddressListControllerType)controllerType { self = [super init]; if (self) { _controllerType = controllerType; } return self; } - (void)loadView { [super loadView]; [self addAllSubviews]; [self configTableViewCell]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[self navigationController] setNavigationBarHidden:NO animated:YES]; [self configNavigationTitle]; [self configRightNaviItem]; [self requestAddressList]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ #pragma mark - Request - (void)requestAddressList { [self.hudView show:YES]; [FKAddressListRequest requestAddressList:FKAddressListRequestAll deleagate:self]; } - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction { if (direction==RefreshDirectionTop) { [self requestAddressList]; } } #pragma mark - Response - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header { [self.hudView hide:YES]; [self finishLoadingData]; if ([header.code intValue] == RESPONSE_MSG_NORMAL) { if (identify == FKAddressListRequestAll) { NSArray *array = [FKAddressListReform parseAddressList:response]; self.viewModel.addressListArray = [NSMutableArray arrayWithArray:array]; if ([self.dataSource respondsToSelector:@selector(currentSelectedAddressID)]) { /** * 设置前一个页面选中的地址为选中状态 */ NSString *addressID = [self.dataSource currentSelectedAddressID]; [self.viewModel setAddressSelected:addressID]; /** * 更新前一个提交订单页面选中的地址 */ if (addressID.length > 0) { WeakSelf(weakSelf); __block BOOL isCompareAddress = NO; [self.viewModel.addressListArray enumerateObjectsUsingBlock:^(ShipAddress *obj, NSUInteger idx, BOOL *stop) { if ([addressID isEqualToString:obj.addressID]) { if ([weakSelf.delegate respondsToSelector:@selector(addressListController:didUpdateAddress:)]) { [weakSelf.delegate addressListController:weakSelf didUpdateAddress:obj]; } isCompareAddress = YES; *stop = YES; } }]; if (!isCompareAddress) { if ([weakSelf.delegate respondsToSelector:@selector(addressListController:didUpdateAddress:)]) { [weakSelf.delegate addressListController:weakSelf didUpdateAddress:nil]; } } } } if (array.count > 0) { [self.statusView removeFromSuperview]; } else { [self showStatusTipInView:self.view image:[UIImage imageNamed:@"StatusNoAddressIcon"] title:@"没有填写收货地址哦"]; } } if (identify == FKAddressListRequestDelete) { [self requestAddressList]; return; } [self.tableView reloadData]; } else { [FLProgressHUDHelper showText:header.msg inView:self.view]; } } - (void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header { [self.hudView hide:YES]; [self finishLoadingData]; [FLProgressHUDHelper showText:header.msg inView:self.view]; } #pragma mark - TableView Delegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.viewModel.addressListArray.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 10; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return CGFLOAT_MIN; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { ShipAddress *item = [self.viewModel itemAtIndex:indexPath.section]; if (item) { if (_controllerType == FKAddressListControllerTypeManage) { return [FKAddressListManageCell cellHeightWith:item.detailAddressWithTip]; } if (_controllerType == FKAddressListControllerTypeChoice) { return [FKAddressListChoiceCell cellHeightWith:item.detailAddressWithTip]; } } return 44; } - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { id cell; if (_controllerType == FKAddressListControllerTypeManage) { cell = [tableView dequeueReusableCellWithIdentifier:FKAddressListManageCellIdentifier]; } else { cell = [tableView dequeueReusableCellWithIdentifier:FKAddressListChoiceCellIdentifier]; } ShipAddress *item = [self.viewModel itemAtIndex:indexPath.section]; if (item && [cell respondsToSelector:@selector(configAddressListCell:withIndex:delegate:)]) { [cell configAddressListCell:item withIndex:indexPath.section delegate:self]; } return (UITableViewCell*)cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (_controllerType == FKAddressListControllerTypeChoice) { if ([self.delegate respondsToSelector:@selector(addressListController:didSelectAddress:)]) { ShipAddress *item = [self.viewModel itemAtIndex:indexPath.section]; if (item) { [self.delegate addressListController:self didSelectAddress:item]; } } } } #pragma mark - FKAddressListCellDelegate - (void)didClickModifyAddressAtIndex:(NSInteger)index { ShipAddress *item = [self.viewModel itemAtIndex:index]; if (item) { FKAddressDetailController *controller = [[FKAddressDetailController alloc] initWithAddress:item type:FKAddressDetailControllerTypeEdit]; controller.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:controller animated:YES]; } } - (void)didClickDefaultAddressAtIndex:(NSInteger)index { ShipAddress *item = [self.viewModel itemAtIndex:index]; if (item && ![item.isDefault isEqualToString:@"1"]) { [self.viewModel.addressListArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if (idx == index) { item.isDefault = @"1"; } else { ((ShipAddress*)obj).isDefault = @"0"; } }]; [FKAddressDetailRequest requestModifyAddress:FKAddressListRequestDefault address:item deleagate:self]; } } - (void)didClickDeleteAddressAtIndex:(NSInteger)index { ShipAddress *item = [self.viewModel itemAtIndex:index]; if (item) { UIExtendAlertView *alert = [[UIExtendAlertView alloc] initWithTitle:@"提示" message:@"确定要删除该地址么?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; alert.userInfo = @{@"addressID": item.addressID}; alert.delegate = self; alert.tag = FKAddressListControllerDeleteTag; [alert show]; } } #pragma mark - AlertView Delegate - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (alertView.tag == FKAddressListControllerDeleteTag && buttonIndex == 1) { if ([alertView isKindOfClass:[UIExtendAlertView class]]) { UIExtendAlertView *extAlertView = (UIExtendAlertView*)alertView; NSDictionary *userInfo = extAlertView.userInfo; [FKAddressListRequest requestDeleteAddress:FKAddressListRequestDelete addressID:userInfo[@"addressID"] deleagate:self]; } } } #pragma mark - Action - (IBAction)clickRightItemAction:(UIButton*)sender { UIViewController *controller; if (_controllerType == FKAddressListControllerTypeManage) { controller = [[FKAddressDetailController alloc] initWithAddress:nil type:FKAddressDetailControllerTypeAdd]; } else { controller = [[FKAddressListController alloc] initWithType:FKAddressListControllerTypeManage]; } controller.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:controller animated:YES]; } #pragma mark - Private - (void)addAllSubviews { [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.bottom.equalTo(self.view); }]; } - (void)configTableViewCell { [self initRefreshControlWithTableView:self.tableView]; self.refreshControl.bottomEnabled = NO; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.tableView registerClass:[FKAddressListManageCell class] forCellReuseIdentifier:FKAddressListManageCellIdentifier]; [self.tableView registerClass:[FKAddressListChoiceCell class] forCellReuseIdentifier:FKAddressListChoiceCellIdentifier]; } - (void)configNavigationTitle { self.navigationItem.title = [FKAddressListViewModel navigationTitle:_controllerType]; } - (void)configRightNaviItem { UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:[FKAddressListViewModel rightNaviItemTitle:_controllerType] style:UIBarButtonItemStyleDone target:self action:@selector(clickRightItemAction:)]; self.navigationItem.rightBarButtonItem = barItem; } #pragma mark - Property - (FKAddressListViewModel *)viewModel { if (!_viewModel) { _viewModel = [FKAddressListViewModel new]; } return _viewModel; } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.dataSource = self; _tableView.delegate = self; _tableView.backgroundColor = UIColorFromRGB(0xf4f4f4); if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } } return _tableView; } @end