Files
keyboard/keyBoard/Class/Search/VC/KBSearchResultVC.m

237 lines
8.2 KiB
Mathematica
Raw Normal View History

2025-11-08 20:04:50 +08:00
//
// KBSearchResultVC.m
// keyBoard
//
#import "KBSearchResultVC.h"
#import "KBSearchBarView.h"
#import "KBSkinCardCell.h"
2025-12-17 20:30:01 +08:00
#import "KBSearchVM.h"
#import "KBSearchThemeModel.h"
2025-12-17 20:37:53 +08:00
#import "KBSkinDetailVC.h"
2025-11-08 20:04:50 +08:00
static NSString * const kResultCellId = @"KBSkinCardCell";
@interface KBSearchResultVC ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
2025-11-08 20:49:05 +08:00
// +
@property (nonatomic, strong) UIView *topBar;
@property (nonatomic, strong) UIButton *backButton;
2025-11-08 20:04:50 +08:00
@property (nonatomic, strong) KBSearchBarView *searchBarView;
//
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
2025-12-17 20:30:01 +08:00
//
@property (nonatomic, strong) NSMutableArray<KBSearchThemeModel *> *resultItems;
@property (nonatomic, strong) KBSearchVM *viewModel;
2025-11-08 20:04:50 +08:00
@end
@implementation KBSearchResultVC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//
2025-11-08 20:49:05 +08:00
[self.view addSubview:self.topBar];
[self.topBar addSubview:self.backButton];
[self.topBar addSubview:self.searchBarView];
2025-11-08 20:04:50 +08:00
[self.view addSubview:self.collectionView];
2025-11-08 20:49:05 +08:00
// Masonry
[self.topBar mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_top).offset(KB_STATUSBAR_HEIGHT + 8);
make.left.right.equalTo(self.view);
make.height.mas_equalTo(44);
}];
[self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.topBar).offset(12);
make.centerY.equalTo(self.topBar);
make.width.mas_equalTo(28);
make.height.mas_equalTo(36);
}];
2025-11-08 20:04:50 +08:00
[self.searchBarView mas_makeConstraints:^(MASConstraintMaker *make) {
2025-11-08 20:49:05 +08:00
make.centerY.equalTo(self.backButton);
make.left.equalTo(self.backButton.mas_right).offset(12);
2025-11-17 15:06:05 +08:00
// make.width.mas_equalTo(315);
2025-11-08 20:49:05 +08:00
make.height.mas_equalTo(36);
2025-11-17 15:06:05 +08:00
// make.right.lessThanOrEqualTo(self.topBar).offset(-16);
make.right.equalTo(self.view).offset(-16);
2025-11-08 20:04:50 +08:00
}];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
2025-11-08 20:49:05 +08:00
make.top.equalTo(self.topBar.mas_bottom).offset(12);
2025-11-08 20:04:50 +08:00
make.left.right.bottom.equalTo(self.view);
}];
//
if (self.defaultKeyword.length > 0) {
[self.searchBarView updateKeyword:self.defaultKeyword];
[self performSearch:self.defaultKeyword];
}
}
2025-11-11 16:46:05 +08:00
// Base VC
2025-11-08 20:49:05 +08:00
2025-11-08 20:04:50 +08:00
#pragma mark - Private
2025-12-17 20:30:01 +08:00
///
2025-11-08 20:04:50 +08:00
- (void)performSearch:(NSString *)keyword {
2025-12-17 20:30:01 +08:00
__weak typeof(self) weakSelf = self;
[self.viewModel searchThemesWithName:keyword completion:^(NSArray<KBSearchThemeModel *> * _Nullable themes, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
NSLog(@"[KBSearchResultVC] search failed: %@", error);
return;
}
[weakSelf.resultItems removeAllObjects];
if (themes.count > 0) {
[weakSelf.resultItems addObjectsFromArray:themes];
}
[weakSelf.collectionView reloadData];
});
}];
2025-11-08 20:04:50 +08:00
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.resultItems.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
KBSkinCardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kResultCellId forIndexPath:indexPath];
2025-12-17 20:30:01 +08:00
KBSearchThemeModel *model = self.resultItems[indexPath.item];
[cell configWithTitle:model.themeName ?: @""
imageURL:model.themePreviewImageUrl
price:[self priceTextForTheme:model]];
2025-11-08 20:04:50 +08:00
return cell;
}
2025-12-17 20:37:53 +08:00
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
KBSearchThemeModel *model = self.resultItems[indexPath.item];
NSString *themeId = [model.themeId stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (themeId.length == 0) { return; }
KBSkinDetailVC *vc = [[KBSkinDetailVC alloc] init];
vc.themeId = themeId;
[self.navigationController pushViewController:vc animated:YES];
}
2025-11-08 20:04:50 +08:00
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//
CGFloat width = collectionView.bounds.size.width;
CGFloat inset = 16; //
CGFloat spacing = 12; //
CGFloat w = floor((width - inset * 2 - spacing) / 2.0);
2025-11-17 15:06:05 +08:00
// CGFloat h = w * 0.75 + 8 + 20 + 10 + 6 + 8; // KBSkinCardCell
return CGSizeMake(w, KBFit(197));
2025-11-08 20:04:50 +08:00
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(8, 16, 20, 16);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 12;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 16;
}
#pragma mark - Lazy
- (KBSearchBarView *)searchBarView {
if (!_searchBarView) {
_searchBarView = [[KBSearchBarView alloc] init];
_searchBarView.placeholder = @"Themes";
2025-11-10 15:38:30 +08:00
KBWeakSelf
2025-11-08 20:04:50 +08:00
_searchBarView.onSearch = ^(NSString * _Nonnull keyword) {
[weakSelf performSearch:keyword];
};
}
return _searchBarView;
}
2025-12-17 20:30:01 +08:00
- (NSString *)priceTextForTheme:(KBSearchThemeModel *)model {
if (model.themePrice > 0.0) {
return [NSString stringWithFormat:@"%.2f", model.themePrice];
}
return @"0";
}
2025-11-08 20:49:05 +08:00
- (UIView *)topBar {
if (!_topBar) {
_topBar = [[UIView alloc] init];
_topBar.backgroundColor = [UIColor whiteColor];
}
return _topBar;
}
- (UIButton *)backButton {
if (!_backButton) {
_backButton = [UIButton buttonWithType:UIButtonTypeSystem];
UIImage *img = nil;
if (@available(iOS 13.0, *)) {
img = [UIImage systemImageNamed:@"chevron.left"];
}
if (img) {
[_backButton setImage:img forState:UIControlStateNormal];
} else {
[_backButton setTitle:@"<" forState:UIControlStateNormal];
_backButton.titleLabel.font = [UIFont systemFontOfSize:22 weight:UIFontWeightSemibold];
}
[_backButton setTintColor:[UIColor blackColor]];
[_backButton addTarget:self action:@selector(onTapBack) forControlEvents:UIControlEventTouchUpInside];
}
return _backButton;
}
- (void)onTapBack { [self.navigationController popViewControllerAnimated:YES]; }
2025-11-08 20:04:50 +08:00
- (UICollectionViewFlowLayout *)flowLayout {
if (!_flowLayout) {
_flowLayout = [[UICollectionViewFlowLayout alloc] init];
_flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
}
return _flowLayout;
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
// cell
[_collectionView registerClass:KBSkinCardCell.class forCellWithReuseIdentifier:kResultCellId];
}
return _collectionView;
}
2025-12-17 20:30:01 +08:00
- (NSMutableArray<KBSearchThemeModel *> *)resultItems {
2025-11-08 20:04:50 +08:00
if (!_resultItems) {
_resultItems = [NSMutableArray array];
}
return _resultItems;
}
2025-12-17 20:30:01 +08:00
- (KBSearchVM *)viewModel {
if (!_viewModel) {
_viewModel = [[KBSearchVM alloc] init];
}
return _viewModel;
}
2025-11-08 20:04:50 +08:00
@end