// // YXEditRoomViewController.m // Temperature // // Created by TC on 2025/2/7. // #import "YXEditRoomViewController.h" #import "YXHomeRoomModel.h" #import "YXRoomManagerTableViewCell.h" #import "RDSRootControl.h" @interface YXEditRoomViewController () @property (weak, nonatomic) UIView *contentView; @property (weak, nonatomic) UITableView *tableView; @property (strong, nonatomic) NSMutableArray *dataArray; @property (strong, nonatomic) UILabel *emptyLabel; @end @implementation YXEditRoomViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; self.navigationItem.title = @"房间管理"; [self addLeftItem]; [self addRightItem]; [self createTableView]; } -(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_deleteRoomsWithIndex:(NSInteger)index { YXHomeRoomModel *model = self.dataArray[index];// NSArray *deleteArray = @[model.record_id]; NSString *homeIdStr = self.homeID.length == 0 ? TheDataManager.current_home_id : self.homeID; RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_deleteRoomsRecordIds:deleteArray homeId:homeIdStr success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 9999) { [RDSRootControl shareControl].isLoginSuccess = NO; TheDataManager.token = @""; } if ([responseObject[@"code"] intValue] == 0) { [weakSelf.dataArray removeObjectAtIndex:index]; [weakSelf.tableView reloadData]; } else{ [RDSHudShower showBottomToast:responseObject[@"message"]]; } } failure:^(NSError *error) { }]; } -(void)p_updateRoomsSequence { NSMutableArray *idArray = [[NSMutableArray alloc]initWithCapacity:5]; for (int i = 0; i < self.dataArray.count; i++) { YXHomeRoomModel *model = self.dataArray[i]; [idArray addObject:model.record_id]; } RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_putRoomsSequence:idArray success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 9999) { [RDSRootControl shareControl].isLoginSuccess = NO; TheDataManager.token = @""; } if ([responseObject[@"code"] intValue] == 0) { [weakSelf.navigationController popViewControllerAnimated:YES]; } else{ [RDSHudShower showBottomToast:responseObject[@"message"]]; } } failure:^(NSError *error) { }]; } -(void)addLeftItem { UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [leftBtn setTitle:@"取消" forState:UIControlStateNormal]; [leftBtn addTarget:self action:@selector(leftBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [leftBtn setTitleColor:[UIColor colorWithHexString:@"#267AFF"] forState:UIControlStateNormal]; leftBtn.titleLabel.font = [UIFont systemFontOfSize:16]; UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn]; self.navigationItem.leftBarButtonItem = leftItem; } -(void)leftBtnAction:(UIButton *)btn { [self.navigationController popViewControllerAnimated:YES]; } -(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 { [self.tableView setEditing:NO animated:YES]; [self p_updateRoomsSequence]; } -(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)createTableView { 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(@(-kSafeBottomHeight)); }]; [self.tableView setEditing:YES animated:YES]; } - (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.hidden = YES; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } -(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 - edit -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; } -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [self p_deleteRoomsWithIndex:indexPath.row]; } } #pragma mark - move -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { [self.dataArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row]; [tableView exchangeSubviewAtIndex:sourceIndexPath.row withSubviewAtIndex:destinationIndexPath.row]; } /* #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