123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- //
- // 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 ()
- <UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate, FLNetworkDelegate, FKAddressListCellDelegate>
- @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<FKAddressListCellDataSource> 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
|