// // 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 "RADetailSingleLineCell.h" #import "RADetailMultLineCell.h" #import "RADetailActionSubCell.h" #import #import "RADetailActionSelectionModel.h" #import "RAOrderEditViewController.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.actionLayout.delegate = actionModel; [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; 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 { UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; // __weak typeof(self) weakSelf = self; if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) { UIAlertAction *googleMapAction = [UIAlertAction actionWithTitle:@"Google Map" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?center=37.782652,-122.410126&directionsmode=driving"] 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 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 { switch (model.actionType) { case RADetailActionTypeRemote: { } break; case RADetailActionTypeLocal: { switch (model.actionSubType) { case RADetailActionSubTypeEnum: { [self handleEnumAction:model forCell:subCell]; } 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]; } #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 = @"Update Action"; vc.orderID = self.orderID; vc.actionID = model.actionID; [self.navigationController pushViewController:vc animated:YES]; } @end