// // 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() @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