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