// // RAOrderDetailViewController.m // Apex And Drivers // // Created by Jack on 2018/6/2. // Copyright © 2018年 USAI. All rights reserved. // #import "RAOrderDetailViewController.h" #import "RADetailBaseModel.h" #import "RADetailSingleLineModel.h" #import "RADetailMultLineModel.h" #import "RADetailLocationModel.h" #import "RADetailActionCollectionModel.h" #import "RADetailMapModel.h" #import "RADetailPhotoModel.h" #import "RADetailSignatureModel.h" #import "RADetailMultPhotoModel.h" #import "RAProgressHUD.h" #import "UIScrollView+Empty.h" #import "RAEmptyView.h" #import "RAPhotoCell.h" @interface RAOrderDetailSectionModel : NSObject @property (nonatomic,strong) NSArray *values; @property (nonatomic,copy) NSString *title; @property (nonatomic,assign) CGFloat tableWidth; - (NSInteger)itemCount; @end @implementation RAOrderDetailSectionModel - (instancetype)initWithTableViewWidth:(CGFloat)width { if (self = [super init]) { self.tableWidth = width; } return self; } - (void)setValues:(NSArray *)values { // NSMutableArray *tmpItems = [values mutableCopy]; // // NSString *url_0 = @"http://a.hiphotos.baidu.com/image/pic/item/4a36acaf2edda3ccc4a53e450ce93901213f9216.jpg"; // NSString *url_1 = @"http://g.hiphotos.baidu.com/image/pic/item/5243fbf2b211931376d158d568380cd790238dc1.jpg"; // NSString *url_2 = @"http://b.hiphotos.baidu.com/image/pic/item/0b46f21fbe096b631f4b3b3301338744ebf8ac07.jpg"; // NSString *url_3 = @"http://b.hiphotos.baidu.com/image/pic/item/80cb39dbb6fd5266f9aeaf69a618972bd50736c1.jpg"; // NSString *url_4 = @"http://e.hiphotos.baidu.com/image/pic/item/4b90f603738da977f53a9d57bd51f8198618e3b1.jpg"; // // [tmpItems addObject:@{ // @"type" : @7, // @"title" : @"Container Photo", // @"photos" : @[@{@"url":url_0},@{@"url":url_1},@{@"url":url_2},@{@"url":url_3},@{@"url":url_4}], // @"key" : @"test_photo" // }]; // // values = tmpItems; NSMutableArray *modelArr = [NSMutableArray array]; for (int i = 0; i < values.count; i++) { NSDictionary *value = [values objectAtIndex:i]; RAOrderDetailValueType type = [[value objectForKey:@"type"] intValue]; switch (type) { case RAOrderDetailValueTypeSingleLine: { RADetailSingleLineModel *model = [[RADetailSingleLineModel alloc] init]; model.width = self.tableWidth; [model setValuesForKeysWithDictionary:value]; [modelArr addObject:model]; } break; case RAOrderDetailValueTypeMultipleLine: { RADetailMultLineModel *model = [[RADetailMultLineModel alloc] init]; model.width = self.tableWidth; [model setValuesForKeysWithDictionary:value]; [modelArr addObject:model]; } break; case RAOrderDetailValueTypeAction: { RADetailActionCollectionModel *model = [[RADetailActionCollectionModel alloc] init]; model.width = self.tableWidth; [model setValuesForKeysWithDictionary:value]; [modelArr addObject:model]; } break; case RAOrderDetailValueTypeLocation: { RADetailLocationModel *model = [[RADetailLocationModel alloc] init]; model.width = self.tableWidth; [model setValuesForKeysWithDictionary:value]; [modelArr addObject:model]; } break; case RAOrderDetailValueTypeMap: { RADetailMapModel *model = [[RADetailMapModel alloc] init]; model.width = self.tableWidth; [model setValuesForKeysWithDictionary:value]; [modelArr addObject:model]; } break; case RAOrderDetailValueTypePhoto: { RADetailPhotoModel *model = [[RADetailPhotoModel alloc] init]; model.width = self.tableWidth; [model setValuesForKeysWithDictionary:value]; [modelArr addObject:model]; } break; case RAOrderDetailValueTypeSignature: { RADetailSignatureModel *model = [[RADetailSignatureModel alloc] init]; model.width = self.tableWidth; [model setValuesForKeysWithDictionary:value]; [modelArr addObject:model]; } break; case RAOrderDetailValueTypeMultiplePhoto: { RADetailMultPhotoModel *model = [[RADetailMultPhotoModel alloc] init]; model.width = self.tableWidth; [model setValuesForKeysWithDictionary:value]; [modelArr addObject:model]; } default: break; } } _values = modelArr; } - (void)setValue:(id)value forUndefinedKey:(NSString *)key { } - (NSInteger)itemCount { return self.values.count; } - (RADetailBaseModel *)modelForIndex:(NSInteger)index { return [self.values objectAtIndex:index]; } @end #pragma mark - View Controller @interface RAOrderDetailViewController () @property (strong, nonatomic) IBOutlet UITableView *detailTableView; @property (nonatomic,strong) NSMutableArray *sectionArray; @property (nonatomic,strong) UIRefreshControl *refreshControl; @property (nonatomic,assign) BOOL firstRequestOk; @end @implementation RAOrderDetailViewController + (instancetype)viewControllerFromStoryboard { RAOrderDetailViewController *detailVC = [[UIStoryboard storyboardWithName:@"Detail" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]]; return detailVC; } - (void)viewDidLoad { [super viewDidLoad]; self.label_cache.text = NSLocalizedString(@"cache content", nil); [self.label_cache sizeToFit]; self.label_cache.layer.borderColor=ApexDriverGrayColor.CGColor; self.label_cache.layer.borderWidth = 1.0f; self.label_cache.layer.cornerRadius = 5.0f; self.label_cache.layer.masksToBounds = YES; // Do any additional setup after loading the view. [self configureTable]; [self loadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Config - (void)configureTable { if (@available(iOS 11.0, *)) { self.detailTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { self.automaticallyAdjustsScrollViewInsets = NO; } self.detailTableView.tableFooterView = [UIView new]; self.detailTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0); UIRefreshControl *refresh = [[UIRefreshControl alloc] init]; [refresh addTarget:self action:@selector(refreshControlValueChanged:) forControlEvents:UIControlEventValueChanged]; [self.detailTableView addSubview:refresh]; self.refreshControl = refresh; // empty __weak typeof(self) weakSelf = self; self.detailTableView.emptyView = [RAEmptyView emptyViewWithTapBlk:^(id sender) { [weakSelf loadData]; }]; [RAPhotoCell regist2TableView:self.detailTableView]; } #pragma mark - Action - (void)refreshControlValueChanged:(UIRefreshControl *)refresh { self.label_cache.hidden=true; [self loadData]; } #pragma mark - Data - (void)reloadData { self.label_cache.hidden=true; [self loadData]; } - (void)loadData { if (self.loading) { return; } self.loading = YES; [self.detailTableView hideEmpty]; // show progress RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view]; __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSDictionary *json = [RADataProvider requestOrderDetail:self.orderID type:self.orderType type2:self.orderType2 statusNo:self.status_no]; dispatch_async(dispatch_get_main_queue(), ^{ // dismiss progress [hud dismiss]; if (weakSelf.refreshControl.isRefreshing) { [weakSelf.refreshControl endRefreshing]; } if (weakSelf) { __strong typeof(weakSelf) strongSelf = weakSelf; int result = [[json objectForKey:@"result"] intValue]; if (result == RESULT_TRUE) { if (!strongSelf.firstRequestOk) { // 刷新Home或More的backendFlag圆点 [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationCheckDetail object:nil userInfo:@{@"order_id":strongSelf.orderID}]; strongSelf.firstRequestOk = YES; } NSArray *sectionArray = [json objectForKey:@"sections"]; [strongSelf.sectionArray removeAllObjects]; CGFloat width = CGRectGetWidth(strongSelf.detailTableView.bounds); for (int i = 0; i < sectionArray.count; i++) { NSDictionary *section = [sectionArray objectAtIndex:i]; RAOrderDetailSectionModel *sectionModel = [[RAOrderDetailSectionModel alloc] initWithTableViewWidth:width]; [sectionModel setValuesForKeysWithDictionary:section]; [strongSelf.sectionArray addObject:sectionModel]; } if([json[@"iscache"] boolValue]==true) { self.label_cache.hidden=false; } [strongSelf.detailTableView reloadData]; } else { [strongSelf.sectionArray removeAllObjects]; strongSelf.detailTableView.contentOffset = CGPointZero; [strongSelf.detailTableView reloadData]; // process error NSString *msg = [json objectForKey:@"err_msg"]; // [strongSelf showAlert:msg]; [strongSelf showAlertTilte:NSLocalizedString(@"Warning", nil) message:msg]; } } weakSelf.loading = NO; if (weakSelf.sectionNumber == 0) { [weakSelf.detailTableView showEmpty]; } else { [weakSelf.detailTableView hideEmpty]; } }); }); } #pragma mark - Getter - (UITableView *)tableView { return self.detailTableView; } - (NSMutableArray *)sectionArray { if (!_sectionArray) { _sectionArray = [NSMutableArray array]; } return _sectionArray; } - (NSInteger)sectionNumber { return self.sectionArray.count; } - (NSInteger)numberOfItemForSection:(NSInteger)section { RAOrderDetailSectionModel *model = [[self sectionArray] objectAtIndex:section]; return [model itemCount]; } - (RADetailBaseModel *)modelForIndexPath:(NSIndexPath *)indexPath { RAOrderDetailSectionModel *model = [[self sectionArray] objectAtIndex:indexPath.section]; return [model modelForIndex:indexPath.row]; } - (NSString *)titleForSection:(NSInteger)section { RAOrderDetailSectionModel *model = [[self sectionArray] objectAtIndex:section]; return model.title; } @end