SearchResultViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //
  2. // SearchResultViewController.m
  3. // AICity
  4. //
  5. // Created by 刘伟伟 on 2023/7/13.
  6. // Copyright © 2023 wei.z. All rights reserved.
  7. //
  8. #import "SearchResultViewController.h"
  9. #import "SearchCommonBarView.h"
  10. #import "ZFNormalViewController.h"
  11. @interface SearchResultViewController ()<UITableViewDelegate,UITableViewDataSource>
  12. @property(nonatomic,strong)SearchCommonBarView *barView;
  13. @property(nonatomic,strong)UITableView *tableView;
  14. @property(nonatomic,strong)UIImageView *noDataView;
  15. @end
  16. @implementation SearchResultViewController
  17. -(UIStatusBarStyle)preferredStatusBarStyle{
  18. return UIStatusBarStyleLightContent;
  19. }
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.fd_prefersNavigationBarHidden = true;
  23. self.view.backgroundColor = rgba(18, 23, 41, 1);
  24. [self.view addSubview:self.barView];
  25. [self.view addSubview:self.tableView];
  26. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  27. make.left.right.bottom.equalTo(self.view);
  28. make.top.equalTo(self.barView.mas_bottom);
  29. }];
  30. [self addNotDataView];
  31. [self search:self.txt];
  32. }
  33. -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
  34. [self.view endEditing:YES];
  35. }
  36. -(void)addNotDataView{
  37. UIImageView *noDataView = [UIImageView new];
  38. self.noDataView = noDataView;
  39. self.noDataView.hidden = true;
  40. noDataView.image = [UIImage imageNamed:@"nocollect_1"];
  41. [self.view addSubview:noDataView];
  42. [noDataView mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.size.mas_equalTo(CGSizeMake(246, 180));
  44. make.centerX.equalTo(self.view);
  45. make.top.equalTo(self.barView.mas_bottom).offset(188);
  46. }];
  47. UILabel *label = [UILabel new];
  48. label.textColor = rgba(255, 255, 255, 0.4);
  49. label.text = @"暂无内容";
  50. label.font = [UIFont systemFontOfSize:16];
  51. [noDataView addSubview:label];
  52. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  53. make.centerX.equalTo(noDataView);
  54. make.bottom.equalTo(noDataView);
  55. }];
  56. }
  57. -(void)setTxt:(NSString *)txt{
  58. _txt=txt;
  59. self.barView.textField.text = txt;
  60. }
  61. -(void)setSearchResArr:(NSArray *)searchResArr{
  62. _searchResArr = searchResArr;
  63. if([searchResArr isKindOfClass:[NSNull class]]){
  64. self.noDataView.hidden = false;
  65. _searchResArr = [NSArray array];
  66. }else{
  67. self.noDataView.hidden = (searchResArr.count>0);
  68. }
  69. }
  70. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  71. return self.searchResArr.count;
  72. // return 3;
  73. }
  74. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  75. searchResCell1 *cell = [searchResCell1 cellWithTableView:tableView];
  76. cell.data = self.searchResArr[indexPath.row];
  77. return cell;
  78. }
  79. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  80. return 130+10;
  81. }
  82. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  83. [self.view endEditing:YES];
  84. NSDictionary *dic = self.searchResArr[indexPath.row];
  85. [self playItem:[dic[@"vid"] intValue]];
  86. }
  87. -(void)playItem:(int)vid{
  88. ZFNormalViewController *vc = [[ZFNormalViewController alloc] init];
  89. vc.vid = vid;
  90. [self.navigationController pushViewController:vc animated:true];
  91. }
  92. -(UITableView *)tableView{
  93. if(!_tableView){
  94. _tableView =[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
  95. _tableView.delegate = self;
  96. _tableView.dataSource = self;
  97. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  98. _tableView.showsVerticalScrollIndicator = false;
  99. _tableView.showsHorizontalScrollIndicator = false;
  100. _tableView.backgroundColor = rgba(18, 23, 41, 1);
  101. // _tableView.backgroundColor = [UIColor redColor];
  102. if (@available(iOS 11, *)) {
  103. _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  104. _tableView.estimatedRowHeight = 0;
  105. _tableView.estimatedSectionFooterHeight = 0;
  106. _tableView.estimatedSectionHeaderHeight = 0;
  107. UIWindow *keyWindow = UIApplication.sharedApplication.keyWindow;
  108. UIEdgeInsets safeAreaInsets = keyWindow.safeAreaInsets;
  109. CGFloat topSafeArea = safeAreaInsets.top;
  110. _tableView.contentInset = UIEdgeInsetsMake(-topSafeArea, 0, BAR_HEIGHT, 0);
  111. // _tableView2.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0);
  112. }
  113. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 150000 // only Xcode 13+ needs and can compile this
  114. if (@available(iOS 15.0, *)) {
  115. _tableView.sectionHeaderTopPadding = 0;
  116. }
  117. #endif
  118. }
  119. return _tableView;
  120. }
  121. -(SearchCommonBarView *)barView{
  122. if(!_barView){
  123. CGFloat h=0;
  124. if(@available(iOS 13.0, *)){
  125. UIStatusBarManager *sc= [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager;
  126. CGRect f=sc.statusBarFrame;
  127. h=f.size.height;
  128. }else{
  129. h=[UIApplication sharedApplication].statusBarFrame.size.height;
  130. }
  131. if(h>=40){
  132. h=30;
  133. }
  134. if(self.txt){
  135. _barView.textField.text = self.txt;
  136. }
  137. _barView =[[SearchCommonBarView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 72-20+h)];
  138. _barView.backgroundColor = rgba(18, 23, 41, 1);
  139. __weak typeof(self) weakSelf = self;
  140. _barView.searchAction = ^(NSString * _Nonnull tit) {
  141. [weakSelf search:tit];
  142. };
  143. }
  144. return _barView;
  145. }
  146. -(void)search:(NSString *)keyword{
  147. [self.view endEditing:true];
  148. NSArray *tarr = [[NSUserDefaults standardUserDefaults] objectForKey:@"mylocationhistory"];
  149. if(![tarr containsObject:keyword]){
  150. NSMutableArray *marr = [NSMutableArray array];
  151. [marr addObjectsFromArray:tarr];
  152. [marr addObject:keyword];
  153. [[NSUserDefaults standardUserDefaults] setObject:marr forKey:@"mylocationhistory"];
  154. }
  155. // [self.tableView reloadData];
  156. //搜索影片
  157. __weak typeof(self) weakSelf = self;
  158. [RequestTool post:@"search/searchMovie" params:@{@"search_key":keyword,@"page":@{@"page":@(1),@"pageSize":@(10)}} success:^(id responseObject) {
  159. NSLog(@"home/homePage-%@",responseObject);
  160. if([responseObject[@"code"] intValue] == 0){
  161. weakSelf.searchResArr = responseObject[@"data"][@"list"];
  162. [weakSelf.tableView reloadData];
  163. }else{
  164. weakSelf.searchResArr = @[];
  165. [weakSelf.tableView reloadData];
  166. }
  167. } failure:^(NSError *error) {}];
  168. }
  169. @end
  170. @interface searchResCell1()
  171. @end
  172. @implementation searchResCell1
  173. + (instancetype)cellWithTableView:(UITableView *)tableView{
  174. static NSString *identifier = @"searchResCell1";
  175. searchResCell1 *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  176. if (cell == nil) {
  177. cell = [[searchResCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
  178. }
  179. return cell;
  180. }
  181. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  182. {
  183. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  184. if (self) {
  185. [self creatView];
  186. }
  187. return self;
  188. }
  189. -(void)setData:(NSDictionary *)data{
  190. _data = data;
  191. UIImageView *iv = [self.contentView viewWithTag:100];
  192. [iv sd_setImageWithURL:[NSURL URLWithString:data[@"vertical_cover_url"]]];
  193. UILabel *nameLabel = [self.contentView viewWithTag:101];
  194. nameLabel.text = [NSString stringWithFormat:@"%@ %.1f",data[@"ch_name"],[data[@"score"] floatValue]];
  195. NSDate* date = [NSDate dateWithTimeIntervalSince1970:[data[@"screen_time"] doubleValue]];
  196. NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
  197. [formatter setDateFormat:@"yyyy"];
  198. NSString* dateString = [formatter stringFromDate:date];
  199. UILabel *detailLabel = [self.contentView viewWithTag:102];
  200. detailLabel.text = [NSString stringWithFormat:@"%@·%@·%@ | %d集全",dateString,[data[@"movie_type"] intValue] == 1 ? @"电视剧" :@"电影",data[@"country"],[data[@"total_episodes"] intValue]] ;
  201. //@"2023·电视剧·韩国 | 10集全"
  202. UILabel *roleLabel = [self.contentView viewWithTag:103];
  203. roleLabel.text = @"主演:";
  204. int vid = [data[@"vid"] intValue];
  205. UIButton *btn = [self.contentView viewWithTag:150];
  206. if([UserModel collectContainVid:vid]){
  207. [btn setTitle:@"已收藏" forState:UIControlStateNormal];
  208. [btn setImage:[UserModel imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
  209. }else{
  210. [btn setImage:[UIImage imageNamed:@"Frame13"] forState:UIControlStateNormal];
  211. [btn setTitle:@"收藏" forState:UIControlStateNormal];
  212. }
  213. }
  214. -(void)addCollectAction{
  215. int vid = [self.data[@"vid"] intValue];
  216. UIButton *btn = [self.contentView viewWithTag:150];
  217. if([UserModel collectContainVid:vid]){
  218. }else{
  219. [UserModel addCollect:self.data];
  220. [btn setTitle:@"已收藏" forState:UIControlStateNormal];
  221. [btn setImage:[UserModel imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
  222. }
  223. }
  224. -(void)creatView{
  225. self.selectionStyle = UITableViewCellSelectionStyleNone;
  226. self.backgroundColor = [UIColor clearColor];
  227. UIView *bgv = [UIView new];
  228. bgv.backgroundColor = rgba(255, 255, 255, 0.03);
  229. [self.contentView addSubview:bgv];
  230. [bgv mas_makeConstraints:^(MASConstraintMaker *make) {
  231. make.top.bottom.equalTo(self.contentView);
  232. make.left.equalTo(self.contentView).offset(12);
  233. make.right.equalTo(self.contentView).offset(-12);
  234. }];
  235. UIImageView *iv=[UIImageView new];
  236. iv.tag = 100;
  237. iv.image = [UIImage imageNamed:@""];
  238. iv.backgroundColor = [UIColor grayColor];
  239. [self.contentView addSubview:iv];
  240. [iv mas_makeConstraints:^(MASConstraintMaker *make) {
  241. make.centerY.equalTo(self.contentView);
  242. make.left.equalTo(self.contentView).offset(12);
  243. make.size.mas_equalTo(CGSizeMake(96, 130));
  244. }];
  245. UILabel *nameLabel = [UILabel new];
  246. nameLabel.font= [UIFont boldSystemFontOfSize:18];
  247. nameLabel.textColor = rgba(255, 212, 0, 1);
  248. nameLabel.text = @"你的名字 9.9";
  249. nameLabel.tag = 101;
  250. [self.contentView addSubview:nameLabel];
  251. [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  252. make.left.equalTo(iv.mas_right).offset(8);
  253. make.top.equalTo(self.contentView).offset(5);
  254. }];
  255. UILabel *detailLabel = [UILabel new];
  256. detailLabel.font= [UIFont systemFontOfSize:12];
  257. detailLabel.textColor = rgba(255, 255, 255, 1);
  258. detailLabel.text = @"2023·电视剧·韩国 | 10集全";
  259. detailLabel.tag = 102;
  260. [self.contentView addSubview:detailLabel];
  261. [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  262. make.left.equalTo(nameLabel);
  263. make.top.equalTo(nameLabel.mas_bottom).offset(12);
  264. }];
  265. UILabel *roleLabel = [UILabel new];
  266. roleLabel.font= [UIFont systemFontOfSize:12];
  267. roleLabel.textColor = rgba(255, 255, 255, 0.4);
  268. roleLabel.text = @"主演:李恩泉、李恩泉、李恩泉、李恩泉";
  269. roleLabel.tag = 103;
  270. [self.contentView addSubview:roleLabel];
  271. [roleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  272. make.left.equalTo(nameLabel);
  273. make.top.equalTo(detailLabel.mas_bottom).offset(4);
  274. }];
  275. UIButton *leftButton =[UIButton buttonWithType:UIButtonTypeCustom];
  276. // [leftButton setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
  277. [leftButton setBackgroundColor:rgba(239, 164, 0, 1)];
  278. [leftButton setImage:[UIImage imageNamed:@"Frame12"] forState:UIControlStateNormal];
  279. [leftButton setTitle:@"播放" forState:UIControlStateNormal];
  280. [leftButton setTitleColor:rgba(109, 66, 0, 1) forState:UIControlStateNormal];
  281. leftButton.titleLabel.font = [UIFont systemFontOfSize:12];
  282. leftButton.adjustsImageWhenHighlighted = false;
  283. // [leftButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  284. [self.contentView addSubview:leftButton];
  285. leftButton.userInteractionEnabled = false;
  286. [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
  287. make.size.mas_equalTo(CGSizeMake(82, 32));
  288. make.left.equalTo(nameLabel);
  289. make.bottom.equalTo(iv.mas_bottom).offset(-5);
  290. }];
  291. UIButton *rightButton =[UIButton buttonWithType:UIButtonTypeCustom];
  292. // [rightButton setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
  293. [rightButton setBackgroundColor:rgba(255, 255, 255, 0.05)];
  294. [rightButton setImage:[UIImage imageNamed:@"Frame13"] forState:UIControlStateNormal];
  295. [rightButton setTitle:@"收藏" forState:UIControlStateNormal];
  296. [rightButton setTitleColor:rgba(255, 212, 0, 1) forState:UIControlStateNormal];
  297. rightButton.titleLabel.font = [UIFont systemFontOfSize:12];
  298. rightButton.adjustsImageWhenHighlighted = false;
  299. [rightButton addTarget:self action:@selector(addCollectAction) forControlEvents:UIControlEventTouchUpInside];
  300. rightButton.tag = 150;
  301. [self.contentView addSubview:rightButton];
  302. [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
  303. make.size.mas_equalTo(CGSizeMake(82, 32));
  304. make.left.equalTo(leftButton.mas_right).offset(8);
  305. make.centerY.equalTo(leftButton);
  306. }];
  307. iv.layer.cornerRadius = 8;
  308. iv.layer.masksToBounds = true;
  309. leftButton.layer.cornerRadius = 8;
  310. leftButton.layer.masksToBounds = true;
  311. rightButton.layer.cornerRadius = 8;
  312. rightButton.layer.masksToBounds = true;
  313. bgv.layer.cornerRadius = 8;
  314. bgv.layer.masksToBounds = true;
  315. }
  316. -(void)setFrame:(CGRect)frame{
  317. frame.origin.y += 10;
  318. frame.size.height -= 10;
  319. [super setFrame:frame];
  320. }
  321. @end