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
//
//  PBProjectSearchView.m
//  IphoneBIMe
//
//  Created by zjf on 2020/6/5.
//  Copyright © 2020 ProBIM. All rights reserved.
//
 
#import "PBProjectSearchView.h"
#import "PBProjectTableViewCell.h"
#import "PBProjectModel.h"
#import "PBTabBarController.h"
#import "LBKeyWordLayout.h"
#import "KeyWordCell.h"
@interface PBProjectSearchView()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataList;
@property (nonatomic, weak) UISearchBar *searchBar;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (strong, nonatomic) LBKeyWordLayout *layout;
@property (strong, nonatomic) NSArray *items;
@end
@implementation PBProjectSearchView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    UIImageView *searchBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search_bg"]];
    [self addSubview:searchBg];
    [searchBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self).offset(10);
        make.right.equalTo(self).offset(-10);
        make.height.equalTo(@46);
    }];
    
    UIButton *backBtn = [UIButton z_bgImageButton:[UIImage imageNamed:@"arrow_left"]];
    [backBtn addTarget:self action:@selector(backActon) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(searchBg).offset(10);
        make.left.equalTo(searchBg).offset(10);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    UISearchBar *searchBar = [[UISearchBar alloc] init];
    [searchBar setImage:[[UIImage alloc] init] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    searchBar.backgroundColor = [UIColor clearColor];
    searchBar.backgroundImage = [[UIImage alloc]init];
    if (@available(iOS 13.0, *)){
         searchBar.searchTextField.backgroundColor = UIColor.clearColor;
    }else{
        UITextField*searchField = [searchBar valueForKey:@"_searchField"];
        searchField.backgroundColor = UIColor.clearColor;
    }
    searchBar.placeholder = @"搜索";
    searchBar.delegate = self;
    [self addSubview:searchBar];
    [searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(searchBg);
        make.left.equalTo(searchBg).offset(34);
        make.right.equalTo(searchBg).offset(-10);
        make.height.equalTo(@46);
    }];
    [searchBar becomeFirstResponder];
    self.searchBar = searchBar;
    UIView *linV = [[UIView alloc] init];
    linV.backgroundColor = PBColor(244, 245, 246);
    [self addSubview:linV];
    [linV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(searchBg.mas_bottom).offset(15);
        make.left.right.equalTo(self);
        make.height.equalTo(@6);
    }];
    UILabel *historyL = [UILabel z_labelWithText:@"搜索历史" Color:PBColor(40, 58, 79) isBold:YES Font:16];
    [self addSubview:historyL];
    [historyL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(linV.mas_bottom).offset(15);
        make.left.equalTo(self).offset(15);
        make.size.mas_equalTo(CGSizeMake(68, 24));
    }];
    UIButton *clearBtn = [UIButton z_bgImageButton:[UIImage imageNamed:@"delete"]];
    [clearBtn addTarget:self action:@selector(clearhistory) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:clearBtn];
    [clearBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(historyL);
        make.right.equalTo(self).offset(-18);
        make.size.mas_equalTo(CGSizeMake(24, 24));
    }];
    self.items = [self readSearchHistory];
//    _historicalRecordV = [[MSSAutoresizeLabelFlow alloc] initWithFrame:CGRectMake(0, 150, PBScreenWidth, 200) titles:array selectedHandler:^(NSUInteger index, NSString *title) {
//        NSLog(@"%@",title);
//        self.searchBar.text = title;
//        [self searchActionWithTitle:title];
//    }];
    self.layout = [[LBKeyWordLayout alloc] init];
    self.layout.estimatedItemSize = CGSizeMake(80, 30);
    self.layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 150, PBScreenWidth, 400) collectionViewLayout:self.layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[KeyWordCell class] forCellWithReuseIdentifier:@"KeyWordCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self addSubview:self.collectionView];
    [self addSubview:self.tableView];
    self.tableView.hidden = YES;
}
- (void)searchActionWithTitle:(NSString *)title {
    [self.searchBar resignFirstResponder];
    self.tableView.hidden = NO;
    [self loadProjectList];
    NSArray *arr = [self readSearchHistory];
    NSMutableArray *arrM = [[NSMutableArray alloc] init];
    for (NSString *obj in arr) {
        if (![obj isEqualToString:title]) {
            [arrM addObject:obj];
        }
    }
    [arrM insertObject:title atIndex:0];
    [self writeSearchHistoryWithData:arrM.copy];
    self.items = arrM.copy;
    [self.collectionView reloadData];
}
- (void)backActon {
    if (self.cancelSearchBlock) {
        self.cancelSearchBlock();
    }
}
- (void)clearhistory {
    [self deleteSearchHistory];
}
#pragma makr - UISearchBarDelegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self searchActionWithTitle:searchBar.text];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    if (!self.tableView.hidden) {
        self.tableView.hidden = YES;
    }
}
- (void)loadProjectList {
//    [YJProgressHUD showCustomAnimation:@"" inview:self];
    [YJProgressHUD showProgress:@"" inView:self];
    [[PBNetworkTools sharedTools] getProjectListWithKeyword:self.searchBar.text andIsPublic:@"" andSort:@"CreateDate desc" andSkip:@"0" andtake:@"999" andOnlyFavorite:@"" andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) {
        if(error) {
            NSLog(@"%@",error);
            [YJProgressHUD showMessage:@"加载项目列表失败" inView:self];
            return;
        }
        NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str];
        if (networkModel.Ret == 1) {
            [YJProgressHUD hide];
            self.dataList = [NSArray yy_modelArrayWithClass:[PBProjectModel class] json:[networkModel.Data valueForKey:@"rows"]];
            [self.tableView reloadData];
        }else {
            [YJProgressHUD showMessage:networkModel.Msg inView:self];
        }
    }];
}
#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataList.count;
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cellId";
    PBProjectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell)
        cell = [[PBProjectTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    cell.projectModel = self.dataList[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PBProjectModel *projectModel = self.dataList[indexPath.row];
    if (self.projectDetailBlock) {
        self.projectDetailBlock(projectModel);
    }
}
#pragma mark - GET
 
- (UITableView *)tableView {
    if (!_tableView)
    {
        CGFloat bottomViewH = 71.f - 20;
        if (IS_IPHONE_X) {
            bottomViewH = bottomViewH + IPHONE_X_BOTTOM;
        }
        _tableView = [[UITableView alloc] init];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(67);
            make.left.right.bottom.equalTo(self);
            make.bottom.equalTo(self).offset(-bottomViewH);
        }];
    }
    return _tableView;
}
 
#pragma mark - 读取本地历史记录
-(NSArray *)readSearchHistory
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [array lastObject];
    NSString *documentPath = [documents stringByAppendingPathComponent:@"searchHistory.plist"];
    NSArray *arr = [[NSArray alloc]initWithContentsOfFile:documentPath];
    return arr;
}
#pragma mark - 删除本地历史记录
-(void)deleteSearchHistory {
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [array lastObject];
    NSString *documentPath = [documents stringByAppendingPathComponent:@"searchHistory.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:documentPath error:nil];
    self.items = @[];
    [self.collectionView reloadData];
}
#pragma mark - 插入本地历史记录
- (void)writeSearchHistoryWithData:(NSArray *)history {
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [array lastObject];
    NSString *documentPath = [documents stringByAppendingPathComponent:@"searchHistory.plist"];
    [history writeToFile:documentPath atomically:YES];
}
#pragma mark - UICollectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.items.count;
}
 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    KeyWordCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"KeyWordCell" forIndexPath:indexPath];
    cell.maxWidthConstraint.constant = CGRectGetWidth(collectionView.bounds) - self.layout.sectionInset.left - self.layout.sectionInset.right - cell.layoutMargins.left - cell.layoutMargins.right - 10;
    cell.textLabel.numberOfLines = 1;
    cell.textLabel.text = self.items[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.searchBar.text = self.items[indexPath.row];
    [self searchActionWithTitle:self.items[indexPath.row]];
}
 
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
 
@end