| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- //
- // 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 <MapKit/MapKit.h>
- #import "RADetailActionSelectionModel.h"
- #import "RAOrderEditViewController.h"
- #import "RAProgressHUD.h"
- #import <AddressBook/AddressBook.h>
- //#import <Contacts/Contacts.h>
- #import "RADetailMapCell.h"
- #import "RADetailPhotoModel.h"
- #import "RADetailMapModel.h"
- #import "RAPhotoPreviewController.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];
- 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;
-
- 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 - 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:@"Google Map" 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:@"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.street
- }]];
- [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];
-
- 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:@"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
|