RAOrderDetailViewController+TableViewDataSource.m 13 KB

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