123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- //
- // FKIdCardManageController.m
- // FirstLink
- //
- // Created by jack on 15/11/25.
- // Copyright © 2015年 FirstLink. All rights reserved.
- //
- #import "FKIdCardManageController.h"
- #import "FKIdCardManageViewModel.h"
- #import "FKIdCardManageCell.h"
- #import "FKIdCardManageReform.h"
- #import "FKIdCardManageRequest.h"
- #import "FKAddIdCardController.h"
- #import "FKEditIdCardController.h"
- static NSString * ID_MANAGE_CELL_IDENTIFY = @"ID_MANAGE_CELL_IDENTIFY";
- @interface FKIdCardManageController () <UITableViewDataSource, UITableViewDelegate, FLNetworkDelegate>
- @property (nonatomic, strong) UITableView *tableView;
- @property (nonatomic, strong) FKIdCardManageViewModel *viewModel;
- @end
- @implementation FKIdCardManageController
- - (void)viewDidLoad{
- [super viewDidLoad];
- [self addAllSubviews];
- }
- - (void)viewWillAppear:(BOOL)animated{
- [super viewWillAppear:animated];
-
- [self configNavBar];
- [self addRefreshControl];
- [self requestInitalData];
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
- }
- - (void)configNavBar{
-
- [[self navigationController] setNavigationBarHidden:NO animated:YES];
- self.navigationItem.title = @"管理身份证信息";
- self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加"
- style:UIBarButtonItemStyleDone
- target:self
- action:@selector(addNewIdCard)];
- }
- - (void)addRefreshControl{
-
- [self initRefreshControlWithTableView:self.tableView];
- self.refreshControl.bottomEnabled = NO;
- }
- - (void)autoRefresh{
- [self.refreshControl startRefreshingDirection:RefreshDirectionTop];
- }
- - (void)addAllSubviews{
-
- [self.view addSubview:self.tableView];
-
- [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.insets(UIEdgeInsetsZero);
- }];
- }
- - (void)requestInitalData{
-
- [self.hudView show:YES];
- [FKIdCardManageRequest requestIdCardListDelegate:self];
- }
- #pragma mark - refresh control
- - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction {
- if (direction == RefreshDirectionTop) {
- [FKIdCardManageRequest requestIdCardListDelegate:self];
- }
- }
- #pragma mark - response
- - (void)networkDidSuccessResponse:(NSDictionary*)response identify:(int)identify header:(MSGHeader*)header{
- [self.hudView hide:NO];
- [self.refreshControl finishRefreshingDirection:RefreshDirectionTop];
-
- if (header.code.intValue == RESPONSE_MSG_NORMAL) {
- if (identify == ID_CARD_LIST_REQ) {
- self.viewModel.idCardArray = [FKIdCardManageReform parserIdCardListWitDict:response];
- self.viewModel.lastDefaultIndex = [self.viewModel currentDefaultIndex];
-
- if (self.viewModel.idCardArray.count > 0) {
- [self.statusView removeFromSuperview];
- }
- [self.tableView reloadData];
-
- // 更新提交订单页面的身份信息
- if ([self.dataSource respondsToSelector:@selector(currentSelectedIDCard)]) {
- NSString *itemID = [self.dataSource currentSelectedIDCard];
- if (itemID.length > 0) {
- WeakSelf(weakSelf);
- [self.viewModel.idCardArray enumerateObjectsUsingBlock:^(FKIdCardItem *obj, NSUInteger idx, BOOL *stop)
- {
- if ([itemID isEqualToString:obj.itemID]) {
- if ([weakSelf.delegate respondsToSelector:@selector(idCardManageController:didUpdateItem:)]) {
- [weakSelf.delegate idCardManageController:weakSelf didUpdateItem:obj];
- }
- *stop = YES;
- }
- }];
- }
- }
- }
- } else {
- [FLProgressHUDHelper showText:header.msg inView:self.view];
- if (identify == ID_CARD_DELETE_REQ){
- [self autoRefresh];
- }else if (identify == ID_CARD_SET_DEFAULT_REQ){
- [self.viewModel setDefaultAtIndex:self.viewModel.lastDefaultIndex];
- [self.tableView reloadData];
- }
- }
-
- if (self.viewModel.idCardArray.count == 0) {
- [self showStatusTipInView:self.view image:[UIImage imageNamed:@"StatusNoIDCardIcon"] title:@"没有填写身份信息哦"];
- }else{
- [self.statusView removeFromSuperview];
- }
- }
- - (void)networkDidReceiveError:(NSError*)error identify:(int)identify header:(MSGHeader*)header{
- [self.hudView hide:NO];
- [self.refreshControl finishRefreshingDirection:RefreshDirectionTop];
-
- if (identify == ID_CARD_SET_DEFAULT_REQ) {
- [self.viewModel setDefaultAtIndex:self.viewModel.lastDefaultIndex];
- [self.tableView reloadData];
- } else if (identify == ID_CARD_DELETE_REQ) {
- [self autoRefresh];
- }
-
- [FLProgressHUDHelper showText:header.msg inView:self.view];
- }
- #pragma mark - tableView dataSource
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
-
- return self.viewModel.idCardArray.count;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
-
- return 1;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- FKIdCardManageCell *cell = [tableView dequeueReusableCellWithIdentifier:ID_MANAGE_CELL_IDENTIFY];
-
- FKIdCardItem *item = [self.viewModel idCardItemAtIndex:indexPath.section];
-
- if (item && cell) {
- [cell configWithIdCardItem:item];
-
- cell.setDefaultButton.tag = cell.editButton.tag = cell.deleteButton.tag = indexPath.section;
-
- [cell.setDefaultButton addTarget:self
- action:@selector(clickDefaultBtn:)
- forControlEvents:UIControlEventTouchUpInside];
-
- [cell.editButton addTarget:self
- action:@selector(clickEditBtn:)
- forControlEvents:UIControlEventTouchUpInside];
- [cell.deleteButton addTarget:self
- action:@selector(clickDeleteBtn:)
- forControlEvents:UIControlEventTouchUpInside];
- }
-
- return cell;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- return 260;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
-
- return 10;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- FKIdCardItem *item = [self.viewModel idCardItemAtIndex:indexPath.section];
- if (item && [self.delegate respondsToSelector:@selector(idCardManageController:didSelectItem:)]) {
- [self.delegate idCardManageController:self didSelectItem:item];
- }
- }
- #pragma mark - action
- - (void)addNewIdCard{
-
- FKAddIdCardController *addVc = [[FKAddIdCardController alloc]init];
- [self.navigationController pushViewController:addVc animated:YES];
- }
- - (void)clickDefaultBtn:(UIButton *)sender{
- if (sender.selected == YES) return;
- sender.selected = YES;
-
- FKIdCardItem *item = [self.viewModel idCardItemAtIndex:sender.tag];
- if (item) {
- [FKIdCardManageRequest requestSetDefaultIdCardWithId:item.itemID delegate:self];
- [self.viewModel setDefaultAtIndex:sender.tag];
- self.viewModel.lastDefaultIndex = sender.tag;
- [self.tableView reloadData];
- }
- }
- - (void)clickEditBtn:(UIButton *)sender{
-
- FKIdCardItem *item = [self.viewModel idCardItemAtIndex:sender.tag];
- FKEditIdCardController *editVC = [[FKEditIdCardController alloc]initWithDefaultIdCard:item];
- [self.navigationController pushViewController:editVC animated:YES];
- }
- - (void)clickDeleteBtn:(UIButton *)sender{
-
- if (sender.tag < 0) return;
- FKIdCardItem *targetItem = [self.viewModel idCardItemAtIndex:sender.tag];
-
- if (targetItem.itemID.length) {
- [FKIdCardManageRequest requestDeleteIdCardWithId:targetItem.itemID delegate:self];
-
- if ([self.dataSource respondsToSelector:@selector(currentSelectedIDCard)]) {
- NSString *idCardNum = [self.dataSource currentSelectedIDCard];
- if (idCardNum.length > 0 && [idCardNum isEqualToString:targetItem.itemID]) {
- if ([self.delegate respondsToSelector:@selector(idCardManageController:didRemoveItem:)]) {
- [self.delegate idCardManageController:self didRemoveItem:targetItem];
- }
- }
- }
- }
- [self.viewModel deleteIdCardAtIndex:sender.tag];
- [self.tableView reloadData];
- }
- #pragma mark - property
- - (UITableView *)tableView
- {
- if (_tableView == nil) {
- _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
- _tableView.dataSource = self;
- _tableView.delegate = self;
- _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- _tableView.sectionFooterHeight = 0;
- _tableView.sectionHeaderHeight = 10;
- _tableView.backgroundColor = UIColorFromRGB(0xf4f4f4);
-
- if (@available(iOS 11.0, *)) {
- _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- }
- [_tableView registerClass:[FKIdCardManageCell class] forCellReuseIdentifier:ID_MANAGE_CELL_IDENTIFY];
- }
- return _tableView;
- }
- - (FKIdCardManageViewModel *)viewModel{
- if (_viewModel == nil) {
- _viewModel = [[FKIdCardManageViewModel alloc]init];
- }
- return _viewModel;
- }
- @end
|