// // YXRoomManagerViewController.m // Temperature // // Created by TC on 2025/2/6. // #import "YXRoomManagerViewController.h" #import "YXHomeRoomModel.h" #import "YXRoomManagerTableViewCell.h" #import "YXEditRoomViewController.h" #import "YXSetRoomViewController.h" #import "RDSRootControl.h" @interface YXRoomManagerViewController () @property (weak, nonatomic) UIView *contentView; @property (weak, nonatomic) UITableView *tableView; @property (strong, nonatomic) NSMutableArray *dataArray; @property (strong, nonatomic) UILabel *emptyLabel; @end @implementation YXRoomManagerViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; self.navigationItem.title = @"房间管理"; [self addRightItem]; self.dataArray = [[NSMutableArray alloc]initWithCapacity:5]; [self createSubViews]; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self rds_getRoomList]; } - (void)rds_getRoomList { NSString *homeIdStr = self.homeID.length == 0 ? TheDataManager.current_home_id : self.homeID; RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_getRoomListWithHomeId:homeIdStr success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 9999) { [RDSRootControl shareControl].isLoginSuccess = NO; TheDataManager.token = @""; } if ([responseObject[@"code"] intValue] == 0) { NSArray *data = responseObject[@"data"]; NSMutableArray *rooms = [YXHomeRoomModel mj_objectArrayWithKeyValuesArray:data]; for (YXHomeRoomModel *model in rooms) { model.devices = [YXHomeDeviceModel mj_objectArrayWithKeyValuesArray:model.devices]; } weakSelf.dataArray = rooms; TheDataManager.rooms = rooms.mutableCopy; [weakSelf.tableView reloadData]; if (weakSelf.dataArray.count == 0) { weakSelf.emptyLabel.hidden = NO; }else{ weakSelf.emptyLabel.hidden = YES; } } else{ [RDSHudShower showBottomToast:responseObject[@"message"]]; } } failure:^(NSError *error) { }]; } -(void)p_createRoomWithName:(NSString *)name { NSString *homeIdStr = self.homeID.length == 0 ? TheDataManager.current_home_id : self.homeID; RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_createRoom:name homeId:homeIdStr success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 0) { [RDSHudShower showBottomToast:@"房间添加成功"]; [weakSelf rds_getRoomList]; // [weakSelf popViewControllerAnimated:YES]; } else{ [RDSHudShower showBottomToast:responseObject[@"message"]]; } } failure:^(NSError *error) { }]; } -(void)addRightItem { UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [rightBtn setTitle:@"编辑" forState:UIControlStateNormal]; [rightBtn addTarget:self action:@selector(rightBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [rightBtn setTitleColor:[UIColor colorWithHexString:@"#267AFF"] forState:UIControlStateNormal]; rightBtn.titleLabel.font = [UIFont systemFontOfSize:16]; UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn]; self.navigationItem.rightBarButtonItem = rightItem; } -(void)rightBtnAction:(UIButton *)btn { YXEditRoomViewController *editVc = [[YXEditRoomViewController alloc]init]; editVc.homeID = self.homeID; [self.navigationController pushViewController:editVc animated:YES]; } -(UILabel *)emptyLabel { if (_emptyLabel == nil) { _emptyLabel = [[UILabel alloc]init]; _emptyLabel.text = @"暂无数据"; _emptyLabel.font = [UIFont systemFontOfSize:20]; _emptyLabel.textColor = [UIColor colorWithHexString:@"333333"]; _emptyLabel.textAlignment = NSTextAlignmentCenter; _emptyLabel.hidden = YES; [self.view addSubview:_emptyLabel]; [_emptyLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.width.equalTo(@200); make.centerX.equalTo(@0); make.height.equalTo(@50); make.centerY.equalTo(@-30); }]; } return _emptyLabel; } -(void)createSubViews { UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10) style:UITableViewStyleGrouped]; tableView.delegate = self; tableView.dataSource = self; tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [tableView registerNib:[UINib nibWithNibName:@"YXRoomManagerTableViewCell" bundle:nil] forCellReuseIdentifier:@"roomManagerCellID"]; tableView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; [self.view addSubview:tableView]; self.tableView = tableView; [tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@10); make.right.equalTo(@0); make.left.equalTo(@0); make.bottom.equalTo(@(-kTabbarHeight)); }]; UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [addBtn setTitle:@"+ 添加房间" forState:UIControlStateNormal]; [addBtn addTarget:self action:@selector(addBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [addBtn setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal]; addBtn.titleLabel.font = [UIFont systemFontOfSize:16]; addBtn.backgroundColor = [UIColor colorWithHexString:@"#267AFF"]; [self.view addSubview:addBtn]; [addBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.height.equalTo(@45); make.left.equalTo(@20); make.right.equalTo(@-20); make.bottom.equalTo(@(-kSafeBottomHeight)); }]; addBtn.layer.cornerRadius = 22.5; } -(void)addBtnAction:(UIButton *)btn { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"添加房间" message:nil preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"完成" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UITextField *txt = [alertController.textFields objectAtIndex:0]; NSLog(@"%@",txt.text); if (txt.text.length > 0) { [self p_createRoomWithName:txt.text]; } }]; [alertController addAction:sureAction]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alertController addAction:cancelAction]; [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"请输入房间名称"; }]; [self presentViewController:alertController animated:YES completion:nil]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { YXRoomManagerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"roomManagerCellID" forIndexPath:indexPath]; cell.selectionStyle = UITableViewCellSelectionStyleNone; YXHomeRoomModel *model = self.dataArray[indexPath.row]; cell.nameLabel.text = model.name; cell.numberLabel.text = [NSString stringWithFormat:@"%ld个设备",model.devices.count]; cell.picImgView.image = [UIImage imageNamed:@"home_right_arrow"]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { YXHomeRoomModel *model = self.dataArray[indexPath.row]; YXSetRoomViewController *roomVc = [[YXSetRoomViewController alloc]init]; roomVc.model = model; [self.navigationController pushViewController:roomVc animated:YES]; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerView = [[UIView alloc]init]; return headerView; } -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *footerView = [[UIView alloc]init]; return footerView; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0.01; } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.01; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 65; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end