RAOrderDetailViewController.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. @property (nonatomic,strong) UIRefreshControl *refreshControl;
  82. @end
  83. @implementation RAOrderDetailViewController
  84. + (instancetype)viewControllerFromStoryboard {
  85. RAOrderDetailViewController *detailVC = [[UIStoryboard storyboardWithName:@"Detail" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
  86. return detailVC;
  87. }
  88. - (void)viewDidLoad {
  89. [super viewDidLoad];
  90. // Do any additional setup after loading the view.
  91. [self configureTable];
  92. [self loadData];
  93. }
  94. - (void)didReceiveMemoryWarning {
  95. [super didReceiveMemoryWarning];
  96. // Dispose of any resources that can be recreated.
  97. }
  98. - (void)configureTable {
  99. if (@available(iOS 11.0, *)) {
  100. self.detailTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  101. } else {
  102. self.automaticallyAdjustsScrollViewInsets = NO;
  103. }
  104. self.detailTableView.tableFooterView = [UIView new];
  105. self.detailTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  106. UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
  107. [refresh addTarget:self action:@selector(refreshControlValueChanged:) forControlEvents:UIControlEventValueChanged];
  108. [self.detailTableView addSubview:refresh];
  109. self.refreshControl = refresh;
  110. }
  111. #pragma mark - Action
  112. - (void)refreshControlValueChanged:(UIRefreshControl *)refresh {
  113. [self loadData];
  114. }
  115. #pragma mark - Data
  116. - (void)loadData {
  117. if (self.loading) {
  118. return;
  119. }
  120. self.loading = YES;
  121. // show progress
  122. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  123. __weak typeof(self) weakSelf = self;
  124. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  125. NSDictionary *json = [RADataProvider requestOrderDetail:self.orderID type:self.orderType type2:self.orderType2];
  126. dispatch_async(dispatch_get_main_queue(), ^{
  127. // dismiss progress
  128. [hud dismiss];
  129. if (weakSelf.refreshControl.isRefreshing) {
  130. [weakSelf.refreshControl endRefreshing];
  131. }
  132. if (weakSelf) {
  133. __strong typeof(weakSelf) strongSelf = weakSelf;
  134. int result = [[json objectForKey:@"result"] intValue];
  135. if (result == RESULT_TRUE) {
  136. NSArray *sectionArray = [json objectForKey:@"sections"];
  137. [strongSelf.sectionArray removeAllObjects];
  138. CGFloat width = CGRectGetWidth(strongSelf.detailTableView.bounds);
  139. for (int i = 0; i < sectionArray.count; i++) {
  140. NSDictionary *section = [sectionArray objectAtIndex:i];
  141. RAOrderDetailSectionModel *sectionModel = [[RAOrderDetailSectionModel alloc] initWithTableViewWidth:width];
  142. [sectionModel setValuesForKeysWithDictionary:section];
  143. [strongSelf.sectionArray addObject:sectionModel];
  144. }
  145. [strongSelf.detailTableView reloadData];
  146. } else {
  147. [strongSelf.sectionArray removeAllObjects];
  148. strongSelf.detailTableView.contentOffset = CGPointZero;
  149. [strongSelf.detailTableView reloadData];
  150. // process error
  151. NSString *msg = [json objectForKey:@"err_msg"];
  152. // [strongSelf showAlert:msg];
  153. [strongSelf showAlertTilte:@"Warning" message:msg];
  154. }
  155. }
  156. self.loading = NO;
  157. });
  158. });
  159. }
  160. #pragma mark - Getter
  161. - (NSMutableArray *)sectionArray {
  162. if (!_sectionArray) {
  163. _sectionArray = [NSMutableArray array];
  164. }
  165. return _sectionArray;
  166. }
  167. - (NSInteger)sectionNumber {
  168. return self.sectionArray.count;
  169. }
  170. - (NSInteger)numberOfItemForSection:(NSInteger)section {
  171. RAOrderDetailSectionModel *model = [[self sectionArray] objectAtIndex:section];
  172. return [model itemCount];
  173. }
  174. - (RADetailBaseModel *)modelForIndexPath:(NSIndexPath *)indexPath {
  175. RAOrderDetailSectionModel *model = [[self sectionArray] objectAtIndex:indexPath.section];
  176. return [model modelForIndex:indexPath.row];
  177. }
  178. - (NSString *)titleForSection:(NSInteger)section {
  179. RAOrderDetailSectionModel *model = [[self sectionArray] objectAtIndex:section];
  180. return model.title;
  181. }
  182. @end