Files
featherVoice/QXLive/HomePage(声播)/View/房间/设置/QXRoomBgSettingView.m
2025-08-08 10:49:36 +08:00

292 lines
12 KiB
Objective-C

//
// QXRoomBgSettingView.m
// QXLive
//
// Created by 启星 on 2025/6/23.
//
#import "QXRoomBgSettingView.h"
#import "QXMineNetwork.h"
#import "UIImage+QX.h"
#import "QXOSSManager.h"
#import "QXAlertView.h"
#import <TZImagePickerController.h>
@interface QXRoomBgSettingView()<UIGestureRecognizerDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong)UIView *bgView;
@property (nonatomic,strong)UIButton *publicBgBtn;
@property (nonatomic,strong)UIButton *myBgBtn;
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)NSMutableArray *publicArray;
@property (nonatomic,strong)NSMutableArray *myBgArray;
@property (nonatomic,strong)NSString* roomCoverUrl;
@end
@implementation QXRoomBgSettingView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initSubiews];
}
return self;
}
-(void)initSubiews{
self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
tap.delegate = self;
[self addGestureRecognizer:tap];
self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, ScaleWidth(348))];
self.bgView.backgroundColor = [UIColor whiteColor];
[self.bgView addRoundedCornersWithRadius:16];
[self addSubview:self.bgView];
self.publicBgBtn = [[UIButton alloc] initWithFrame:CGRectMake(16, 18, 70, 24)];
[self.publicBgBtn setTitle:QXText(@"公共背景") forState:(UIControlStateNormal)];
self.publicBgBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
[self.publicBgBtn setTitleColor:QXConfig.textColor forState:(UIControlStateSelected)];
[self.publicBgBtn setTitleColor:RGB16(0x999999) forState:(UIControlStateNormal)];
self.publicBgBtn.selected = YES;
[self.publicBgBtn addTarget:self action:@selector(typeAction:) forControlEvents:UIControlEventTouchUpInside];
[self.bgView addSubview:self.publicBgBtn];
self.myBgBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.publicBgBtn.right+16, 18, 70, 24)];
[self.myBgBtn setTitle:QXText(@"我的背景") forState:(UIControlStateNormal)];
self.myBgBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[self.myBgBtn setTitleColor:QXConfig.textColor forState:(UIControlStateSelected)];
[self.myBgBtn setTitleColor:RGB16(0x999999) forState:(UIControlStateNormal)];
[self.myBgBtn addTarget:self action:@selector(typeAction:) forControlEvents:UIControlEventTouchUpInside];
[self.bgView addSubview:self.myBgBtn];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 20;
layout.minimumInteritemSpacing = 20;
int itemWidth = (self.width-16*2-22*2)/3;
layout.itemSize = CGSizeMake(itemWidth, itemWidth);
layout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 16);
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.publicBgBtn.bottom+12, self.bgView.width, self.bgView.height-self.publicBgBtn.bottom-12) collectionViewLayout:layout];
self.collectionView.scrollEnabled = NO;
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[QXRoomBgSettingCell class] forCellWithReuseIdentifier:@"QXRoomBgSettingCell"];
[self.bgView addSubview:self.collectionView];
[self getRoomBgList];
}
-(void)getRoomBgList{
MJWeakSelf
[QXMineNetwork getRoomBgListSuccessBlock:^(NSArray<QXRoomBgListModel *> * _Nonnull publicArray, NSArray<QXRoomBgListModel *> * _Nonnull myBgArray) {
[weakSelf.publicArray removeAllObjects];
[weakSelf.myBgArray removeAllObjects];
[weakSelf.publicArray addObjectsFromArray:publicArray];
[weakSelf.myBgArray addObjectsFromArray:myBgArray];
QXRoomBgListModel *md = [[QXRoomBgListModel alloc] init];
md.image_url = @"mine_room_cover_add";
[weakSelf.myBgArray insertObject:md atIndex:0];
[weakSelf.collectionView reloadData];
} failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) {
}];
}
-(void)typeAction:(UIButton*)sender{
if (sender.selected) {
return;
}
if (sender == self.publicBgBtn) {
self.publicBgBtn.selected = YES;
self.myBgBtn.selected = NO;
self.publicBgBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
self.myBgBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
}else{
self.publicBgBtn.selected = NO;
self.myBgBtn.selected = YES;
self.myBgBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
self.publicBgBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
}
[self.collectionView reloadData];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (self.publicBgBtn.selected == YES) {
return self.publicArray.count;
}
return self.myBgArray.count;
}
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
QXRoomBgSettingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QXRoomBgSettingCell" forIndexPath:indexPath];
QXRoomBgListModel *model;
if (self.publicBgBtn.selected == YES) {
model = self.publicArray[indexPath.row];
}else{
model = self.myBgArray[indexPath.row];
}
if ([model.image_url hasPrefix:@"http"] || [model.image_url hasPrefix:@"https"]) {
[cell.bgImageView sd_setImageWithURL:[NSURL URLWithString:model.image_url]];
}else{
cell.bgImageView.image = [UIImage imageNamed:model.image_url];
}
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
QXRoomBgListModel *model;
if (self.publicBgBtn.selected == YES) {
model = self.publicArray[indexPath.row];
}else{
model = self.myBgArray[indexPath.row];
}
if (self.publicBgBtn.selected == YES) {
[self showAlertViewWithUrl:model.image_url];
}else{
if (indexPath.row == 0) {
[self selectAlbumPhoto];
}else{
[self showAlertViewWithUrl:model.image_url];
}
}
}
-(void)showAlertViewWithUrl:(NSString*)imageUrl{
MJWeakSelf
QXAlertView *al = [[QXAlertView alloc] initWithFrame:CGRectMake(0, 0, ScaleWidth(275), ScaleWidth(200))];
al.message = @"确定要更换房间背景吗?";
al.commitBlock = ^{
[weakSelf changeRoomBgWithUrl:imageUrl];
};
[[QXGlobal shareGlobal] showView:al controller:self.viewController popType:(PopViewTypeTopToCenter) tapDismiss:NO finishBlock:^{
}];
}
-(void)changeRoomBgWithUrl:(NSString*)imageUrl{
MJWeakSelf
[QXMineNetwork roomInfoEditWithRoomId:self.roomId room_name:@"" room_cover:@"" room_intro:@"" room_background:imageUrl successBlock:^(NSDictionary * _Nonnull dict) {
[weakSelf hide];
} failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) {
showToast(msg);
}];
}
-(void)selectAlbumPhoto {
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
imagePickerVc.maxImagesCount = 1;
imagePickerVc.allowCameraLocation = NO;
imagePickerVc.allowPickingOriginalPhoto = NO;
imagePickerVc.allowTakeVideo = NO;
imagePickerVc.allowPickingVideo = NO;
imagePickerVc.showSelectBtn = NO;
imagePickerVc.allowCrop = NO;
MJWeakSelf
[imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
if (photos.count) {
NSData *imageData = [photos.firstObject qx_compressImageQualityWithToByte:(SCREEN_WIDTH*SCREEN_HEIGHT*2)];
[weakSelf OSSUploadPhotoWithFileData:imageData contentType:[NSString contentTypeWithImageData:imageData]];
}
}];
imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
[imagePickerVc.navigationBar setBackgroundImage:[UIImage imageWithColor:RGB16(0x333333)] forBarMetrics:UIBarMetricsDefault];
[self.viewController presentViewController:imagePickerVc animated:YES completion:nil];
}
- (void)OSSUploadPhotoWithFileData:(NSData *)fileData contentType:(NSString *)contentType {
[self OSSUploadPhotoWithFileData:fileData contentType:contentType isVideoCover:NO];
}
- (void)OSSUploadPhotoWithFileData:(NSData *)fileData contentType:(NSString *)contentType isVideoCover:(BOOL)isVideoCover {
if (!fileData || !contentType) {
QXLOG(@"资源加载错误");
return;
}
if (fileData.length > 1024*1024 && [contentType isEqualToString:IMG_FILE_BASE_PATH]) {
QXLOG(@"请不要上传超过1M的头像");
return;
}
NSMutableArray *files = [[NSMutableArray alloc] initWithObjects:fileData, nil];
NSString *fileBasePath = IMG_FILE_BASE_PATH;// 默认图片上传
NSString *fileName = [NSString stringWithFormat:@"%@%@/%@.%@",fileBasePath,[[QXOSSManager sharedInstance] currentDate],[NSUUID UUID].UUIDString,contentType];
NSMutableArray *fileNames = [[NSMutableArray alloc] initWithObjects:fileName, nil];
__weak typeof(self)weakSelf = self;
[QXProjectTools showLoadingInView:self.viewController.view];
[[QXOSSManager sharedInstance] uploadFile:files withObjectKey:fileNames isAsync:YES complete:^(NSArray<NSString *> *names, UploadImageState state) {
dispatch_async(dispatch_get_main_queue(), ^{
[QXProjectTools hideLoadingInView:self.viewController.view];
});
if (state == UploadImageSuccess) {
NSString *fileName = [names lastObject];
NSString *fileUrl =[NSString stringWithFormat:@"https://%@.%@/%@",OSS_BUCKET_NAME,OSSEndPoint,fileName];
weakSelf.roomCoverUrl = fileUrl;
[weakSelf uploadImage];
}else{
QXLOG(@"文件上传失败,请重新尝试");
}
}];
}
-(void)uploadImage{
MJWeakSelf
[QXMineNetwork uploadRoomBgWithImageUrl:self.roomCoverUrl Id:@"" successBlock:^(NSDictionary * _Nonnull dict) {
[weakSelf getRoomBgList];
} failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) {
}];
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return touch.view == self;
}
-(void)showInView:(UIView *)view{
[view addSubview:self];
[UIView animateWithDuration:0.3 animations:^{
self.bgView.y = SCREEN_HEIGHT-ScaleWidth(348);
}];
}
-(void)hide{
[UIView animateWithDuration:0.3 animations:^{
self.bgView.y = SCREEN_HEIGHT;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
-(NSMutableArray *)publicArray{
if (!_publicArray) {
_publicArray = [NSMutableArray array];
}
return _publicArray;
}
-(NSMutableArray *)myBgArray{
if (!_myBgArray) {
_myBgArray = [NSMutableArray array];
}
return _myBgArray;
}
@end
@implementation QXRoomBgSettingCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initSubView];
}
return self;
}
-(void)initSubView{
self.bgImageView = [[UIImageView alloc] init];
self.bgImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.bgImageView addRoundedCornersWithRadius:10];
[self.contentView addSubview:self.bgImageView];
[self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.bottom.equalTo(self.contentView);
}];
}
@end