RAOrderDetailViewController+TableViewDataSource.m 16 KB

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