No Description

FKIdCardManageController.m 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //
  2. // FKIdCardManageController.m
  3. // FirstLink
  4. //
  5. // Created by jack on 15/11/25.
  6. // Copyright © 2015年 FirstLink. All rights reserved.
  7. //
  8. #import "FKIdCardManageController.h"
  9. #import "FKIdCardManageViewModel.h"
  10. #import "FKIdCardManageCell.h"
  11. #import "FKIdCardManageReform.h"
  12. #import "FKIdCardManageRequest.h"
  13. #import "FKAddIdCardController.h"
  14. #import "FKEditIdCardController.h"
  15. static NSString * ID_MANAGE_CELL_IDENTIFY = @"ID_MANAGE_CELL_IDENTIFY";
  16. @interface FKIdCardManageController () <UITableViewDataSource, UITableViewDelegate, FLNetworkDelegate>
  17. @property (nonatomic, strong) UITableView *tableView;
  18. @property (nonatomic, strong) FKIdCardManageViewModel *viewModel;
  19. @end
  20. @implementation FKIdCardManageController
  21. - (void)viewDidLoad{
  22. [super viewDidLoad];
  23. [self addAllSubviews];
  24. }
  25. - (void)viewWillAppear:(BOOL)animated{
  26. [super viewWillAppear:animated];
  27. [self configNavBar];
  28. [self addRefreshControl];
  29. [self requestInitalData];
  30. }
  31. - (void)viewWillDisappear:(BOOL)animated {
  32. [super viewWillDisappear:animated];
  33. }
  34. - (void)configNavBar{
  35. [[self navigationController] setNavigationBarHidden:NO animated:YES];
  36. self.navigationItem.title = @"管理身份证信息";
  37. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加"
  38. style:UIBarButtonItemStyleDone
  39. target:self
  40. action:@selector(addNewIdCard)];
  41. }
  42. - (void)addRefreshControl{
  43. [self initRefreshControlWithTableView:self.tableView];
  44. self.refreshControl.bottomEnabled = NO;
  45. }
  46. - (void)autoRefresh{
  47. [self.refreshControl startRefreshingDirection:RefreshDirectionTop];
  48. }
  49. - (void)addAllSubviews{
  50. [self.view addSubview:self.tableView];
  51. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  52. make.edges.insets(UIEdgeInsetsZero);
  53. }];
  54. }
  55. - (void)requestInitalData{
  56. [self.hudView show:YES];
  57. [FKIdCardManageRequest requestIdCardListDelegate:self];
  58. }
  59. #pragma mark - refresh control
  60. - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction {
  61. if (direction == RefreshDirectionTop) {
  62. [FKIdCardManageRequest requestIdCardListDelegate:self];
  63. }
  64. }
  65. #pragma mark - response
  66. - (void)networkDidSuccessResponse:(NSDictionary*)response identify:(int)identify header:(MSGHeader*)header{
  67. [self.hudView hide:NO];
  68. [self.refreshControl finishRefreshingDirection:RefreshDirectionTop];
  69. if (header.code.intValue == RESPONSE_MSG_NORMAL) {
  70. if (identify == ID_CARD_LIST_REQ) {
  71. self.viewModel.idCardArray = [FKIdCardManageReform parserIdCardListWitDict:response];
  72. self.viewModel.lastDefaultIndex = [self.viewModel currentDefaultIndex];
  73. if (self.viewModel.idCardArray.count > 0) {
  74. [self.statusView removeFromSuperview];
  75. }
  76. [self.tableView reloadData];
  77. // 更新提交订单页面的身份信息
  78. if ([self.dataSource respondsToSelector:@selector(currentSelectedIDCard)]) {
  79. NSString *itemID = [self.dataSource currentSelectedIDCard];
  80. if (itemID.length > 0) {
  81. WeakSelf(weakSelf);
  82. [self.viewModel.idCardArray enumerateObjectsUsingBlock:^(FKIdCardItem *obj, NSUInteger idx, BOOL *stop)
  83. {
  84. if ([itemID isEqualToString:obj.itemID]) {
  85. if ([weakSelf.delegate respondsToSelector:@selector(idCardManageController:didUpdateItem:)]) {
  86. [weakSelf.delegate idCardManageController:weakSelf didUpdateItem:obj];
  87. }
  88. *stop = YES;
  89. }
  90. }];
  91. }
  92. }
  93. }
  94. } else {
  95. [FLProgressHUDHelper showText:header.msg inView:self.view];
  96. if (identify == ID_CARD_DELETE_REQ){
  97. [self autoRefresh];
  98. }else if (identify == ID_CARD_SET_DEFAULT_REQ){
  99. [self.viewModel setDefaultAtIndex:self.viewModel.lastDefaultIndex];
  100. [self.tableView reloadData];
  101. }
  102. }
  103. if (self.viewModel.idCardArray.count == 0) {
  104. [self showStatusTipInView:self.view image:[UIImage imageNamed:@"StatusNoIDCardIcon"] title:@"没有填写身份信息哦"];
  105. }else{
  106. [self.statusView removeFromSuperview];
  107. }
  108. }
  109. - (void)networkDidReceiveError:(NSError*)error identify:(int)identify header:(MSGHeader*)header{
  110. [self.hudView hide:NO];
  111. [self.refreshControl finishRefreshingDirection:RefreshDirectionTop];
  112. if (identify == ID_CARD_SET_DEFAULT_REQ) {
  113. [self.viewModel setDefaultAtIndex:self.viewModel.lastDefaultIndex];
  114. [self.tableView reloadData];
  115. } else if (identify == ID_CARD_DELETE_REQ) {
  116. [self autoRefresh];
  117. }
  118. [FLProgressHUDHelper showText:header.msg inView:self.view];
  119. }
  120. #pragma mark - tableView dataSource
  121. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  122. return self.viewModel.idCardArray.count;
  123. }
  124. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  125. return 1;
  126. }
  127. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  128. FKIdCardManageCell *cell = [tableView dequeueReusableCellWithIdentifier:ID_MANAGE_CELL_IDENTIFY];
  129. FKIdCardItem *item = [self.viewModel idCardItemAtIndex:indexPath.section];
  130. if (item && cell) {
  131. [cell configWithIdCardItem:item];
  132. cell.setDefaultButton.tag = cell.editButton.tag = cell.deleteButton.tag = indexPath.section;
  133. [cell.setDefaultButton addTarget:self
  134. action:@selector(clickDefaultBtn:)
  135. forControlEvents:UIControlEventTouchUpInside];
  136. [cell.editButton addTarget:self
  137. action:@selector(clickEditBtn:)
  138. forControlEvents:UIControlEventTouchUpInside];
  139. [cell.deleteButton addTarget:self
  140. action:@selector(clickDeleteBtn:)
  141. forControlEvents:UIControlEventTouchUpInside];
  142. }
  143. return cell;
  144. }
  145. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  146. return 260;
  147. }
  148. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  149. return 10;
  150. }
  151. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  152. FKIdCardItem *item = [self.viewModel idCardItemAtIndex:indexPath.section];
  153. if (item && [self.delegate respondsToSelector:@selector(idCardManageController:didSelectItem:)]) {
  154. [self.delegate idCardManageController:self didSelectItem:item];
  155. }
  156. }
  157. #pragma mark - action
  158. - (void)addNewIdCard{
  159. FKAddIdCardController *addVc = [[FKAddIdCardController alloc]init];
  160. [self.navigationController pushViewController:addVc animated:YES];
  161. }
  162. - (void)clickDefaultBtn:(UIButton *)sender{
  163. if (sender.selected == YES) return;
  164. sender.selected = YES;
  165. FKIdCardItem *item = [self.viewModel idCardItemAtIndex:sender.tag];
  166. if (item) {
  167. [FKIdCardManageRequest requestSetDefaultIdCardWithId:item.itemID delegate:self];
  168. [self.viewModel setDefaultAtIndex:sender.tag];
  169. self.viewModel.lastDefaultIndex = sender.tag;
  170. [self.tableView reloadData];
  171. }
  172. }
  173. - (void)clickEditBtn:(UIButton *)sender{
  174. FKIdCardItem *item = [self.viewModel idCardItemAtIndex:sender.tag];
  175. FKEditIdCardController *editVC = [[FKEditIdCardController alloc]initWithDefaultIdCard:item];
  176. [self.navigationController pushViewController:editVC animated:YES];
  177. }
  178. - (void)clickDeleteBtn:(UIButton *)sender{
  179. if (sender.tag < 0) return;
  180. FKIdCardItem *targetItem = [self.viewModel idCardItemAtIndex:sender.tag];
  181. if (targetItem.itemID.length) {
  182. [FKIdCardManageRequest requestDeleteIdCardWithId:targetItem.itemID delegate:self];
  183. if ([self.dataSource respondsToSelector:@selector(currentSelectedIDCard)]) {
  184. NSString *idCardNum = [self.dataSource currentSelectedIDCard];
  185. if (idCardNum.length > 0 && [idCardNum isEqualToString:targetItem.itemID]) {
  186. if ([self.delegate respondsToSelector:@selector(idCardManageController:didRemoveItem:)]) {
  187. [self.delegate idCardManageController:self didRemoveItem:targetItem];
  188. }
  189. }
  190. }
  191. }
  192. [self.viewModel deleteIdCardAtIndex:sender.tag];
  193. [self.tableView reloadData];
  194. }
  195. #pragma mark - property
  196. - (UITableView *)tableView
  197. {
  198. if (_tableView == nil) {
  199. _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  200. _tableView.dataSource = self;
  201. _tableView.delegate = self;
  202. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  203. _tableView.sectionFooterHeight = 0;
  204. _tableView.sectionHeaderHeight = 10;
  205. _tableView.backgroundColor = UIColorFromRGB(0xf4f4f4);
  206. if (@available(iOS 11.0, *)) {
  207. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  208. }
  209. [_tableView registerClass:[FKIdCardManageCell class] forCellReuseIdentifier:ID_MANAGE_CELL_IDENTIFY];
  210. }
  211. return _tableView;
  212. }
  213. - (FKIdCardManageViewModel *)viewModel{
  214. if (_viewModel == nil) {
  215. _viewModel = [[FKIdCardManageViewModel alloc]init];
  216. }
  217. return _viewModel;
  218. }
  219. @end