// // RAOrderDetailViewController+TableViewDataSource.m // Apex And Drivers // // Created by Jack on 2018/6/2. // Copyright © 2018年 USAI. All rights reserved. // #import "RAOrderDetailViewController+TableViewDataSource.h" #import "RADetailBaseModel.h" #import "RADetailSingleLineModel.h" #import "RADetailMultLineModel.h" #import "RADetailLocationModel.h" #import "RADetailActionCollectionModel.h" #import "RADetailActionModel.h" #import "RADetailSignatureModel.h" #import "RADetailSingleLineCell.h" #import "RADetailMultLineCell.h" #import "RADetailActionSubCell.h" #import #import "RADetailActionSelectionModel.h" #import "RAOrderEditViewController.h" #import "RAProgressHUD.h" #import //#import #import "RADetailPhotoModel.h" #import "RADetailMapModel.h" #import "RAPhotoPreviewController.h" #import "RAPhotoCell.h" #import "RADetailMultPhotoModel.h" #import "RAPhotoItemModel.h" @implementation RAOrderDetailViewController (TableViewDataSource) - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { RADetailBaseModel *model = [self modelForIndexPath:indexPath]; RAOrderDetailValueType type = model.type; switch (type) { case RAOrderDetailValueTypeSingleLine: { RADetailSingleLineModel *singleModel = (RADetailSingleLineModel *)model; RADetailSingleLineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailSingleLineCell" forIndexPath:indexPath]; [cell setModel:singleModel]; return cell; } break; case RAOrderDetailValueTypeMultipleLine: { RADetailMultLineModel *multModel = (RADetailMultLineModel *)model; RADetailMultLineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailMultLineCell" forIndexPath:indexPath]; [cell setModel:multModel]; return cell; } break; case RAOrderDetailValueTypeAction: { RADetailActionCollectionModel *actionModel = (RADetailActionCollectionModel *)model; RADetailActionsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailActionsCell" forIndexPath:indexPath]; [cell setModel:actionModel]; cell.delegate = self; return cell; } break; case RAOrderDetailValueTypeLocation: { RADetailLocationModel *locationModel = (RADetailLocationModel *)model; RADetailLocationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailLocationCell" forIndexPath:indexPath]; [cell setModel:locationModel]; cell.delegate = self; return cell; } break; case RAOrderDetailValueTypeMap: { RADetailMapCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailMapCell" forIndexPath:indexPath]; RADetailMapModel *mapModel = (RADetailMapModel *)model; [cell setModel:mapModel]; cell.delegate = self; return cell; } break; case RAOrderDetailValueTypePhoto: { RADetailPhotoModel *photoModel = (RADetailPhotoModel *)model; RADetailPhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailPhotoCell" forIndexPath:indexPath]; [cell setModel:photoModel]; cell.delegate = self; return cell; } break; case RAOrderDetailValueTypeSignature: { RADetailSignatureModel *signatureModel = (RADetailSignatureModel *)model; RADetailSignatureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailSignatureCell" forIndexPath:indexPath]; [cell setModel:signatureModel]; cell.delegate = self; return cell; } break; case RAOrderDetailValueTypeMultiplePhoto: { RADetailMultPhotoModel *photoModel = (RADetailMultPhotoModel *)model; RAPhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:RAPhotoCell.reuseId forIndexPath:indexPath]; cell.model = photoModel.model; cell.delegate = self; return cell; } break; default: break; } UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"detaulCell"]; return cell; } - (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self numberOfItemForSection:section]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [self sectionNumber]; } #pragma mark - Map - (void)openMapWithTitle:(NSString *)title model:(RADetailMapModel *)model { if (model == nil) { return; } CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([model.lat doubleValue], [model.lon doubleValue]); MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]]; toLocation.name = title; [MKMapItem openMapsWithItems:@[toLocation] launchOptions:@{ MKLaunchOptionsMapCenterKey : [NSValue valueWithMKCoordinate:coordinate] }]; } - (void)mapCell:(RADetailMapCell *)cell clickMapButtonWithModel:(RADetailMapModel *)model { NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; NSString *title = [self titleForSection:indexPath.section]; [self openMapWithTitle:title model:model]; } #pragma mark - Photo/Signature - (void)previewImage:(UIImage *)img { RAPhotoPreviewController *previewVC = [RAPhotoPreviewController viewControllerFromStoryboard]; previewVC.image = img; previewVC.canDelete = NO; [self.navigationController pushViewController:previewVC animated:YES]; } - (void)photoCell:(RADetailPhotoCell *)cell didClickPhoto:(RADetailPhotoModel *)model { [self previewImage:model.photo]; } - (void)signatureCell:(RADetailSignatureCell *)cell didClickSignature:(RADetailSignatureModel *)model { [self previewImage:model.signature]; } #pragma mark - Location Delegate - (void)locationCell:(RADetailLocationCell *)cell didClickNavigation:(RADetailLocationModel *)model { if (model.street == nil) { return; } // @"东大街芷泉段6号" UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) { UIAlertAction *googleMapAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Google Map", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?q=%@&directionsmode=driving",model.street] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) { NSLog(@"%u",success); }]; // NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?center=37.782652,-122.410126&directionsmode=driving"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; }]; [alertVC addAction:googleMapAction]; } UIAlertAction *appleMapAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Apple Map", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation]; // MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(37.782652, -122.410126) addressDictionary:nil]]; MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:kCLLocationCoordinate2DInvalid addressDictionary:@{ (__bridge id)kABPersonAddressStreetKey : model.street }]]; [MKMapItem openMapsWithItems:@[currentLocation, toLocation] launchOptions:@{ MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey: @(YES) }]; }]; [alertVC addAction:appleMapAction]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alertVC addAction:cancelAction]; [self presentViewController:alertVC animated:YES completion:nil]; } #pragma mark - Action Delegate - (void)actionsCell:(RADetailActionsCell *)cell didClickSubCell:(RADetailActionSubCell *)subCell forModel:(RADetailActionModel *)model { if (model.alert) { UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:model.alertTitle message:model.alertMsg preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; UIAlertAction *yesAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"YES", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self handleActionForModel:model withSubCell:subCell]; }]; [alertVC addAction:cancelAction]; [alertVC addAction:yesAction]; [self presentViewController:alertVC animated:YES completion:nil]; } else { [self handleActionForModel:model withSubCell:subCell]; } } - (void)handleActionForModel:(RADetailActionModel *)model withSubCell:(RADetailActionSubCell *)subCell { switch (model.actionType) { case RADetailActionTypeRemote: { [self handleRemoteAction:model]; } break; case RADetailActionTypeLocal: { switch (model.actionSubType) { case RADetailActionSubTypeEnum: { [self handleEnumAction:model forCell:subCell]; } break; case RADetailActionSubTypeUpdate: { [self showUpdateForModel:model]; } break; default: break; } } break; default: break; } } #pragma mark - Action - (void)handleEnumAction:(RADetailActionModel *)model forCell:(RADetailActionSubCell *)subCell { RADetailActionSelectionViewController *vc = [RADetailActionSelectionViewController viewControllerFromStoryboard]; vc.actions = model.enums; vc.delegate = self; vc.modalPresentationStyle = UIModalPresentationPopover; vc.preferredContentSize = CGSizeMake(250, 300); vc.popoverPresentationController.sourceView = subCell; vc.popoverPresentationController.sourceRect = subCell.bounds; vc.popoverPresentationController.delegate = self; [self presentViewController:vc animated:YES completion:nil]; } - (void)handleRemoteAction:(RADetailActionModel *)model { RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view]; __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSDictionary *json = [RADataProvider reportOrder:self.orderID type:self.orderType actionType:model.actionSubType actionIndex:model.index actionName:model.actionTitle toURL:model.url withParams:model.params]; dispatch_async(dispatch_get_main_queue(), ^{ // dismiss progress [hud dismiss]; if (weakSelf) { __strong typeof(weakSelf) strongSelf = weakSelf; int result = [[json objectForKey:@"result"] intValue]; if (result == RESULT_TRUE) { // if (model.actionSubType == RADetailActionSubTypeAccept) { // RASingleton.sharedInstance.requiredBackgroundLocation = YES; // } [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationReloadHome object:nil]; switch (model.remoteActionType) { case RADetailRemoteActionTypeGoHome:{ [strongSelf.navigationController popToRootViewControllerAnimated:YES]; } break; case RADetailRemoteActionTypeReload: { [strongSelf reloadData]; } break; default: break; } } else { // process error NSString *msg = [json objectForKey:@"err_msg"]; [strongSelf showAlertTilte:NSLocalizedString(@"Warning", nil) message:msg]; } } }); }); } - (void)showUpdateForModel:(RADetailActionModel *)model { if (model.actionSubType != RADetailActionSubTypeUpdate) { return; } RAOrderEditViewController *vc = [RAOrderEditViewController viewControllerFromStoryboard]; vc.title = model.actionTitle; vc.orderID = self.orderID; vc.actionID = model.actionID; vc.actionIdx = model.index; vc.actionTitle = model.actionTitle; vc.orderType2 = self.orderType2; [self.navigationController pushViewController:vc animated:YES]; } #pragma mark - Popover Delegate -(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{ return UIModalPresentationNone;//不适配 } #pragma mark - ActionSelection Delegate - (void)detailSelectAction:(RADetailActionSelectionModel *)model { RAOrderEditViewController *vc = [RAOrderEditViewController viewControllerFromStoryboard]; // RAOrderEditViewController *vc = [[RAOrderEditViewController alloc] init]; // 使用代码直接创建,在Push动画过程中会卡一下,具体原因不明 vc.title = model.actionTitle; vc.orderID = self.orderID; vc.actionID = model.actionID; vc.actionTitle = model.actionTitle; vc.orderType2 = self.orderType2; vc.actionIdx = model.actionIndex; [self.navigationController pushViewController:vc animated:YES]; } #pragma mark - Mult Photo Cell Delegate - (void)photoCell:(RAPhotoCell *)cell didClickPhotoItem:(RAPhotoItemModel *)model { [self previewImage:model.photo]; } #pragma mark - Private - (void)instance:(id)obj playSEL:(SEL)selector parameters:(NSArray *)params { if (!obj || !selector) { return; } if ([obj respondsToSelector:selector]) { NSMethodSignature *signature = [[obj class] instanceMethodSignatureForSelector:selector]; NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; [invocation setTarget:obj]; [invocation setSelector:selector]; if (params && params.count > 0) { for (int i = 0; i < params.count; i++) { NSObject *obj = params[i]; [invocation setArgument:&obj atIndex:i+2]; } [invocation retainArguments]; // 防止参数被释放 } [invocation invoke]; } } @end