// // XYMyVoiceRoomListViewController.m // Starbuds // // Created by 翟玉磊 on 2020/7/7. // Copyright © 2020 翟玉磊. All rights reserved. // #import "XYMyVoiceRoomListViewController.h" #import "XYMyVoiceRoomListViewModel.h" #import "XYMyVoiceRoomListModel.h" #import "XYMyVoiceRoomListTableViewCell.h" #import "XYVoiceRoomCreateViewController.h" @interface XYMyVoiceRoomListViewController () @property (nonatomic, strong) XYMyVoiceRoomListViewModel *viewModel; @property (nonatomic, strong) UIButton *createRoomButton; @end @implementation XYMyVoiceRoomListViewController - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:@"VOICE_ROOM_CREATE_SUCCESS_NOTIFICATION" object:nil]; } /// 重写init方法,配置你想要的属性 - (instancetype)init { self = [super init]; if (self) { } return self; } - (void)viewDidLoad { [super viewDidLoad]; /// 设置 [self _setup]; /// 设置导航栏 [self _setupNavigationItem]; /// 设置子控件 [self _setupSubViews]; /// 布局子空间 [self _makeSubViewsConstraints]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(voiceRoomCreateSuccessNotification:) name:@"VOICE_ROOM_CREATE_SUCCESS_NOTIFICATION" object:nil]; } - (void)voiceRoomCreateSuccessNotification:(NSNotification *)notification { [self tableViewDidTriggerHeaderRefresh]; } #pragma mark — Override - (void)bindViewModel { self.viewModel = XYMyVoiceRoomListViewModel.new; } - (void)tableViewDidTriggerHeaderRefresh { [self.viewModel loadData:^(id _Nullable responseModel) { [self tableViewDidFinishTriggerHeader:YES reload:YES]; [self.tableView zyl_configEmptyViewWithType:ZYLEmptyDataViewTypeDefault emptyInfo:kLocalizedString(@"你还没有属于自己的房间哦~") errorInfo:nil offsetTop:250.0f hasData:self.viewModel.dataSource.count>0 hasError:NO reloadBlock:nil]; self.createRoomButton.hidden = self.viewModel.dataSource.count>0; } failure:^(ZYLNetworkError * _Nullable error) { [self tableViewDidFinishTriggerHeader:YES reload:NO]; [self.tableView zyl_configEmptyViewWithType:ZYLEmptyDataViewTypeDefault emptyInfo:kLocalizedString(@"你还没有属于自己的房间哦~") errorInfo:error.domain offsetTop:250.0f hasData:self.viewModel.dataSource.count>0 hasError:error!=nil reloadBlock:nil]; }]; } - (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath { return [XYMyVoiceRoomListTableViewCell cellWithTableView:tableView]; } - (void)configureCell:(XYMyVoiceRoomListTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object { [cell configureModel:object]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 64.0f; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; XYMyVoiceRoomListModel *model = [self.viewModel.dataSource objectAtIndex:indexPath.row]; [[XYChatRoomManager sharedInstance] showChatRoomWithRoomId:model.roomId roomCoverUrl:model.roomCover completion:^{ }]; } #pragma mark - 事件处理Or辅助方法 - (void)createRoomButtonAction:(id)sender { [self.navigationController pushViewController:XYVoiceRoomCreateViewController.new animated:YES]; } #pragma mark - 初始化 - (void)_setup{ self.title = kLocalizedString(@"我的语音房"); } #pragma mark - 设置导航栏 - (void)_setupNavigationItem{ self.navigationItem.rightBarButtonItem = [UIBarButtonItem systemItemWithTitle:kLocalizedString(@"创建房间") titleColor:Color_TextFont imageName:nil target:self selector:@selector(createRoomButtonAction:) textType:YES]; } #pragma mark - 设置子控件 - (void)_setupSubViews{ [self.view addSubview:self.createRoomButton]; } #pragma mark - 布局子控件 - (void)_makeSubViewsConstraints{ [self.createRoomButton mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.view); make.bottom.equalTo(self.view).offset(-52.0f); make.width.equalTo(@160.0f); make.height.equalTo(@42.0f); }]; [self.createRoomButton addViewBorder:Color_Clear redian:21]; } #pragma mark - Setter & Getter - (UIButton *)createRoomButton { if (!_createRoomButton) { _createRoomButton = [UIButton createButtonTextColor:Color_White textFont:Font_B(18)]; [_createRoomButton setBackgroundImage:[UIImage commonRedGradientColorImageWithImgSize:CGSizeMake(160.0f, 42.0f)] forState:UIControlStateNormal]; [_createRoomButton setTitle:@"创建房间" forState:UIControlStateNormal]; [_createRoomButton addTarget:self action:@selector(createRoomButtonAction:) forControlEvents:UIControlEventTouchUpInside]; _createRoomButton.hidden = YES; } return _createRoomButton; } @end