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