新增搜索

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

@@ -13,6 +13,8 @@
#import "KBHistoryMoreCell.h"
#import "KBSearchResultVC.h"
#import "UICollectionViewLeftAlignedLayout.h"
#import "KBSearchVM.h"
#import "KBShopThemeModel.h"
static NSString * const kTagCellId = @"KBTagCell";
@@ -37,9 +39,10 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
//
@property (nonatomic, strong) NSMutableArray<NSString *> *historyWords; //
@property (nonatomic, strong) NSArray<NSDictionary *> *recommendItems; // title/price
@property (nonatomic, copy) NSArray<KBShopThemeModel *> *recommendedThemes; //
@property (nonatomic, assign) BOOL historyExpanded; //
@property (nonatomic, assign) CGFloat lastCollectionWidth; //
@property (nonatomic, strong) KBSearchVM *viewModel;
@end
@implementation KBSearchVC
@@ -89,16 +92,10 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
KBLocalized(@"水果萝卜"),
KBLocalized(@"熟冻帝王蟹"),
KBLocalized(@"赣南脐橙")] mutableCopy];
self.recommendItems = @[
@{@"title":@"Dopamine", @"price":@"20"},
@{@"title":@"Dopamine", @"price":@"20"},
@{@"title":@"Dopamine", @"price":@"20"},
@{@"title":@"Dopamine", @"price":@"20"},
@{@"title":@"Dopamine", @"price":@"20"},
@{@"title":@"Dopamine", @"price":@"20"},
];
self.recommendedThemes = @[];
[self.collectionView reloadData];
[self fetchRecommendedThemes];
}
- (void)viewDidLayoutSubviews {
@@ -252,7 +249,7 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
NSArray *list = [self currentDisplayHistory];
return list.count; //
}
return self.recommendItems.count;
return self.recommendedThemes.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
@@ -269,8 +266,10 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
}
}
KBSkinCardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kSkinCellId forIndexPath:indexPath];
NSDictionary *it = self.recommendItems[indexPath.item];
[cell configWithTitle:it[@"title"] imageURL:nil price:it[@"price"]];
KBShopThemeModel *model = self.recommendedThemes[indexPath.item];
[cell configWithTitle:model.themeName ?: @""
imageURL:model.themePreviewImageUrl
price:[self priceTextForTheme:model]];
return cell;
}
@@ -398,6 +397,28 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
- (void)onTapBack { [self.navigationController popViewControllerAnimated:YES]; }
- (void)fetchRecommendedThemes {
__weak typeof(self) weakSelf = self;
[self.viewModel fetchRecommendedThemesWithCompletion:^(NSArray<KBShopThemeModel *> * _Nullable themes, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
NSLog(@"[KBSearchVC] fetch recommended failed: %@", error);
return;
}
weakSelf.recommendedThemes = themes ?: @[];
NSIndexSet *sections = [NSIndexSet indexSetWithIndex:KBSearchSectionRecommend];
[weakSelf.collectionView reloadSections:sections];
});
}];
}
- (NSString *)priceTextForTheme:(KBShopThemeModel *)model {
if (model.themePrice > 0.0) {
return [NSString stringWithFormat:@"%.2f", model.themePrice];
}
return @"0";
}
- (UICollectionViewLeftAlignedLayout *)flowLayout {
if (!_flowLayout) {
_flowLayout = [[UICollectionViewLeftAlignedLayout alloc] init];
@@ -423,4 +444,11 @@ typedef NS_ENUM(NSInteger, KBSearchSection) {
return _collectionView;
}
- (KBSearchVM *)viewModel {
if (!_viewModel) {
_viewModel = [[KBSearchVM alloc] init];
}
return _viewModel;
}
@end