// // MOSearchUserVC.m // MiMoLive // // Created by SuperC on 2023/12/25. // //一页的size #define kPageSize 15 #import "MOSearchUserVC.h" #import "MOHistoryListCell.h" #import "MOContactSearchTxf.h" #import "MOUserHomePageVC.h" #import "MOShowLivePagesVC.h" #import "UITextField+CustomClearButton.h" @interface MOSearchUserVC () @property (nonatomic, strong) BigBtn *backButton; @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) UIImageView *searchBgImgView; @property (nonatomic, strong) UIImageView *searchIconView; @property (nonatomic, strong) UITextField *searchTextField; @property (nonatomic, strong) NSMutableArray *dataArr; /** 没有更多的数据视图 */ @property (nonatomic, strong) MONoMoreDataView *noMoreDataView; @property (nonatomic, copy) NSString *next; @end @implementation MOSearchUserVC - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:YES animated:animated]; [[IQKeyboardManager sharedManager] setEnable:NO]; } - (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.searchTextField becomeFirstResponder]; }); } - (void)backButtonAction { [self.navigationController popViewControllerAnimated:YES]; } #pragma mark - UITextFieldDelegate - (BOOL)textFieldShouldReturn:(UITextField *)textField{ if(textField.text.length != 0){ self.next = @""; NSDictionary *basePage = @{@"size":@(kPageSize),@"next":self.next}; NSDictionary *baseDict = @{@"search":textField.text, @"page":basePage}; [self.tableView.mj_footer resetNoMoreData]; [self toSearchSomeOneWithDict:baseDict]; } return YES; } - (void)toSearchSomeOneWithDict:(NSDictionary *)dict{ WEAKSELF [kHttpManager toSearchTheUserInfoWithParams:dict andBlock:^(id _Nonnull data, NSError * _Nonnull error) { __strong typeof(weakSelf) self = weakSelf; [self.tableView.mj_footer endRefreshing]; if(kCode_Success){ // MOLogV(@"%@",[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:0 error:nil] encoding:NSUTF8StringEncoding]); MOLiveMemberData *baseModel = [MOLiveMemberData modelObjectWithDictionary:data[@"data"]]; if(baseModel.next == nil || baseModel.next.length == 0){ if(self.next == nil || self.next.length == 0){ //第一页 self.dataArr = [baseModel.userBase mutableCopy]; } else{ //最后一页 [self.dataArr addObjectsFromArray:baseModel.userBase]; } self.next = @""; [self.tableView.mj_footer endRefreshingWithNoMoreData]; } else{ self.next = baseModel.next; [self.dataArr addObjectsFromArray:baseModel.userBase]; } if(self.dataArr.count == 0){ [MBProgressHUD showTipMessageInWindow:NSLocalString(@"mimo_common_search_no_user_tip")]; } [self.tableView reloadData]; self.tableView.mj_footer.hidden = (self.dataArr.count > 0) ? NO : YES; self.noMoreDataView.isHaveData = (self.dataArr.count > 0) ? YES : NO; } else{ kShowNetError(data) } }]; } #pragma mark - UITableViewDelegate,UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.dataArr.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ WEAKSELF MOUserBase *model = self.dataArr[indexPath.section]; MOHistoryListCell *cell = [tableView dequeueReusableCellWithIdentifier:MOHistoryListCell_ID]; if (cell == nil){ cell = [[MOHistoryListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MOHistoryListCell_ID]; } cell.cellIndexPath = indexPath; cell.cellModel = model; // cell.searchText = self.searchTextField.text; cell.followBtnBlock = ^(MOUserBase * _Nonnull cellModel, NSIndexPath * _Nonnull cellIndexPath) { cellModel.follow = YES; for (MOUserBase *tempModel in weakSelf.dataArr) { if([tempModel.userProfile.id isEqualToString:cellModel.userProfile.id]){ tempModel.follow = YES; break; } } NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:cellIndexPath.section]; [weakSelf.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone]; }; cell.headBtnClickBlock = ^(MOUserBase * _Nonnull cellModel) { if(cellModel.liveRoomId.length > 0){ //去直播间 [weakSelf toShowTheLiveRoomWith:cellModel.liveRoomId]; } else{ //去主页 if(cellModel.userProfile.id.length > 0){ [weakSelf toShowTheUserHomePageWith:cellModel.userProfile.id]; } } }; return cell; } - (void)toShowTheLiveRoomWith:(NSString *)roomId{ [MOShowAgoraKitManager shareManager].isFirstShow = YES; [MOShowAgoraKitManager shareManager].isCreatLive = NO; MOLiveList *model = [[MOLiveList alloc] init]; model.id = roomId; MOShowLivePagesVC *vc = [[MOShowLivePagesVC alloc] init]; vc.userType = MOShowLiveAudienceType; vc.roomList = @[model].mutableCopy; vc.focusIndex = 0; // UINavigationController *newNav = [[UINavigationController alloc] initWithRootViewController:vc]; // newNav.modalPresentationStyle = UIModalPresentationFullScreen; // [self.navigationController presentViewController:newNav animated:YES completion:nil]; vc.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:vc animated:YES]; } - (void)toShowTheUserHomePageWith:(NSString *)userId{ MOUserHomePageVC *vc = [[MOUserHomePageVC alloc] init]; vc.userId = userId; [self.navigationController pushViewController:vc animated:YES]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES]; MOUserBase *model = self.dataArr[indexPath.section]; //去主页 if(model.userProfile.id.length > 0){ [self toShowTheUserHomePageWith:model.userProfile.id]; } } - (void)setupUI { self.view.backgroundColor = [MOTools colorWithHexString:@"#FFFFFF"]; [self.view addSubview:self.backButton]; [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(kTopSafeAreaInset + 15); make.left.mas_equalTo(16); make.size.mas_equalTo(CGSizeMake(24, 24)); }]; [self.view addSubview:self.searchBgImgView]; [self.searchBgImgView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.backButton.mas_right).offset(12); make.right.mas_equalTo(-16); make.centerY.equalTo(self.backButton); make.height.mas_equalTo(36); }]; [self.view addSubview:self.searchIconView]; [self.searchIconView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.searchBgImgView).offset(12); make.centerY.mas_equalTo(self.searchBgImgView); make.size.mas_equalTo(CGSizeMake(18, 18)); }]; [self.view addSubview:self.searchTextField]; [self.searchTextField mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.searchIconView.mas_right).offset(8); make.right.equalTo(self.searchBgImgView).offset(-10); make.top.bottom.equalTo(self.searchBgImgView); }]; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.searchBgImgView.mas_bottom).offset(12); make.left.right.bottom.mas_equalTo(0); }]; self.next = @""; WEAKSELF self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ if(weakSelf.next.length == 0){ return; } NSDictionary *basePage = @{@"size":@(kPageSize),@"next":weakSelf.next}; NSDictionary *baseDict = @{@"search":weakSelf.searchTextField.text, @"page":basePage}; [weakSelf toSearchSomeOneWithDict:baseDict]; }]; self.tableView.mj_footer.hidden = YES; self.tableView.backgroundView = self.noMoreDataView; } #pragma mark - Lazy - (UITableView *)tableView{ if (!_tableView) { _tableView = [[UITableView alloc] init]; if (@available(iOS 11.0, *)) { _tableView.estimatedRowHeight = 0; _tableView.estimatedSectionFooterHeight = 0; _tableView.estimatedSectionHeaderHeight = 0; _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } //iOS15适配 if (@available(iOS 15.0, *)) { self.tableView.sectionHeaderTopPadding = 0; } _tableView.backgroundColor = [UIColor clearColor]; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.dataSource = self; _tableView.delegate = self; _tableView.showsVerticalScrollIndicator = NO; _tableView.showsHorizontalScrollIndicator = NO; _tableView.rowHeight = 64.0; [_tableView registerClass:[MOHistoryListCell class] forCellReuseIdentifier:MOHistoryListCell_ID]; _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; _tableView.backgroundView = self.noMoreDataView; } return _tableView; } - (NSMutableArray *)dataArr{ if(!_dataArr){ _dataArr = [NSMutableArray array]; } return _dataArr; } - (MONoMoreDataView *)noMoreDataView{ if(!_noMoreDataView){ _noMoreDataView = [MONoMoreDataView new]; _noMoreDataView.hidden = YES; } return _noMoreDataView; } - (BigBtn *)backButton { if (!_backButton) { _backButton = [[BigBtn alloc] init]; [_backButton setImage:[UIImage imageNamed:@"v_2_icon_new_back_black"] forState:UIControlStateNormal]; [_backButton addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside]; } return _backButton; } - (UIImageView *)searchBgImgView { if (!_searchBgImgView) { _searchBgImgView = [[UIImageView alloc] initWithImage:[MOTools createImageWithColor:[MOTools colorWithHexString:@"#F3F4FA"]]]; _searchBgImgView.layer.masksToBounds = YES; _searchBgImgView.layer.cornerRadius = 8; } return _searchBgImgView; } - (UIImageView *)searchIconView { if (!_searchIconView) { _searchIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_search_36"]]; } return _searchIconView; } - (UITextField *)searchTextField { if (!_searchTextField) { _searchTextField = [[UITextField alloc] init]; _searchTextField.returnKeyType = UIReturnKeySearch; _searchTextField.delegate = self; [_searchTextField setFont:[MOTextTools mediumFont:12]]; _searchTextField.textColor = [MOTools colorWithHexString:@"#5C5E66"]; NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:NSLocalString(@"mimo_contact_search_hint") attributes:@{ NSForegroundColorAttributeName : [MOTools colorWithHexString:@"#878A99"], NSFontAttributeName : [MOTextTools mediumFont:12] }]; _searchTextField.attributedPlaceholder = placeholderString; UIImage *clearIcon = [UIImage imageNamed:@"icon_textfield_clear"]; [_searchTextField enableCustomClearButtonWithImage:clearIcon]; } return _searchTextField; } @end