// // XPRoomSearchRecordViewController.m // xplan-ios // // Created by GreenLand on 2022/9/5. // #import "XPRoomSearchRecordViewController.h" ///Third #import ///Tool #import "ThemeColor.h" #import "AccountInfoStorage.h" #import "TTPopup.h" #import "NSArray+Safe.h" ///View #import "XPHomeAttentionCollectionViewCell.h" #import "XPHomeSearchRecordCell.h" #import "XPRoomViewController.h" #import "XPMineUserInfoViewController.h" ///P #import "XPInRoomRecordPresenter.h" #import "XPInRoomRecordProtocol.h" ///Model #import "XPMineFootPrintModel.h" #import "HomeEveryOneSearchModel.h" NSString * const XPConstSearchRecordStoreKey = @"XPConstSearchRecordStoreKey"; @interface XPRoomSearchRecordViewController () @property (nonatomic, strong) UIStackView *contentStackView; ///大家都在搜 @property (nonatomic, strong) UIView *allSearchContentView; ///大家都在搜标题 @property (nonatomic, strong) UILabel *everyOneTitle; @property (nonatomic, strong) UICollectionView *everyOneCollectionView; ///搜索记录容器 @property (nonatomic, strong) UIView *searchContentView; ///进房记录容器 @property (nonatomic, strong) UIView *roomContentView; ///填充占位view @property (nonatomic, strong) UIView *placeHolderView; ///搜索记录 @property (nonatomic, strong) UILabel *recordTitle; ///清空搜索记录 @property (nonatomic, strong) UIButton *clearRecordBtn; ///进房记录 @property (nonatomic, strong) UILabel *roomTitle; ///清空进房记录 @property (nonatomic, strong) UIButton *clearRoomBtn; ///搜索记录列表 @property (nonatomic, strong) UICollectionView *searchCollectionView; ///进房记录列表 @property (nonatomic, strong) UICollectionView *roomCollectionView; ///搜索记录数据源 @property (nonatomic, strong) NSMutableArray *searchList; ///进房记录数据源 @property (nonatomic, strong) NSMutableArray *inRoomList; ///大家都在搜数据源 @property (nonatomic, strong) NSMutableArray *everyoneSearchList; @end @implementation XPRoomSearchRecordViewController - (XPInRoomRecordPresenter *)createPresenter { return [[XPInRoomRecordPresenter alloc] init]; } - (void)viewDidLoad { [super viewDidLoad]; [self initSubViews]; [self initSubViewConstraints]; [self initSearchRecordData]; [self.presenter getInRoomRecordList]; [self.presenter getEveryoneSearchList]; } - (void)initSearchRecordData { [self.searchList removeAllObjects]; NSArray *array = [self searchRecordList]; [self.searchList addObjectsFromArray:array]; self.searchContentView.hidden = array.count == 0; [self.searchCollectionView reloadData]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } #pragma mark - Private Method - (void)initSubViews { [self.view addSubview:self.contentStackView]; [self.contentStackView addArrangedSubview:self.allSearchContentView]; [self.contentStackView addArrangedSubview:self.searchContentView]; [self.contentStackView addArrangedSubview:self.roomContentView]; [self.contentStackView addArrangedSubview:self.placeHolderView]; [self.allSearchContentView addSubview:self.everyOneTitle]; [self.allSearchContentView addSubview:self.everyOneCollectionView]; [self.searchContentView addSubview:self.recordTitle]; [self.searchContentView addSubview:self.clearRecordBtn]; [self.searchContentView addSubview:self.searchCollectionView]; [self.roomContentView addSubview:self.roomTitle]; [self.roomContentView addSubview:self.clearRoomBtn]; [self.roomContentView addSubview:self.roomCollectionView]; } - (void)initSubViewConstraints { [self.contentStackView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.right.left.bottom.mas_equalTo(self.view); }]; [self.allSearchContentView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.mas_equalTo(0); make.bottom.mas_equalTo(self.everyOneCollectionView); }]; [self.everyOneTitle mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.allSearchContentView).mas_offset(16); make.top.mas_equalTo(self.allSearchContentView).mas_offset(16); make.height.mas_equalTo(20); }]; [self.everyOneCollectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self.allSearchContentView); make.top.mas_equalTo(self.everyOneTitle.mas_bottom).mas_offset(8); make.height.mas_equalTo(26); }]; [self.searchContentView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(0); make.bottom.mas_equalTo(self.searchCollectionView.mas_bottom); }]; [self.recordTitle mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(16); make.top.mas_equalTo(self.searchContentView).mas_offset(20); make.height.mas_equalTo(20); }]; [self.clearRecordBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(-16); make.centerY.mas_equalTo(self.recordTitle); make.width.height.mas_equalTo(30); }]; [self.searchCollectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self.view); make.height.mas_equalTo(26); make.top.mas_equalTo(self.recordTitle.mas_bottom).mas_offset(8); }]; [self.roomContentView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(0); make.bottom.mas_equalTo(self.roomCollectionView.mas_bottom); }]; [self.roomTitle mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(16); make.top.mas_equalTo(self.roomContentView).mas_offset(20); make.height.mas_equalTo(20); }]; [self.clearRoomBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(-16); make.centerY.mas_equalTo(self.roomTitle); make.width.height.mas_equalTo(30); }]; [self.roomCollectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self.view); make.height.mas_equalTo(75); make.top.mas_equalTo(self.roomTitle.mas_bottom).mas_offset(8); }]; } #pragma mark - UICollectionViewDelegate - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (collectionView == self.searchCollectionView) { return self.searchList.count; } else if (collectionView == self.roomCollectionView) { return self.inRoomList.count; } else { return self.everyoneSearchList.count; } } - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { if (collectionView == self.searchCollectionView) { XPHomeSearchRecordCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPHomeSearchRecordCell class]) forIndexPath:indexPath]; cell.recordString = [self.searchList safeObjectAtIndex1:indexPath.row]; return cell; } else if (collectionView == self.everyOneCollectionView) { XPHomeSearchRecordCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPHomeSearchRecordCell class]) forIndexPath:indexPath]; HomeEveryOneSearchModel *model = [self.everyoneSearchList safeObjectAtIndex1:indexPath.row]; cell.recordString = model.word; return cell; } else { XPHomeAttentionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPHomeAttentionCollectionViewCell class]) forIndexPath:indexPath]; cell.recordRoom = self.inRoomList[indexPath.row]; return cell; } } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (collectionView == self.searchCollectionView) { NSString *str = self.searchList[indexPath.row]; if (self.delegate && [self.delegate respondsToSelector:@selector(xPRoomSearchRecordViewControllerSearchWithWord:)]) { [self.delegate xPRoomSearchRecordViewControllerSearchWithWord:str]; } } else if (collectionView == self.everyOneCollectionView) { HomeEveryOneSearchModel *model = [self.everyoneSearchList safeObjectAtIndex1:indexPath.row]; if (self.delegate && [self.delegate respondsToSelector:@selector(xPRoomSearchRecordViewControllerEveryoneSearch: sid:)]) { [self.delegate xPRoomSearchRecordViewControllerEveryoneSearch:model.word sid:model.sid]; } } else { XPMineFootPrintModel *resultModel = self.inRoomList[indexPath.row]; [self dismissViewControllerAnimated:YES completion:nil]; if (resultModel.valid) { [XPRoomViewController openRoom:resultModel.roomUid viewController:self.presentingViewController]; } else { XPMineUserInfoViewController * infoVC = [[XPMineUserInfoViewController alloc] init]; infoVC.uid = resultModel.roomUid.integerValue; [(UINavigationController *)self.presentingViewController pushViewController:infoVC animated:YES]; } } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if (collectionView == self.searchCollectionView) { NSString *str = [self.searchList safeObjectAtIndex1:indexPath.row]; CGFloat width = [str boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]} context:nil].size.width; return CGSizeMake(width + 26, 26); } else if(collectionView == self.everyOneCollectionView){ HomeEveryOneSearchModel *model = [self.everyoneSearchList safeObjectAtIndex1:indexPath.row]; CGFloat width = [model.word boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]} context:nil].size.width; return CGSizeMake(width + 26, 26); } else { return CGSizeMake(56, 75); } } #pragma mark - 搜索记录操作 /// 获取搜索记录 - (NSArray *)searchRecordList { NSString *key = [NSString stringWithFormat:@"%@_%@", XPConstSearchRecordStoreKey, [AccountInfoStorage instance].getUid]; NSArray *list = [[NSUserDefaults standardUserDefaults] objectForKey:key]; return list; } /// 保存搜索记录 - (void)storeSearchRecord:(NSString *)record { if (record == nil || record.length <= 0) { return; } NSString *key = [NSString stringWithFormat:@"%@_%@", XPConstSearchRecordStoreKey, [AccountInfoStorage instance].getUid]; NSMutableArray *list = [[[NSUserDefaults standardUserDefaults] objectForKey:key] mutableCopy]; if (list == nil) { list = [[NSMutableArray alloc] init]; } [list removeObject:record];//去重 [list insertObject:record atIndex:0];//插入 if (list.count > 20) {//最多保存20条记录 [list removeLastObject]; } [[NSUserDefaults standardUserDefaults] setValue:list forKey:key]; [[NSUserDefaults standardUserDefaults] synchronize]; } /// 清空搜索记录 - (void)cleanSearchRecord { NSString *key = [NSString stringWithFormat:@"%@_%@", XPConstSearchRecordStoreKey, [AccountInfoStorage instance].getUid]; [[NSUserDefaults standardUserDefaults] removeObjectForKey:key]; [[NSUserDefaults standardUserDefaults] synchronize]; } - (void)updateSearchRecord { [self initSearchRecordData]; } ///获取大家都在搜列表成功 - (void)getEveryoneSearchListSuccess:(NSArray *)array { [self.everyoneSearchList removeAllObjects]; [self.everyoneSearchList addObjectsFromArray:array]; self.allSearchContentView.hidden = array.count == 0; [self.everyOneCollectionView reloadData]; } ///获取进房记录成功 - (void)getInRoomRecordSuccess:(NSArray *)array { [self.inRoomList removeAllObjects]; [self.inRoomList addObjectsFromArray:array]; [self.roomCollectionView reloadData]; if (array.count == 0) { self.roomContentView.hidden = YES; } } ///清除进房记录成功 - (void)cleanInRoomRecordSuccess { [self.inRoomList removeAllObjects]; [self.roomCollectionView reloadData]; self.roomContentView.hidden = YES; } #pragma mark - action - (void)onClearSearchRecordClick:(UIButton *)sender { TTAlertConfig * config = [[TTAlertConfig alloc] init]; config.confirmButtonConfig.title = @"清空"; config.cancelButtonConfig.title = @"手滑了"; config.message = @"确定清空搜索记录吗?"; [TTPopup alertWithConfig:config confirmHandler:^{ [self cleanSearchRecord]; [self.searchList removeAllObjects]; [self.searchCollectionView reloadData]; self.searchContentView.hidden = YES; } cancelHandler:^{ }]; } - (void)onClearRoomClick:(UIButton *)sender { TTAlertConfig * config = [[TTAlertConfig alloc] init]; config.confirmButtonConfig.title = @"清空"; config.cancelButtonConfig.title = @"手滑了"; config.message = @"确定清空进房记录吗?"; [TTPopup alertWithConfig:config confirmHandler:^{ [self.presenter cleanInRoomRecord]; } cancelHandler:^{ }]; } #pragma mark - getter - (UIStackView *)contentStackView { if (!_contentStackView) { _contentStackView = [[UIStackView alloc] init]; _contentStackView.axis = UILayoutConstraintAxisVertical; _contentStackView.distribution = UIStackViewDistributionFill; _contentStackView.alignment = UIStackViewAlignmentFill; _contentStackView.alignment = UIStackViewAlignmentCenter; _contentStackView.spacing = 0; } return _contentStackView; } - (UIView *)allSearchContentView { if (!_allSearchContentView) { _allSearchContentView = [[UIView alloc] init]; _allSearchContentView.hidden = YES; } return _allSearchContentView; } - (UILabel *)everyOneTitle { if (!_everyOneTitle) { UILabel *label = [[UILabel alloc] init]; label.text = @"大家都在搜"; label.font = [UIFont systemFontOfSize:14 weight:UIFontWeightBold]; label.textColor = [ThemeColor mainTextColor]; _everyOneTitle = label; } return _everyOneTitle; } - (UICollectionView *)everyOneCollectionView{ if (!_everyOneCollectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.minimumLineSpacing = 8; layout.minimumInteritemSpacing = 8; layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15); layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; _everyOneCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _everyOneCollectionView.showsHorizontalScrollIndicator = NO; _everyOneCollectionView.dataSource = self; _everyOneCollectionView.delegate = self; _everyOneCollectionView.backgroundColor = [UIColor clearColor]; [_everyOneCollectionView registerClass:[XPHomeSearchRecordCell class] forCellWithReuseIdentifier:NSStringFromClass([XPHomeSearchRecordCell class])]; } return _everyOneCollectionView; } - (UILabel *)recordTitle { if (!_recordTitle) { UILabel *label = [[UILabel alloc] init]; label.text = @"搜索记录"; label.font = [UIFont systemFontOfSize:14 weight:UIFontWeightBold]; label.textColor = [ThemeColor mainTextColor]; _recordTitle = label; } return _recordTitle; } - (UILabel *)roomTitle { if (!_roomTitle) { UILabel *label = [[UILabel alloc] init]; label.text = @"进房记录"; label.font = [UIFont systemFontOfSize:14 weight:UIFontWeightBold]; label.textColor = [ThemeColor mainTextColor]; _roomTitle = label; } return _roomTitle; } - (UIButton *)clearRecordBtn { if (!_clearRecordBtn) { UIButton *btn = [[UIButton alloc] init]; [btn setImage:[UIImage imageNamed:@"sessionList_clear"] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(onClearSearchRecordClick:) forControlEvents:UIControlEventTouchUpInside]; _clearRecordBtn = btn; } return _clearRecordBtn; } - (UIButton *)clearRoomBtn { if (!_clearRoomBtn) { UIButton *btn = [[UIButton alloc] init]; [btn setImage:[UIImage imageNamed:@"sessionList_clear"] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(onClearRoomClick:) forControlEvents:UIControlEventTouchUpInside]; _clearRoomBtn = btn; } return _clearRoomBtn; } - (UICollectionView *)searchCollectionView{ if (!_searchCollectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.minimumLineSpacing = 8; layout.minimumInteritemSpacing = 8; layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15); layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; _searchCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _searchCollectionView.showsHorizontalScrollIndicator = NO; _searchCollectionView.dataSource = self; _searchCollectionView.delegate = self; _searchCollectionView.backgroundColor = [UIColor clearColor]; [_searchCollectionView registerClass:[XPHomeSearchRecordCell class] forCellWithReuseIdentifier:NSStringFromClass([XPHomeSearchRecordCell class])]; } return _searchCollectionView; } - (UICollectionView *)roomCollectionView{ if (!_roomCollectionView) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.minimumLineSpacing = 8; layout.minimumInteritemSpacing = 0; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15); _roomCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; _roomCollectionView.showsHorizontalScrollIndicator = NO; _roomCollectionView.dataSource = self; _roomCollectionView.delegate = self; _roomCollectionView.backgroundColor = [UIColor clearColor]; [_roomCollectionView registerClass:[XPHomeAttentionCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([XPHomeAttentionCollectionViewCell class])]; } return _roomCollectionView; } - (NSMutableArray *)searchList { if (!_searchList) { _searchList = [NSMutableArray array]; } return _searchList; } - (NSMutableArray *)inRoomList { if (!_inRoomList) { _inRoomList = [NSMutableArray array]; } return _inRoomList; } - (NSMutableArray *)everyoneSearchList { if (!_everyoneSearchList) { _everyoneSearchList = [NSMutableArray array]; } return _everyoneSearchList; } - (UIView *)searchContentView { if (!_searchContentView) { _searchContentView = [[UIView alloc] init]; } return _searchContentView; } - (UIView *)roomContentView { if (!_roomContentView) { _roomContentView = [[UIView alloc] init]; } return _roomContentView; } - (UIView *)placeHolderView { if (!_placeHolderView) { _placeHolderView = [[UIView alloc] init]; _placeHolderView.backgroundColor = [UIColor clearColor]; [_placeHolderView setContentHuggingPriority:UILayoutPriorityDragThatCanResizeScene forAxis:UILayoutConstraintAxisHorizontal]; [_placeHolderView setContentCompressionResistancePriority:UILayoutPriorityFittingSizeLevel forAxis:UILayoutConstraintAxisHorizontal]; } return _placeHolderView; } @end