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