Files
peko-ios/YuMi/Modules/YMLogin/View/RegionListViewController.m
2024-12-20 19:05:43 +08:00

517 lines
18 KiB
Objective-C

//
// RegionListViewController.m
// YuMi
//
// Created by P on 2024/12/13.
//
#import "RegionListViewController.h"
#import "PIHomeCategoryTitleModel.h"
#import "LoginFullInfoPresenter.h"
#import "RegionListInfo.h"
@interface RegionListCell : UICollectionViewCell
@property(nonatomic, strong) RegionListInfo *regionModel;
@property(nonatomic, strong) PIHomeCategoryTitleModel *tagModel;
@property(nonatomic, strong) UIView *selectedBG;
@property(nonatomic, strong) UIImageView *selectedMark;
@property(nonatomic, strong) NetImageView *flagImageView;
@property(nonatomic, strong) UILabel *titleLabel;
+ (void)registerTo:(UICollectionView *)collectionView;
+ (RegionListCell *)cellFor:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath;
@end
@implementation RegionListCell
+ (void)registerTo:(UICollectionView *)collectionView {
[collectionView registerClass:[self class] forCellWithReuseIdentifier:NSStringFromClass([self class])];
}
+ (RegionListCell *)cellFor:(UICollectionView *)collectionView
indexPath:(NSIndexPath *)indexPath {
RegionListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([self class])
forIndexPath:indexPath];
// [cell.contentView setBackgroundColor:UIColorFromRGB(0xF7F7F7)];
// [cell setCornerRadius:19];
return cell;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self.contentView addSubview:self.flagImageView];
[self.contentView addSubview:self.titleLabel];
[self.contentView addSubview:self.selectedMark];
[self.flagImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(14);
make.centerY.mas_equalTo(self.contentView);
make.size.mas_equalTo(CGSizeMake(30, 22));
}];
[self.selectedMark mas_makeConstraints:^(MASConstraintMaker *make) {
make.trailing.mas_equalTo(-14);
make.centerY.mas_equalTo(self.contentView);
make.size.mas_equalTo(CGSizeMake(20, 20));
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.flagImageView.mas_trailing).offset(6);
make.trailing.mas_equalTo(self.selectedMark.mas_leading).offset(-6);
make.centerY.mas_equalTo(self.contentView);
make.height.mas_equalTo(20);
}];
UIView *line = [[UIView alloc] init];
line.backgroundColor = UIColorFromRGB(0xe4e4e4);
[self.contentView addSubview:line];
[line mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.contentView);
make.trailing.mas_equalTo(self.selectedMark);
make.leading.mas_equalTo(self.flagImageView);
make.height.mas_equalTo(1);
}];
// [self.contentView addSubview:self.selectedBG];
// [self.selectedBG mas_makeConstraints:^(MASConstraintMaker *make) {
// make.edges.mas_equalTo(self.contentView);
// }];
}
return self;
}
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
// self.selectedBG.hidden = !selected;
self.selectedMark.hidden = !selected;
// if (selected) {
// [self.contentView setBackgroundColor:UIColorFromRGB(0xFFFAF5)];
// } else {
// [self.contentView setBackgroundColor:UIColorFromRGB(0xF7F7F7)];
// }
}
- (void)prepareForReuse {
[super prepareForReuse];
self.flagImageView.image = nil;
}
- (void)setTagModel:(PIHomeCategoryTitleModel *)tagModel {
_tagModel = tagModel;
self.flagImageView.imageUrl = tagModel.icon;
self.titleLabel.text = tagModel.name;
[self.flagImageView mas_updateConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(14);
make.centerY.mas_equalTo(self.contentView);
make.size.mas_equalTo(CGSizeMake(40, 30));
}];
}
- (void)setRegionModel:(RegionListInfo *)regionModel {
_regionModel = regionModel;
self.flagImageView.imageUrl = regionModel.icon;
self.titleLabel.text = regionModel.name;
}
#pragma mark -
- (UIView *)selectedBG {
if (!_selectedBG) {
_selectedBG = [[UIView alloc] init];
[_selectedBG setCornerRadius:19
corners:kCALayerMaxXMaxYCorner | kCALayerMaxXMinYCorner | kCALayerMinXMaxYCorner | kCALayerMinXMinYCorner
borderWidth:1
borderColor:UIColorFromRGB(0xFF8C03)];
_selectedBG.hidden = YES;
}
return _selectedBG;
}
- (NetImageView *)flagImageView {
if (!_flagImageView) {
_flagImageView = [[NetImageView alloc] init];
_flagImageView.contentMode = UIViewContentModeScaleAspectFit;
}
return _flagImageView;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [UILabel labelInitWithText:@"" font:kFontMedium(15) textColor:UIColorFromRGB(0x313131)];
}
return _titleLabel;
}
- (UIImageView *)selectedMark {
if (!_selectedMark) {
_selectedMark = [[UIImageView alloc] initWithImage:kImage(@"region_list_selected")];
_selectedMark.hidden = YES;
}
return _selectedMark;
}
@end
@interface RegionListViewController () <UISearchBarDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
@property(nonatomic, assign) NSInteger type;
@property(nonatomic, assign) CGFloat displayHeight;
@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) UISearchBar *searchBar;
@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, strong) UIButton *comfirmButton;
@property(nonatomic, strong) UIView *bgView;
@property(nonatomic, strong) UIView *emptyView;
@property(nonatomic, copy) NSArray <PIHomeCategoryTitleModel*> *tagDataSource;
@property(nonatomic, strong) PIHomeCategoryTitleModel *selectedTag;
@property(nonatomic, copy) NSArray <RegionListInfo *> *regionList;
@property(nonatomic, strong) NSMutableArray <RegionListInfo *> *filteredRegionList;
@property(nonatomic, strong) RegionListInfo *selectedRegion;
@property(nonatomic, assign) NSInteger defaultIndex;
@end
@implementation RegionListViewController
- (LoginFullInfoPresenter *)createPresenter {
return [[LoginFullInfoPresenter alloc] init];
}
- (instancetype)initForRegionList:(NSArray <RegionListInfo *>*)regionInfoList {
if (self = [super init]) {
self.type = 1;
self.displayHeight = KScreenHeight/2;
self.regionList = regionInfoList;
self.filteredRegionList = [regionInfoList mutableCopy];
[self.collectionView reloadData];
}
return self;
}
- (instancetype)initForHomeTags:(NSArray <PIHomeCategoryTitleModel*>*)tags
currentItem:(nonnull PIHomeCategoryTitleModel *)itemModel {
if (self = [super init]) {
self.type = 2;
self.modalPresentationStyle = UIModalPresentationOverFullScreen;
self.displayHeight = KScreenHeight*2/3;
NSMutableArray *temp = @[].mutableCopy;
for (PIHomeCategoryTitleModel *model in tags) {
if (![NSString isEmpty:model.regionId]) {
[temp addObject:model];
}
}
self.defaultIndex = [temp indexOfObject:itemModel];
self.selectedTag = itemModel;
self.tagDataSource = temp;
[self.collectionView reloadData];
[self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:self.defaultIndex inSection:0]
animated:NO
scrollPosition:UICollectionViewScrollPositionNone];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = YMLocalizedString(@"1.0.31_text_1");
switch (self.type) {
case 1:
[self setupUIForFullDisplay];
break;
case 2:
[self setupUIForHalfScreen];
break;
default:
break;
}
}
- (void)setupUIForFullDisplay {
self.view.backgroundColor = [UIColor whiteColor];
// self.comfirmButton.frame = CGRectMake(0, 0, 48, 44);
// UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.comfirmButton];
// self.navigationItem.rightBarButtonItem = rightBarButtonItem;
[self.view addSubview:self.titleLabel];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(10);
make.centerX.mas_equalTo(self.view);
make.height.mas_equalTo(34);
}];
[self.view addSubview:self.comfirmButton];
[self.comfirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.titleLabel);
make.trailing.mas_equalTo(-15);
}];
[self.view addSubview:self.searchBar];
[self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10);
make.leading.trailing.mas_equalTo(self.view).inset(16);
make.height.mas_equalTo(34);
}];
[self.view addSubview:self.collectionView];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.searchBar.mas_bottom).offset(8);
make.bottom.leading.trailing.mas_equalTo(0);
}];
[self.view addSubview:self.emptyView];
[self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.collectionView).offset(8);
make.centerX.mas_equalTo(self.collectionView);
make.size.mas_equalTo(CGSizeMake(120, 120));
}];
}
- (void)setupUIForHalfScreen {
self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
[self.view addSubview:self.titleLabel];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(KScreenHeight - self.displayHeight);
make.centerX.mas_equalTo(self.view);
make.height.mas_equalTo(34);
}];
[self.view addSubview:self.comfirmButton];
[self.comfirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.titleLabel);
make.trailing.mas_equalTo(-15);
}];
[self.view addSubview:self.collectionView];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(8);
make.bottom.leading.trailing.mas_equalTo(0);
}];
[self.view insertSubview:self.bgView belowSubview:self.titleLabel];
[self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.titleLabel).offset(-10);
make.leading.bottom.trailing.mas_equalTo(self.view);
}];
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[b addTarget:self action:@selector(didTapSpace) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:b atIndex:0];
[b mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.leading.trailing.mas_equalTo(self.view);
make.height.mas_equalTo(KScreenHeight/2);
}];
}
- (void)didTapComfirm {
if (self.type == 1) {
if (self.handleTapRegionComfirm) {
self.handleTapRegionComfirm(self.selectedRegion);
}
} else if (self.type == 2) {
if (self.handleTapCategiryComfirm) {
self.handleTapCategiryComfirm(self.selectedTag);
}
}
[self dismissViewControllerAnimated:YES completion:^{}];
}
- (void)didTapSpace {
@kWeakify(self);
[self dismissViewControllerAnimated:YES completion:^{
@kStrongify(self);
if (self.handleDismiss) {
self.handleDismiss();
}
}];
}
#pragma mark - UISearchBarDelegate
// 当点击 "Search" 按钮时触发
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder]; // 收起键盘
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if (searchText.length == 0) {
// 输入为空时,显示原始数据
self.filteredRegionList = [self.regionList copy];
} else {
// 过滤数据
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", searchText];
self.filteredRegionList = [[self.regionList filteredArrayUsingPredicate:predicate] mutableCopy];
}
[self.collectionView reloadData];
self.emptyView.hidden = self.filteredRegionList.count > 0;
}
// 点击取消按钮时重置数据
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[searchBar resignFirstResponder];
searchBar.text = @"";
self.filteredRegionList = [self.regionList mutableCopy];
[self.collectionView reloadData];
}
#pragma mark -
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (self.type == 1) {
return self.filteredRegionList.count;
} else if (self.type == 2) {
return self.tagDataSource.count;
}
return 0;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
RegionListCell *cell = [RegionListCell cellFor:collectionView indexPath:indexPath];
if (self.type == 1) {
cell.regionModel = [self.filteredRegionList xpSafeObjectAtIndex:indexPath.row];
} else if (self.type == 2) {
cell.tagModel = [self.tagDataSource xpSafeObjectAtIndex:indexPath.row];
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (self.type == 1) {
self.selectedRegion = [self.filteredRegionList xpSafeObjectAtIndex:indexPath.row];
} else if (self.type == 2) {
self.selectedTag = [self.tagDataSource xpSafeObjectAtIndex:indexPath.row];
if (self.handleTapCategiryComfirm) {
self.handleTapCategiryComfirm(self.selectedTag);
}
}
}
#pragma mark -
- (UISearchBar *)searchBar {
if (!_searchBar) {
UIColor *bgColor = UIColorFromRGB(0xf4f4f4);
_searchBar = [[UISearchBar alloc] init];
_searchBar.delegate = self;
_searchBar.placeholder = YMLocalizedString(@"XPHomeSearchNavView1");
_searchBar.barTintColor = bgColor;
[_searchBar setCornerRadius:34/2];
if (@available(iOS 13.0, *)) {
_searchBar.searchTextField.backgroundColor = bgColor;
_searchBar.searchTextField.textColor = UIColorFromRGB(0x313131);
} else {
UITextField *searchField = [self findTextFieldInSearchBar:_searchBar];
if (searchField) {
searchField.backgroundColor = bgColor;
searchField.textColor = UIColorFromRGB(0x313131);
}
}
}
return _searchBar;
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// if (self.type == 1) {
layout.itemSize = CGSizeMake(KScreenWidth - 30, 48);
// } else if (self.type == 2) {
// layout.itemSize = CGSizeMake(kGetScaleWidth(166), 38);
// }
layout.sectionInset = UIEdgeInsetsMake(0, 16, kSafeAreaBottomHeight, 16);
layout.minimumLineSpacing = 0;
layout.minimumInteritemSpacing = 10;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 44, KScreenWidth, KScreenHeight-44)
collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor whiteColor];
[RegionListCell registerTo:_collectionView];
}
return _collectionView;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [UILabel labelInitWithText:YMLocalizedString(@"1.0.31_text_2")
font:kFontSemibold(16)
textColor:UIColorFromRGB(0x313131)];
}
return _titleLabel;
}
- (UIButton *)comfirmButton {
if (!_comfirmButton) {
_comfirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_comfirmButton setTitleColor:UIColorFromRGB(0xFF8C03) forState:UIControlStateNormal];
[_comfirmButton setTitle:YMLocalizedString(@"TTAlertConfig0") forState:UIControlStateNormal];
[_comfirmButton addTarget:self
action:@selector(didTapComfirm)
forControlEvents:UIControlEventTouchUpInside];
}
return _comfirmButton;
}
- (UITextField *)findTextFieldInSearchBar:(UISearchBar *)searchBar {
for (UIView *subview in searchBar.subviews) {
for (UIView *nestedSubview in subview.subviews) {
if ([nestedSubview isKindOfClass:[UITextField class]]) {
return (UITextField *)nestedSubview;
}
}
}
return nil;
}
- (UIView *)bgView {
if (!_bgView) {
UIView *bg = [[UIView alloc] init];
bg.backgroundColor = [UIColor whiteColor];
[bg setCornerRadius:16 cornerMask:kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner];
bg.userInteractionEnabled = NO;
_bgView = bg;
}
return _bgView;
}
- (UIView *)emptyView {
if (!_emptyView) {
_emptyView = [[UIView alloc] init];
UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImageConstant defaultEmptyPlaceholder_UFO]];
[_emptyView addSubview:icon];
[icon mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.centerX.mas_equalTo(_emptyView);
make.size.mas_equalTo(CGSizeMake(80, 80));
}];
UILabel *emptyTitle = [UILabel labelInitWithText:YMLocalizedString(@"XPMomentsRecommendViewController0")
font:kFontRegular(14)
textColor:[UIColor colorWithWhite:0 alpha:0.4]];
emptyTitle.textAlignment = NSTextAlignmentCenter;
[_emptyView addSubview:emptyTitle];
[emptyTitle mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(_emptyView);
make.top.mas_equalTo(icon.mas_bottom).offset(6);
make.height.mas_equalTo(22);
}];
_emptyView.hidden = YES;
}
return _emptyView;
}
@end