RAOrderDetailViewController+TableViewDataSource.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. if (model.alert) {
  108. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:model.alertTitle message:model.alertMsg preferredStyle:UIAlertControllerStyleAlert];
  109. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  110. }];
  111. UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  112. [self handleActionForModel:model withSubCell:subCell];
  113. }];
  114. [alertVC addAction:cancelAction];
  115. [alertVC addAction:yesAction];
  116. [self presentViewController:alertVC animated:YES completion:nil];
  117. } else {
  118. [self handleActionForModel:model withSubCell:subCell];
  119. }
  120. }
  121. - (void)handleActionForModel:(RADetailActionModel *)model withSubCell:(RADetailActionSubCell *)subCell {
  122. switch (model.actionType) {
  123. case RADetailActionTypeRemote: {
  124. [self handleRemoteAction:model];
  125. }
  126. break;
  127. case RADetailActionTypeLocal: {
  128. switch (model.actionSubType) {
  129. case RADetailActionSubTypeEnum: {
  130. [self handleEnumAction:model forCell:subCell];
  131. }
  132. break;
  133. case RADetailActionSubTypeUpdate: {
  134. [self showUpdateForModel:model];
  135. }
  136. break;
  137. default:
  138. break;
  139. }
  140. }
  141. break;
  142. default:
  143. break;
  144. }
  145. }
  146. #pragma mark - Action
  147. - (void)handleEnumAction:(RADetailActionModel *)model forCell:(RADetailActionSubCell *)subCell {
  148. RADetailActionSelectionViewController *vc = [RADetailActionSelectionViewController viewControllerFromStoryboard];
  149. vc.actions = model.enums;
  150. vc.delegate = self;
  151. vc.modalPresentationStyle = UIModalPresentationPopover;
  152. vc.preferredContentSize = CGSizeMake(250, 300);
  153. vc.popoverPresentationController.sourceView = subCell;
  154. vc.popoverPresentationController.sourceRect = subCell.bounds;
  155. vc.popoverPresentationController.delegate = self;
  156. [self presentViewController:vc animated:YES completion:nil];
  157. }
  158. - (void)handleRemoteAction:(RADetailActionModel *)model {
  159. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  160. __weak typeof(self) weakSelf = self;
  161. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  162. NSDictionary *json = [RADataProvider reportAcionToURL:model.url withParams:model.params];
  163. dispatch_async(dispatch_get_main_queue(), ^{
  164. // dismiss progress
  165. [hud dismiss];
  166. if (weakSelf) {
  167. __strong typeof(weakSelf) strongSelf = weakSelf;
  168. int result = [[json objectForKey:@"result"] intValue];
  169. if (result == RESULT_TRUE) {
  170. if (model.actionSubType == RADetailActionSubTypeAccept) {
  171. RASingleton.sharedInstance.requiredLocation = YES;
  172. }
  173. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationReloadHome object:nil];
  174. [strongSelf.navigationController popToRootViewControllerAnimated:YES];
  175. } else {
  176. // process error
  177. NSString *msg = [json objectForKey:@"err_msg"];
  178. [strongSelf showAlertTilte:@"Warning" message:msg];
  179. }
  180. }
  181. });
  182. });
  183. }
  184. - (void)showUpdateForModel:(RADetailActionModel *)model {
  185. if (model.actionSubType != RADetailActionSubTypeUpdate) {
  186. return;
  187. }
  188. RAOrderEditViewController *vc = [RAOrderEditViewController viewControllerFromStoryboard];
  189. vc.title = model.actionTitle;
  190. vc.orderID = self.orderID;
  191. vc.actionID = model.actionID;
  192. vc.actionTitle = model.actionTitle;
  193. vc.orderType2 = self.orderType2;
  194. [self.navigationController pushViewController:vc animated:YES];
  195. }
  196. #pragma mark - Popover Delegate
  197. -(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
  198. return UIModalPresentationNone;//不适配
  199. }
  200. #pragma mark - ActionSelection Delegate
  201. - (void)detailSelectAction:(RADetailActionSelectionModel *)model {
  202. RAOrderEditViewController *vc = [RAOrderEditViewController viewControllerFromStoryboard];
  203. // RAOrderEditViewController *vc = [[RAOrderEditViewController alloc] init]; // 使用代码直接创建,在Push动画过程中会卡一下,具体原因不明
  204. vc.title = model.actionTitle;
  205. vc.orderID = self.orderID;
  206. vc.actionID = model.actionID;
  207. vc.actionTitle = model.actionTitle;
  208. vc.orderType2 = self.orderType2;
  209. [self.navigationController pushViewController:vc animated:YES];
  210. }
  211. #pragma mark - Private
  212. - (void)instance:(id)obj playSEL:(SEL)selector parameters:(NSArray *)params {
  213. if (!obj || !selector) {
  214. return;
  215. }
  216. if ([obj respondsToSelector:selector]) {
  217. NSMethodSignature *signature = [[obj class] instanceMethodSignatureForSelector:selector];
  218. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
  219. [invocation setTarget:obj];
  220. [invocation setSelector:selector];
  221. if (params && params.count > 0) {
  222. for (int i = 0; i < params.count; i++) {
  223. NSObject *obj = params[i];
  224. [invocation setArgument:&obj atIndex:i+2];
  225. }
  226. [invocation retainArguments]; // 防止参数被释放
  227. }
  228. [invocation invoke];
  229. }
  230. }
  231. @end