// // YYXQPickGiftAlert.m // YaYin // // Created by bj_szd on 2023/7/12. // Copyright © 2023 YaYin. All rights reserved. // #import "YYXQPickGiftAlert.h" #import "BJGiftItemCell.h" #import "BJRoomGiftModel.h" @interface YYXQPickGiftAlert () @property (nonatomic, strong) NSArray *dataArray; @property (nonatomic, assign) NSInteger selectedIndex; @property (nonatomic, copy) NSString *rid; @property (nonatomic, copy) NSString *micro_id; @end @implementation YYXQPickGiftAlert - (void)awakeFromNib { [super awakeFromNib]; self.selectedIndex = -1; [self createUI]; [self fetchGiftData]; } - (void)fetchGiftData { NSDictionary *params = @{@"page":@"1", @"page_limit":@"1000"}; [[AFNetworkRequset shared] postRequestWithParams:params Path:@"/api/pub_room/get_wish_gift_list" Loading:NO Hud:NO Success:^(id _Nonnull responseDic) { NSArray *arr = [BJRoomGiftModel mj_objectArrayWithKeyValuesArray:responseDic[@"data"]]; self.dataArray = arr; [self.collectionView reloadData]; } Failure:^(id _Nonnull errorData) { }]; } - (void)createUI { [self.confirmBtn setJianBianWithCGSize:CGSizeMake(ScreenWidth-30*2, 44)]; WEAK_SELF [self.touchImgV dg_Tapped:^{ [weakSelf removeFromSuperview]; }]; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; CGFloat itemW = (ScreenWidth-15*2-15*2)/3; layout.itemSize = CGSizeMake(itemW, 126); layout.minimumInteritemSpacing = 15; layout.minimumLineSpacing = 15; layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15); _collectionView.collectionViewLayout = layout; _collectionView.delegate = self; _collectionView.dataSource = self; [_collectionView registerClass:[BJGiftItemCell class] forCellWithReuseIdentifier:@"BJGiftItemCell"]; _collectionView.showsHorizontalScrollIndicator = NO; _collectionView.pagingEnabled = YES; _collectionView.multipleTouchEnabled = NO; } - (void)onShowWith:(NSString *)rid micro_id:(NSString *)micro_id { self.rid = rid; self.micro_id = micro_id; [MainWindow() addSubview:self]; } - (IBAction)onConfirm:(id)sender { if (self.selectedIndex == -1) { [HelpPageDefine showMessage:@"请选择礼物"]; return; } BJRoomGiftModel *model = self.dataArray[self.selectedIndex]; NSDictionary *params = @{@"rid":C_string(self.rid), @"micro_id":C_string(self.micro_id), @"gid":model.gid}; [[AFNetworkRequset shared] postRequestWithParams:params Path:@"/api/pub_room/oprate_user_wish_gift" 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{ BJGiftItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BJGiftItemCell" forIndexPath:indexPath]; BJRoomGiftModel *model = self.dataArray[indexPath.row]; cell.model = model; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { BJRoomGiftModel *model = self.dataArray[indexPath.row]; self.selectedIndex = indexPath.row; for (BJRoomGiftModel *model in self.dataArray) { model.isItemSelected = NO; } model.isItemSelected = YES; [self.collectionView reloadData]; } @end