RAOrderDetailViewController+TableViewDataSource.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "RADetailSingleLineCell.h"
  16. #import "RADetailMultLineCell.h"
  17. #import "RADetailActionSubCell.h"
  18. #import <MapKit/MapKit.h>
  19. #import "RADetailActionSelectionModel.h"
  20. #import "RAOrderEditViewController.h"
  21. #import "RAProgressHUD.h"
  22. #import <AddressBook/AddressBook.h>
  23. //#import <Contacts/Contacts.h>
  24. @implementation RAOrderDetailViewController (TableViewDataSource)
  25. - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
  26. RADetailBaseModel *model = [self modelForIndexPath:indexPath];
  27. RAOrderDetailValueType type = model.type;
  28. switch (type) {
  29. case RAOrderDetailValueTypeSingleLine: {
  30. RADetailSingleLineModel *singleModel = (RADetailSingleLineModel *)model;
  31. RADetailSingleLineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailSingleLineCell" forIndexPath:indexPath];
  32. [cell setModel:singleModel];
  33. return cell;
  34. }
  35. break;
  36. case RAOrderDetailValueTypeMultipleLine: {
  37. RADetailMultLineModel *multModel = (RADetailMultLineModel *)model;
  38. RADetailMultLineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailMultLineCell" forIndexPath:indexPath];
  39. [cell setModel:multModel];
  40. return cell;
  41. }
  42. break;
  43. case RAOrderDetailValueTypeAction: {
  44. RADetailActionCollectionModel *actionModel = (RADetailActionCollectionModel *)model;
  45. RADetailActionsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailActionsCell" forIndexPath:indexPath];
  46. [cell setModel:actionModel];
  47. cell.delegate = self;
  48. return cell;
  49. }
  50. break;
  51. case RAOrderDetailValueTypeLocation: {
  52. RADetailLocationModel *locationModel = (RADetailLocationModel *)model;
  53. RADetailLocationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailLocationCell" forIndexPath:indexPath];
  54. [cell setModel:locationModel];
  55. cell.delegate = self;
  56. return cell;
  57. }
  58. break;
  59. default:
  60. break;
  61. }
  62. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"detaulCell"];
  63. return cell;
  64. }
  65. - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  66. return [self numberOfItemForSection:section];
  67. }
  68. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  69. return [self sectionNumber];
  70. }
  71. #pragma mark - Location Delegate
  72. - (void)locationCell:(RADetailLocationCell *)cell didClickNavigation:(RADetailLocationModel *)model {
  73. // @"东大街芷泉段6号"
  74. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  75. if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
  76. UIAlertAction *googleMapAction = [UIAlertAction actionWithTitle:@"Google Map" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  77. NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?q=%@&directionsmode=driving",model.location]
  78. stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  79. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
  80. NSLog(@"%u",success);
  81. }];
  82. // NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?center=37.782652,-122.410126&directionsmode=driving"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  83. // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
  84. }];
  85. [alertVC addAction:googleMapAction];
  86. }
  87. UIAlertAction *appleMapAction = [UIAlertAction actionWithTitle:@"Apple Map" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  88. MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
  89. // MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(37.782652, -122.410126) addressDictionary:nil]];
  90. MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:kCLLocationCoordinate2DInvalid
  91. addressDictionary:@{
  92. (__bridge id)kABPersonAddressStreetKey : model.location
  93. }]];
  94. [MKMapItem openMapsWithItems:@[currentLocation, toLocation] launchOptions:@{
  95. MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,
  96. MKLaunchOptionsShowsTrafficKey: @(YES)
  97. }];
  98. }];
  99. [alertVC addAction:appleMapAction];
  100. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  101. }];
  102. [alertVC addAction:cancelAction];
  103. [self presentViewController:alertVC animated:YES completion:nil];
  104. }
  105. #pragma mark - Action Delegate
  106. - (void)actionsCell:(RADetailActionsCell *)cell didClickSubCell:(RADetailActionSubCell *)subCell forModel:(RADetailActionModel *)model {
  107. switch (model.actionType) {
  108. case RADetailActionTypeRemote: {
  109. [self handleRemoteAction:model];
  110. }
  111. break;
  112. case RADetailActionTypeLocal: {
  113. switch (model.actionSubType) {
  114. case RADetailActionSubTypeEnum: {
  115. [self handleEnumAction:model forCell:subCell];
  116. }
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. break;
  123. default:
  124. break;
  125. }
  126. }
  127. #pragma mark - Action
  128. - (void)handleEnumAction:(RADetailActionModel *)model forCell:(RADetailActionSubCell *)subCell {
  129. RADetailActionSelectionViewController *vc = [RADetailActionSelectionViewController viewControllerFromStoryboard];
  130. vc.actions = model.enums;
  131. vc.delegate = self;
  132. vc.modalPresentationStyle = UIModalPresentationPopover;
  133. vc.preferredContentSize = CGSizeMake(250, 300);
  134. vc.popoverPresentationController.sourceView = subCell;
  135. vc.popoverPresentationController.sourceRect = subCell.bounds;
  136. vc.popoverPresentationController.delegate = self;
  137. [self presentViewController:vc animated:YES completion:nil];
  138. }
  139. - (void)handleRemoteAction:(RADetailActionModel *)model {
  140. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  141. __weak typeof(self) weakSelf = self;
  142. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  143. NSDictionary *json = [RADataProvider reportAcionToURL:model.url withParams:model.params];
  144. dispatch_async(dispatch_get_main_queue(), ^{
  145. // dismiss progress
  146. [hud dismiss];
  147. if (weakSelf) {
  148. __strong typeof(weakSelf) strongSelf = weakSelf;
  149. int result = [[json objectForKey:@"result"] intValue];
  150. if (result == RESULT_TRUE) {
  151. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationReloadHome object:nil];
  152. [strongSelf.navigationController popViewControllerAnimated:YES];
  153. } else {
  154. // process error
  155. }
  156. }
  157. });
  158. });
  159. }
  160. #pragma mark - Popover Delegate
  161. -(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
  162. return UIModalPresentationNone;//不适配
  163. }
  164. #pragma mark - ActionSelection Delegate
  165. - (void)detailSelectAction:(RADetailActionSelectionModel *)model {
  166. RAOrderEditViewController *vc = [RAOrderEditViewController viewControllerFromStoryboard];
  167. // RAOrderEditViewController *vc = [[RAOrderEditViewController alloc] init]; // 使用代码直接创建,在Push动画过程中会卡一下,具体原因不明
  168. vc.title = model.detailActionTitle;
  169. vc.orderID = self.orderID;
  170. vc.actionID = model.actionID;
  171. vc.actionTitle = model.actionTitle;
  172. [self.navigationController pushViewController:vc animated:YES];
  173. }
  174. #pragma mark - Private
  175. - (void)instance:(id)obj playSEL:(SEL)selector parameters:(NSArray *)params {
  176. if (!obj || !selector) {
  177. return;
  178. }
  179. if ([obj respondsToSelector:selector]) {
  180. NSMethodSignature *signature = [[obj class] instanceMethodSignatureForSelector:selector];
  181. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
  182. [invocation setTarget:obj];
  183. [invocation setSelector:selector];
  184. if (params && params.count > 0) {
  185. for (int i = 0; i < params.count; i++) {
  186. NSObject *obj = params[i];
  187. [invocation setArgument:&obj atIndex:i+2];
  188. }
  189. [invocation retainArguments]; // 防止参数被释放
  190. }
  191. [invocation invoke];
  192. }
  193. }
  194. @end