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
//
//  PBIssueHeaderView.m
//  IphoneBIMe
//
//  Created by zjf on 2019/1/10.
//  Copyright © 2019 ProBIM. All rights reserved.
//
 
#import "PBIssueHeaderView.h"
#import "PBImageCollectionViewCell.h"
#import "PBProjectModel.h"
 
static NSString *const ImageCellID = @"ImageCellID";
@interface PBIssueHeaderView ()<UICollectionViewDelegate, UICollectionViewDataSource, SDPhotoBrowserDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, assign) BOOL isAddIssue;
@property (nonatomic, strong) UIButton *selectBtn;
@end
@implementation PBIssueHeaderView
 
- (instancetype)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}
 
- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    UILabel *titleL = [UILabel z_labelWithText:@"关联图片(选填)" Color:PromptColor isBold:NO Font:TitleFontSize];
    [self addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(13);
        make.left.equalTo(self).offset(16);
        make.height.equalTo(@22);
        make.width.equalTo(@132);
    }];
 
    self.selectBtn = [UIButton z_textButton:@"请选择" fontSize:MarkedFontSize normalColor:TitleColor];
    [self.selectBtn addTarget:self action:@selector(chooseImageAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.selectBtn];
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-16);
        make.centerY.equalTo(titleL);
    }];
    
    UIView *lineV = [[UIView alloc] init];
    lineV.backgroundColor = PBColor(240, 240, 240);
    [self addSubview:lineV];
    [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleL.mas_bottom).offset(16);
        make.left.equalTo(self).offset(20);
        make.right.equalTo(self).offset(-20);
        make.height.equalTo(@1);
    }];
    
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout.alloc init];
    layout.itemSize = CGSizeMake(80, 80);
    layout.minimumLineSpacing = 10;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout];
    [self.collectionView registerClass:[PBImageCollectionViewCell class] forCellWithReuseIdentifier:ImageCellID];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineV.mas_bottom).offset(16);
        make.left.equalTo(self).offset(33);
        make.right.equalTo(self).offset(-32);
        make.height.equalTo(@80);
    }];
    self.collectionView.hidden = YES;
    UIView *bottomV = [[UIView alloc] init];
    bottomV.backgroundColor = PBColor(242, 242, 242);
    [self addSubview:bottomV];
    [bottomV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.equalTo(@10);
    }];
}
- (void)chooseImageAction:(UIButton *)button {
    if (self.ChooseImageBlock) {
        self.ChooseImageBlock();
    }
}
 
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.isAddIssue) {
        if (self.viewPointImage != nil) {
            return self.imageArr.count + 1;
        }else {
            return self.imageArr.count;
        }
    }else {
        if (self.viewPointImageUrl != nil && ![self.viewPointImageUrl  isEqualToString:@""]) {
            return self.imageDocArr.count + 1;
        }else {
            return self.imageDocArr.count;
        }
    }
}
 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PBImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ImageCellID forIndexPath:indexPath];
    if (self.isAddIssue) {
        if (indexPath.row == 0 && self.viewPointImage != nil) {
            cell.image = self.viewPointImage;
            cell.viewPointFlag = @"viewPoint";
        }else {
            NSInteger index = indexPath.row;
            if (self.viewPointImage) {
                index = indexPath.row - 1;
            }
            cell.image = self.imageArr[index];
        }
    }else {
        if (indexPath.row == 0 && self.viewPointImageUrl != nil && ![self.viewPointImageUrl isEqualToString:@""]) {
            cell.imageUrl = self.viewPointImageUrl;
            cell.viewPointFlag = @"viewPoint";
        }else {
            NSInteger index = indexPath.row;
                    if (self.viewPointImageUrl != nil && ![self.viewPointImageUrl isEqualToString:@""]) {
                        index = indexPath.row - 1;
                    }
                    NSDictionary *RelationDoc = self.imageDocArr[index];
            //        NSString *url = [NSString stringWithFormat:@"%@/api/Doc/GetHideFile?ProjectID=%@&FileId=%@&FileType=Issue",BimUrl,self.projectMdoel.bimcomposerid,[RelationDoc valueForKey:@"TargetID"]];
                    NSString *url = [NSString stringWithFormat:@"%@%@",BaseUrl,[RelationDoc valueForKey:@"bf_path"]];
                    cell.imageUrl = url;
                    cell.roleType = self.roleType;
        }
    }
    cell.DeleteImageBlock = ^{
        if (self.DeleteImageBlock) {
            NSInteger index = indexPath.row;
            if (self.isAddIssue) {
                if (self.viewPointImage) {
                    index = indexPath.row - 1;
                }
            }else {
                if (self.viewPointImageUrl) {
                    index = indexPath.row - 1;
                }
            }
            self.DeleteImageBlock(index);
        }  
    };
    cell.OpenViewPointBlock = ^{
        if (self.OpenViewPointBlock) {
            self.OpenViewPointBlock();
        }
    };
    return cell;
}
 
 
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    SDPhotoBrowser *photoBrowser = [SDPhotoBrowser new];
    photoBrowser.delegate = self;
    photoBrowser.currentImageIndex = indexPath.item;
    if (self.isAddIssue) {
        if (self.viewPointImage) {
            photoBrowser.imageCount = self.imageArr.count + 1;
        }else {
            photoBrowser.imageCount = self.imageArr.count;
        }
    }else {
        if (self.viewPointImageUrl) {
            photoBrowser.imageCount = self.imageDocArr.count + 1;
        }else {
            photoBrowser.imageCount = self.imageDocArr.count;
        }
    }
    photoBrowser.sourceImagesContainerView = self.collectionView;
    [photoBrowser show];
}
 
 
#pragma mark  SDPhotoBrowserDelegate
// 返回临时占位图片(即原来的小图)
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    // 不建议用此种方式获取小图,这里只是为了简单实现展示而已
    PBImageCollectionViewCell *cell = (PBImageCollectionViewCell *)[self collectionView:self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
    if (self.isAddIssue) {
        return cell.imageV.image;
    }else {
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:cell.imageUrl]];
        UIImage *image = [UIImage imageWithData:data];;
        return image;
    }
}
 
- (void)setImageArr:(NSMutableArray *)imageArr {
    _imageArr = imageArr;
    self.isAddIssue = YES;
    [self.collectionView reloadData];
    if (imageArr.count == 0 && self.viewPointImage == nil) {
        self.collectionView.hidden = YES;
    }else {
        self.collectionView.hidden = NO;
    }
}
 
- (void)setImageDocArr:(NSMutableArray *)imageDocArr {
    _imageDocArr = imageDocArr;
    self.isAddIssue = NO;
    [self.collectionView reloadData];
    if (imageDocArr.count == 0 && [self.viewPointImageUrl isEqualToString:@""]) {
        self.collectionView.hidden = YES;
    }else {
        self.collectionView.hidden = NO;
    }
}
 
- (void)setProjectMdoel:(PBProjectModel *)projectMdoel {
    _projectMdoel = projectMdoel;
}
- (void)setRoleType:(RoleType)roleType {
    _roleType = roleType;
    if (roleType == CREATOR) {
        [self.selectBtn setTitleColor:TitleColor forState:UIControlStateNormal];
    }else {
        [self.selectBtn setTitleColor:PromptColor forState:UIControlStateNormal];
    }
}
- (void)setViewPointImage:(UIImage *)viewPointImage {
    _viewPointImage = viewPointImage;
}
- (void)setViewPointImageUrl:(NSString *)viewPointImageUrl {
    _viewPointImageUrl = viewPointImageUrl;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
 
@end