RAOrderDetailViewController+TableViewDataSource.m 9.6 KB

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