You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
367 lines
12 KiB
367 lines
12 KiB
// |
|
// TFSearchViewController.m |
|
// TFReader |
|
// |
|
// Created by 谢腾飞 on 2020/12/24. |
|
// Copyright © 2020 xtfei_2011@126.com. All rights reserved. |
|
// |
|
|
|
#import "TFSearchViewController.h" |
|
#import "TFBookStoreNovelStyleTwoCell.h" |
|
#import "TFProductionListViewCell.h" |
|
#import "TFHotspotSearchView.h" |
|
#import "TFSearchBar.h" |
|
#import "TFBookStoreLabelModel.h" |
|
#import "TFSearchModel.h" |
|
|
|
@interface TFSearchViewController ()<UITableViewDelegate, UITableViewDataSource, TFSearchBarDelegate> |
|
|
|
@property (nonatomic ,strong) TFHotspotSearchView *hotspotSearchView; |
|
@property (nonatomic ,strong) TFBookStoreLabelModel *labelModel; |
|
@property (nonatomic ,strong) TFSearchBar *searchBar; |
|
@property (nonatomic ,assign) BOOL inSearchState; |
|
@property (nonatomic ,strong) NSString *searchStr; |
|
|
|
@end |
|
|
|
@implementation TFSearchViewController |
|
|
|
- (void)viewDidLoad |
|
{ |
|
[super viewDidLoad]; |
|
|
|
[self initialize]; |
|
[self createSubviews]; |
|
[self netRequest]; |
|
} |
|
|
|
- (void)viewWillAppear:(BOOL)animated |
|
{ |
|
[super viewWillAppear:animated]; |
|
|
|
[self setStatusBarDefaultStyle]; |
|
} |
|
|
|
- (void)viewWillDisappear:(BOOL)animated |
|
{ |
|
[super viewWillDisappear:animated]; |
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:Notification_Show_Tabbar object:nil]; |
|
} |
|
|
|
- (void)initialize |
|
{ |
|
[self hiddenNavigationBar:YES]; |
|
|
|
self.view.backgroundColor = [UIColor whiteColor]; |
|
} |
|
|
|
- (void)createSubviews |
|
{ |
|
self.searchBar = [[TFSearchBar alloc] initWithFrame:CGRectMake(5, PUB_NAVBAR_OFFSET + 25, SCREEN_WIDTH - 2 * 5, 40)]; |
|
self.searchBar.placeholderText = self.placeholder ?: TFLocalizedString(@"搜索您感兴趣的书籍"); |
|
self.searchBar.backgroundColor = [UIColor clearColor]; |
|
self.searchBar.delegate = self; |
|
[self.view addSubview:self.searchBar]; |
|
|
|
self.mainTableView.delegate = self; |
|
self.mainTableView.dataSource = self; |
|
self.mainTableView.contentInset = UIEdgeInsetsMake(0, 0, PUB_TABBAR_OFFSET, 0); |
|
[self.view addSubview:self.mainTableView]; |
|
|
|
[self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) { |
|
make.left.mas_equalTo(0); |
|
make.top.mas_equalTo(PUB_NAVBAR_HEIGHT + kHalfMargin); |
|
make.width.mas_equalTo(SCREEN_WIDTH); |
|
make.height.mas_equalTo(SCREEN_HEIGHT - PUB_NAVBAR_HEIGHT - kHalfMargin); |
|
}]; |
|
|
|
[self setEmptyOnView:self.mainTableView title:TFLocalizedString(@"未搜索到相关内容") buttonTitle:nil tapBlock:^{ |
|
}]; |
|
|
|
WS(weakSelf) |
|
self.hotspotSearchView = [[TFHotspotSearchView alloc] init]; |
|
self.hotspotSearchView.bookClickBlock = ^(NSString *hotspotBook) { |
|
weakSelf.searchBar.searchText = hotspotBook; |
|
[weakSelf searchButtonClickedWithSearchText:hotspotBook]; |
|
}; |
|
[self.mainTableView setTableHeaderView:self.hotspotSearchView]; |
|
|
|
|
|
self.mainTableView.mj_header = [TFRefreshHeader headerWithRefreshingBlock:^{ |
|
weakSelf.currentPageNumber = 1; |
|
[weakSelf netRequest]; |
|
}]; |
|
|
|
self.mainTableView.mj_footer = [TFRefreshFooter footerWithRefreshingBlock:^{ |
|
weakSelf.currentPageNumber ++; |
|
[weakSelf netRequest]; |
|
}]; |
|
|
|
[self.mainTableView hideRefreshHeader]; |
|
[self.mainTableView hideRefreshFooter]; |
|
[self.mainTableView titleLabel].hidden = YES; |
|
} |
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section |
|
{ |
|
return self.inSearchState ? self.dataSourceArray.count : self.labelModel ? 1 : 0; |
|
} |
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath |
|
{ |
|
WS(weakSelf) |
|
if (self.inSearchState) { |
|
static NSString *cellName = @"TFProductionListViewCell"; |
|
TFProductionListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName]; |
|
if (!cell) { |
|
cell = [[TFProductionListViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName]; |
|
} |
|
cell.productionType = self.productionType; |
|
cell.productionModel = [self.dataSourceArray objectOrNilAtIndex:indexPath.row]; |
|
|
|
return cell; |
|
|
|
} else { |
|
static NSString *cellName = @"TFBookStoreNovelStyleTwoCell"; |
|
TFBookStoreNovelStyleTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName]; |
|
if (!cell) { |
|
cell = [[TFBookStoreNovelStyleTwoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName]; |
|
} |
|
cell.cellDidSelectItemBlock = ^(NSInteger production_id) { |
|
|
|
switch (weakSelf.productionType) { |
|
#if TF_Enable_Book |
|
case TFProductionTypeNovel: { |
|
TFNovelDetailViewController *novelDetail = [[TFNovelDetailViewController alloc] init]; |
|
novelDetail.book_id = production_id; |
|
[weakSelf.navigationController pushViewController:novelDetail animated:YES]; |
|
} |
|
break; |
|
#endif |
|
|
|
#if TF_Enable_Comic |
|
case TFProductionTypeComic: { |
|
TFComicDetailViewController *comicDetail = [[TFComicDetailViewController alloc] init]; |
|
comicDetail.comic_id = production_id; |
|
[weakSelf.navigationController pushViewController:comicDetail animated:YES]; |
|
} |
|
break; |
|
#endif |
|
|
|
#if TF_Enable_Audio |
|
case TFProductionTypeAudio: { |
|
TFAudioDetailViewController *audioDetail = [[TFAudioDetailViewController alloc] init]; |
|
audioDetail.audio_id = production_id; |
|
[weakSelf.navigationController pushViewController:audioDetail animated:YES]; |
|
} |
|
break; |
|
#endif |
|
|
|
default: |
|
break; |
|
} |
|
}; |
|
cell.labelModel = self.labelModel; |
|
|
|
return cell; |
|
} |
|
} |
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section |
|
{ |
|
return CGFLOAT_MIN; |
|
} |
|
|
|
|
|
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section |
|
{ |
|
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, PUB_TABBAR_OFFSET == 0 ? 10 : PUB_TABBAR_OFFSET)]; |
|
view.backgroundColor = [UIColor clearColor]; |
|
return view; |
|
} |
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath |
|
{ |
|
if (self.inSearchState) { |
|
TFProductionModel *t_model = [self.dataSourceArray objectOrNilAtIndex:indexPath.row]; |
|
|
|
switch (self.productionType) { |
|
#if TF_Enable_Book |
|
case TFProductionTypeNovel: { |
|
TFNovelDetailViewController *vc = [[TFNovelDetailViewController alloc] init]; |
|
vc.book_id = t_model.production_id; |
|
[self.navigationController pushViewController:vc animated:YES]; |
|
} |
|
break; |
|
#endif |
|
|
|
#if TF_Enable_Comic |
|
case TFProductionTypeComic: { |
|
TFComicDetailViewController *vc = [[TFComicDetailViewController alloc] init]; |
|
vc.comic_id = t_model.production_id; |
|
[self.navigationController pushViewController:vc animated:YES]; |
|
} |
|
break; |
|
#endif |
|
|
|
#if TF_Enable_Audio |
|
case TFProductionTypeAudio: { |
|
TFAudioDetailViewController *vc = [[TFAudioDetailViewController alloc] init]; |
|
vc.audio_id = t_model.production_id; |
|
[self.navigationController pushViewController:vc animated:YES]; |
|
} |
|
break; |
|
#endif |
|
|
|
default: |
|
break; |
|
} |
|
} |
|
} |
|
|
|
- (void)scrollViewDidScroll:(UIScrollView *)scrollView |
|
{ |
|
[self.searchBar searchBarResignFirstResponder]; |
|
} |
|
|
|
- (void)cancelButtonClicked |
|
{ |
|
if (self.searchStr.length > 0) { |
|
self.searchBar.searchText = @""; |
|
[self.searchBar resignFirstResponder]; |
|
[self searchBarCleanText]; |
|
} else { |
|
[self.navigationController popViewControllerAnimated:YES]; |
|
} |
|
} |
|
|
|
- (void)searchButtonClickedWithSearchText:(NSString *)searchText |
|
{ |
|
self.currentPageNumber = 1; |
|
self.inSearchState = YES; |
|
self.searchStr = searchText; |
|
[self searchNetRequest]; |
|
} |
|
|
|
- (void)searchBarCleanText |
|
{ |
|
self.currentPageNumber = 1; |
|
self.inSearchState = NO; |
|
self.searchStr = @""; |
|
self.hotspotSearchView.hidden = NO; |
|
[self.hotspotSearchView setNormalFrame]; |
|
[self.mainTableView reloadData]; |
|
[self.mainTableView xtfei_startLoading]; |
|
[self.mainTableView hideRefreshFooter]; |
|
[self.mainTableView hideRefreshHeader]; |
|
[self.mainTableView titleLabel].hidden = YES; |
|
} |
|
|
|
- (void)searchNetRequest |
|
{ |
|
// if ([self.searchStr isEqualToString:TFLocalizedString(@" 切换至测试版 ")]) { |
|
// [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Review_State object:@"1"]; |
|
// return; |
|
// } |
|
// |
|
// if ([self.searchStr isEqualToString:TFLocalizedString(@" 强行切换至测试版 ")]) { |
|
//#ifdef Notification_Force_Review_State |
|
// [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Force_Review_State object:@"1"]; |
|
//#else |
|
// [[NSNotificationCenter defaultCenter] postNotificationName:@"mDidForceChangeMagicState" object:@"1"]; |
|
//#endif |
|
// return; |
|
// } |
|
|
|
NSString *url = @""; |
|
switch (self.productionType) { |
|
case TFProductionTypeNovel: |
|
url = Book_Search_List; |
|
break; |
|
case TFProductionTypeComic: |
|
url = Comic_Search_List; |
|
break; |
|
case TFProductionTypeAudio: |
|
url = Audio_Search_List; |
|
break; |
|
|
|
default: |
|
break; |
|
} |
|
|
|
WS(weakSelf) |
|
NSMutableDictionary *params = [NSMutableDictionary dictionary]; |
|
params[@"keyword"] = self.searchStr; |
|
params[@"page_num"] = [TFUtilsHelper formatStringWithInteger:self.currentPageNumber]; |
|
|
|
[TFNetworkTools POST:url parameters:params model:TFProductionListModel.class success:^(BOOL isSuccess, TFProductionListModel *_Nullable t_model, TFNetworkRequestModel *requestModel) { |
|
|
|
[weakSelf.mainTableView endRefreshing]; |
|
|
|
if (isSuccess) { |
|
if (weakSelf.currentPageNumber == 1) { |
|
weakSelf.hotspotSearchView.hidden = YES; |
|
weakSelf.hotspotSearchView.frame = CGRectMake(0, 0, 0.1, 0.1); |
|
// [weakSelf.mainTableView showRefreshHeader]; |
|
// [weakSelf.mainTableView showRefreshFooter]; |
|
[weakSelf.dataSourceArray removeAllObjects]; |
|
weakSelf.dataSourceArray = [NSMutableArray arrayWithArray:t_model.list]; |
|
} else { |
|
[weakSelf.dataSourceArray addObjectsFromArray:t_model.list]; |
|
} |
|
if (t_model.total_page <= t_model.current_page) { |
|
[weakSelf.mainTableView hideRefreshFooter]; |
|
} |
|
} |
|
|
|
[weakSelf.mainTableView reloadData]; |
|
[weakSelf.mainTableView xtfei_endLoading]; |
|
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { |
|
[weakSelf.mainTableView endRefreshing]; |
|
[weakSelf.mainTableView xtfei_endLoading]; |
|
}]; |
|
} |
|
|
|
- (void)netRequest |
|
{ |
|
NSString *url = @""; |
|
switch (self.productionType) { |
|
case TFProductionTypeNovel: |
|
url = Book_Search; |
|
break; |
|
case TFProductionTypeComic: |
|
url = Comic_Search; |
|
break; |
|
case TFProductionTypeAudio: |
|
url = Audio_Search; |
|
break; |
|
|
|
default: |
|
break; |
|
} |
|
|
|
WS(weakSelf) |
|
[TFNetworkTools POST:url parameters:nil model:TFSearchModel.class success:^(BOOL isSuccess, TFSearchModel *_Nullable t_model, TFNetworkRequestModel *requestModel) { |
|
if (isSuccess) { |
|
for (TFProductionModel *tt_model in t_model.list) { |
|
tt_model.productionType = weakSelf.productionType; |
|
} |
|
weakSelf.hotspotSearchView.hotspotBookArray = t_model.hot_word; |
|
|
|
// if (t_model.hot_word.count > 0) { |
|
// weakSelf.searchBar.placeholderText = [t_model.hot_word objectOrNilAtIndex:(arc4random() % (t_model.hot_word.count - 1))]; |
|
// } else { |
|
// weakSelf.searchBar.placeholderText = TFLocalizedString(@"搜索您感兴趣的书籍"); |
|
// } |
|
|
|
weakSelf.labelModel = [[TFBookStoreLabelModel alloc] init]; |
|
weakSelf.labelModel.list = t_model.list; |
|
weakSelf.labelModel.label = TFLocalizedString(@"热搜榜"); |
|
weakSelf.labelModel.style = 2; |
|
} |
|
[weakSelf.mainTableView reloadData]; |
|
} failure:nil]; |
|
} |
|
|
|
@end
|
|
|