// // YYJYRelationView.m // YaYin // // Created by bj_szd on 2023/7/12. // Copyright © 2023 YaYin. All rights reserved. // #import "YYJYRelationView.h" #import "YYJYRelationCell.h" @interface YYJYRelationView () @property (nonatomic, strong) NSArray *dataArray; @property (nonatomic, assign) NSInteger selectedIndex; @end @implementation YYJYRelationView - (void)awakeFromNib { [super awakeFromNib]; self.selectedIndex = -1; [self createUI]; } - (void)setViewModel:(RCMicRoomViewModel *)viewModel { _viewModel = viewModel; [self fetchRelationData]; } - (IBAction)onDismiss:(id)sender { [self removeFromSuperview]; } - (void)createUI { [self.applyBtn setJianBianWithCGSize:CGSizeMake(ScreenWidth-34*2, 44)]; WEAK_SELF [self.touchImgV dg_Tapped:^{ [weakSelf removeFromSuperview]; }]; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; CGFloat itemW = (ScreenWidth-15*2-12*2)/3; layout.itemSize = CGSizeMake(itemW, 50); layout.minimumInteritemSpacing = 0; layout.minimumLineSpacing = 12; layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15); _collectionView.collectionViewLayout = layout; _collectionView.delegate = self; _collectionView.dataSource = self; [_collectionView registerNib:[UINib nibWithNibName:@"YYJYRelationCell" bundle:nil] forCellWithReuseIdentifier:@"YYJYRelationCell"]; } - (void)fetchRelationData { NSDictionary *params = @{@"type":@"1"}; [[AFNetworkRequset shared] postRequestWithParams:params Path:@"/api/dating_room/get_relation_list" Loading:NO Hud:NO Success:^(id _Nonnull responseDic) { NSArray *arr = [YYJYRelationModel mj_objectArrayWithKeyValuesArray:responseDic[@"data"]]; self.dataArray = arr; [self.collectionView reloadData]; } Failure:^(id _Nonnull errorData) { }]; } - (void)setDataDict:(NSDictionary *)dataDict { _dataDict = dataDict; NSString *head_pic_1 = [dataDict[@"user_info"] safeStringForKey:@"head_pic"]; NSString *nick_name_1 = [dataDict[@"user_info"] safeStringForKey:@"nick_name"]; NSString *head_pic_2 = [dataDict[@"receive_info"] safeStringForKey:@"head_pic"]; NSString *nick_name_2 = [dataDict[@"receive_info"] safeStringForKey:@"nick_name"]; [self.avatarImgV_1 sd_setImageWithURL:[NSURL URLWithString:head_pic_1] placeholderImage:kDefaultUserIcon]; self.nicknameLab_1.text = nick_name_1; [self.avatarImgV_2 sd_setImageWithURL:[NSURL URLWithString:head_pic_2] placeholderImage:kDefaultUserIcon]; self.nicknameLab_2.text = nick_name_2; } - (IBAction)onApplyUp:(id)sender { if (self.selectedIndex == -1) { [HelpPageDefine showMessage:@"请选择关系"]; return; } YYJYRelationModel *model = self.dataArray[self.selectedIndex]; NSString *uid_1 = [self.dataDict[@"user_info"] safeStringForKey:@"uid"]; NSString *uid_2 = [self.dataDict[@"receive_info"] safeStringForKey:@"uid"]; NSDictionary *params = @{@"rid":C_string(self.viewModel.roomInfo.roomId), @"id":model.id, @"user_id":uid_1, @"receive_uid":uid_2}; [[AFNetworkRequset shared] postRequestWithParams:params Path:@"/api/dating_room/binding_user_relation" Loading:YES Hud:YES Success:^(id _Nonnull responseDic) { [self removeFromSuperview]; } Failure:^(id _Nonnull errorData) { }]; } #pragma mark - UICollectionViewDelegate && UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.dataArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ YYJYRelationCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YYJYRelationCell" forIndexPath:indexPath]; YYJYRelationModel *model = self.dataArray[indexPath.row]; cell.titleLab.text = model.name; if (self.selectedIndex == indexPath.row) { cell.bgImgV.image = ImageNamed(@"relation_item_sel"); cell.titleLab.textColor = kBlackColor; }else { cell.bgImgV.image = ImageNamed(@"relation_item_nor"); cell.titleLab.textColor = kWhiteColor; } return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { self.selectedIndex = indexPath.row; [self.collectionView reloadData]; } @end