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

427 lines
17 KiB
Mathematica
Raw Normal View History

2025-11-07 22:22:41 +08:00
//
// KBSearchVC.m
// keyBoard
//
// Created by Mac on 2025/11/7.
//
#import "KBSearchVC.h"
#import "KBSearchBarView.h"
#import "KBSearchSectionHeader.h"
#import "KBTagCell.h"
#import "KBSkinCardCell.h"
2025-11-08 20:04:50 +08:00
#import "KBHistoryMoreCell.h"
#import "KBSearchResultVC.h"
#import "UICollectionViewLeftAlignedLayout.h"
2025-11-07 22:22:41 +08:00
2025-11-08 20:04:50 +08:00
static NSString * const kTagCellId = @"KBTagCell";
2025-11-07 22:22:41 +08:00
static NSString * const kSkinCellId = @"KBSkinCardCell";
2025-11-08 20:04:50 +08:00
static NSString * const kHeaderId = @"KBSearchSectionHeader";
static NSString * const kMoreCellId = @"KBHistoryMoreCell";
static NSString * const kMoreToken = @"__KB_MORE__"; //
2025-11-07 22:22:41 +08:00
typedef NS_ENUM(NSInteger, KBSearchSection) {
KBSearchSectionHistory = 0,
KBSearchSectionRecommend = 1,
};
@interface KBSearchVC ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
2025-11-08 20:49:05 +08:00
// +
@property (nonatomic, strong) UIView *topBar;
@property (nonatomic, strong) UIButton *backButton;
2025-11-07 22:22:41 +08:00
@property (nonatomic, strong) KBSearchBarView *searchBarView;
//
@property (nonatomic, strong) UICollectionView *collectionView;
2025-11-08 20:04:50 +08:00
@property (nonatomic, strong) UICollectionViewLeftAlignedLayout *flowLayout;
2025-11-07 22:22:41 +08:00
//
@property (nonatomic, strong) NSMutableArray<NSString *> *historyWords; //
@property (nonatomic, strong) NSArray<NSDictionary *> *recommendItems; // title/price
2025-11-08 20:04:50 +08:00
@property (nonatomic, assign) BOOL historyExpanded; //
@property (nonatomic, assign) CGFloat lastCollectionWidth; //
2025-11-07 22:22:41 +08:00
@end
@implementation KBSearchVC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// UI
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-07 22:22:41 +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);
}];
[self.searchBarView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.backButton);
make.left.equalTo(self.backButton.mas_right).offset(12);
2025-11-17 14:53:23 +08:00
// make.width.mas_equalTo(315);
make.right.equalTo(self.view).offset(-16);
2025-11-08 20:49:05 +08:00
make.height.mas_equalTo(36);
make.right.lessThanOrEqualTo(self.topBar).offset(-16);
}];
2025-11-07 22:22:41 +08:00
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
2025-11-17 15:39:03 +08:00
make.top.equalTo(self.topBar.mas_bottom).offset(0);
2025-11-07 22:22:41 +08:00
make.left.right.bottom.equalTo(self.view);
}];
//
2025-11-17 20:07:39 +08:00
self.historyWords = [@[KBLocalized(@"果冻橙"),
KBLocalized(@"芒果"),
KBLocalized(@"有机水果卷心菜"),
KBLocalized(@"水果萝卜"),
KBLocalized(@"熟冻帝王蟹"),
KBLocalized(@"赣南脐橙")] mutableCopy];
2025-11-07 22:22:41 +08:00
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.collectionView reloadData];
}
2025-11-08 20:04:50 +08:00
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGFloat w = self.collectionView.bounds.size.width;
if (w > 0 && fabs(w - self.lastCollectionWidth) > 0.5) {
self.lastCollectionWidth = w;
[self.collectionView reloadData];
}
}
2025-11-11 16:46:05 +08:00
// BaseViewController
// VC Base
2025-11-08 20:49:05 +08:00
2025-11-07 22:22:41 +08:00
#pragma mark - Private
2025-11-08 20:04:50 +08:00
/// /
2025-11-07 22:22:41 +08:00
- (void)performSearch:(NSString *)kw {
NSString *trim = [kw stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (trim.length == 0) { return; }
2025-11-08 20:04:50 +08:00
//
for (NSInteger i = (NSInteger)self.historyWords.count - 1; i >= 0; i--) {
NSString *old = self.historyWords[i];
if ([old caseInsensitiveCompare:trim] == NSOrderedSame) {
[self.historyWords removeObjectAtIndex:i];
}
}
//
2025-11-07 22:22:41 +08:00
[self.historyWords insertObject:trim atIndex:0];
[self.collectionView reloadData];
}
2025-11-08 20:04:50 +08:00
///
- (void)openResultForKeyword:(NSString *)kw {
NSString *trim = [kw stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (trim.length == 0) { return; }
KBSearchResultVC *vc = [[KBSearchResultVC alloc] init];
vc.defaultKeyword = trim;
[self.navigationController pushViewController:vc animated:YES];
}
///
- (NSArray<NSString *> *)currentDisplayHistory {
if (self.historyExpanded) { return self.historyWords; }
if (self.collectionView.bounds.size.width <= 0) { return self.historyWords; }
CGFloat width = self.collectionView.bounds.size.width;
UIEdgeInsets inset = UIEdgeInsetsMake(8, 16, 12, 16);
CGFloat available = width - inset.left - inset.right;
CGFloat itemSpacing = 8.0; //
NSMutableArray<NSString *> *display = [NSMutableArray array];
NSInteger line = 1;
CGFloat lineUsed = 0.0; //
// 2 item display 便退
NSMutableArray<NSNumber *> *lineWidths = [NSMutableArray array];
NSMutableArray<NSNumber *> *lineIndexes = [NSMutableArray array];
for (NSInteger i = 0; i < self.historyWords.count; i++) {
NSString *text = self.historyWords[i];
CGSize sz = [KBTagCell sizeForText:text];
CGFloat w = sz.width;
CGFloat need = (lineUsed == 0 ? w : (lineUsed + itemSpacing + w));
if (need <= available) {
//
[display addObject:text];
if (line == 2) {
[lineWidths addObject:@(w)];
[lineIndexes addObject:@(display.count - 1)];
}
lineUsed = need;
continue;
}
//
if (line == 1) {
// 2
line = 2;
lineUsed = 0.0;
[lineWidths removeAllObjects];
[lineIndexes removeAllObjects];
// 2
if (w <= available) {
[display addObject:text];
[lineWidths addObject:@(w)];
[lineIndexes addObject:@(display.count - 1)];
lineUsed = w;
continue;
} else {
//
[display addObject:kMoreToken];
return display;
}
}
// 2
CGSize moreSize = [KBHistoryMoreCell fixedSize];
CGFloat moreNeed = (lineUsed == 0 ? moreSize.width : (lineUsed + itemSpacing + moreSize.width));
if (moreNeed <= available) {
[display addObject:kMoreToken];
return display;
}
// 退
while (lineIndexes.count > 0) {
NSNumber *lastIndexNum = lineIndexes.lastObject; // display
NSNumber *lastWidthNum = lineWidths.lastObject;
[display removeObjectAtIndex:lastIndexNum.integerValue];
[lineIndexes removeLastObject];
[lineWidths removeLastObject];
//
lineUsed = 0.0;
for (NSInteger k = 0; k < lineWidths.count; k++) {
CGFloat iw = lineWidths[k].doubleValue;
lineUsed = (k == 0 ? iw : (lineUsed + itemSpacing + iw));
}
//
moreNeed = (lineUsed == 0 ? moreSize.width : (lineUsed + itemSpacing + moreSize.width));
if (moreNeed <= available) {
[display addObject:kMoreToken];
return display;
}
}
//
[display addObject:kMoreToken];
return display;
}
//
return display;
}
2025-11-07 22:22:41 +08:00
///
- (void)clearHistory {
[self.historyWords removeAllObjects];
[self.collectionView reloadData];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 2; // +
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (section == KBSearchSectionHistory) {
2025-11-08 20:04:50 +08:00
NSArray *list = [self currentDisplayHistory];
return list.count; //
2025-11-07 22:22:41 +08:00
}
return self.recommendItems.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == KBSearchSectionHistory) {
2025-11-08 20:04:50 +08:00
NSArray *list = [self currentDisplayHistory];
NSString *text = list[indexPath.item];
if ([text isEqualToString:kMoreToken]) {
KBHistoryMoreCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kMoreCellId forIndexPath:indexPath];
return cell;
} else {
KBTagCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kTagCellId forIndexPath:indexPath];
[cell config:text];
return cell;
}
2025-11-07 22:22:41 +08:00
}
KBSkinCardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kSkinCellId forIndexPath:indexPath];
NSDictionary *it = self.recommendItems[indexPath.item];
[cell configWithTitle:it[@"title"] imageURL:nil price:it[@"price"]];
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if (kind == UICollectionElementKindSectionHeader) {
KBSearchSectionHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:kHeaderId forIndexPath:indexPath];
if (indexPath.section == KBSearchSectionHistory) {
// sizeForHeader 0
[header configWithTitle:@"Historical Search" showTrash:self.historyWords.count > 0];
2025-11-10 15:38:30 +08:00
KBWeakSelf
2025-11-07 22:22:41 +08:00
header.onTapTrash = ^{ [weakSelf clearHistory]; };
} else {
[header configWithTitle:@"Recommended Skin" showTrash:NO];
}
return header;
}
return [UICollectionReusableView new];
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat width = collectionView.bounds.size.width;
if (indexPath.section == KBSearchSectionHistory) {
2025-11-08 20:04:50 +08:00
NSArray *list = [self currentDisplayHistory];
NSString *t = list[indexPath.item];
if ([t isEqualToString:kMoreToken]) {
return [KBHistoryMoreCell fixedSize];
}
2025-11-07 22:22:41 +08:00
return [KBTagCell sizeForText:t];
}
//
CGFloat inset = 16; //
CGFloat spacing = 12; //
CGFloat w = floor((width - inset*2 - spacing) / 2.0);
// 0.75w + + +
2025-11-17 14:53:23 +08:00
// CGFloat h = w*0.75 + 8 + 20 + 10 + 6 + 8; //
return CGSizeMake(w, KBFit(197));
2025-11-07 22:22:41 +08:00
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
if (section == KBSearchSectionHistory) {
return UIEdgeInsetsMake(8, 16, 12, 16);
}
2025-11-17 15:39:03 +08:00
return UIEdgeInsetsMake(8, 16, 16, 16);
2025-11-07 22:22:41 +08:00
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return section == KBSearchSectionHistory ? 8 : 12;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return section == KBSearchSectionHistory ? 8 : 16;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
// header 0
if (section == KBSearchSectionHistory) {
return self.historyWords.count == 0 ? CGSizeZero : CGSizeMake(collectionView.bounds.size.width, 40);
}
2025-11-17 15:39:03 +08:00
return CGSizeMake(collectionView.bounds.size.width, 24);
2025-11-07 22:22:41 +08:00
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == KBSearchSectionHistory) {
2025-11-08 20:04:50 +08:00
NSArray *list = [self currentDisplayHistory];
NSString *kw = list[indexPath.item];
if ([kw isEqualToString:kMoreToken]) {
//
self.historyExpanded = YES;
[self.collectionView reloadData];
} else {
[self.searchBarView updateKeyword:kw];
[self performSearch:kw];
[self openResultForKeyword:kw];
}
2025-11-07 22:22:41 +08:00
}
}
#pragma mark - Lazy
- (KBSearchBarView *)searchBarView {
if (!_searchBarView) {
_searchBarView = [[KBSearchBarView alloc] init];
2025-11-17 15:06:05 +08:00
_searchBarView.bgCornerRadius = 18;
2025-11-07 22:22:41 +08:00
_searchBarView.placeholder = @"Themes";
2025-11-10 15:38:30 +08:00
KBWeakSelf
2025-11-07 22:22:41 +08:00
_searchBarView.onSearch = ^(NSString * _Nonnull keyword) {
2025-11-08 20:04:50 +08:00
// +
2025-11-07 22:22:41 +08:00
[weakSelf performSearch:keyword];
2025-11-08 20:04:50 +08:00
[weakSelf openResultForKeyword:keyword];
2025-11-07 22:22:41 +08:00
};
}
return _searchBarView;
}
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];
2025-11-08 11:48:06 +08:00
}
2025-11-08 20:49:05 +08:00
return _backButton;
2025-11-08 11:48:06 +08:00
}
2025-11-08 20:49:05 +08:00
- (void)onTapBack { [self.navigationController popViewControllerAnimated:YES]; }
2025-11-08 20:04:50 +08:00
- (UICollectionViewLeftAlignedLayout *)flowLayout {
2025-11-07 22:22:41 +08:00
if (!_flowLayout) {
2025-11-08 20:04:50 +08:00
_flowLayout = [[UICollectionViewLeftAlignedLayout alloc] init];
2025-11-07 22:22:41 +08:00
_flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
_flowLayout.sectionHeadersPinToVisibleBounds = NO;
}
return _flowLayout;
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
2025-11-17 20:26:39 +08:00
_collectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
2025-11-07 22:22:41 +08:00
// cell & header
[_collectionView registerClass:KBTagCell.class forCellWithReuseIdentifier:kTagCellId];
[_collectionView registerClass:KBSkinCardCell.class forCellWithReuseIdentifier:kSkinCellId];
2025-11-08 20:04:50 +08:00
[_collectionView registerClass:KBHistoryMoreCell.class forCellWithReuseIdentifier:kMoreCellId];
2025-11-07 22:22:41 +08:00
[_collectionView registerClass:KBSearchSectionHeader.class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kHeaderId];
}
return _collectionView;
}
@end