RAOrderDetailViewController+TableViewDataSource.m 13 KB

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