// // PBAddCommentsController.m // IphoneBIMe // // Created by zjf on 2018/9/3. // Copyright © 2018年 ProBIM. All rights reserved. // #import "PBAddCommentsController.h" #import "PBImageCollectionViewCell.h" #import "PBIssueDetailModel.h" #import "PBProjectModel.h" #import "PBCommentsFooterView.h" #import "PBIssueListModel.h" #import "PBCommentsModel.h" #import "IQKeyboardManager.h" #define ALineCount 3 #define Spacing 10 #define whiteColor [UIColor whiteColor] #define separateColor [UIColor z_colorWithR:242 G:242 B:242] static NSString *const cellID = @"cellID"; @interface PBAddCommentsController () @property (nonatomic, weak) UITextView *textView; @property (nonatomic, weak) UIView *linView; @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *selectedPhotos; @property (nonatomic, strong) PBCommentsFooterView *tableFooterView; @property (nonatomic, weak) UIView *bottomV; @property (nonatomic, assign) CGFloat height; @end @implementation PBAddCommentsController - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[IQKeyboardManager sharedManager] setEnable:NO]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[IQKeyboardManager sharedManager] setEnable:YES]; } - (void)viewDidLoad { [super viewDidLoad]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.f) { self.edgesForExtendedLayout = UIRectEdgeNone; self.navigationController.interactivePopGestureRecognizer.enabled = YES; self.navigationController.interactivePopGestureRecognizer.delegate = self; } // if (IS_IPHONE_X) { // self.height = 34.0f; // }else { // self.height = 0.0f; // } // [PBNoteCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil]; // // [PBNoteCenter addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil]; [self setupNav]; [self setupUI]; } //- (void)keyboardWillShow:(NSNotification *)notification { // CGRect frame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; // [self.bottomV mas_updateConstraints:^(MASConstraintMaker *make) { // make.bottom.equalTo(self.view).offset(-frame.size.height); // }]; //} //- (void)keyboardWillHidden:(NSNotification *)notification{ // [self.bottomV mas_updateConstraints:^(MASConstraintMaker *make) { // make.bottom.equalTo(self.view).offset(-(self.height)); // }]; //} - (void)setupNav { self.title = @"新增评论"; PBBackNavItem *backNav = [PBBackNavItem backNacItem]; backNav.title = @"取消"; [backNav addTarget:self action:@selector(backItemAction) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *backNavItem = [[UIBarButtonItem alloc] initWithCustomView:backNav]; self.navigationItem.leftBarButtonItem = backNavItem; UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction)]; self.navigationItem.rightBarButtonItem = saveItem; } - (void)backItemAction { [self.navigationController popViewControllerAnimated:YES]; } - (void)saveAction { [YJProgressHUD showProgress:@"" inView:self.view]; [[PBNetworkTools sharedTools] AddCommentWithIssueId:self.issueListModel.IssueId andOrganizeId:self.projectModel.bimcomposerid andText:self.textView.text 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 hide]; NSDictionary *commentDic = @{ @"issue_talkid":[networkModel.Data valueForKey:@"Issue_TalkId"], @"issueid":[networkModel.Data valueForKey:@"IssueId"], @"userid":[networkModel.Data valueForKey:@"UserId"], @"content":[networkModel.Data valueForKey:@"Content"], @"createdate":[networkModel.Data valueForKey:@"CreateDate"], @"contenttype":[networkModel.Data valueForKey:@"ContentType"], @"deletemark":[networkModel.Data valueForKey:@"DeleteMark"], @"userofunread":[networkModel.Data valueForKey:@"UserOfUnRead"], @"issue_ptalkid":[networkModel.Data valueForKey:@"Issue_PTalkId"], @"commentpatchguid":[networkModel.Data valueForKey:@"CommentPatchGUID"], @"realname":Realname }; if (self.SaveCommentsBlock) { self.SaveCommentsBlock(commentDic); } [self.navigationController popViewControllerAnimated:YES]; }else { [YJProgressHUD showMessage:networkModel.Msg inView:self.view]; } }]; } - (void)uploadFileToideFolderWithImage:(UIImage *)image { [YJProgressHUD showProgress:@"" inView:self.view]; [[PBNetworkTools sharedTools] UploadFileToHideFolder_MulPropWithImage:image andProjectID:self.projectModel.bimcomposerid andFileType:@"Issue" andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { [YJProgressHUD showMessage:@"上传图片失败" inView:self.view]; return; } NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; NSDictionary *dict = [NSString convertTodictionaryOrArr:str]; if (dict){ [self saveimageCommentsWithFile:dict]; }else { [YJProgressHUD showMessage:@"上传图片失败" inView:self.view]; } }]; } - (void)saveimageCommentsWithFile:(NSDictionary *)file { [[PBNetworkTools sharedTools] AddImageCommentWithIssueId:self.issueListModel.IssueId andFileID:[file valueForKey:@"FileId"] andFielName:[file valueForKey:@"FileName"] 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 hide]; NSDictionary *commentDic = @{ @"issue_talkid":[networkModel.Data valueForKey:@"Issue_TalkId"], @"issueid":[networkModel.Data valueForKey:@"IssueId"], @"userid":[networkModel.Data valueForKey:@"UserId"], @"content":[networkModel.Data valueForKey:@"Content"], @"createdate":[networkModel.Data valueForKey:@"CreateDate"], @"contenttype":[networkModel.Data valueForKey:@"ContentType"], @"deletemark":[networkModel.Data valueForKey:@"DeleteMark"], @"userofunread":[networkModel.Data valueForKey:@"UserOfUnRead"], @"issue_ptalkid":[networkModel.Data valueForKey:@"Issue_PTalkId"], @"commentpatchguid":[networkModel.Data valueForKey:@"CommentPatchGUID"], @"realname":Realname, @"contenttypeJson":@{@"type":@"image", @"contentImgId":[file valueForKey:@"FileId"]} }; if (self.SaveCommentsBlock) { self.SaveCommentsBlock(commentDic); } [self.navigationController popViewControllerAnimated:YES]; }else { [YJProgressHUD showMessage:networkModel.Msg inView:self.view]; } }]; } - (void)setupUI { self.view.backgroundColor = whiteColor; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.right.equalTo(self.view); make.bottom.equalTo(self.view).offset(-(67 + self.height)); }]; self.tableView.tableFooterView = [UIView new]; // self.tableFooterView.frame = CGRectMake(0, 0, self.view.width, 60); // self.tableView.tableFooterView = self.tableFooterView; // // UIView *bottomV = [[UIView alloc] init]; // bottomV.backgroundColor = whiteColor; // [self.view addSubview:bottomV]; // [bottomV mas_makeConstraints:^(MASConstraintMaker *make) { // make.size.mas_equalTo(CGSizeMake(self.view.width, 67)); // make.centerX.equalTo(self.view); // make.bottom.equalTo(self.view).offset(self.height); // }]; // // UIView *linView = [[UIView alloc] init]; // linView.backgroundColor = [UIColor z_colorWithR:242 G:242 B:242]; // [bottomV addSubview:linView]; // [linView mas_makeConstraints:^(MASConstraintMaker *make) { // make.top.equalTo(bottomV); // make.left.right.equalTo(bottomV); // make.height.equalTo(@10); // }]; // // UILabel *imageL = [UILabel z_labelWithText:@"评论图片" Color:TitleColor isBold:NO Font:TitleFontSize]; // [bottomV addSubview:imageL]; // [imageL mas_makeConstraints:^(MASConstraintMaker *make) { // make.top.equalTo(linView.mas_bottom).offset(18); // make.left.equalTo(self.view).offset(18); // make.size.mas_equalTo(CGSizeMake(66, 22)); // }]; // UIButton *chooseImageBtn = [UIButton z_textButton:@"请选择图片" fontSize:MarkedFontSize normalColor:IgnoreColor]; // [chooseImageBtn addTarget:self action:@selector(chooseImageAction) forControlEvents:UIControlEventTouchUpInside]; // [bottomV addSubview:chooseImageBtn]; // [chooseImageBtn mas_makeConstraints:^(MASConstraintMaker *make) { // make.centerY.equalTo(imageL); // make.right.equalTo(bottomV).offset(-16); // }]; // // self.bottomV = bottomV; } - (UIView *)tableHeaderView { UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 150)]; bgView.backgroundColor = whiteColor; UITextView *textView = [[UITextView alloc] init]; self.textView = textView; textView.textColor= TitleColor; textView.font = [UIFont systemFontOfSize:TitleFontSize]; [textView becomeFirstResponder]; // textView.delegate = self; [bgView addSubview:textView]; [textView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(bgView).offset(16); make.left.equalTo(bgView).offset(16); make.right.equalTo(bgView).offset(-16); make.height.equalTo(@118); }]; UIView *linView = [[UIView alloc] init]; self.linView = linView; linView.backgroundColor = whiteColor; [bgView addSubview:linView]; [linView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(bgView); make.height.equalTo(@10); make.bottom.equalTo(bgView); }]; return bgView; } - (void)chooseImageAction { [self takePhotoLibrary]; } - (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 { LFImagePickerController *imagePicker = [[LFImagePickerController alloc] initWithMaxImagesCount:1 delegate:self]; imagePicker.allowTakePicture = YES;//内部显示相机 imagePicker.isSelectOriginalPhoto = YES;//是否选择原图 imagePicker.allowPickingType = LFPickingMediaTypePhoto;//用户是否可以选择视频 imagePicker.allowPickingOriginalPhoto = NO;//原图按钮将隐藏,用户将不能发送原图 imagePicker.maxPhotoBytes = 1024 * 1024 * 100;//最大图片所选字节大小 if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f) { imagePicker.syncAlbum = YES; /** 实时同步相册 */ } [self presentViewController:imagePicker animated:YES completion:nil]; } - (void)lf_imagePickerController:(LFImagePickerController *)picker didFinishPickingResult:(NSArray */*> *)results { 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]; } } if (imageArrM.count) { [self uploadFileToideFolderWithImage:imageArrM[0]]; } // self.linView.backgroundColor = separateColor; // [self.selectedPhotos addObjectsFromArray:imageArrM.copy]; // [self setupTableFooterViewHeight]; } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] init]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.tableHeaderView = [self tableHeaderView]; } return _tableView; } - (PBCommentsFooterView *)tableFooterView { __weak typeof(self) weakSelf = self; if (_tableFooterView == nil) { _tableFooterView = [[PBCommentsFooterView alloc] init]; _tableFooterView.deleteImageBlock = ^(NSInteger index) { [weakSelf.selectedPhotos removeObjectAtIndex:index]; [weakSelf setupTableFooterViewHeight]; if (self.selectedPhotos.count == 0) { weakSelf.linView.backgroundColor = whiteColor; } }; } return _tableFooterView; } - (void)setupTableFooterViewHeight { NSInteger more = self.selectedPhotos.count % ALineCount == 0 ? 0 : 1; NSInteger rowNumber = (self.selectedPhotos.count / ALineCount) + more; CGFloat itemWH = (PBScreenWidth - 4 * Spacing) / ALineCount; CGFloat collectViewH = (itemWH *rowNumber) + (rowNumber - 1) * Spacing; collectViewH = collectViewH < 0 ? 28 : collectViewH; self.tableFooterView.frame = CGRectMake(0, 0, self.view.width, collectViewH + 32); self.tableFooterView.selectedPhotos = self.selectedPhotos; self.tableView.tableFooterView = self.tableFooterView; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 0; } - (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { return nil; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { [self.textView resignFirstResponder]; } - (NSMutableArray *)selectedPhotos { if (_selectedPhotos == nil) { _selectedPhotos = [[NSMutableArray alloc] init]; } return _selectedPhotos; } - (void)setProjectModel:(PBProjectModel *)projectModel { _projectModel = projectModel; } - (void)setIssueDetailModel:(PBIssueDetailModel *)issueDetailModel { _issueDetailModel = issueDetailModel; } - (void)setTalkId:(NSString *)talkId { _talkId = talkId; } - (void)setIssueListModel:(PBIssueListModel *)issueListModel { _issueListModel = issueListModel; } - (void)dealloc { [PBNoteCenter removeObserver:self]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #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