// // QXMyRoomViewController.m // SoundRiver // // Created by mac on 2020/7/17. // #import "QXCreateRoomViewController.h" #import "TZImagePickerController/TZImagePickerController.h" #import "QXTextView.h" #import "QXOSSManager.h" #import "UIImage+QX.h" #import "QXTimer.h" #import "QXMineNetwork.h" @interface QXCreateRoomViewController () @property (nonatomic,strong) UILabel *roomCoverLabel; @property (nonatomic,strong) UIImageView *roomImageView; @property (nonatomic,strong) UIButton *roomImageBtn; @property (nonatomic,strong) NSString* roomCoverUrl; @property (nonatomic,strong) UILabel *roomNameLabel; @property (nonatomic,strong) UIView *textFieldbgView; @property (nonatomic,strong) UITextField *textField; @property (nonatomic,strong) UIButton *deleteBtn; @property (nonatomic,strong) UIButton *randomBtn; @property (nonatomic,strong) UILabel *roomNoticeLabel; @property (nonatomic,strong) UIView *noticeBgView; @property (nonatomic,strong) QXTextView *textView; @property (nonatomic,strong) UILabel *warningLabel; @property (nonatomic,strong) UIButton *submitBtn; ///< 提交 @property (nonatomic,assign)NSInteger selectedIndex; @property (nonatomic,strong) QXTimer *timer; @end @implementation QXCreateRoomViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } -(void)setNavgationItems{ [super setNavgationItems]; self.navigationItem.title = QXText(@"创建房间"); } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:NO animated:YES]; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.view endEditing:YES]; } - (void)initSubViews { self.selectedIndex = -1; [self.view addSubview:self.roomImageView]; [self.view addSubview:self.roomImageBtn]; [self.view addSubview:self.roomCoverLabel]; [self.roomImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.view); make.size.mas_equalTo(CGSizeMake(120, 120)); make.top.mas_equalTo(NavContentHeight+20); }]; [self.roomImageBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.top.equalTo(self.roomImageView); }]; [self.roomCoverLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.roomImageView.mas_bottom).offset(10); make.centerX.mas_equalTo(self.view); make.height.mas_equalTo(24); }]; [self.view addSubview:self.roomNameLabel]; [self.view addSubview:self.textFieldbgView]; [self.view addSubview:self.randomBtn]; [self.textFieldbgView addSubview:self.textField]; [self.textFieldbgView addSubview:self.deleteBtn]; [self.roomNameLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.roomCoverLabel.mas_bottom).offset(12); make.left.mas_equalTo(16); make.right.mas_equalTo(-16); make.height.mas_equalTo(24); }]; [self.randomBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(-16); make.height.mas_equalTo(44); make.top.equalTo(self.roomNameLabel.mas_bottom).offset(12); make.width.mas_equalTo(65); }]; [self.textFieldbgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.roomNameLabel.mas_bottom).offset(12); make.left.equalTo(self.roomNameLabel); make.right.equalTo(self.randomBtn.mas_left).offset(-8); make.height.mas_equalTo(44); }]; [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.equalTo(self.textFieldbgView); make.right.equalTo(self.textFieldbgView).offset(-12); make.width.mas_equalTo(44); }]; [self.textField mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.equalTo(self.textFieldbgView); make.left.equalTo(self.textFieldbgView).offset(12); make.right.equalTo(self.deleteBtn.mas_left).offset(-4); }]; [self.view addSubview:self.roomNoticeLabel]; [self.view addSubview:self.noticeBgView]; [self.noticeBgView addSubview:self.textView]; [self.roomNoticeLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.textFieldbgView.mas_bottom).offset(12); make.left.equalTo(self.roomNameLabel); make.height.mas_equalTo(24); }]; [self.noticeBgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.roomNoticeLabel.mas_bottom).offset(12); make.left.equalTo(self.roomNameLabel); make.right.mas_equalTo(-16); make.height.mas_equalTo(150); }]; [self.textView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.mas_equalTo(5); make.right.mas_equalTo(-5); make.bottom.mas_equalTo(-5); }]; [self.view addSubview:self.submitBtn]; [self.submitBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view).offset(38); make.right.equalTo(self.view).offset(-38); make.height.mas_equalTo(42); make.bottom.equalTo(self.view).offset(-kSafeAreaBottom); }]; [self.warningLabel sizeToFit]; self.warningLabel.frame = CGRectMake((SCREEN_WIDTH-self.warningLabel.width)/2.0-8, SCREEN_HEIGHT-kSafeAreaBottom-42-8-21, self.warningLabel.width+16, 21); [self.view addSubview:self.warningLabel]; _timer = [QXTimer scheduledTimerWithTimeInterval:3 repeats:YES queue:dispatch_get_main_queue() block:^{ [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ self->_warningLabel.y = SCREEN_HEIGHT-kSafeAreaBottom-42-8-21+5; } completion:^(BOOL finished) { [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ self->_warningLabel.y = SCREEN_HEIGHT-kSafeAreaBottom-42-8-21-5; } completion:^(BOOL finished) { }]; }]; }]; if (self.roomModel) { [self.roomImageView sd_setImageWithURL:[NSURL URLWithString:self.roomModel.room_info.room_cover]]; self.textField.text = self.roomModel.room_info.room_name; self.textView.text = self.roomModel.room_info.room_intro; if ([self.roomModel.room_info.room_intro isExist]) { self.textView.placehoulderLabel.hidden = YES; } self.roomCoverUrl = self.roomModel.room_info.room_cover; [self.submitBtn setTitle:QXText(@"确认修改") forState:UIControlStateNormal]; self.warningLabel.hidden = YES; self.navigationItem.title = QXText(@"编辑房间"); }else{ [self.submitBtn setTitle:QXText(@"提交审核") forState:UIControlStateNormal]; self.warningLabel.hidden = NO; [self getRandomName]; } } -(void)getRandomName{ MJWeakSelf [QXMineNetwork getRoomRandomNameSuccessBlock:^(NSString * _Nonnull name) { weakSelf.textField.text = name; } failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) { }]; } #pragma mark - Request Data - (void)requestCreateMineRoomData { if (self.textField.text.length == 0) { showToast(@"请输入房间名称"); return; } if (self.roomCoverUrl.length == 0) { showToast(@"请上传房间封面"); return; } MJWeakSelf if (self.roomModel) { [QXMineNetwork roomInfoEditWithRoomId:self.roomModel.room_info.room_id room_name:self.textField.text room_cover:self.roomCoverUrl room_intro:self.textView.text room_background:@"" successBlock:^(NSDictionary * _Nonnull dict) { if (weakSelf.editSucceessBlock) { weakSelf.roomModel.room_info.room_name = weakSelf.textField.text; weakSelf.roomModel.room_info.room_cover = weakSelf.roomCoverUrl; weakSelf.roomModel.room_info.room_intro = weakSelf.textView.text; weakSelf.editSucceessBlock(weakSelf.roomModel); } [weakSelf.navigationController popViewControllerAnimated:YES]; } failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) { showToast(msg) }]; }else{ [QXMineNetwork createRoomWithName:self.textField.text room_cover:self.roomCoverUrl room_intro:self.textView.text successBlock:^(NSDictionary * _Nonnull dict) { if (weakSelf.createSucceessBlock) { weakSelf.createSucceessBlock(); } [weakSelf.navigationController popViewControllerAnimated:YES]; } 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 = YES; imagePickerVc.cropRect = CGRectMake(0, (SCREEN_HEIGHT-SCREEN_WIDTH)/2, SCREEN_WIDTH, SCREEN_WIDTH); MJWeakSelf [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray *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 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.view]; [[QXOSSManager sharedInstance] uploadFile:files withObjectKey:fileNames isAsync:YES complete:^(NSArray *names, UploadImageState state) { dispatch_async(dispatch_get_main_queue(), ^{ [QXProjectTools hideLoadingInView:self.view]; }); if (state == UploadImageSuccess) { NSString *fileName = [names lastObject]; NSString *fileUrl =[NSString stringWithFormat:@"https://%@.%@/%@",OSS_BUCKET_NAME,OSSEndPoint,fileName]; weakSelf.roomCoverUrl = fileUrl; [weakSelf.roomImageView sd_setImageWithURL:[NSURL URLWithString:fileUrl] placeholderImage:[UIImage imageNamed:@"mine_room_cover_add"]]; }else{ QXLOG(@"文件上传失败,请重新尝试"); } }]; } /// 跳转到我的直播间 - (void)jumpMineLivingRoomWith:(int64_t)roomId { } #pragma mark - UITextFieldDelegate - (BOOL)textFieldShouldReturn:(UITextField *)textField { return [self.textField resignFirstResponder]; } - (void)textFieldDidChanged:(UITextField *)textField { if (textField.text.length > 20) { [textField deleteBackward]; } } #pragma mark - Btn Click //- (void)onChatBtnClick:(UIButton *)btn { // [MineAnalyticsManager DataBuryingClickInitRoomSelectTag:@{@"tag":@"chat"}]; // btn.selected = !btn.selected; // self.gameBtn.selected = NO; //} //- (void)onKSongBtnClick:(UIButton *)btn { // [MineAnalyticsManager DataBuryingClickInitRoomSelectTag:@{@"tag":@"song"}]; // btn.selected = !btn.selected; // self.chatBtn.selected = NO; //} //-(void)typeBtnClick:(UIButton*)sender{ // self.typeBtn.selected = !self.typeBtn.selected; // sender.selected = !sender.selected; // self.typeBtn = sender; //} - (void)onSubmitBtnClick:(UIButton *)btn { [self requestCreateMineRoomData]; } -(void)clearAllAction{ self.textField.text = @""; } #pragma mark - Getter - (UILabel *)roomNameLabel { if (!_roomNameLabel) { _roomNameLabel = [[UILabel alloc] init]; _roomNameLabel.font = [UIFont boldSystemFontOfSize:16]; _roomNameLabel.textColor = RGB16A(0x000000, 0.45); _roomNameLabel.text = QXText(@"房间标题"); } return _roomNameLabel; } -(UIView *)textFieldbgView{ if (!_textFieldbgView) { _textFieldbgView = [[UIView alloc] init]; _textFieldbgView.layer.masksToBounds = YES; _textFieldbgView.layer.cornerRadius = 11; _textFieldbgView.backgroundColor = [UIColor colorWithHexString:@"#F0EEF7"]; } return _textFieldbgView; } -(UIButton *)deleteBtn{ if (!_deleteBtn) { _deleteBtn = [[UIButton alloc] init]; [_deleteBtn setImage:[UIImage imageNamed:@"Plus Circle"] forState:(UIControlStateNormal)]; [_deleteBtn addTarget:self action:@selector(clearAllAction) forControlEvents:(UIControlEventTouchUpInside)]; } return _deleteBtn; } - (UITextField *)textField { if (!_textField) { _textField = [[UITextField alloc] init]; _textField.layer.cornerRadius = 5; _textField.layer.masksToBounds = YES; _textField.font = [UIFont boldSystemFontOfSize:16]; _textField.textColor = QXConfig.textColor; _textField.placeholder = QXText(@"请输入房间标题"); _textField.textAlignment = NSTextAlignmentLeft; _textField.delegate = self; _textField.returnKeyType = UIReturnKeyDone; [_textField addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged]; } return _textField; } - (UILabel *)roomNoticeLabel { if (!_roomNoticeLabel) { _roomNoticeLabel = [[UILabel alloc] init]; _roomNoticeLabel.font = [UIFont boldSystemFontOfSize:15]; _roomNoticeLabel.textColor = RGB16A(0x000000, 0.45); _roomNoticeLabel.text = QXText(@"房间公告"); } return _roomNoticeLabel; } -(UILabel *)roomCoverLabel{ if (!_roomCoverLabel) { _roomCoverLabel = [[UILabel alloc] init]; _roomCoverLabel.font = [UIFont systemFontOfSize:14]; _roomCoverLabel.textColor = QXConfig.textColor; _roomCoverLabel.text = QXText(@"房间封面"); } return _roomCoverLabel; } -(UIImageView *)roomImageView{ if (!_roomImageView) { _roomImageView = [[UIImageView alloc] init]; _roomImageView.image = [UIImage imageNamed:@"mine_room_cover_add"]; _roomImageView.contentMode = UIViewContentModeScaleAspectFill; _roomImageView.clipsToBounds = YES; } return _roomImageView; } -(UIView *)noticeBgView{ if (!_noticeBgView) { _noticeBgView = [[UIView alloc] init]; _noticeBgView.backgroundColor = RGB16(0xF0EEF7); [_noticeBgView addRoundedCornersWithRadius:11]; } return _noticeBgView; } -(QXTextView *)textView{ if (!_textView) { _textView = [[QXTextView alloc] init]; _textView.font = [UIFont systemFontOfSize:12]; _textView.textColor = QXConfig.textColor; _textView.placehoulder = QXText(@"本房间严禁刷屏,禁止非法广告宣传,禁止引战地域黑,语言攻击等"); _textView.backgroundColor = [UIColor clearColor]; } return _textView; } -(UIButton *)roomImageBtn{ if (!_roomImageBtn) { _roomImageBtn = [[UIButton alloc] init]; [_roomImageBtn addTarget:self action:@selector(selectAlbumPhoto) forControlEvents:(UIControlEventTouchUpInside)]; } return _roomImageBtn; } -(UIButton *)randomBtn{ if (!_randomBtn) { _randomBtn = [[UIButton alloc] init]; [_randomBtn setTitle:QXText(@"随机名称") forState:(UIControlStateNormal)]; _randomBtn.titleLabel.font = [UIFont systemFontOfSize:14]; [_randomBtn setTitleColor:QXConfig.themeColor forState:(UIControlStateNormal)]; [_randomBtn addTarget:self action:@selector(getRandomName) forControlEvents:UIControlEventTouchUpInside]; } return _randomBtn; } - (UIButton *)submitBtn { if (!_submitBtn) { _submitBtn = [[UIButton alloc] init]; [_submitBtn setTitle:QXText(@"提交审核") forState:UIControlStateNormal]; _submitBtn.titleLabel.font = [UIFont systemFontOfSize:15]; [_submitBtn setTitleColor:QXConfig.btnTextColor forState:UIControlStateNormal]; _submitBtn.backgroundColor = QXConfig.themeColor; _submitBtn.layer.masksToBounds = YES; _submitBtn.layer.cornerRadius = 21; [_submitBtn addTarget:self action:@selector(onSubmitBtnClick:) forControlEvents:UIControlEventTouchUpInside]; } return _submitBtn; } -(UILabel *)warningLabel{ if (!_warningLabel) { _warningLabel = [[UILabel alloc] init]; // _warningLabel.backgroundColor = RGB16(0x333333); // [_warningLabel addRoundedCornersWithRadius:3]; _warningLabel.textColor = RGB16(0xEEA24C); _warningLabel.font = [UIFont systemFontOfSize:11]; _warningLabel.textAlignment = NSTextAlignmentCenter; _warningLabel.text = QXText(@"创建房间需要审核,通过后可开启直播"); } return _warningLabel; } @end