RAOrderDetailViewController.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // RAOrderDetailViewController.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/6/2.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RAOrderDetailViewController.h"
  9. #import "RADetailBaseModel.h"
  10. #import "RADetailSingleLineModel.h"
  11. #import "RADetailMultLineModel.h"
  12. #import "RADetailLocationModel.h"
  13. #import "RADetailActionCollectionModel.h"
  14. @interface RAOrderDetailSectionModel : NSObject
  15. @property (nonatomic,strong) NSArray <RADetailBaseModel *> *values;
  16. @property (nonatomic,copy) NSString *title;
  17. @property (nonatomic,assign) CGFloat tableWidth;
  18. - (NSInteger)itemCount;
  19. @end
  20. @implementation RAOrderDetailSectionModel
  21. - (instancetype)initWithTableViewWidth:(CGFloat)width {
  22. if (self = [super init]) {
  23. self.tableWidth = width;
  24. }
  25. return self;
  26. }
  27. - (void)setValues:(NSArray *)values {
  28. NSMutableArray *modelArr = [NSMutableArray array];
  29. for (int i = 0; i < values.count; i++) {
  30. NSDictionary *value = [values objectAtIndex:i];
  31. RAOrderDetailValueType type = [[value objectForKey:@"type"] intValue];
  32. switch (type) {
  33. case RAOrderDetailValueTypeSingleLine: {
  34. RADetailSingleLineModel *model = [[RADetailSingleLineModel alloc] init];
  35. model.width = self.tableWidth;
  36. [model setValuesForKeysWithDictionary:value];
  37. [modelArr addObject:model];
  38. }
  39. break;
  40. case RAOrderDetailValueTypeMultipleLine: {
  41. RADetailMultLineModel *model = [[RADetailMultLineModel alloc] init];
  42. model.width = self.tableWidth;
  43. [model setValuesForKeysWithDictionary:value];
  44. [modelArr addObject:model];
  45. }
  46. break;
  47. case RAOrderDetailValueTypeAction: {
  48. RADetailActionCollectionModel *model = [[RADetailActionCollectionModel alloc] init];
  49. model.width = self.tableWidth;
  50. [model setValuesForKeysWithDictionary:value];
  51. [modelArr addObject:model];
  52. }
  53. break;
  54. case RAOrderDetailValueTypeLocation: {
  55. RADetailLocationModel *model = [[RADetailLocationModel alloc] init];
  56. model.width = self.tableWidth;
  57. [model setValuesForKeysWithDictionary:value];
  58. [modelArr addObject:model];
  59. }
  60. break;
  61. default:
  62. break;
  63. }
  64. }
  65. _values = modelArr;
  66. }
  67. - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
  68. }
  69. - (NSInteger)itemCount {
  70. return self.values.count;
  71. }
  72. - (RADetailBaseModel *)modelForIndex:(NSInteger)index {
  73. return [self.values objectAtIndex:index];
  74. }
  75. @end
  76. #pragma mark - View Controller
  77. @interface RAOrderDetailViewController ()
  78. @property (strong, nonatomic) IBOutlet UITableView *detailTableView;
  79. @property (nonatomic,strong) NSMutableArray <RAOrderDetailSectionModel *> *sectionArray;
  80. @end
  81. @implementation RAOrderDetailViewController
  82. + (instancetype)viewControllerFromStoryboard {
  83. RAOrderDetailViewController *detailVC = [[UIStoryboard storyboardWithName:@"Detail" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
  84. return detailVC;
  85. }
  86. - (void)viewDidLoad {
  87. [super viewDidLoad];
  88. // Do any additional setup after loading the view.
  89. [self configureTable];
  90. [self loadData];
  91. }
  92. - (void)didReceiveMemoryWarning {
  93. [super didReceiveMemoryWarning];
  94. // Dispose of any resources that can be recreated.
  95. }
  96. - (void)configureTable {
  97. self.detailTableView.tableFooterView = [UIView new];
  98. self.detailTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  99. }
  100. #pragma mark - Data
  101. - (void)loadData {
  102. // show progress
  103. __weak typeof(self) weakSelf = self;
  104. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  105. NSDictionary *json = [RADataProvider requestOrderDetail:self.orderID];
  106. dispatch_async(dispatch_get_main_queue(), ^{
  107. // dismiss progress
  108. if (weakSelf) {
  109. __strong typeof(weakSelf) strongSelf = weakSelf;
  110. int result = [[json objectForKey:@"result"] intValue];
  111. if (result == RESULT_TRUE) {
  112. NSArray *sectionArray = [json objectForKey:@"sections"];
  113. [strongSelf.sectionArray removeAllObjects];
  114. CGFloat width = CGRectGetWidth(strongSelf.detailTableView.bounds);
  115. for (int i = 0; i < sectionArray.count; i++) {
  116. NSDictionary *section = [sectionArray objectAtIndex:i];
  117. RAOrderDetailSectionModel *sectionModel = [[RAOrderDetailSectionModel alloc] initWithTableViewWidth:width];
  118. [sectionModel setValuesForKeysWithDictionary:section];
  119. [strongSelf.sectionArray addObject:sectionModel];
  120. }
  121. [strongSelf.detailTableView reloadData];
  122. } else {
  123. // process error
  124. }
  125. }
  126. });
  127. });
  128. }
  129. #pragma mark - Getter
  130. - (NSMutableArray *)sectionArray {
  131. if (!_sectionArray) {
  132. _sectionArray = [NSMutableArray array];
  133. }
  134. return _sectionArray;
  135. }
  136. - (NSInteger)sectionNumber {
  137. return self.sectionArray.count;
  138. }
  139. - (NSInteger)numberOfItemForSection:(NSInteger)section {
  140. RAOrderDetailSectionModel *model = [[self sectionArray] objectAtIndex:section];
  141. return [model itemCount];
  142. }
  143. - (RADetailBaseModel *)modelForIndexPath:(NSIndexPath *)indexPath {
  144. RAOrderDetailSectionModel *model = [[self sectionArray] objectAtIndex:indexPath.section];
  145. return [model modelForIndex:indexPath.row];
  146. }
  147. - (NSString *)titleForSection:(NSInteger)section {
  148. RAOrderDetailSectionModel *model = [[self sectionArray] objectAtIndex:section];
  149. return model.title;
  150. }
  151. @end