// // YXSetRoomViewController.m // Temperature // // Created by TC on 2025/2/9. // #import "YXSetRoomViewController.h" #import "YXSetRoomModel.h" #import "YXSetRoomTableViewCell.h" #import #import "RDSRootControl.h" @interface YXSetRoomViewController () @property (weak, nonatomic) UIView *contentView; @property (weak, nonatomic) UITableView *tableView; @property (weak, nonatomic) UILabel *textLabel; @property (strong, nonatomic) NSMutableArray *inArray; @property (strong, nonatomic) NSMutableArray *outArray; @property (strong, nonatomic) NSMutableArray *deletArray; @property (strong, nonatomic) YXSetRoomModel *setRoomModel; @end @implementation YXSetRoomViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; self.navigationItem.title = @"房间设置"; [self addRightItem]; [self createTableView]; self.setRoomModel = [[YXSetRoomModel alloc]init]; self.deletArray = [[NSMutableArray alloc]init]; self.textLabel.text = self.model.name; [self getDeviceList]; } -(void)getDeviceList { RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_getAllDevicesWithRoomId:self.model.record_id success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 9999) { [RDSRootControl shareControl].isLoginSuccess = NO; TheDataManager.token = @""; } if ([responseObject[@"code"] integerValue] == 0) { NSDictionary *dataDic = responseObject[@"data"]; NSArray *inArray = dataDic[@"in_room"]; NSArray *outArray = dataDic[@"out_room"]; weakSelf.setRoomModel = [YXSetRoomModel mj_objectWithKeyValues:dataDic]; weakSelf.setRoomModel.in_room = [YXSetRoomDeviceModel mj_objectArrayWithKeyValuesArray:inArray]; weakSelf.setRoomModel.out_room = [YXSetRoomDeviceModel mj_objectArrayWithKeyValuesArray:outArray]; weakSelf.inArray = [[NSMutableArray alloc]initWithArray:weakSelf.setRoomModel.in_room]; weakSelf.outArray = [[NSMutableArray alloc]initWithArray:weakSelf.setRoomModel.out_room]; [weakSelf.tableView reloadData]; } } 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 { [self.tableView setEditing:NO animated:YES]; NSMutableArray *allArray = [[NSMutableArray alloc]initWithCapacity:5]; for (int i = 0; i < self.inArray.count; i++) { YXSetRoomDeviceModel *deviceModel = self.inArray[i]; NSString *deviceId = deviceModel.record_id; NSString *roomId = self.model.record_id; NSDictionary *dic = @{ @"room_id":roomId, @"device_id":deviceId }; [allArray addObject:dic]; } for (int i = 0; i < self.deletArray.count; i++) { YXSetRoomDeviceModel *deviceModel = self.deletArray[i]; NSString *deviceId = deviceModel.record_id; NSDictionary *dic = @{ @"room_id":@"", @"device_id":deviceId }; [allArray addObject:dic]; } RDS_WEAKSELF(weakSelf) NSString *name = self.textLabel.text; [RDSDemoApiHelper rds_updateRoomName:name devices:allArray record_id:self.model.record_id success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 9999) { [RDSRootControl shareControl].isLoginSuccess = NO; TheDataManager.token = @""; } if ([responseObject[@"code"] intValue] == 0) { [self.navigationController popViewControllerAnimated:YES]; } else{ [RDSHudShower showBottomToast:responseObject[@"message"]]; } } failure:^(NSError *error) { }]; } -(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:@"YXSetRoomTableViewCell" bundle:nil] forCellReuseIdentifier:@"setRoomCellID"]; 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 createTableHeaderView]; [self.tableView setEditing:YES animated:YES]; } -(void)createTableHeaderView { UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 75)]; headerView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; UIView *bgView = [[UIView alloc]init]; bgView.backgroundColor = [UIColor whiteColor]; [headerView addSubview:bgView]; [bgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@10); make.right.equalTo(@-10); make.left.equalTo(@10); make.bottom.equalTo(@0); }]; UILabel *nameLabel = [[UILabel alloc]init]; nameLabel.textColor = [UIColor colorWithHexString:@"#333333"]; nameLabel.font = [UIFont systemFontOfSize:15]; nameLabel.text = @"房间名称"; [bgView addSubview:nameLabel]; [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@0); make.left.equalTo(@10); make.bottom.equalTo(@0); make.width.equalTo(@150); }]; UIImageView *imgView = [[UIImageView alloc]init]; imgView.image = [UIImage imageNamed:@"home_right_arrow"]; [bgView addSubview:imgView]; [imgView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(@0); make.right.equalTo(@-30); make.width.equalTo(@9); make.height.equalTo(@15); }]; UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 150, 50)]; textLabel.textColor = [UIColor colorWithHexString:@"#999999"]; textLabel.font = [UIFont systemFontOfSize:15]; textLabel.textAlignment = NSTextAlignmentRight; [bgView addSubview:textLabel]; [textLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@0); make.right.equalTo(imgView.mas_left).offset(-10); make.bottom.equalTo(@0); make.width.equalTo(@150); }]; self.textLabel = textLabel; self.tableView.tableHeaderView = headerView; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(setRoomNameAction)]; [bgView addGestureRecognizer:tap]; } -(void)setRoomNameAction { RDS_WEAKSELF(weakSelf) 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); weakSelf.textLabel.text = 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 = @"请输入房间名称"; textField.text = weakSelf.model.name; }]; [self presentViewController:alertController animated:YES completion:nil]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) { return self.inArray.count; } if (section == 1) { return self.outArray.count; } return 0; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { YXSetRoomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"setRoomCellID" forIndexPath:indexPath]; cell.selectionStyle = UITableViewCellSelectionStyleNone; if (indexPath.section == 0) { YXSetRoomDeviceModel *model = self.inArray[indexPath.row]; if (model.icon.length > 0) { [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", TheApiHelper.baseUrlHeadImg, model.icon]]]; } cell.titleLabel.text = model.name; } if (indexPath.section == 1) { YXSetRoomDeviceModel * model = self.outArray[indexPath.row]; if (model.icon.length > 0) { [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", TheApiHelper.baseUrlHeadImg, model.icon]]]; } cell.titleLabel.text = model.name; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 50)]; UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 150, 50)]; titleLabel.textColor = [UIColor colorWithHexString:@"#999999"]; titleLabel.font = [UIFont systemFontOfSize:15]; [headerView addSubview:titleLabel]; if (section == 0) { titleLabel.text = @"房间设备"; } else if (section == 1) { titleLabel.text = @"不在此房间设备"; } return headerView; } -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *footerView = [[UIView alloc]init]; return footerView; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 50; } -(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 { if (indexPath.section == 0) { return UITableViewCellEditingStyleDelete; } return UITableViewCellEditingStyleInsert; } -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete && indexPath.section == 0) { YXSetRoomDeviceModel *model = self.inArray[indexPath.row]; [self.inArray removeObjectAtIndex:indexPath.row]; [self.outArray addObject:model]; [self.deletArray addObject:model]; [tableView reloadData]; } if (editingStyle == UITableViewCellEditingStyleInsert && indexPath.section == 1) { YXSetRoomDeviceModel *model = self.outArray[indexPath.row]; [self.outArray removeObjectAtIndex:indexPath.row]; [self.inArray addObject:model]; for (YXSetRoomDeviceModel *deletModel in self.deletArray) { if ([model.record_id isEqualToString:deletModel.record_id]) { [self.deletArray removeObject:model]; } } [tableView reloadData]; } } #pragma mark - move -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { return YES; } return NO; } -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { if (sourceIndexPath.section == 0 && destinationIndexPath.section == 0) { [self.inArray 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