// // 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" @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 *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; 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; @end @implementation RAOrderDetailViewController + (instancetype)viewControllerFromStoryboard { RAOrderDetailViewController *detailVC = [[UIStoryboard storyboardWithName:@"Detail" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]]; return detailVC; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self configureTable]; [self loadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)configureTable { self.detailTableView.tableFooterView = [UIView new]; self.detailTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0); } #pragma mark - Data - (void)loadData { // show progress __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSDictionary *json = [RADataProvider requestOrderDetail:self.orderID]; dispatch_async(dispatch_get_main_queue(), ^{ // dismiss progress if (weakSelf) { __strong typeof(weakSelf) strongSelf = weakSelf; int result = [[json objectForKey:@"result"] intValue]; if (result == RESULT_TRUE) { 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]; } [strongSelf.detailTableView reloadData]; } else { // process error } } }); }); } #pragma mark - Getter - (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