This commit is contained in:
启星
2025-08-08 10:49:36 +08:00
parent 6400cf78bb
commit b5ce3d580a
8780 changed files with 978183 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
//
// TUISearchBar.h
// Pods
//
// Created by harvy on 2020/12/23.
// Copyright © 2023 Tencent. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@class TUISearchBar;
@protocol TUISearchBarDelegate <NSObject>
@optional
- (void)searchBarDidEnterSearch:(TUISearchBar *)searchBar;
- (void)searchBarDidCancelClicked:(TUISearchBar *)searchBar;
- (void)searchBar:(TUISearchBar *)searchBar searchText:(NSString *)key;
@end
@interface TUISearchBar : UIView
@property(nonatomic, strong, readonly) UISearchBar *searchBar;
@property(nonatomic, weak) id<TUISearchBarDelegate> delegate;
// use weak, prevent circular references
@property(nonatomic, weak) UIViewController *parentVC;
- (void)setEntrance:(BOOL)isEntrance;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,135 @@
//
// TUISearchBar.m
// Pods
//
// Created by harvy on 2020/12/23.
// Copyright © 2023 Tencent. All rights reserved.
//
#import "TUISearchBar.h"
#import <TIMCommon/TIMDefine.h>
#import <TUICore/TUICore.h>
#import <TUICore/TUIDarkModel.h>
#import <TUICore/TUIGlobalization.h>
#import <TUICore/TUIThemeManager.h>
#import <TUICore/UIView+TUILayout.h>
#import "TUISearchViewController.h"
@interface TUISearchBar () <UISearchBarDelegate>
@property(nonatomic, strong) UISearchBar *searchBar;
@property(nonatomic, assign) BOOL isEntrance;
@end
@implementation TUISearchBar
@synthesize delegate;
- (void)setEntrance:(BOOL)isEntrance {
self.isEntrance = isEntrance;
[self setupViews];
}
- (UIColor *)bgColorOfSearchBar {
return TIMCommonDynamicColor(@"head_bg_gradient_start_color", @"#EBF0F6");
}
- (void)setupViews {
self.backgroundColor = self.bgColorOfSearchBar;
_searchBar = [[UISearchBar alloc] init];
_searchBar.placeholder = TIMCommonLocalizableString(Search);
_searchBar.backgroundImage = [UIImage new];
_searchBar.barTintColor = UIColor.redColor;
_searchBar.showsCancelButton = NO;
_searchBar.delegate = self;
_searchBar.showsCancelButton = !self.isEntrance;
_searchBar.searchTextField.textAlignment = isRTL()?NSTextAlignmentRight:NSTextAlignmentLeft;
if (@available(iOS 13.0, *)) {
_searchBar.searchTextField.backgroundColor = TIMCommonDynamicColor(@"search_textfield_bg_color", @"#FEFEFE");
}
[self addSubview:_searchBar];
[self enableCancelButton];
}
- (void)layoutSubviews {
[super layoutSubviews];
self.searchBar.frame = CGRectMake(10, 5, self.mm_w - 10 - 10, self.mm_h - 5 - 5);
[self updateSearchIcon];
}
- (void)updateSearchIcon {
if ([self.searchBar isFirstResponder] || self.searchBar.text.length || !self.isEntrance) {
[self.searchBar setPositionAdjustment:UIOffsetZero forSearchBarIcon:UISearchBarIconSearch];
self.backgroundColor = self.superview.backgroundColor;
} else {
[self.searchBar setPositionAdjustment:UIOffsetMake(0.5 * (self.mm_w - 10 - 10) - 40, 0) forSearchBarIcon:UISearchBarIconSearch];
self.backgroundColor = self.bgColorOfSearchBar;
}
}
- (void)showSearchVC {
TUISearchViewController *vc = [[TUISearchViewController alloc] init];
TUINavigationController *nav = [[TUINavigationController alloc] initWithRootViewController:(UIViewController *)vc];
nav.modalPresentationStyle = UIModalPresentationFullScreen;
UIViewController *parentVC = self.parentVC;
if (parentVC) {
[parentVC presentViewController:nav animated:NO completion:nil];
}
}
#pragma mark - UISearchBarDelegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
[self showSearchVC];
if (self.isEntrance && [self.delegate respondsToSelector:@selector(searchBarDidEnterSearch:)]) {
[self.delegate searchBarDidEnterSearch:self];
}
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf updateSearchIcon];
});
return !self.isEntrance;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
if ([self.delegate respondsToSelector:@selector(searchBarDidCancelClicked:)]) {
[self.delegate searchBarDidCancelClicked:self];
}
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
if ([self.delegate respondsToSelector:@selector(searchBar:searchText:)]) {
[self.delegate searchBar:self searchText:searchBar.text];
}
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if ([self.delegate respondsToSelector:@selector(searchBar:searchText:)]) {
[self.delegate searchBar:self searchText:searchBar.text];
}
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
[self enableCancelButton];
}
- (void)enableCancelButton {
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
UIButton *cancelBtn = [weakSelf.searchBar valueForKeyPath:@"cancelButton"];
for (UIButton *view in cancelBtn.subviews) {
if ([view isKindOfClass:UIButton.class]) {
view.userInteractionEnabled = YES;
view.enabled = YES;
}
}
cancelBtn.enabled = YES;
cancelBtn.userInteractionEnabled = YES;
[cancelBtn setTitleColor:[UIColor systemBlueColor] forState:UIControlStateNormal];
[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[ [UISearchBar class] ]].title = TIMCommonLocalizableString(TUIKitSearchItemCancel);
;
});
}
@end

View File

@@ -0,0 +1,21 @@
//
// TUISearchResultHeaderFooterView.h
// Pods
//
// Created by harvy on 2020/12/24.
// Copyright © 2023 Tencent. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface TUISearchResultHeaderFooterView : UITableViewHeaderFooterView
@property(nonatomic, assign) BOOL isFooter;
@property(nonatomic, copy) NSString* __nullable title;
@property(nonatomic, copy) dispatch_block_t __nullable onTap;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,171 @@
//
// TUISearchResultHeaderFooterView.m
// Pods
//
// Created by harvy on 2020/12/24.
// Copyright © 2023 Tencent. All rights reserved.
//
#import "TUISearchResultHeaderFooterView.h"
#import <TIMCommon/TIMDefine.h>
#import <TUICore/TUIThemeManager.h>
@interface TUISearchResultHeaderFooterView ()
@property(nonatomic, strong) UIImageView *iconView;
@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) UIImageView *accessoryView;
@property(nonatomic, strong) UIView *separtorView;
@end
@implementation TUISearchResultHeaderFooterView
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
[self setupViews];
}
return self;
}
- (void)setupViews {
[self.contentView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]];
self.contentView.backgroundColor = TIMCommonDynamicColor(@"form_bg_color", @"#FFFFFF");
_iconView = [[UIImageView alloc] init];
_iconView.image = [UIImage imageNamed:TUISearchImagePath(@"search")];
[self.contentView addSubview:_iconView];
_titleLabel = [[UILabel alloc] init];
_titleLabel.text = @"";
_titleLabel.font = [UIFont systemFontOfSize:12.0];
_titleLabel.rtlAlignment = TUITextRTLAlignmentLeading;
[self.contentView addSubview:_titleLabel];
_accessoryView = [[UIImageView alloc] init];
_accessoryView.image = [[UIImage imageNamed:TUISearchImagePath(@"right")] rtl_imageFlippedForRightToLeftLayoutDirection];
[self.contentView addSubview:_accessoryView];
_separtorView = [[UIView alloc] init];
_separtorView.backgroundColor = TIMCommonDynamicColor(@"separator_color", @"#DBDBDB");
[self.contentView addSubview:_separtorView];
}
- (void)tap:(UITapGestureRecognizer *)tap {
if (self.onTap) {
self.onTap();
}
}
- (void)setIsFooter:(BOOL)isFooter {
_isFooter = isFooter;
self.iconView.hidden = !self.isFooter;
self.iconView.hidden = !self.isFooter;
self.accessoryView.hidden = !self.isFooter;
UIColor *footerColor = TIMCommonDynamicColor(@"primary_theme_color", @"#147AFF");
self.titleLabel.textColor = self.isFooter ? footerColor : [UIColor darkGrayColor];
// tell constraints they need updating
[self setNeedsUpdateConstraints];
// update constraints now so we can animate the change
[self updateConstraintsIfNeeded];
[self layoutIfNeeded];
}
- (void)setTitle:(NSString *)title {
_title = title;
self.titleLabel.text = title;
// tell constraints they need updating
[self setNeedsUpdateConstraints];
// update constraints now so we can animate the change
[self updateConstraintsIfNeeded];
[self layoutIfNeeded];
}
+ (BOOL)requiresConstraintBasedLayout {
return YES;
}
// this is Apple's recommended place for adding/updating constraints
- (void)updateConstraints {
[super updateConstraints];
if (self.isFooter) {
[self.iconView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.contentView);
make.height.width.mas_equalTo(20);
if(isRTL()){
make.right.mas_equalTo(self.contentView.mas_right).mas_offset(-10);
}
else {
make.left.mas_equalTo(self.contentView.mas_left).mas_offset(10);
}
}];
[self.titleLabel sizeToFit];
[self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
if(isRTL()) {
make.right.mas_equalTo(self.iconView.mas_left).mas_offset(-10);
}
else{
make.left.mas_equalTo(self.iconView.mas_right).mas_offset(10);
}
make.leading.mas_equalTo(self.iconView.mas_trailing).mas_offset(10);
make.centerY.mas_equalTo(self.contentView);
make.width.mas_equalTo(self.titleLabel.frame.size.width);
make.height.mas_equalTo(self.titleLabel.font.lineHeight);
}];
[self.accessoryView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.height.width.mas_equalTo(10);
make.centerY.mas_equalTo(self.contentView);
if(isRTL()) {
make.left.mas_equalTo(self.contentView.mas_left).mas_offset(10);
}else {
make.right.mas_equalTo(self.contentView.mas_right).mas_offset(-10);
}
}];
[self.separtorView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(10);
make.bottom.mas_equalTo(self.contentView);
make.width.mas_equalTo(self.contentView);
make.height.mas_equalTo(1);
}];
MASAttachKeys(self.iconView,self.titleLabel,self.accessoryView,self.separtorView);
} else {
[self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(self.contentView.mas_leading).mas_offset(10);
make.centerY.mas_equalTo(self.contentView);
make.trailing.mas_equalTo(self.contentView.mas_trailing).mas_offset(-10);
make.height.mas_equalTo(self.titleLabel.font.lineHeight);
}];
[self.separtorView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.leading.mas_equalTo(10);
make.bottom.mas_equalTo(self.contentView).mas_offset(-1);
make.width.mas_equalTo(self.contentView);
make.height.mas_equalTo(1);
}];
MASAttachKeys(self.titleLabel,self.separtorView);
}
}
- (void)layoutSubviews {
[super layoutSubviews];
}
- (void)setFrame:(CGRect)frame {
if (self.isFooter) {
CGSize size = frame.size;
size.height -= 10;
frame.size = size;
}
[super setFrame:frame];
}
@end

View File

@@ -0,0 +1,24 @@
//
// TUISearchResultListController.h
// Pods
//
// Created by harvy on 2020/12/25.
// Copyright © 2023 Tencent. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "TUISearchDataProvider.h"
@class TUISearchResultCellModel;
NS_ASSUME_NONNULL_BEGIN
@interface TUISearchResultListController : UIViewController
- (instancetype)initWithResults:(NSArray<TUISearchResultCellModel *> *__nullable)results
keyword:(NSString *__nullable)keyword
module:(TUISearchResultModule)module
param:(NSDictionary<TUISearchParamKey, id> *__nullable)param;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,310 @@
//
// TUISearchResultListController.m
// Pods
//
// Created by harvy on 2020/12/25.
// Copyright © 2023 Tencent. All rights reserved.
//
#import "TUISearchResultListController.h"
#import <TIMCommon/TIMDefine.h>
#import <TUICore/TUICore.h>
#import "TUISearchBar.h"
#import "TUISearchResultCell.h"
#import "TUISearchResultCellModel.h"
#import "TUISearchResultHeaderFooterView.h"
@interface TUISearchResultListController () <UITableViewDelegate, UITableViewDataSource, TUISearchBarDelegate, TUISearchResultDelegate>
@property(nonatomic, strong) NSMutableArray<TUISearchResultCellModel *> *results;
@property(nonatomic, copy) NSString *keyword;
@property(nonatomic, assign) TUISearchResultModule module;
@property(nonatomic, strong) NSDictionary<TUISearchParamKey, id> *param;
@property(nonatomic, strong) TUISearchDataProvider *dataProvider;
@property(nonatomic, strong) TUISearchBar *searchBar;
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, assign) BOOL allowPageRequest;
@property(nonatomic, assign) NSUInteger pageIndex;
@end
@implementation TUISearchResultListController
static NSString *const Id = @"cell";
static NSString *const HFId = @"HFId";
- (instancetype)initWithResults:(NSArray<TUISearchResultCellModel *> *__nullable)results
keyword:(NSString *__nullable)keyword
module:(TUISearchResultModule)module
param:(NSDictionary<TUISearchParamKey, id> *__nullable)param;
{
if (self = [super init]) {
_results = (module == TUISearchResultModuleChatHistory) ? [NSMutableArray arrayWithArray:@[]] : [NSMutableArray arrayWithArray:results];
_keyword = keyword;
_module = module;
_dataProvider = [[TUISearchDataProvider alloc] init];
_dataProvider.delegate = self;
_param = param;
_allowPageRequest = (module == TUISearchResultModuleChatHistory);
_pageIndex = 0;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.backgroundColor = [UIColor groupTableViewBackgroundColor];
[self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[UIColor groupTableViewBackgroundColor]]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
UIImage *image = [UIImage imageNamed:TIMCommonImagePath(@"nav_back")];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
image = [image rtl_imageFlippedForRightToLeftLayoutDirection];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(back)];
self.navigationItem.leftBarButtonItems = @[ back ];
self.navigationItem.leftItemsSupplementBackButton = NO;
_searchBar = [[TUISearchBar alloc] init];
[_searchBar setEntrance:NO];
_searchBar.delegate = self;
_searchBar.searchBar.text = self.keyword;
self.navigationItem.titleView = _searchBar;
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.rowHeight = 60.f;
[_tableView registerClass:TUISearchResultCell.class forCellReuseIdentifier:Id];
[_tableView registerClass:TUISearchResultHeaderFooterView.class forHeaderFooterViewReuseIdentifier:HFId];
[self.view addSubview:_tableView];
if (self.module == TUISearchResultModuleChatHistory) {
[self.dataProvider searchForKeyword:self.searchBar.searchBar.text forModules:self.module param:self.param];
}
}
- (void)back {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
self.tableView.frame = self.view.bounds;
self.searchBar.frame = CGRectMake(0, 0, self.view.mm_w, 44);
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBar.backgroundColor = [UIColor groupTableViewBackgroundColor];
[self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[UIColor groupTableViewBackgroundColor]]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.navigationController.navigationBar.backgroundColor = nil;
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = nil;
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.results.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TUISearchResultCell *cell = [tableView dequeueReusableCellWithIdentifier:Id forIndexPath:indexPath];
if (indexPath.row >= self.results.count) {
return cell;
}
[cell fillWithData:self.results[indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
if (indexPath.row >= self.results.count) {
return;
}
TUISearchResultCellModel *cellModel = self.results[indexPath.row];
[self onSelectModel:cellModel module:self.module];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return self.results.count == 0 ? 0 : 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
TUISearchResultHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HFId];
headerView.isFooter = NO;
headerView.title = titleForModule(self.module, YES);
return headerView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return nil;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self.view endEditing:YES];
[self.searchBar endEditing:YES];
if (self.allowPageRequest && scrollView.contentOffset.y + scrollView.bounds.size.height > scrollView.contentSize.height) {
self.allowPageRequest = NO;
NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:self.param];
param[TUISearchChatHistoryParamKeyPage] = @(self.pageIndex);
param[TUISearchChatHistoryParamKeyCount] = @(TUISearchDefaultPageSize);
self.param = [NSDictionary dictionaryWithDictionary:param];
[self.dataProvider searchForKeyword:self.searchBar.searchBar.text forModules:self.module param:self.param];
}
}
- (void)onBack {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)onSelectModel:(TUISearchResultCellModel *)cellModel module:(TUISearchResultModule)module {
[self.searchBar endEditing:YES];
if (module == TUISearchResultModuleChatHistory) {
if (![cellModel.context isKindOfClass:NSDictionary.class]) {
if ([cellModel.context isKindOfClass:V2TIMMessage.class]) {
V2TIMMessage *message = cellModel.context;
NSString *title = message.userID;
if (cellModel.title.length > 0) {
title = cellModel.title;
}
NSDictionary *param = @{
TUICore_TUIChatObjectFactory_ChatViewController_Title : title ?: @"",
TUICore_TUIChatObjectFactory_ChatViewController_UserID : message.userID ?: @"",
TUICore_TUIChatObjectFactory_ChatViewController_GroupID : message.groupID ?: @"",
TUICore_TUIChatObjectFactory_ChatViewController_HighlightKeyword : self.searchBar.searchBar.text ?: @"",
TUICore_TUIChatObjectFactory_ChatViewController_LocateMessage : message,
};
[self.navigationController pushViewController:TUICore_TUIChatObjectFactory_ChatViewController_Classic param:param forResult:nil];
return;
}
return;
}
NSDictionary *convInfo = cellModel.context;
NSString *conversationId = convInfo[kSearchChatHistoryConversationId];
V2TIMConversation *conversation = convInfo[kSearchChatHistoryConverationInfo];
NSArray *msgs = convInfo[kSearchChatHistoryConversationMsgs];
if (msgs.count == 1) {
NSString *title = cellModel.title ?: cellModel.titleAttributeString.string;
NSDictionary *param = @{
TUICore_TUIChatObjectFactory_ChatViewController_Title : title ?: @"",
TUICore_TUIChatObjectFactory_ChatViewController_UserID : conversation.userID ?: @"",
TUICore_TUIChatObjectFactory_ChatViewController_GroupID : conversation.groupID ?: @"",
TUICore_TUIChatObjectFactory_ChatViewController_HighlightKeyword : self.searchBar.searchBar.text ?: @"",
TUICore_TUIChatObjectFactory_ChatViewController_LocateMessage : msgs.firstObject,
};
[self.navigationController pushViewController:TUICore_TUIChatObjectFactory_ChatViewController_Classic param:param forResult:nil];
return;
}
NSMutableArray *results = [NSMutableArray array];
for (V2TIMMessage *message in msgs) {
TUISearchResultCellModel *model = [[TUISearchResultCellModel alloc] init];
model.title = message.nickName ?: message.sender;
NSString *desc = [TUISearchDataProvider matchedTextForMessage:message withKey:self.searchBar.searchBar.text];
model.detailsAttributeString = [TUISearchDataProvider attributeStringWithText:desc key:self.searchBar.searchBar.text];
model.avatarUrl = message.faceURL;
model.groupType = conversation.groupID;
model.avatarImage = conversation.type == V2TIM_C2C ? DefaultAvatarImage : DefaultGroupAvatarImageByGroupType(conversation.groupType);
model.context = message;
[results addObject:model];
}
TUISearchResultListController *vc =
[[TUISearchResultListController alloc] initWithResults:results
keyword:self.searchBar.searchBar.text
module:module
param:@{TUISearchChatHistoryParamKeyConversationId : conversationId}];
[self.navigationController pushViewController:vc animated:YES];
return;
}
NSDictionary *param = nil;
NSString *title = cellModel.title ?: cellModel.titleAttributeString.string;
if (module == TUISearchResultModuleContact && [cellModel.context isKindOfClass:V2TIMFriendInfo.class]) {
V2TIMFriendInfo *friend = cellModel.context;
param = @{
TUICore_TUIChatObjectFactory_ChatViewController_Title : title ?: @"",
TUICore_TUIChatObjectFactory_ChatViewController_UserID : friend.userID ?: @"",
};
}
if (module == TUISearchResultModuleGroup && [cellModel.context isKindOfClass:V2TIMGroupInfo.class]) {
V2TIMGroupInfo *group = cellModel.context;
param = @{
TUICore_TUIChatObjectFactory_ChatViewController_Title : title ?: @"",
TUICore_TUIChatObjectFactory_ChatViewController_GroupID : group.groupID ?: @"",
};
}
[self.navigationController pushViewController:TUICore_TUIChatObjectFactory_ChatViewController_Classic param:param forResult:nil];
}
#pragma mark - TUISearchBarDelegate
- (void)searchBarDidCancelClicked:(TUISearchBar *)searchBar {
[self dismissViewControllerAnimated:NO completion:nil];
}
- (void)searchBar:(TUISearchBar *)searchBar searchText:(NSString *)key {
[self.results removeAllObjects];
self.allowPageRequest = YES;
self.pageIndex = 0;
NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:self.param];
param[TUISearchChatHistoryParamKeyPage] = @(self.pageIndex);
self.param = [NSDictionary dictionaryWithDictionary:param];
[self.dataProvider searchForKeyword:key forModules:self.module param:self.param];
}
#pragma mark - TUISearchResultDelegate
- (void)onSearchResults:(NSDictionary<NSNumber *, NSArray<TUISearchResultCellModel *> *> *)results forModules:(TUISearchResultModule)modules {
NSArray *arrayM = [results objectForKey:@(modules)];
if (arrayM == nil) {
arrayM = @[];
}
[self.results addObjectsFromArray:arrayM];
[self.tableView reloadData];
self.allowPageRequest = (arrayM.count >= TUISearchDefaultPageSize);
self.pageIndex = (arrayM.count < TUISearchDefaultPageSize) ? self.pageIndex : self.pageIndex + 1;
if (!self.allowPageRequest) {
}
}
- (void)onSearchError:(NSString *)errMsg {
NSLog(@"search error: %@", errMsg);
}
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end

View File

@@ -0,0 +1,17 @@
//
// TUISearchViewController.h
// Pods
//
// Created by harvy on 2020/12/24.
// Copyright © 2023 Tencent. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface TUISearchViewController : UIViewController
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,299 @@
//
// TUISearchViewController.m
// Pods
//
// Created by harvy on 2020/12/24.
// Copyright © 2023 Tencent. All rights reserved.
//
#import "TUISearchViewController.h"
#import <TIMCommon/TIMDefine.h>
#import <TUICore/TUICore.h>
#import "TUISearchBar.h"
#import "TUISearchDataProvider.h"
#import "TUISearchResultCell.h"
#import "TUISearchResultCellModel.h"
#import "TUISearchResultHeaderFooterView.h"
#import "TUISearchResultListController.h"
@interface TUISearchViewController () <UITableViewDelegate, UITableViewDataSource, TUISearchBarDelegate, TUISearchResultDelegate>
@property(nonatomic, strong) TUISearchBar *searchBar;
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) TUISearchDataProvider *dataProvider;
@end
@implementation TUISearchViewController
static NSString *const Id = @"cell";
static NSString *const HFId = @"HFId";
- (void)viewDidLoad {
[super viewDidLoad];
_dataProvider = [[TUISearchDataProvider alloc] init];
_dataProvider.delegate = self;
[self setupViews];
[TUITool addUnsupportNotificationInVC:self];
}
- (void)dealloc {
NSLog(@"%s dealloc", __FUNCTION__);
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)setupViews {
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
_searchBar = [[TUISearchBar alloc] init];
[_searchBar setEntrance:NO];
_searchBar.delegate = self;
self.navigationItem.titleView = _searchBar;
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.rowHeight = 60.f;
[_tableView registerClass:TUISearchResultCell.class forCellReuseIdentifier:Id];
[_tableView registerClass:TUISearchResultHeaderFooterView.class forHeaderFooterViewReuseIdentifier:HFId];
[self.view addSubview:_tableView];
[_searchBar.searchBar becomeFirstResponder];
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
self.tableView.frame = self.view.bounds;
self.searchBar.frame = CGRectMake(0, 0, self.view.mm_w, 44);
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
#pragma mark - TUISearchResultDelegate
- (void)onSearchError:(NSString *)errMsg {
}
- (void)onSearchResults:(NSDictionary<NSNumber *, NSArray<TUISearchResultCellModel *> *> *)results forModules:(TUISearchResultModule)modules {
[self.tableView reloadData];
}
#pragma mark - UITableViewDataSourceTUITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.dataProvider.resultSet.allKeys.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *results = [self resultForSection:section];
return results.count > kMaxNumOfPerModule ? kMaxNumOfPerModule : results.count;
}
- (TUISearchResultCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TUISearchResultCell *cell = [tableView dequeueReusableCellWithIdentifier:Id forIndexPath:indexPath];
TUISearchResultModule module = TUISearchResultModuleContact;
NSArray *results = [self resultForSection:indexPath.section module:&module];
if (results.count <= indexPath.row) {
return cell;
}
[cell fillWithData:results[indexPath.row]];
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
TUISearchResultModule module = TUISearchResultModuleContact;
NSArray *results = [self resultForSection:section module:&module];
if (results.count < kMaxNumOfPerModule) {
return [UIView new];
}
__weak typeof(self) weakSelf = self;
TUISearchResultHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HFId];
footerView.isFooter = YES;
footerView.title = titleForModule(module, NO);
footerView.onTap = ^{
[weakSelf onSelectMoreModule:module results:results];
};
return footerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
NSArray *results = [self resultForSection:section];
if (results.count < kMaxNumOfPerModule) {
return 10;
}
return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
TUISearchResultModule module = TUISearchResultModuleContact;
[self resultForSection:section module:&module];
TUISearchResultHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HFId];
headerView.isFooter = NO;
headerView.title = titleForModule(module, YES);
headerView.onTap = nil;
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self.view endEditing:YES];
[self.searchBar endEditing:YES];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
TUISearchResultModule module = TUISearchResultModuleContact;
NSArray *results = [self resultForSection:indexPath.section module:&module];
if (results.count <= indexPath.row) {
return;
}
TUISearchResultCellModel *cellModel = results[indexPath.row];
[self onSelectModel:cellModel module:module];
}
#pragma mark - action
- (void)onSelectModel:(TUISearchResultCellModel *)cellModel module:(TUISearchResultModule)module {
[self.searchBar endEditing:YES];
if (module == TUISearchResultModuleChatHistory) {
if (![cellModel.context isKindOfClass:NSDictionary.class]) {
return;
}
NSDictionary *convInfo = cellModel.context;
NSString *conversationId = convInfo[kSearchChatHistoryConversationId];
V2TIMConversation *conversation = convInfo[kSearchChatHistoryConverationInfo];
NSArray *msgs = convInfo[kSearchChatHistoryConversationMsgs];
if (msgs.count == 1) {
NSString *title = cellModel.title ?: cellModel.titleAttributeString.string;
NSDictionary *param = @{
TUICore_TUIChatObjectFactory_ChatViewController_Title : title ?: @"",
TUICore_TUIChatObjectFactory_ChatViewController_UserID : conversation.userID ?: @"",
TUICore_TUIChatObjectFactory_ChatViewController_GroupID : conversation.groupID ?: @"",
TUICore_TUIChatObjectFactory_ChatViewController_HighlightKeyword : self.searchBar.searchBar.text ?: @"",
TUICore_TUIChatObjectFactory_ChatViewController_LocateMessage : msgs.firstObject,
};
[self.navigationController pushViewController:TUICore_TUIChatObjectFactory_ChatViewController_Classic param:param forResult:nil];
return;
}
NSMutableArray *results = [NSMutableArray array];
for (V2TIMMessage *message in msgs) {
TUISearchResultCellModel *model = [[TUISearchResultCellModel alloc] init];
model.title = message.nickName ?: message.sender;
NSString *desc = [TUISearchDataProvider matchedTextForMessage:message withKey:self.searchBar.searchBar.text];
model.detailsAttributeString = [TUISearchDataProvider attributeStringWithText:desc key:self.searchBar.searchBar.text];
model.avatarUrl = message.faceURL;
model.groupType = conversation.groupType;
model.avatarImage = conversation.type == V2TIM_C2C ? DefaultAvatarImage : DefaultGroupAvatarImageByGroupType(conversation.groupType);
;
model.context = message;
[results addObject:model];
}
TUISearchResultListController *vc =
[[TUISearchResultListController alloc] initWithResults:results
keyword:self.searchBar.searchBar.text
module:module
param:@{TUISearchChatHistoryParamKeyConversationId : conversationId}];
[self.navigationController pushViewController:vc animated:YES];
return;
}
NSDictionary *param = nil;
if (module == TUISearchResultModuleContact && [cellModel.context isKindOfClass:V2TIMFriendInfo.class]) {
V2TIMFriendInfo *friend = cellModel.context;
NSString *title = cellModel.title ?: cellModel.titleAttributeString.string;
param = @{
TUICore_TUIChatObjectFactory_ChatViewController_Title : title ?: @"",
TUICore_TUIChatObjectFactory_ChatViewController_UserID : friend.userID ?: @"",
};
}
if (module == TUISearchResultModuleGroup && [cellModel.context isKindOfClass:V2TIMGroupInfo.class]) {
V2TIMGroupInfo *group = cellModel.context;
NSString *title = cellModel.title ?: cellModel.titleAttributeString.string;
param = @{
TUICore_TUIChatObjectFactory_ChatViewController_Title : title ?: @"",
TUICore_TUIChatObjectFactory_ChatViewController_GroupID : group.groupID ?: @"",
};
}
[self.navigationController pushViewController:TUICore_TUIChatObjectFactory_ChatViewController_Classic param:param forResult:nil];
}
- (void)onSelectMoreModule:(TUISearchResultModule)module results:(NSArray<TUISearchResultCellModel *> *)results {
TUISearchResultListController *vc = [[TUISearchResultListController alloc] initWithResults:results
keyword:self.searchBar.searchBar.text
module:module
param:nil];
[self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - viewmodel
- (NSArray *)resultForSection:(NSInteger)section {
return [self resultForSection:section module:nil];
}
- (NSArray *)resultForSection:(NSInteger)section module:(TUISearchResultModule *)module {
NSArray *keys = self.dataProvider.resultSet.allKeys;
if (section >= keys.count) {
return 0;
}
keys = [keys sortedArrayUsingComparator:^NSComparisonResult(NSNumber *obj1, NSNumber *obj2) {
return [obj1 intValue] < [obj2 intValue] ? NSOrderedAscending : NSOrderedDescending;
}];
NSNumber *key = keys[section];
if (module) {
*module = (TUISearchResultModule)[key integerValue];
}
return [self.dataProvider.resultSet objectForKey:key];
}
#pragma mark - TUISearchBarDelegate
- (void)searchBarDidCancelClicked:(TUISearchBar *)searchBar {
[self dismissViewControllerAnimated:NO completion:nil];
}
- (void)searchBar:(TUISearchBar *)searchBar searchText:(NSString *)key {
[self.dataProvider searchForKeyword:key forModules:TUISearchResultModuleAll param:nil];
}
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
@interface IUSearchView : UIView
@property(nonatomic, strong) UIView *view;
@end
@implementation IUSearchView
- (instancetype)init {
self = [super init];
if (self) {
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
[self addSubview:self.view];
}
return self;
}
@end