RAOrderDetailViewController.m 9.4 KB

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