RAOrderDetailViewController+TableViewDataSource.m 17 KB

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