RAOrderDetailViewController.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "RADetailMapModel.h"
  15. #import "RADetailPhotoModel.h"
  16. #import "RADetailSignatureModel.h"
  17. #import "RAProgressHUD.h"
  18. @interface RAOrderDetailSectionModel : NSObject
  19. @property (nonatomic,strong) NSArray <RADetailBaseModel *> *values;
  20. @property (nonatomic,copy) NSString *title;
  21. @property (nonatomic,assign) CGFloat tableWidth;
  22. - (NSInteger)itemCount;
  23. @end
  24. @implementation RAOrderDetailSectionModel
  25. - (instancetype)initWithTableViewWidth:(CGFloat)width {
  26. if (self = [super init]) {
  27. self.tableWidth = width;
  28. }
  29. return self;
  30. }
  31. - (void)setValues:(NSArray *)values {
  32. NSMutableArray *modelArr = [NSMutableArray array];
  33. for (int i = 0; i < values.count; i++) {
  34. NSDictionary *value = [values objectAtIndex:i];
  35. RAOrderDetailValueType type = [[value objectForKey:@"type"] intValue];
  36. switch (type) {
  37. case RAOrderDetailValueTypeSingleLine: {
  38. RADetailSingleLineModel *model = [[RADetailSingleLineModel alloc] init];
  39. model.width = self.tableWidth;
  40. [model setValuesForKeysWithDictionary:value];
  41. [modelArr addObject:model];
  42. }
  43. break;
  44. case RAOrderDetailValueTypeMultipleLine: {
  45. RADetailMultLineModel *model = [[RADetailMultLineModel alloc] init];
  46. model.width = self.tableWidth;
  47. [model setValuesForKeysWithDictionary:value];
  48. [modelArr addObject:model];
  49. }
  50. break;
  51. case RAOrderDetailValueTypeAction: {
  52. RADetailActionCollectionModel *model = [[RADetailActionCollectionModel alloc] init];
  53. model.width = self.tableWidth;
  54. [model setValuesForKeysWithDictionary:value];
  55. [modelArr addObject:model];
  56. }
  57. break;
  58. case RAOrderDetailValueTypeLocation: {
  59. RADetailLocationModel *model = [[RADetailLocationModel alloc] init];
  60. model.width = self.tableWidth;
  61. [model setValuesForKeysWithDictionary:value];
  62. [modelArr addObject:model];
  63. }
  64. break;
  65. case RAOrderDetailValueTypeMap: {
  66. RADetailMapModel *model = [[RADetailMapModel alloc] init];
  67. model.width = self.tableWidth;
  68. [model setValuesForKeysWithDictionary:value];
  69. [modelArr addObject:model];
  70. }
  71. break;
  72. case RAOrderDetailValueTypePhoto: {
  73. RADetailPhotoModel *model = [[RADetailPhotoModel alloc] init];
  74. model.width = self.tableWidth;
  75. [model setValuesForKeysWithDictionary:value];
  76. [modelArr addObject:model];
  77. }
  78. break;
  79. case RAOrderDetailValueTypeSignature: {
  80. RADetailSignatureModel *model = [[RADetailSignatureModel alloc] init];
  81. model.width = self.tableWidth;
  82. [model setValuesForKeysWithDictionary:value];
  83. [modelArr addObject:model];
  84. }
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90. _values = modelArr;
  91. }
  92. - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
  93. }
  94. - (NSInteger)itemCount {
  95. return self.values.count;
  96. }
  97. - (RADetailBaseModel *)modelForIndex:(NSInteger)index {
  98. return [self.values objectAtIndex:index];
  99. }
  100. @end
  101. #pragma mark - View Controller
  102. @interface RAOrderDetailViewController ()
  103. @property (strong, nonatomic) IBOutlet UITableView *detailTableView;
  104. @property (nonatomic,strong) NSMutableArray <RAOrderDetailSectionModel *> *sectionArray;
  105. @property (nonatomic,strong) UIRefreshControl *refreshControl;
  106. @end
  107. @implementation RAOrderDetailViewController
  108. + (instancetype)viewControllerFromStoryboard {
  109. RAOrderDetailViewController *detailVC = [[UIStoryboard storyboardWithName:@"Detail" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
  110. return detailVC;
  111. }
  112. - (void)viewDidLoad {
  113. [super viewDidLoad];
  114. // Do any additional setup after loading the view.
  115. [self configureTable];
  116. [self loadData];
  117. }
  118. - (void)didReceiveMemoryWarning {
  119. [super didReceiveMemoryWarning];
  120. // Dispose of any resources that can be recreated.
  121. }
  122. - (void)configureTable {
  123. if (@available(iOS 11.0, *)) {
  124. self.detailTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  125. } else {
  126. self.automaticallyAdjustsScrollViewInsets = NO;
  127. }
  128. self.detailTableView.tableFooterView = [UIView new];
  129. self.detailTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  130. UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
  131. [refresh addTarget:self action:@selector(refreshControlValueChanged:) forControlEvents:UIControlEventValueChanged];
  132. [self.detailTableView addSubview:refresh];
  133. self.refreshControl = refresh;
  134. }
  135. #pragma mark - Action
  136. - (void)refreshControlValueChanged:(UIRefreshControl *)refresh {
  137. [self loadData];
  138. }
  139. #pragma mark - Data
  140. - (void)loadData {
  141. if (self.loading) {
  142. return;
  143. }
  144. self.loading = YES;
  145. // show progress
  146. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  147. __weak typeof(self) weakSelf = self;
  148. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  149. NSDictionary *json = [RADataProvider requestOrderDetail:self.orderID type:self.orderType type2:self.orderType2];
  150. dispatch_async(dispatch_get_main_queue(), ^{
  151. // dismiss progress
  152. [hud dismiss];
  153. if (weakSelf.refreshControl.isRefreshing) {
  154. [weakSelf.refreshControl endRefreshing];
  155. }
  156. if (weakSelf) {
  157. __strong typeof(weakSelf) strongSelf = weakSelf;
  158. int result = [[json objectForKey:@"result"] intValue];
  159. if (result == RESULT_TRUE) {
  160. NSArray *sectionArray = [json objectForKey:@"sections"];
  161. [strongSelf.sectionArray removeAllObjects];
  162. CGFloat width = CGRectGetWidth(strongSelf.detailTableView.bounds);
  163. for (int i = 0; i < sectionArray.count; i++) {
  164. NSDictionary *section = [sectionArray objectAtIndex:i];
  165. RAOrderDetailSectionModel *sectionModel = [[RAOrderDetailSectionModel alloc] initWithTableViewWidth:width];
  166. [sectionModel setValuesForKeysWithDictionary:section];
  167. [strongSelf.sectionArray addObject:sectionModel];
  168. }
  169. [strongSelf.detailTableView reloadData];
  170. } else {
  171. [strongSelf.sectionArray removeAllObjects];
  172. strongSelf.detailTableView.contentOffset = CGPointZero;
  173. [strongSelf.detailTableView reloadData];
  174. // process error
  175. NSString *msg = [json objectForKey:@"err_msg"];
  176. // [strongSelf showAlert:msg];
  177. [strongSelf showAlertTilte:@"Warning" message:msg];
  178. }
  179. }
  180. self.loading = NO;
  181. });
  182. });
  183. }
  184. #pragma mark - Getter
  185. - (NSMutableArray *)sectionArray {
  186. if (!_sectionArray) {
  187. _sectionArray = [NSMutableArray array];
  188. }
  189. return _sectionArray;
  190. }
  191. - (NSInteger)sectionNumber {
  192. return self.sectionArray.count;
  193. }
  194. - (NSInteger)numberOfItemForSection:(NSInteger)section {
  195. RAOrderDetailSectionModel *model = [[self sectionArray] objectAtIndex:section];
  196. return [model itemCount];
  197. }
  198. - (RADetailBaseModel *)modelForIndexPath:(NSIndexPath *)indexPath {
  199. RAOrderDetailSectionModel *model = [[self sectionArray] objectAtIndex:indexPath.section];
  200. return [model modelForIndex:indexPath.row];
  201. }
  202. - (NSString *)titleForSection:(NSInteger)section {
  203. RAOrderDetailSectionModel *model = [[self sectionArray] objectAtIndex:section];
  204. return model.title;
  205. }
  206. @end