Files
yinmeng-ios/xplan-ios/Main/Home/View/XPRoomSearchRecordViewController.m
2022-09-09 17:17:37 +08:00

393 lines
14 KiB
Objective-C

//
// XPRoomSearchRecordViewController.m
// xplan-ios
//
// Created by GreenLand on 2022/9/5.
//
#import "XPRoomSearchRecordViewController.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "ThemeColor.h"
#import "AccountInfoStorage.h"
#import "TTPopup.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"
NSString * const XPConstSearchRecordStoreKey = @"XPConstSearchRecordStoreKey";
@interface XPRoomSearchRecordViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, XPInRoomRecordProtocol>
///搜索记录容器
@property (nonatomic, strong) UIView *searchContentView;
///进房记录容器
@property (nonatomic, strong) UIView *roomContentView;
///搜索记录
@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;
@end
@implementation XPRoomSearchRecordViewController
- (XPInRoomRecordPresenter *)createPresenter {
return [[XPInRoomRecordPresenter alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initSubViews];
[self initSubViewConstraints];
[self initSearchRecordData];
[self.presenter getInRoomRecordList];
}
- (void)initSearchRecordData {
[self.searchList removeAllObjects];
NSArray *array = [self searchRecordList];
[self.searchList addObjectsFromArray:array];
[self.searchCollectionView reloadData];
if (!array.count) {
[self.searchContentView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(0);
}];
self.searchContentView.hidden = YES;
} else {
self.searchContentView.hidden = NO;
[self.searchContentView mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(30+26+8);
}];
}
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
#pragma mark - Private Method
- (void)initSubViews {
[self.view addSubview:self.searchContentView];
[self.view addSubview:self.roomContentView];
[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.searchContentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(0);
make.top.mas_equalTo(0);
make.height.mas_equalTo(30+26+8);
}];
[self.recordTitle mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(16);
make.top.mas_equalTo(self.searchContentView).mas_offset(10);
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.top.mas_equalTo(self.searchContentView.mas_bottom).mas_offset(24);
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);
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 {
return self.inRoomList.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 objectAtIndex:indexPath.row];
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 {
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 objectAtIndex: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 {
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)getInRoomRecordSuccess:(NSArray<XPMineFootPrintModel *> *)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 mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(0);
}];
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
- (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;
}
- (UIView *)searchContentView {
if (!_searchContentView) {
_searchContentView = [[UIView alloc] init];
}
return _searchContentView;
}
- (UIView *)roomContentView {
if (!_roomContentView) {
_roomContentView = [[UIView alloc] init];
}
return _roomContentView;
}
@end