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
//
//  PBPersonListViewController.m
//  IphoneBIMe
//
//  Created by zjf on 2021/1/5.
//  Copyright © 2021 ProBIM. All rights reserved.
//
 
#import "PBPersonListViewController.h"
#import "PBPersonVCTableViewCell.h"
#import "PBMultiSelectPersonCell.h"
#import "PBProjectModel.h"
#import "PBRoleViewController.h"
#import "PBUserModel.h"
#import "PBUserCollectionViewCell.h"
#import "PBSelectedPersonController.h"
static NSString *const cellID = @"cellID";
static NSString *const MScellID = @"MScellID";
static NSString *collectionCell = @"collectionCell";
@interface PBPersonListViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, weak) UIView *bottomV;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, weak) UIButton *determineBtn;
@property (nonatomic, weak) UIButton *arrowBtn;
@end
 
@implementation PBPersonListViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (!self.isMultiSelect) {
        return;
    }
    NSMutableArray *list = [[NSMutableArray alloc] init];
    for (NSDictionary *obj in self.dataList) {
        NSMutableArray *arrM = [[NSMutableArray alloc] init];
        for (PBUserModel *model in [obj valueForKey:@"Users"]) {
            for (PBUserModel *selectModel in self.selectList) {
                if ([model.UserId isEqualToString:selectModel.UserId]) {
                    model.isSelect = YES;
                    break;
                }else{
                    model.isSelect = NO;
                }
            }
            [arrM addObject:model];
        }
        NSDictionary *dict = @{@"FirstLetter":[obj valueForKey:@"FirstLetter"], @"Users":arrM.copy};
        [list addObject:dict];
    }
    self.dataList = list;
    [self.tableView reloadData];
    if (self.selectList.count > 0) {
        self.bottomV.hidden = NO;
        self.arrowBtn.hidden = NO;
        self.collectionView.hidden = NO;
        self.determineBtn.hidden = NO;
        [self.determineBtn setTitle:[NSString stringWithFormat:@"确定(%zd)",self.selectList.count] forState:UIControlStateNormal];
        [self.collectionView reloadData];
    }else {
        self.bottomV.hidden = YES;
        self.arrowBtn.hidden = YES;
        self.collectionView.hidden = YES;
        self.determineBtn.hidden = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNav];
    [self setupUI];
    [self loadData];
}
- (void)setupNav {
    self.title = @"请指定人员";
    UIBarButtonItem *backNavItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Doc_preview_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backItemAction)];
    self.navigationItem.leftBarButtonItem = backNavItem;
}
- (void)backItemAction {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)setupUI {
    self.view.backgroundColor = PBColor(244, 245, 246);
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = PBColor(244, 245, 246);
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.bounces = NO;
    self.tableView.rowHeight = 56;
    self.tableView.sectionIndexColor = PBColor(97, 111, 125);
    if (@available(iOS 15.0, *)) {
        self.tableView.sectionHeaderTopPadding = 0;
    };
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[PBPersonVCTableViewCell class] forCellReuseIdentifier:cellID];
    [self.tableView registerClass:[PBMultiSelectPersonCell class] forCellReuseIdentifier:MScellID];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 56, 0);
    [self.view addSubview:self.tableView];
    CGFloat bottomH = 0.f;
    if (IS_IPHONE_X) {
        bottomH = 0.f + IPHONE_X_BOTTOM;
    }
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-(bottomH));
    }];
    self.tableView.tableHeaderView = [self setupTableHearderView];
    
    UIView *bottomV = [[UIView alloc] init];
    bottomV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomV];
    [bottomV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.tableView);
        make.height.equalTo(@54);
    }];
    UIButton *arrowBtn = [UIButton z_bgImageButton:[UIImage imageNamed:@"arrow_top"]];
    [arrowBtn addTarget:self action:@selector(arrowTopActon) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:arrowBtn];
    [arrowBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomV).offset(15);
        make.left.equalTo(bottomV).offset(12);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout.alloc init];
    layout.itemSize = CGSizeMake(34, 34);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout];
    [self.collectionView registerClass:[PBUserCollectionViewCell class] forCellWithReuseIdentifier:collectionCell];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomV).mas_offset(10);
        make.left.equalTo(bottomV).offset(46);
        make.right.equalTo(bottomV).offset(-120);
        make.height.equalTo(@34);
    }];
    UIButton *determineBtn = [UIButton z_textButton:[NSString stringWithFormat:@"确定(%zd)",self.selectList.count] boldFontSize:14 normalColor:[UIColor whiteColor]];
    [determineBtn addTarget:self action:@selector(determineBtnAction) forControlEvents:UIControlEventTouchUpInside];
    determineBtn.backgroundColor = PBColor(0, 122, 255);
    [self.view addSubview:determineBtn];
    [determineBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomV).offset(9);
        make.right.equalTo(bottomV).offset(-20);
        make.size.mas_equalTo(CGSizeMake(89, 34));
    }];
    [determineBtn circleViewWithRadius:6];
    self.arrowBtn = arrowBtn;
    self.bottomV = bottomV;
    self.determineBtn = determineBtn;
    if (self.selectList.count > 0 && self.isMultiSelect) {
        self.bottomV.hidden = NO;
        self.arrowBtn.hidden = NO;
        self.collectionView.hidden = NO;
        self.determineBtn.hidden = NO;
        [self.collectionView reloadData];
    }else {
        self.bottomV.hidden = YES;
        self.arrowBtn.hidden = YES;
        self.collectionView.hidden = YES;
        self.determineBtn.hidden = YES;
    }
}
- (void)determineBtnAction {
    [PBNoteCenter postNotificationName:PBNoteCenterUpdatePerson object:self.selectList.copy];
    [self.navigationController popViewControllerAnimated:YES];  
}
- (UIView *)setupTableHearderView {
//    UIView *bgV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 104)];
    UIView *bgV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 60)];
    bgV.backgroundColor = PBColor(244, 245, 246);
//    UIButton *searchBtn = [UIButton z_bgImageButton:[UIImage imageNamed:@"Examine_search_background"]];
//    [searchBtn addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
//    [bgV addSubview:searchBtn];
//    [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(bgV).offset(8);
//        make.left.equalTo(bgV).offset(10);
//        make.right.equalTo(bgV).offset(-10);
//        make.height.equalTo(@36);
//    }];
    UIButton *typeBtn = [[UIButton alloc] init];
    [typeBtn addTarget:self action:@selector(typeAction) forControlEvents:UIControlEventTouchUpInside];
    typeBtn.backgroundColor = [UIColor whiteColor];
    [bgV addSubview:typeBtn];
    [typeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgV).offset(8);
        make.left.right.equalTo(bgV);
        make.height.equalTo(@52);
    }];
    UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"grouping_person"]];
    [typeBtn addSubview:imageV];
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(typeBtn).offset(14);
        make.left.equalTo(typeBtn).offset(12);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    UILabel *titleL = [UILabel z_labelWithText:@"按角色选择" Color:PBColor(97, 111, 125) isBold:YES Font:14];
    [typeBtn addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(typeBtn);
        make.left.equalTo(imageV.mas_right).offset(10);
        make.height.equalTo(@20);
    }];
    UIImageView *rightV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"examine_arrow"]];
    [typeBtn addSubview:rightV];
    [rightV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(typeBtn);
        make.right.equalTo(typeBtn).offset(-27);
        make.size.mas_equalTo(CGSizeMake(16, 16));
    }];
    return bgV;
}
- (void)searchAction {
    
    
}
- (void)arrowTopActon {
    PBSelectedPersonController *selectPersonVC = [[PBSelectedPersonController alloc] init];
    selectPersonVC.selectList = self.selectList;
    [self.navigationController pushViewController:selectPersonVC animated:YES];
}
- (void)typeAction {
    PBRoleViewController *roleVC = [[PBRoleViewController alloc] init];
    roleVC.projectModel = self.projectModel;
    roleVC.isMultiSelect = self.isMultiSelect;
    roleVC.selectList = self.selectList;
    [self.navigationController pushViewController:roleVC animated:YES];  
}
- (void)loadData {
    [YJProgressHUD showProgress:@"" inView:self.view];
    [[PBNetworkTools sharedTools] GetProjectUserSortByLetterRoleId:@"" andOrganizeID:self.projectModel.organizeid andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
        if (error) {
            [YJProgressHUD showMessage:@"加载成员信息失败" inView:self.view];
            return;
        }
        NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
        PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str];
        if (networkModel.Ret == 1) {
            [YJProgressHUD hide];
            NSArray *arr = [networkModel.Data valueForKey:@"List"];
            for (NSDictionary *obj in arr) {
                NSMutableArray *arrM = [[NSMutableArray alloc] init];
                for (NSDictionary *user in [obj valueForKey:@"Users"]) {
                    PBUserModel *model = [PBUserModel yy_modelWithDictionary:user];
                    for (PBUserModel *selectModel in self.selectList) {
                        if ([model.UserId isEqualToString:selectModel.UserId]) {
                            model.isSelect = YES;
                        }
                    }
                    [arrM addObject:model];
                }
                NSDictionary *dict = @{@"FirstLetter":[obj valueForKey:@"FirstLetter"], @"Users":arrM.copy};
                [self.dataList addObject:dict];
            }
            [self.tableView reloadData];
        }else {
            [YJProgressHUD showMessage:networkModel.Msg inView:self.view];
        }
    }];
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataList.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *dict = self.dataList[section];
    NSArray *arr = [dict valueForKey:@"Users"];
    return arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isMultiSelect) {
        PBMultiSelectPersonCell *cell = [tableView dequeueReusableCellWithIdentifier:MScellID forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSDictionary *dict = self.dataList[indexPath.section];
        NSArray *arr = [dict valueForKey:@"Users"];
        cell.userModel = arr[indexPath.row];
        return cell;
    }else {
        PBPersonVCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSDictionary *dict = self.dataList[indexPath.section];
        NSArray *arr = [dict valueForKey:@"Users"];
        cell.userModel = arr[indexPath.row];
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 16;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    //最后一个
    return 0.01;
}
 
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}
#pragma mark 右侧索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    NSMutableArray *arrM = [[NSMutableArray alloc] init];
    for (NSDictionary *dict in self.dataList) {
        [arrM addObject:[dict valueForKey:@"FirstLetter"]];
    };
    return arrM.copy;
}
 
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = PBColor(244, 245, 246);
    NSDictionary *dict = self.dataList[section];
    UILabel *nameL = [UILabel z_labelWithText:[dict valueForKey:@"FirstLetter"] Color:PBColor(97, 111, 125) isBold:YES Font:12];
    [bgView addSubview:nameL];
    [nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(bgView);
        make.left.equalTo(bgView).offset(20);
    }];
    return bgView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.dataList[indexPath.section];
    NSArray *arr = [dict valueForKey:@"Users"];
    PBUserModel *userModel = arr[indexPath.row];
    if (self.isMultiSelect) {
        if (userModel.isSelect) {
            userModel.isSelect = NO;
            for (PBUserModel *model in self.selectList) {
                if ([model.UserId isEqualToString:userModel.UserId]) {
                    [self.selectList removeObject:model];
                    break;
                }
            }
        }else {
            userModel.isSelect = YES;
            [self.selectList addObject:userModel];
        }
        if (self.selectList.count > 0) {
            self.bottomV.hidden = NO;
            self.arrowBtn.hidden = NO;
            self.collectionView.hidden = NO;
            self.determineBtn.hidden = NO;
            [self.determineBtn setTitle:[NSString stringWithFormat:@"确定(%zd)",self.selectList.count] forState:UIControlStateNormal];
        }else {
            self.bottomV.hidden = YES;
            self.arrowBtn.hidden = YES;
            self.collectionView.hidden = YES;
            self.determineBtn.hidden = YES;
        }
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self.collectionView reloadData];
    }else {
        [PBNoteCenter postNotificationName:PBNoteCenterUpdatePerson object:@[userModel]];
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark - UICollectionViewDelegate, UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.selectList.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PBUserCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionCell forIndexPath:indexPath];
    cell.userModel = self.selectList[indexPath.item];
    return cell;
}
- (void)setProjectModel:(PBProjectModel *)projectModel {
    _projectModel = projectModel;
}
- (void)setIsMultiSelect:(BOOL)isMultiSelect {
    _isMultiSelect = isMultiSelect;
}
- (NSMutableArray *)dataList {
    if (_dataList == nil) {
        _dataList = [[NSMutableArray alloc] init];
    }
    return _dataList;
}
- (void)setSelectList:(NSMutableArray *)selectList {
    _selectList = selectList;
    if (_selectList == nil) {
        _selectList = [[NSMutableArray alloc] init];
    }
}
/*
#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