YXSetRoomViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. //
  2. // YXSetRoomViewController.m
  3. // Temperature
  4. //
  5. // Created by TC on 2025/2/9.
  6. //
  7. #import "YXSetRoomViewController.h"
  8. #import "YXSetRoomModel.h"
  9. #import "YXSetRoomTableViewCell.h"
  10. #import <SDWebImage/SDWebImage.h>
  11. #import "RDSRootControl.h"
  12. @interface YXSetRoomViewController ()<UITableViewDataSource,UITableViewDelegate>
  13. @property (weak, nonatomic) UIView *contentView;
  14. @property (weak, nonatomic) UITableView *tableView;
  15. @property (weak, nonatomic) UILabel *textLabel;
  16. @property (strong, nonatomic) NSMutableArray *inArray;
  17. @property (strong, nonatomic) NSMutableArray *outArray;
  18. @property (strong, nonatomic) NSMutableArray *deletArray;
  19. @property (strong, nonatomic) YXSetRoomModel *setRoomModel;
  20. @end
  21. @implementation YXSetRoomViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. self.view.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  25. self.navigationItem.title = @"房间设置";
  26. [self addRightItem];
  27. [self createTableView];
  28. self.setRoomModel = [[YXSetRoomModel alloc]init];
  29. self.deletArray = [[NSMutableArray alloc]init];
  30. self.textLabel.text = self.model.name;
  31. [self getDeviceList];
  32. }
  33. -(void)getDeviceList
  34. {
  35. RDS_WEAKSELF(weakSelf)
  36. [RDSDemoApiHelper rds_getAllDevicesWithRoomId:self.model.record_id success:^(id responseObject) {
  37. if ([responseObject[@"code"] intValue] == 9999) {
  38. [RDSRootControl shareControl].isLoginSuccess = NO;
  39. TheDataManager.token = @"";
  40. }
  41. if ([responseObject[@"code"] integerValue] == 0) {
  42. NSDictionary *dataDic = responseObject[@"data"];
  43. NSArray *inArray = dataDic[@"in_room"];
  44. NSArray *outArray = dataDic[@"out_room"];
  45. weakSelf.setRoomModel = [YXSetRoomModel mj_objectWithKeyValues:dataDic];
  46. weakSelf.setRoomModel.in_room = [YXSetRoomDeviceModel mj_objectArrayWithKeyValuesArray:inArray];
  47. weakSelf.setRoomModel.out_room = [YXSetRoomDeviceModel mj_objectArrayWithKeyValuesArray:outArray];
  48. weakSelf.inArray = [[NSMutableArray alloc]initWithArray:weakSelf.setRoomModel.in_room];
  49. weakSelf.outArray = [[NSMutableArray alloc]initWithArray:weakSelf.setRoomModel.out_room];
  50. [weakSelf.tableView reloadData];
  51. }
  52. } failure:^(NSError *error) {
  53. }];
  54. }
  55. -(void)addRightItem
  56. {
  57. UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  58. [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
  59. [rightBtn addTarget:self action:@selector(rightBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  60. [rightBtn setTitleColor:[UIColor colorWithHexString:@"#267AFF"] forState:UIControlStateNormal];
  61. rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
  62. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
  63. self.navigationItem.rightBarButtonItem = rightItem;
  64. }
  65. -(void)rightBtnAction:(UIButton *)btn
  66. {
  67. [self.tableView setEditing:NO animated:YES];
  68. NSMutableArray *allArray = [[NSMutableArray alloc]initWithCapacity:5];
  69. for (int i = 0; i < self.inArray.count; i++) {
  70. YXSetRoomDeviceModel *deviceModel = self.inArray[i];
  71. NSString *deviceId = deviceModel.record_id;
  72. NSString *roomId = self.model.record_id;
  73. NSDictionary *dic = @{
  74. @"room_id":roomId,
  75. @"device_id":deviceId
  76. };
  77. [allArray addObject:dic];
  78. }
  79. for (int i = 0; i < self.deletArray.count; i++) {
  80. YXSetRoomDeviceModel *deviceModel = self.deletArray[i];
  81. NSString *deviceId = deviceModel.record_id;
  82. NSDictionary *dic = @{
  83. @"room_id":@"",
  84. @"device_id":deviceId
  85. };
  86. [allArray addObject:dic];
  87. }
  88. RDS_WEAKSELF(weakSelf)
  89. NSString *name = self.textLabel.text;
  90. [RDSDemoApiHelper rds_updateRoomName:name devices:allArray record_id:self.model.record_id success:^(id responseObject) {
  91. if ([responseObject[@"code"] intValue] == 9999) {
  92. [RDSRootControl shareControl].isLoginSuccess = NO;
  93. TheDataManager.token = @"";
  94. }
  95. if ([responseObject[@"code"] intValue] == 0) {
  96. [self.navigationController popViewControllerAnimated:YES];
  97. } else{
  98. [RDSHudShower showBottomToast:responseObject[@"message"]];
  99. }
  100. } failure:^(NSError *error) {
  101. }];
  102. }
  103. -(void)createTableView
  104. {
  105. UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10) style:UITableViewStyleGrouped];
  106. tableView.delegate = self;
  107. tableView.dataSource = self;
  108. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  109. [tableView registerNib:[UINib nibWithNibName:@"YXSetRoomTableViewCell" bundle:nil] forCellReuseIdentifier:@"setRoomCellID"];
  110. tableView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  111. [self.view addSubview:tableView];
  112. self.tableView = tableView;
  113. [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  114. make.top.equalTo(@10);
  115. make.right.equalTo(@0);
  116. make.left.equalTo(@0);
  117. make.bottom.equalTo(@(-kSafeBottomHeight));
  118. }];
  119. [self createTableHeaderView];
  120. [self.tableView setEditing:YES animated:YES];
  121. }
  122. -(void)createTableHeaderView
  123. {
  124. UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 75)];
  125. headerView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  126. UIView *bgView = [[UIView alloc]init];
  127. bgView.backgroundColor = [UIColor whiteColor];
  128. [headerView addSubview:bgView];
  129. [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  130. make.top.equalTo(@10);
  131. make.right.equalTo(@-10);
  132. make.left.equalTo(@10);
  133. make.bottom.equalTo(@0);
  134. }];
  135. UILabel *nameLabel = [[UILabel alloc]init];
  136. nameLabel.textColor = [UIColor colorWithHexString:@"#333333"];
  137. nameLabel.font = [UIFont systemFontOfSize:15];
  138. nameLabel.text = @"房间名称";
  139. [bgView addSubview:nameLabel];
  140. [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  141. make.top.equalTo(@0);
  142. make.left.equalTo(@10);
  143. make.bottom.equalTo(@0);
  144. make.width.equalTo(@150);
  145. }];
  146. UIImageView *imgView = [[UIImageView alloc]init];
  147. imgView.image = [UIImage imageNamed:@"home_right_arrow"];
  148. [bgView addSubview:imgView];
  149. [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
  150. make.centerY.equalTo(@0);
  151. make.right.equalTo(@-30);
  152. make.width.equalTo(@9);
  153. make.height.equalTo(@15);
  154. }];
  155. UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 150, 50)];
  156. textLabel.textColor = [UIColor colorWithHexString:@"#999999"];
  157. textLabel.font = [UIFont systemFontOfSize:15];
  158. textLabel.textAlignment = NSTextAlignmentRight;
  159. [bgView addSubview:textLabel];
  160. [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  161. make.top.equalTo(@0);
  162. make.right.equalTo(imgView.mas_left).offset(-10);
  163. make.bottom.equalTo(@0);
  164. make.width.equalTo(@150);
  165. }];
  166. self.textLabel = textLabel;
  167. self.tableView.tableHeaderView = headerView;
  168. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(setRoomNameAction)];
  169. [bgView addGestureRecognizer:tap];
  170. }
  171. -(void)setRoomNameAction
  172. {
  173. RDS_WEAKSELF(weakSelf)
  174. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"房间名称" message:nil preferredStyle:UIAlertControllerStyleAlert];
  175. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"完成" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  176. UITextField *txt = [alertController.textFields objectAtIndex:0];
  177. NSLog(@"%@",txt.text);
  178. weakSelf.textLabel.text = txt.text;
  179. }];
  180. [alertController addAction:sureAction];
  181. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  182. }];
  183. [alertController addAction:cancelAction];
  184. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  185. textField.placeholder = @"请输入房间名称";
  186. textField.text = weakSelf.model.name;
  187. }];
  188. [self presentViewController:alertController animated:YES completion:nil];
  189. }
  190. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  191. {
  192. return 2;
  193. }
  194. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  195. {
  196. if (section == 0) {
  197. return self.inArray.count;
  198. }
  199. if (section == 1) {
  200. return self.outArray.count;
  201. }
  202. return 0;
  203. }
  204. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  205. {
  206. YXSetRoomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"setRoomCellID" forIndexPath:indexPath];
  207. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  208. if (indexPath.section == 0) {
  209. YXSetRoomDeviceModel *model = self.inArray[indexPath.row];
  210. if (model.icon.length > 0) {
  211. [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", TheApiHelper.baseUrlHeadImg, model.icon]]];
  212. }
  213. cell.titleLabel.text = model.name;
  214. }
  215. if (indexPath.section == 1) {
  216. YXSetRoomDeviceModel * model = self.outArray[indexPath.row];
  217. if (model.icon.length > 0) {
  218. [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", TheApiHelper.baseUrlHeadImg, model.icon]]];
  219. }
  220. cell.titleLabel.text = model.name;
  221. }
  222. return cell;
  223. }
  224. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  225. {
  226. }
  227. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  228. {
  229. UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 50)];
  230. UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 150, 50)];
  231. titleLabel.textColor = [UIColor colorWithHexString:@"#999999"];
  232. titleLabel.font = [UIFont systemFontOfSize:15];
  233. [headerView addSubview:titleLabel];
  234. if (section == 0) {
  235. titleLabel.text = @"房间设备";
  236. } else if (section == 1) {
  237. titleLabel.text = @"不在此房间设备";
  238. }
  239. return headerView;
  240. }
  241. -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  242. {
  243. UIView *footerView = [[UIView alloc]init];
  244. return footerView;
  245. }
  246. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  247. {
  248. return 50;
  249. }
  250. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  251. {
  252. return 0.01;
  253. }
  254. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  255. {
  256. return 65;
  257. }
  258. #pragma mark - edit
  259. -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  260. {
  261. return YES;
  262. }
  263. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  264. {
  265. if (indexPath.section == 0) {
  266. return UITableViewCellEditingStyleDelete;
  267. }
  268. return UITableViewCellEditingStyleInsert;
  269. }
  270. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  271. {
  272. if (editingStyle == UITableViewCellEditingStyleDelete && indexPath.section == 0) {
  273. YXSetRoomDeviceModel *model = self.inArray[indexPath.row];
  274. [self.inArray removeObjectAtIndex:indexPath.row];
  275. [self.outArray addObject:model];
  276. [self.deletArray addObject:model];
  277. [tableView reloadData];
  278. }
  279. if (editingStyle == UITableViewCellEditingStyleInsert && indexPath.section == 1) {
  280. YXSetRoomDeviceModel *model = self.outArray[indexPath.row];
  281. [self.outArray removeObjectAtIndex:indexPath.row];
  282. [self.inArray addObject:model];
  283. for (YXSetRoomDeviceModel *deletModel in self.deletArray) {
  284. if ([model.record_id isEqualToString:deletModel.record_id]) {
  285. [self.deletArray removeObject:model];
  286. }
  287. }
  288. [tableView reloadData];
  289. }
  290. }
  291. #pragma mark - move
  292. -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
  293. {
  294. if (indexPath.section == 0) {
  295. return YES;
  296. }
  297. return NO;
  298. }
  299. -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
  300. {
  301. if (sourceIndexPath.section == 0 && destinationIndexPath.section == 0) {
  302. [self.inArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
  303. [tableView exchangeSubviewAtIndex:sourceIndexPath.row withSubviewAtIndex:destinationIndexPath.row];
  304. }
  305. }
  306. /*
  307. #pragma mark - Navigation
  308. // In a storyboard-based application, you will often want to do a little preparation before navigation
  309. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  310. // Get the new view controller using [segue destinationViewController].
  311. // Pass the selected object to the new view controller.
  312. }
  313. */
  314. @end