XYVoiceRoomCreateViewController.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // XYVoiceRoomCreateViewController.m
  3. // Starbuds
  4. //
  5. // Created by 翟玉磊 on 2020/7/7.
  6. // Copyright © 2020 翟玉磊. All rights reserved.
  7. //
  8. #import "XYVoiceRoomCreateViewController.h"
  9. #import "XYVoiceRoomCreateViewModel.h"
  10. #import "XYVoiceRoomCreateCellModel.h"
  11. #import "XYVoiceRoomCreateTableViewCell.h"
  12. #import "XYBankIdentityCardVerIfyViewController.h"
  13. @interface XYVoiceRoomCreateViewController ()<XYVoiceRoomCreateTableViewCellDelegate>
  14. @property (nonatomic, strong) XYVoiceRoomCreateViewModel *viewModel;
  15. @property (nonatomic, strong) UIView *footerView;
  16. @property (nonatomic, strong) UIButton *commitButton;
  17. @end
  18. @implementation XYVoiceRoomCreateViewController
  19. /// 重写init方法,配置你想要的属性
  20. - (instancetype)init
  21. {
  22. self = [super init];
  23. if (self) {
  24. }
  25. return self;
  26. }
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. /// 设置
  30. [self _setup];
  31. /// 设置导航栏
  32. [self _setupNavigationItem];
  33. /// 设置子控件
  34. [self _setupSubViews];
  35. /// 布局子空间
  36. [self _makeSubViewsConstraints];
  37. }
  38. #pragma mark — Override
  39. - (void)bindViewModel {
  40. self.viewModel = XYVoiceRoomCreateViewModel.new;
  41. }
  42. - (void)requestRemoteData {
  43. [self.viewModel getInitValueRequestCompleted:^(NSError * _Nonnull error) {
  44. [self reloadData];
  45. }];
  46. }
  47. - (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath {
  48. return [XYVoiceRoomCreateTableViewCell cellWithTableView:tableView];
  49. }
  50. - (void)configureCell:(XYVoiceRoomCreateTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object {
  51. cell.delegate = self;
  52. [cell setIndexPath:indexPath rowsInSection:self.viewModel.dataSource.count-1];
  53. [cell configureModel:object];
  54. }
  55. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  56. return 60.0f;
  57. }
  58. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  59. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  60. XYVoiceRoomCreateCellModel *model = self.viewModel.dataSource[indexPath.row];
  61. WeakSelf
  62. if ([model.title isEqualToString:kLocalizedString(@"房间标签")]) {
  63. NSMutableArray *tagNames = [NSMutableArray array];
  64. for (XYVoiceRoomTagModel *tagsModel in self.viewModel.voiceRoomTagModels) {
  65. [tagNames addObject:tagsModel.tagName];
  66. }
  67. if (tagNames.count == 0) {
  68. return;
  69. }
  70. BasePickerViewController *tagPicker = [[BasePickerViewController alloc] initWithTitle:model.title dataArray:tagNames target:self sureBlock:^(NSInteger index, id _Nonnull info) {
  71. NSLog(@"tagPicker:%@ 索引:%ld", info, (long)index);
  72. [weakSelf updateModelWithIndexPath:indexPath valueAtIndex:index];
  73. }];
  74. [tagPicker show];
  75. }
  76. if ([model.title isEqualToString:kLocalizedString(@"房间类型")]) {
  77. NSMutableArray *typeNames = [NSMutableArray array];
  78. for (XYVoiceRoomTypeModel *typeModel in self.viewModel.voiceRoomTypeModels) {
  79. [typeNames addObject:typeModel.typeName];
  80. }
  81. if (typeNames.count == 0) {
  82. return;
  83. }
  84. BasePickerViewController *typePicker = [[BasePickerViewController alloc] initWithTitle:model.title dataArray:typeNames target:self sureBlock:^(NSInteger index, id _Nonnull info) {
  85. NSLog(@"tagPicker:%@ 索引:%ld", info, (long)index);
  86. [weakSelf updateModelWithIndexPath:indexPath valueAtIndex:index];
  87. }];
  88. [typePicker show];
  89. }
  90. }
  91. - (void)updateModelWithIndexPath:(NSIndexPath *)indexPath valueAtIndex:(NSInteger)index {
  92. XYVoiceRoomCreateCellModel *model = self.viewModel.dataSource[indexPath.row];
  93. if ([model.title isEqualToString:kLocalizedString(@"房间标签")]) {
  94. XYVoiceRoomTagModel *tagModel = self.viewModel.voiceRoomTagModels[index];
  95. model.content = tagModel.tagName;
  96. model.infoId = tagModel.tagId;
  97. }
  98. if ([model.title isEqualToString:kLocalizedString(@"房间类型")]) {
  99. XYVoiceRoomTypeModel *typeModel = self.viewModel.voiceRoomTypeModels[index];
  100. model.content = typeModel.typeName;
  101. model.infoId = typeModel.typeId;
  102. }
  103. // 刷新cell
  104. [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  105. }
  106. #pragma mark — XYVoiceRoomCreateTableViewCellDelegate
  107. - (void)textChangeWithText:(NSString *)text atIndexPath:(NSIndexPath *)indexPath {
  108. XYVoiceRoomCreateCellModel *model = self.viewModel.dataSource[indexPath.row];
  109. if ([model.title isEqualToString:kLocalizedString(@"房间名称")]) {
  110. model.content = text;
  111. // 更新可提交状态
  112. self.commitButton.enabled = StringIsNotEmpty(text);
  113. }
  114. }
  115. #pragma mark - 事件处理Or辅助方法
  116. // 创建语音房
  117. - (void)commitButtonAction:(id)sender {
  118. NSInteger roomType = -1;
  119. NSString *categoryId = @"";
  120. NSString *roomName = @"";
  121. NSString *roomCover = [XYUserInfoManager nowUser].userAvatar;
  122. for (XYVoiceRoomCreateCellModel *model in self.viewModel.dataSource) {
  123. if ([model.title isEqualToString:kLocalizedString(@"房间名称")]) {
  124. roomName = model.content;
  125. }
  126. if ([model.title isEqualToString:kLocalizedString(@"房间标签")]) {
  127. categoryId = model.infoId;
  128. }
  129. if ([model.title isEqualToString:kLocalizedString(@"房间类型")]) {
  130. roomType = [model.infoId integerValue];
  131. }
  132. }
  133. if (StringIsEmpty(roomName)) {
  134. [SVProgressHUD showInfoWithStatus:kLocalizedString(@"请输入房间名字")];
  135. return;
  136. }
  137. if (StringIsEmpty(categoryId)) {
  138. [SVProgressHUD showInfoWithStatus:kLocalizedString(@"请选择房间标签")];
  139. return;
  140. }
  141. if (roomType == -1) {
  142. [SVProgressHUD showInfoWithStatus:kLocalizedString(@"请选择房间类型")];
  143. return;
  144. }
  145. [SVProgressHUD show];
  146. [[XYVoiceRoomAPIManager new] createRoomWithRoomType:roomType categoryId:categoryId roomName:roomName roomCover:roomCover successHandler:^(ZYLResponseModel *responseModel) {
  147. [SVProgressHUD showSuccessWithStatus:kLocalizedString(@"创建成功")];
  148. // 回调刷新房间列表
  149. [[NSNotificationCenter defaultCenter] postNotificationName:@"VOICE_ROOM_CREATE_SUCCESS_NOTIFICATION" object:nil];
  150. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  151. [self.navigationController popViewControllerAnimated:YES];
  152. });
  153. } failureHandler:^(ZYLNetworkError *error) {
  154. [SVProgressHUD showInfoWithStatus:error.domain];
  155. NSDictionary *data = error.responseObject;
  156. if ([data[@"data"][@"goAuth"] isEqualToString:@"1"]) {//需要认证
  157. CustomActionAlertController *al = [[CustomActionAlertController alloc] initWithTitle:nil message:kLocalizedString(@"根据《网络游戏管理暂行办法》要求,未实名认证部分功能的使用将受到限制") sure:kLocalizedString(@"去认证") cancel:kLocalizedString(@"取消") selctedBlock:^(NSInteger index, NSString * _Nonnull title) {
  158. if ([title isEqualToString:kLocalizedString(@"去认证")]) {
  159. XYBankIdentityCardVerIfyViewController *controller = XYBankIdentityCardVerIfyViewController.new;
  160. controller.targetAction = 2;
  161. [self.navigationController pushViewController:controller animated:YES];
  162. }
  163. }];
  164. [al show];
  165. }
  166. }];
  167. }
  168. #pragma mark - 初始化
  169. - (void)_setup{
  170. self.title = kLocalizedString(@"创建语音房");
  171. }
  172. #pragma mark - 设置导航栏
  173. - (void)_setupNavigationItem{
  174. }
  175. #pragma mark - 设置子控件
  176. - (void)_setupSubViews{
  177. self.tableView.tableFooterView = self.footerView;
  178. }
  179. #pragma mark - 布局子控件
  180. - (void)_makeSubViewsConstraints{
  181. }
  182. #pragma mark - Setter & Getter
  183. - (UIView *)footerView {
  184. if (!_footerView) {
  185. _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64.0f)];
  186. _footerView.backgroundColor = Color_Clear;
  187. [_footerView addSubview:self.commitButton];
  188. }
  189. return _footerView;
  190. }
  191. - (UIButton *)commitButton {
  192. if (!_commitButton) {
  193. _commitButton = [UIButton createButtonTextColor:Color_White textFont:Font_B(16)];
  194. _commitButton.frame = CGRectMake(32.0f, 22.0f, SCREEN_WIDTH - 32.0f * 2, 42.0f);
  195. [_commitButton setTitle:kLocalizedString(@"提交") forState:UIControlStateNormal];
  196. [_commitButton setBackgroundImage:[UIImage commonRedGradientColorImageWithImgSize:_commitButton.size] forState:UIControlStateNormal];
  197. _commitButton.enabled = NO;
  198. [_commitButton addViewBorder:Color_Clear redian:21];
  199. [_commitButton addTarget:self action:@selector(commitButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  200. }
  201. return _commitButton;
  202. }
  203. @end