RAOrderDetailViewController.m 11 KB

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