// // PBApplyInspectionViewController.m // IphoneBIMe // // Created by zjf on 2019/2/28. // Copyright © 2019 ProBIM. All rights reserved. // #import "PBApplyInspectionViewController.h" #import "PBIssueAddModel.h" #import "PBProjectModel.h" #import "PBExamineListModel.h" #import "PBExamineTableHeaderView.h" #import "PBVideoPlayViewController.h" #import "PBCheckHeaderView.h" #import "PBExamineAddModel.h" #import "PBRecordInputTableViewCell.h" #import "LXFCameraController.h" static NSString *const inputCellID = @"inputCellID"; @interface PBApplyInspectionViewController () @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) PBCheckHeaderView *tableheaderView; @property (nonatomic, strong) NSMutableArray *selectedPhotos; @property (nonatomic, strong) NSMutableArray *selectedVideo; @property (nonatomic, strong) NSArray *dataList; @property (nonatomic, strong) NSMutableArray *selectAssetArr; @end @implementation PBApplyInspectionViewController - (void)viewDidLoad { [super viewDidLoad]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.f) { self.edgesForExtendedLayout = UIRectEdgeNone; } [self setupNav]; [self setupUI]; } - (void)setupNav { self.view.backgroundColor = PBColor(244, 245, 246); UIView *topV = [[UIView alloc] init]; topV.backgroundColor = [UIColor whiteColor]; [self.view addSubview:topV]; [topV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.right.equalTo(self.view); make.height.equalTo(@50); }]; UIButton *cancelBtn = [UIButton z_textButton:@"取消" fontSize:16 normalColor:PBColor(97, 111, 125)]; [cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:cancelBtn]; [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(16); make.left.equalTo(self.view).offset(20); make.size.mas_equalTo(CGSizeMake(34, 18)); }]; UILabel *titleL = [UILabel z_labelWithText:@"申请复检" Color:PBColor(97, 111, 125) isBold:YES Font:16]; titleL.textAlignment = NSTextAlignmentCenter; [self.view addSubview:titleL]; [titleL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(16); make.size.mas_equalTo(CGSizeMake(69, 18)); make.centerX.equalTo(self.view); }]; UIView *linV = [[UIView alloc] init]; linV.backgroundColor = PBColor(233, 235, 237); [self.view addSubview:linV]; [linV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(topV.mas_bottom); make.left.right.equalTo(self.view); make.height.equalTo(@1); }]; } - (void)cancelAction { [self dismissViewControllerAnimated:YES completion:nil]; } #pragma mark - 提交保存数据 - (void)saveAction { [self.view endEditing:NO]; UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"确认保存" message:@"保存后将不可更改,是否确认保存" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSMutableArray *arrM; if (self.selectedVideo.count > 0) { arrM = self.selectedVideo; }else { arrM = self.selectedPhotos; } PBExamineAddModel *examindeAddModel = self.dataList[0]; NSString *remark = examindeAddModel.dataStr; [YJProgressHUD showProgress:@"" inView:self.view]; [[PBNetworkTools sharedTools] ApplyToAcceptanceWithExamineID:self.examineListModel.ExamineID andOrganizeId:self.projectModel.organizeid andRectificationRemark:remark andImageArr:arrM andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { [YJProgressHUD showMessage:@"保存失败" inView:self.view]; return; } NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str]; if (networkModel.Ret == 1) { [YJProgressHUD showMessage:@"保存成功" inView:self.view]; if (self.UpdateRecordListBlock) { self.UpdateRecordListBlock(); } [self dismissViewControllerAnimated:YES completion:nil]; }else { [YJProgressHUD showMessage:networkModel.Msg inView:self.view]; } }]; }]; UIAlertAction *canceAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; [alertVC addAction:saveAction]; [alertVC addAction:canceAction]; [self presentViewController:alertVC animated:YES completion:nil]; } - (void)setupUI { __weak typeof(self) weakSelf = self; self.tableView = [[UITableView alloc] init]; self.tableView.dataSource = self; self.tableView.delegate = self; self.tableView.rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 100.f; self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; self.tableView.bounces = NO; [self.tableView registerClass:[PBRecordInputTableViewCell class] forCellReuseIdentifier:inputCellID]; [self.view addSubview:self.tableView]; CGFloat bottomH = 64.f; if (IS_IPHONE_X) { bottomH = 64.f + IPHONE_X_BOTTOM; } [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(51); make.left.equalTo(self.view).offset(10); make.right.equalTo(self.view).offset(-10); make.bottom.equalTo(self.view).offset(-(bottomH)); }]; UIButton *applyBtn = [UIButton z_bgImageButton:[UIImage imageNamed:@"apply_reinspection_nor"]]; [applyBtn addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:applyBtn]; [applyBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.tableView.mas_bottom).offset(7); make.left.equalTo(self.view).offset(10); make.right.equalTo(self.view).offset(-10); make.height.equalTo(@50); }]; self.tableheaderView = [[PBCheckHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 100)]; self.tableheaderView.ChooseImageBlock = ^{ [weakSelf.view endEditing:YES]; [weakSelf chooseImageOrVideo]; }; self.tableheaderView.DeleteImageBlock = ^(NSInteger index) { [weakSelf.view endEditing:YES]; [weakSelf.selectedPhotos removeObjectAtIndex:index]; [weakSelf setTableHeaderViewImage]; }; self.tableheaderView.DeleteVideoBlock = ^{ [weakSelf.view endEditing:YES]; [weakSelf.selectedVideo removeAllObjects]; [weakSelf setTableHeaderViewImage]; }; self.tableheaderView.PlayVideoBlock = ^{ [weakSelf.view endEditing:YES]; NSLog(@"打开视频"); PBVideoPlayViewController *playVC = [[PBVideoPlayViewController alloc] init]; PBResultVideo *result = weakSelf.selectedVideo[0]; playVC.filePath = result.url; [weakSelf.navigationController pushViewController:playVC animated:YES]; }; self.tableheaderView.projectModel = self.projectModel; self.tableheaderView.isAddExamine = YES; self.tableView.tableHeaderView = self.tableheaderView; } - (void)chooseImageOrVideo { if (self.selectedPhotos.count > 0) { [self takePhotoLibrary]; return;; } UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"请选择" preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *videoAction = [UIAlertAction actionWithTitle:@"拍摄" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self videoBtnFunc]; }]; UIAlertAction *imageAction = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self takePhotoLibrary]; }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; [alertVC addAction:videoAction]; [alertVC addAction:imageAction]; [alertVC addAction:cancelAction]; [self presentViewController:alertVC animated:YES completion:nil]; } - (void)videoBtnFunc { LXFCameraController *cameraController = [LXFCameraController defaultCameraController]; __weak LXFCameraController *weakCameraController = cameraController; cameraController.takePhotosCompletionBlock = ^(UIImage *image, NSError *error) { NSLog(@"takePhotosCompletionBlock"); [weakCameraController dismissViewControllerAnimated:YES completion:nil]; NSData *imageData = UIImageJPEGRepresentation(image, 0.5); UIImage *originalImage = [UIImage imageWithData:imageData]; [self.selectedPhotos addObject:originalImage]; [self setTableHeaderViewImage]; }; cameraController.shootCompletionBlock = ^(NSURL *videoUrl, CGFloat videoTimeLength, UIImage *thumbnailImage, NSError *error) { NSLog(@"shootCompletionBlock"); [weakCameraController dismissViewControllerAnimated:YES completion:nil]; PBResultVideo *video = [[PBResultVideo alloc] init]; video.coverImage = thumbnailImage; video.url = videoUrl; video.data = [NSData dataWithContentsOfURL:videoUrl]; [self.selectedVideo addObject:video]; [self setTableHeaderViewImage]; }; cameraController.modalPresentationStyle = UIModalPresentationFullScreen; [self presentViewController:cameraController animated:YES completion:nil]; } //新增图片选择图片后填充 - (void)setTableHeaderViewImage { if (self.selectedVideo.count > 0) { self.tableheaderView.videoArr = self.selectedVideo; }else { self.tableheaderView.imageArr = self.selectedPhotos; } } - (void)takePhotoLibrary { PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus]; if (status == PHAuthorizationStatusNotDetermined) {//未作出回应 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { if(status == PHAuthorizationStatusAuthorized) { dispatch_async(dispatch_get_main_queue(), ^{//用户点击ok [self pushLFImagePickerController]; }); } else { dispatch_async(dispatch_get_main_queue(), ^{//点击不允许访问 NSLog(@"点击不允许访问"); }); } }]; }else if (status == PHAuthorizationStatusRestricted) {//没有被授权访问 可能家长控制 }else if (status == PHAuthorizationStatusDenied) {//已明确否认相册权限 UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"无法访问相册" message:@"请在iPhone的""设置-隐私-相册""中允许访问相册" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { if (@available(iOS 10.0, *)) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil]; } else { NSLog(@"选择取消"); } }]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil]; }else if (status == PHAuthorizationStatusAuthorized){//有权限访问 [self pushLFImagePickerController]; } } - (void)pushLFImagePickerController { NSInteger maxImageCount = 9 - self.selectedPhotos.count; LFImagePickerController *imagePicker = [[LFImagePickerController alloc] initWithMaxImagesCount:maxImageCount delegate:self]; imagePicker.maxVideosCount = 1; imagePicker.allowTakePicture = YES;//内部显示相机 imagePicker.isSelectOriginalPhoto = YES;//是否选择原图 if (self.selectedPhotos.count > 0) { imagePicker.allowPickingType = LFPickingMediaTypePhoto;//用户是否可以选择视频 }else { imagePicker.allowPickingType = LFPickingMediaTypeALL;//用户是否可以选择视频 } imagePicker.allowPickingOriginalPhoto = NO;//原图按钮将隐藏,用户将不能发送原图 imagePicker.maxPhotoBytes = 1024 * 1024 * 100;//最大图片所选字节大小 if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f) { imagePicker.syncAlbum = YES; /** 实时同步相册 */ } // imagePicker.selectedAssets = self.selectAssetArr.copy; [self presentViewController:imagePicker animated:YES completion:nil]; } #pragma mark - LFImagePickerControllerDelegate - (void)lf_imagePickerController:(LFImagePickerController *)picker didFinishPickingResult:(NSArray */*> *)results { // NSMutableArray *assetArr = [[NSMutableArray alloc] init]; NSMutableArray *videoArr = [[NSMutableArray alloc] init]; NSMutableArray *imageArrM = [[NSMutableArray alloc] init]; for (NSInteger i = 0; i < results.count; i++) { LFResultObject *result = results[i]; if ([result isKindOfClass:[LFResultImage class]]){ LFResultImage *resultImage = (LFResultImage *)result; NSData *imageData = UIImageJPEGRepresentation(resultImage.originalImage, 0.5); UIImage *originalImage = [UIImage imageWithData:imageData]; [imageArrM addObject:originalImage]; // [assetArr addObject:result.asset]; }else { LFResultVideo *resultVideo = (LFResultVideo *)result; PBResultVideo *video = [[PBResultVideo alloc] init]; video.coverImage = resultVideo.coverImage; video.url = resultVideo.url; video.data = resultVideo.data; [videoArr addObject:video]; } } if (videoArr.count > 0) { self.selectedVideo = videoArr; [self.selectedPhotos removeAllObjects]; // [self.selectAssetArr removeAllObjects]; }else { [self.selectedPhotos addObjectsFromArray:imageArrM.copy]; // [self.selectAssetArr addObjectsFromArray:assetArr]; } [self setTableHeaderViewImage]; } #pragma mark - UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataList.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { PBRecordInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:inputCellID forIndexPath:indexPath]; PBExamineAddModel *examineAddModel = self.dataList[indexPath.row]; cell.examineAddModel = examineAddModel; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (NSArray *)dataList { if (_dataList == nil) { PBExamineAddModel *examineAddModel = [[PBExamineAddModel alloc] init]; examineAddModel.prompt = @"请输入申请复检内容"; _dataList = @[examineAddModel]; } return _dataList; } - (void)setExamineListModel:(PBExamineListModel *)examineListModel { _examineListModel = examineListModel; } - (NSMutableArray *)selectedPhotos { if (_selectedPhotos == nil) { _selectedPhotos = [[NSMutableArray alloc] init]; } return _selectedPhotos; } - (NSMutableArray *)selectedVideo { if (_selectedVideo == nil) { _selectedVideo = [[NSMutableArray alloc] init]; } return _selectedVideo; } - (void)setIsBackList:(BOOL)isBackList { _isBackList = isBackList; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end