新增搜索
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user