RAOrderDetailViewController+TableViewDataSource.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. //
  2. // RAOrderDetailViewController+TableViewDataSource.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+TableViewDataSource.h"
  9. #import "RADetailBaseModel.h"
  10. #import "RADetailSingleLineModel.h"
  11. #import "RADetailMultLineModel.h"
  12. #import "RADetailLocationModel.h"
  13. #import "RADetailActionCollectionModel.h"
  14. #import "RADetailActionModel.h"
  15. #import "RADetailSignatureModel.h"
  16. #import "RADetailSingleLineCell.h"
  17. #import "RADetailMultLineCell.h"
  18. #import "RADetailActionSubCell.h"
  19. #import <MapKit/MapKit.h>
  20. #import "RADetailActionSelectionModel.h"
  21. #import "RAOrderEditViewController.h"
  22. #import "RAProgressHUD.h"
  23. #import <AddressBook/AddressBook.h>
  24. //#import <Contacts/Contacts.h>
  25. #import "RADetailMapCell.h"
  26. #import "RADetailPhotoModel.h"
  27. #import "RADetailMapModel.h"
  28. #import "RAPhotoPreviewController.h"
  29. @implementation RAOrderDetailViewController (TableViewDataSource)
  30. - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
  31. RADetailBaseModel *model = [self modelForIndexPath:indexPath];
  32. RAOrderDetailValueType type = model.type;
  33. switch (type) {
  34. case RAOrderDetailValueTypeSingleLine: {
  35. RADetailSingleLineModel *singleModel = (RADetailSingleLineModel *)model;
  36. RADetailSingleLineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailSingleLineCell" forIndexPath:indexPath];
  37. [cell setModel:singleModel];
  38. return cell;
  39. }
  40. break;
  41. case RAOrderDetailValueTypeMultipleLine: {
  42. RADetailMultLineModel *multModel = (RADetailMultLineModel *)model;
  43. RADetailMultLineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailMultLineCell" forIndexPath:indexPath];
  44. [cell setModel:multModel];
  45. return cell;
  46. }
  47. break;
  48. case RAOrderDetailValueTypeAction: {
  49. RADetailActionCollectionModel *actionModel = (RADetailActionCollectionModel *)model;
  50. RADetailActionsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailActionsCell" forIndexPath:indexPath];
  51. [cell setModel:actionModel];
  52. cell.delegate = self;
  53. return cell;
  54. }
  55. break;
  56. case RAOrderDetailValueTypeLocation: {
  57. RADetailLocationModel *locationModel = (RADetailLocationModel *)model;
  58. RADetailLocationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailLocationCell" forIndexPath:indexPath];
  59. [cell setModel:locationModel];
  60. cell.delegate = self;
  61. return cell;
  62. }
  63. break;
  64. case RAOrderDetailValueTypeMap: {
  65. RADetailMapCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailMapCell" forIndexPath:indexPath];
  66. RADetailMapModel *mapModel = (RADetailMapModel *)model;
  67. [cell setModel:mapModel];
  68. return cell;
  69. }
  70. break;
  71. case RAOrderDetailValueTypePhoto: {
  72. RADetailPhotoModel *photoModel = (RADetailPhotoModel *)model;
  73. RADetailPhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailPhotoCell" forIndexPath:indexPath];
  74. [cell setModel:photoModel];
  75. cell.delegate = self;
  76. return cell;
  77. }
  78. break;
  79. case RAOrderDetailValueTypeSignature: {
  80. RADetailSignatureModel *signatureModel = (RADetailSignatureModel *)model;
  81. RADetailSignatureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailSignatureCell" forIndexPath:indexPath];
  82. [cell setModel:signatureModel];
  83. cell.delegate = self;
  84. return cell;
  85. }
  86. break;
  87. default:
  88. break;
  89. }
  90. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"detaulCell"];
  91. return cell;
  92. }
  93. - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  94. return [self numberOfItemForSection:section];
  95. }
  96. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  97. return [self sectionNumber];
  98. }
  99. #pragma mark - Photo/Signature
  100. - (void)previewImage:(UIImage *)img {
  101. RAPhotoPreviewController *previewVC = [RAPhotoPreviewController viewControllerFromStoryboard];
  102. previewVC.image = img;
  103. previewVC.canDelete = NO;
  104. [self.navigationController pushViewController:previewVC animated:YES];
  105. }
  106. - (void)photoCell:(RADetailPhotoCell *)cell didClickPhoto:(RADetailPhotoModel *)model {
  107. [self previewImage:model.photo];
  108. }
  109. - (void)signatureCell:(RADetailSignatureCell *)cell didClickSignature:(RADetailSignatureModel *)model {
  110. [self previewImage:model.signature];
  111. }
  112. #pragma mark - Location Delegate
  113. - (void)locationCell:(RADetailLocationCell *)cell didClickNavigation:(RADetailLocationModel *)model {
  114. if (model.street == nil) {
  115. return;
  116. }
  117. // @"东大街芷泉段6号"
  118. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  119. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
  120. UIAlertAction *googleMapAction = [UIAlertAction actionWithTitle:@"Google Map" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  121. NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?q=%@&directionsmode=driving",model.street]
  122. stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  123. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
  124. NSLog(@"%u",success);
  125. }];
  126. // NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?center=37.782652,-122.410126&directionsmode=driving"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  127. // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
  128. }];
  129. [alertVC addAction:googleMapAction];
  130. }
  131. UIAlertAction *appleMapAction = [UIAlertAction actionWithTitle:@"Apple Map" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  132. MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
  133. // MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(37.782652, -122.410126) addressDictionary:nil]];
  134. MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:kCLLocationCoordinate2DInvalid
  135. addressDictionary:@{
  136. (__bridge id)kABPersonAddressStreetKey : model.street
  137. }]];
  138. [MKMapItem openMapsWithItems:@[currentLocation, toLocation] launchOptions:@{
  139. MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
  140. MKLaunchOptionsShowsTrafficKey: @(YES)
  141. }];
  142. }];
  143. [alertVC addAction:appleMapAction];
  144. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  145. }];
  146. [alertVC addAction:cancelAction];
  147. [self presentViewController:alertVC animated:YES completion:nil];
  148. }
  149. #pragma mark - Action Delegate
  150. - (void)actionsCell:(RADetailActionsCell *)cell didClickSubCell:(RADetailActionSubCell *)subCell forModel:(RADetailActionModel *)model {
  151. if (model.alert) {
  152. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:model.alertTitle message:model.alertMsg preferredStyle:UIAlertControllerStyleAlert];
  153. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  154. }];
  155. UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  156. [self handleActionForModel:model withSubCell:subCell];
  157. }];
  158. [alertVC addAction:cancelAction];
  159. [alertVC addAction:yesAction];
  160. [self presentViewController:alertVC animated:YES completion:nil];
  161. } else {
  162. [self handleActionForModel:model withSubCell:subCell];
  163. }
  164. }
  165. - (void)handleActionForModel:(RADetailActionModel *)model withSubCell:(RADetailActionSubCell *)subCell {
  166. switch (model.actionType) {
  167. case RADetailActionTypeRemote: {
  168. [self handleRemoteAction:model];
  169. }
  170. break;
  171. case RADetailActionTypeLocal: {
  172. switch (model.actionSubType) {
  173. case RADetailActionSubTypeEnum: {
  174. [self handleEnumAction:model forCell:subCell];
  175. }
  176. break;
  177. case RADetailActionSubTypeUpdate: {
  178. [self showUpdateForModel:model];
  179. }
  180. break;
  181. default:
  182. break;
  183. }
  184. }
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. #pragma mark - Action
  191. - (void)handleEnumAction:(RADetailActionModel *)model forCell:(RADetailActionSubCell *)subCell {
  192. RADetailActionSelectionViewController *vc = [RADetailActionSelectionViewController viewControllerFromStoryboard];
  193. vc.actions = model.enums;
  194. vc.delegate = self;
  195. vc.modalPresentationStyle = UIModalPresentationPopover;
  196. vc.preferredContentSize = CGSizeMake(250, 300);
  197. vc.popoverPresentationController.sourceView = subCell;
  198. vc.popoverPresentationController.sourceRect = subCell.bounds;
  199. vc.popoverPresentationController.delegate = self;
  200. [self presentViewController:vc animated:YES completion:nil];
  201. }
  202. - (void)handleRemoteAction:(RADetailActionModel *)model {
  203. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  204. __weak typeof(self) weakSelf = self;
  205. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  206. NSDictionary *json = [RADataProvider reportAcionToURL:model.url withParams:model.params];
  207. dispatch_async(dispatch_get_main_queue(), ^{
  208. // dismiss progress
  209. [hud dismiss];
  210. if (weakSelf) {
  211. __strong typeof(weakSelf) strongSelf = weakSelf;
  212. int result = [[json objectForKey:@"result"] intValue];
  213. if (result == RESULT_TRUE) {
  214. if (model.actionSubType == RADetailActionSubTypeAccept) {
  215. RASingleton.sharedInstance.requiredLocation = YES;
  216. }
  217. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationReloadHome object:nil];
  218. [strongSelf.navigationController popToRootViewControllerAnimated:YES];
  219. } else {
  220. // process error
  221. NSString *msg = [json objectForKey:@"err_msg"];
  222. [strongSelf showAlertTilte:@"Warning" message:msg];
  223. }
  224. }
  225. });
  226. });
  227. }
  228. - (void)showUpdateForModel:(RADetailActionModel *)model {
  229. if (model.actionSubType != RADetailActionSubTypeUpdate) {
  230. return;
  231. }
  232. RAOrderEditViewController *vc = [RAOrderEditViewController viewControllerFromStoryboard];
  233. vc.title = model.actionTitle;
  234. vc.orderID = self.orderID;
  235. vc.actionID = model.actionID;
  236. vc.actionTitle = model.actionTitle;
  237. vc.orderType2 = self.orderType2;
  238. [self.navigationController pushViewController:vc animated:YES];
  239. }
  240. #pragma mark - Popover Delegate
  241. -(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
  242. return UIModalPresentationNone;//不适配
  243. }
  244. #pragma mark - ActionSelection Delegate
  245. - (void)detailSelectAction:(RADetailActionSelectionModel *)model {
  246. RAOrderEditViewController *vc = [RAOrderEditViewController viewControllerFromStoryboard];
  247. // RAOrderEditViewController *vc = [[RAOrderEditViewController alloc] init]; // 使用代码直接创建,在Push动画过程中会卡一下,具体原因不明
  248. vc.title = model.actionTitle;
  249. vc.orderID = self.orderID;
  250. vc.actionID = model.actionID;
  251. vc.actionTitle = model.actionTitle;
  252. vc.orderType2 = self.orderType2;
  253. [self.navigationController pushViewController:vc animated:YES];
  254. }
  255. #pragma mark - Private
  256. - (void)instance:(id)obj playSEL:(SEL)selector parameters:(NSArray *)params {
  257. if (!obj || !selector) {
  258. return;
  259. }
  260. if ([obj respondsToSelector:selector]) {
  261. NSMethodSignature *signature = [[obj class] instanceMethodSignatureForSelector:selector];
  262. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
  263. [invocation setTarget:obj];
  264. [invocation setSelector:selector];
  265. if (params && params.count > 0) {
  266. for (int i = 0; i < params.count; i++) {
  267. NSObject *obj = params[i];
  268. [invocation setArgument:&obj atIndex:i+2];
  269. }
  270. [invocation retainArguments]; // 防止参数被释放
  271. }
  272. [invocation invoke];
  273. }
  274. }
  275. @end