説明なし

FKAddressListController.m 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //
  2. // AddressListController.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 15/5/28.
  6. // Copyright (c) 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "FKAddressListController.h"
  9. #import "FKAddressListViewModel.h"
  10. #import "FKAddressListRequest.h"
  11. #import "FKAddressDetailRequest.h"
  12. #import "FKAddressListReform.h"
  13. #import "ShipAddress.h"
  14. #import "FKAddressListManageCell.h"
  15. #import "FKAddressListChoiceCell.h"
  16. #import "UIExtendAlertView.h"
  17. #import "FKAddressListCellDelegate.h"
  18. #import "FKAddressDetailController.h"
  19. static NSString * const FKAddressListManageCellIdentifier = @"FKAddressListManageCellIdentifier";
  20. static NSString * const FKAddressListChoiceCellIdentifier = @"FKAddressListChoiceCellIdentifier";
  21. static NSInteger FKAddressListControllerDeleteTag = 1;
  22. @interface FKAddressListController ()
  23. <UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate, FLNetworkDelegate, FKAddressListCellDelegate>
  24. @property (nonatomic, assign) FKAddressListControllerType controllerType;
  25. @property (nonatomic, strong) FKAddressListViewModel *viewModel;
  26. @property (nonatomic, strong) UITableView *tableView;
  27. @end
  28. @implementation FKAddressListController
  29. - (instancetype)initWithType:(FKAddressListControllerType)controllerType {
  30. self = [super init];
  31. if (self) {
  32. _controllerType = controllerType;
  33. }
  34. return self;
  35. }
  36. - (void)loadView {
  37. [super loadView];
  38. [self addAllSubviews];
  39. [self configTableViewCell];
  40. }
  41. - (void)viewDidLoad {
  42. [super viewDidLoad];
  43. // Do any additional setup after loading the view from its nib.
  44. }
  45. - (void)viewWillAppear:(BOOL)animated {
  46. [super viewWillAppear:animated];
  47. [[self navigationController] setNavigationBarHidden:NO animated:YES];
  48. [self configNavigationTitle];
  49. [self configRightNaviItem];
  50. [self requestAddressList];
  51. }
  52. - (void)viewDidAppear:(BOOL)animated {
  53. [super viewDidAppear:animated];
  54. }
  55. - (void)viewWillDisappear:(BOOL)animated {
  56. [super viewWillDisappear:animated];
  57. }
  58. - (void)didReceiveMemoryWarning {
  59. [super didReceiveMemoryWarning];
  60. // Dispose of any resources that can be recreated.
  61. }
  62. /*
  63. #pragma mark - Navigation
  64. // In a storyboard-based application, you will often want to do a little preparation before navigation
  65. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  66. // Get the new view controller using [segue destinationViewController].
  67. // Pass the selected object to the new view controller.
  68. }
  69. */
  70. #pragma mark - Request
  71. - (void)requestAddressList {
  72. [self.hudView show:YES];
  73. [FKAddressListRequest requestAddressList:FKAddressListRequestAll deleagate:self];
  74. }
  75. - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction {
  76. if (direction==RefreshDirectionTop) {
  77. [self requestAddressList];
  78. }
  79. }
  80. #pragma mark - Response
  81. - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header {
  82. [self.hudView hide:YES];
  83. [self finishLoadingData];
  84. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  85. if (identify == FKAddressListRequestAll) {
  86. NSArray *array = [FKAddressListReform parseAddressList:response];
  87. self.viewModel.addressListArray = [NSMutableArray arrayWithArray:array];
  88. if ([self.dataSource respondsToSelector:@selector(currentSelectedAddressID)]) {
  89. /**
  90. * 设置前一个页面选中的地址为选中状态
  91. */
  92. NSString *addressID = [self.dataSource currentSelectedAddressID];
  93. [self.viewModel setAddressSelected:addressID];
  94. /**
  95. * 更新前一个提交订单页面选中的地址
  96. */
  97. if (addressID.length > 0) {
  98. WeakSelf(weakSelf);
  99. __block BOOL isCompareAddress = NO;
  100. [self.viewModel.addressListArray enumerateObjectsUsingBlock:^(ShipAddress *obj, NSUInteger idx, BOOL *stop)
  101. {
  102. if ([addressID isEqualToString:obj.addressID]) {
  103. if ([weakSelf.delegate respondsToSelector:@selector(addressListController:didUpdateAddress:)]) {
  104. [weakSelf.delegate addressListController:weakSelf didUpdateAddress:obj];
  105. }
  106. isCompareAddress = YES;
  107. *stop = YES;
  108. }
  109. }];
  110. if (!isCompareAddress) {
  111. if ([weakSelf.delegate respondsToSelector:@selector(addressListController:didUpdateAddress:)]) {
  112. [weakSelf.delegate addressListController:weakSelf didUpdateAddress:nil];
  113. }
  114. }
  115. }
  116. }
  117. if (array.count > 0) {
  118. [self.statusView removeFromSuperview];
  119. } else {
  120. [self showStatusTipInView:self.view image:[UIImage imageNamed:@"StatusNoAddressIcon"] title:@"没有填写收货地址哦"];
  121. }
  122. }
  123. if (identify == FKAddressListRequestDelete) {
  124. [self requestAddressList];
  125. return;
  126. }
  127. [self.tableView reloadData];
  128. } else {
  129. [FLProgressHUDHelper showText:header.msg inView:self.view];
  130. }
  131. }
  132. - (void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header {
  133. [self.hudView hide:YES];
  134. [self finishLoadingData];
  135. [FLProgressHUDHelper showText:header.msg inView:self.view];
  136. }
  137. #pragma mark - TableView Delegate
  138. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  139. return self.viewModel.addressListArray.count;
  140. }
  141. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  142. return 1;
  143. }
  144. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  145. return 10;
  146. }
  147. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  148. return CGFLOAT_MIN;
  149. }
  150. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  151. ShipAddress *item = [self.viewModel itemAtIndex:indexPath.section];
  152. if (item) {
  153. if (_controllerType == FKAddressListControllerTypeManage) {
  154. return [FKAddressListManageCell cellHeightWith:item.detailAddressWithTip];
  155. }
  156. if (_controllerType == FKAddressListControllerTypeChoice) {
  157. return [FKAddressListChoiceCell cellHeightWith:item.detailAddressWithTip];
  158. }
  159. }
  160. return 44;
  161. }
  162. - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  163. id<FKAddressListCellDataSource> cell;
  164. if (_controllerType == FKAddressListControllerTypeManage) {
  165. cell = [tableView dequeueReusableCellWithIdentifier:FKAddressListManageCellIdentifier];
  166. } else {
  167. cell = [tableView dequeueReusableCellWithIdentifier:FKAddressListChoiceCellIdentifier];
  168. }
  169. ShipAddress *item = [self.viewModel itemAtIndex:indexPath.section];
  170. if (item && [cell respondsToSelector:@selector(configAddressListCell:withIndex:delegate:)]) {
  171. [cell configAddressListCell:item
  172. withIndex:indexPath.section
  173. delegate:self];
  174. }
  175. return (UITableViewCell*)cell;
  176. }
  177. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  178. if (_controllerType == FKAddressListControllerTypeChoice) {
  179. if ([self.delegate respondsToSelector:@selector(addressListController:didSelectAddress:)]) {
  180. ShipAddress *item = [self.viewModel itemAtIndex:indexPath.section];
  181. if (item) {
  182. [self.delegate addressListController:self didSelectAddress:item];
  183. }
  184. }
  185. }
  186. }
  187. #pragma mark - FKAddressListCellDelegate
  188. - (void)didClickModifyAddressAtIndex:(NSInteger)index {
  189. ShipAddress *item = [self.viewModel itemAtIndex:index];
  190. if (item) {
  191. FKAddressDetailController *controller = [[FKAddressDetailController alloc] initWithAddress:item type:FKAddressDetailControllerTypeEdit];
  192. controller.hidesBottomBarWhenPushed = YES;
  193. [self.navigationController pushViewController:controller animated:YES];
  194. }
  195. }
  196. - (void)didClickDefaultAddressAtIndex:(NSInteger)index {
  197. ShipAddress *item = [self.viewModel itemAtIndex:index];
  198. if (item && ![item.isDefault isEqualToString:@"1"]) {
  199. [self.viewModel.addressListArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  200. if (idx == index) {
  201. item.isDefault = @"1";
  202. } else {
  203. ((ShipAddress*)obj).isDefault = @"0";
  204. }
  205. }];
  206. [FKAddressDetailRequest requestModifyAddress:FKAddressListRequestDefault
  207. address:item
  208. deleagate:self];
  209. }
  210. }
  211. - (void)didClickDeleteAddressAtIndex:(NSInteger)index {
  212. ShipAddress *item = [self.viewModel itemAtIndex:index];
  213. if (item) {
  214. UIExtendAlertView *alert = [[UIExtendAlertView alloc] initWithTitle:@"提示"
  215. message:@"确定要删除该地址么?"
  216. delegate:self
  217. cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
  218. alert.userInfo = @{@"addressID": item.addressID};
  219. alert.delegate = self;
  220. alert.tag = FKAddressListControllerDeleteTag;
  221. [alert show];
  222. }
  223. }
  224. #pragma mark - AlertView Delegate
  225. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  226. if (alertView.tag == FKAddressListControllerDeleteTag && buttonIndex == 1) {
  227. if ([alertView isKindOfClass:[UIExtendAlertView class]]) {
  228. UIExtendAlertView *extAlertView = (UIExtendAlertView*)alertView;
  229. NSDictionary *userInfo = extAlertView.userInfo;
  230. [FKAddressListRequest requestDeleteAddress:FKAddressListRequestDelete
  231. addressID:userInfo[@"addressID"]
  232. deleagate:self];
  233. }
  234. }
  235. }
  236. #pragma mark - Action
  237. - (IBAction)clickRightItemAction:(UIButton*)sender {
  238. UIViewController *controller;
  239. if (_controllerType == FKAddressListControllerTypeManage) {
  240. controller = [[FKAddressDetailController alloc] initWithAddress:nil type:FKAddressDetailControllerTypeAdd];
  241. } else {
  242. controller = [[FKAddressListController alloc] initWithType:FKAddressListControllerTypeManage];
  243. }
  244. controller.hidesBottomBarWhenPushed = YES;
  245. [self.navigationController pushViewController:controller animated:YES];
  246. }
  247. #pragma mark - Private
  248. - (void)addAllSubviews {
  249. [self.view addSubview:self.tableView];
  250. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  251. make.left.right.top.bottom.equalTo(self.view);
  252. }];
  253. }
  254. - (void)configTableViewCell {
  255. [self initRefreshControlWithTableView:self.tableView];
  256. self.refreshControl.bottomEnabled = NO;
  257. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  258. [self.tableView registerClass:[FKAddressListManageCell class] forCellReuseIdentifier:FKAddressListManageCellIdentifier];
  259. [self.tableView registerClass:[FKAddressListChoiceCell class] forCellReuseIdentifier:FKAddressListChoiceCellIdentifier];
  260. }
  261. - (void)configNavigationTitle {
  262. self.navigationItem.title = [FKAddressListViewModel navigationTitle:_controllerType];
  263. }
  264. - (void)configRightNaviItem {
  265. UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:[FKAddressListViewModel rightNaviItemTitle:_controllerType]
  266. style:UIBarButtonItemStyleDone
  267. target:self
  268. action:@selector(clickRightItemAction:)];
  269. self.navigationItem.rightBarButtonItem = barItem;
  270. }
  271. #pragma mark - Property
  272. - (FKAddressListViewModel *)viewModel {
  273. if (!_viewModel) {
  274. _viewModel = [FKAddressListViewModel new];
  275. }
  276. return _viewModel;
  277. }
  278. - (UITableView *)tableView {
  279. if (!_tableView) {
  280. _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  281. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  282. _tableView.dataSource = self;
  283. _tableView.delegate = self;
  284. _tableView.backgroundColor = UIColorFromRGB(0xf4f4f4);
  285. if (@available(iOS 11.0, *)) {
  286. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  287. }
  288. }
  289. return _tableView;
  290. }
  291. @end