RAOrderDetailViewController.m 6.1 KB

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