新增搜索

This commit is contained in:
2025-12-17 20:30:01 +08:00
parent 904a6c932a
commit 1ecb7d60e5
8 changed files with 293 additions and 59 deletions

View File

@@ -6,6 +6,8 @@
#import "KBSearchResultVC.h"
#import "KBSearchBarView.h"
#import "KBSkinCardCell.h"
#import "KBSearchVM.h"
#import "KBSearchThemeModel.h"
static NSString * const kResultCellId = @"KBSkinCardCell";
@@ -20,8 +22,9 @@ static NSString * const kResultCellId = @"KBSkinCardCell";
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
//
@property (nonatomic, strong) NSMutableArray<NSDictionary *> *resultItems; // @{title, price}
//
@property (nonatomic, strong) NSMutableArray<KBSearchThemeModel *> *resultItems;
@property (nonatomic, strong) KBSearchVM *viewModel;
@end
@@ -66,9 +69,6 @@ static NSString * const kResultCellId = @"KBSkinCardCell";
if (self.defaultKeyword.length > 0) {
[self.searchBarView updateKeyword:self.defaultKeyword];
[self performSearch:self.defaultKeyword];
} else {
//
[self loadMockData];
}
}
@@ -76,23 +76,22 @@ static NSString * const kResultCellId = @"KBSkinCardCell";
#pragma mark - Private
///
///
- (void)performSearch:(NSString *)keyword {
// 10
[self.resultItems removeAllObjects];
for (int i = 0; i < 10; i++) {
[self.resultItems addObject:@{ @"title": @"Dopamine", @"price": @"20" }];
}
[self.collectionView reloadData];
}
///
- (void)loadMockData {
[self.resultItems removeAllObjects];
for (int i = 0; i < 12; i++) {
[self.resultItems addObject:@{ @"title": @"Dopamine", @"price": @"20" }];
}
[self.collectionView reloadData];
__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];
});
}];
}
#pragma mark - UICollectionViewDataSource
@@ -107,8 +106,10 @@ static NSString * const kResultCellId = @"KBSkinCardCell";
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
KBSkinCardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kResultCellId forIndexPath:indexPath];
NSDictionary *it = self.resultItems[indexPath.item];
[cell configWithTitle:it[@"title"] imageURL:nil price:it[@"price"]];
KBSearchThemeModel *model = self.resultItems[indexPath.item];
[cell configWithTitle:model.themeName ?: @""
imageURL:model.themePreviewImageUrl
price:[self priceTextForTheme:model]];
return cell;
}
@@ -150,6 +151,13 @@ static NSString * const kResultCellId = @"KBSkinCardCell";
return _searchBarView;
}
- (NSString *)priceTextForTheme:(KBSearchThemeModel *)model {
if (model.themePrice > 0.0) {
return [NSString stringWithFormat:@"%.2f", model.themePrice];
}
return @"0";
}
- (UIView *)topBar {
if (!_topBar) {
_topBar = [[UIView alloc] init];
@@ -199,11 +207,18 @@ static NSString * const kResultCellId = @"KBSkinCardCell";
return _collectionView;
}
- (NSMutableArray<NSDictionary *> *)resultItems {
- (NSMutableArray<KBSearchThemeModel *> *)resultItems {
if (!_resultItems) {
_resultItems = [NSMutableArray array];
}
return _resultItems;
}
- (KBSearchVM *)viewModel {
if (!_viewModel) {
_viewModel = [[KBSearchVM alloc] init];
}
return _viewModel;
}
@end