zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//
//  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 ()<UITableViewDataSource, UITableViewDelegate, LFImagePickerControllerDelegate, UIGestureRecognizerDelegate>
@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 <LFResultObject /* <LFResultImage/LFResultVideo> */*> *)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