// // 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 "RADetailSignatureCell.h" #import #import "RADetailActionSelectionModel.h" #import "RAOrderEditViewController.h" #import "RAProgressHUD.h" #import //#import #import "RADetailMapCell.h" #import "RADetailPhotoCell.h" #import "RADetailPhotoModel.h" #import "RADetailMapModel.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]; return cell; } break; case RAOrderDetailValueTypePhoto: { RADetailPhotoModel *photoModel = (RADetailPhotoModel *)model; RADetailPhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailPhotoCell" forIndexPath:indexPath]; [cell setModel:photoModel]; return cell; } break; case RAOrderDetailValueTypeSignature: { RADetailSignatureModel *signatureModel = (RADetailSignatureModel *)model; RADetailSignatureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RADetailSignatureCell" forIndexPath:indexPath]; [cell setModel:signatureModel]; 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 - Location Delegate - (void)locationCell:(RADetailLocationCell *)cell didClickNavigation:(RADetailLocationModel *)model { // @"东大街芷泉段6号" UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) { UIAlertAction *googleMapAction = [UIAlertAction actionWithTitle:@"Google Map" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?q=%@&directionsmode=driving",model.location] 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:@"Apple Map" 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.location }]]; [MKMapItem openMapsWithItems:@[currentLocation, toLocation] launchOptions:@{ MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey: @(YES) }]; }]; [alertVC addAction:appleMapAction]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" 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:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"YES" 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 reportAcionToURL: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.requiredLocation = YES; } [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationReloadHome object:nil]; [strongSelf.navigationController popToRootViewControllerAnimated:YES]; } else { // process error NSString *msg = [json objectForKey:@"err_msg"]; [strongSelf showAlertTilte:@"Warning" 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.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; [self.navigationController pushViewController:vc animated:YES]; } #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