YXRoomManagerViewController.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // YXRoomManagerViewController.m
  3. // Temperature
  4. //
  5. // Created by TC on 2025/2/6.
  6. //
  7. #import "YXRoomManagerViewController.h"
  8. #import "YXHomeRoomModel.h"
  9. #import "YXRoomManagerTableViewCell.h"
  10. #import "YXEditRoomViewController.h"
  11. #import "YXSetRoomViewController.h"
  12. #import "RDSRootControl.h"
  13. @interface YXRoomManagerViewController ()<UITableViewDataSource,UITableViewDelegate>
  14. @property (weak, nonatomic) UIView *contentView;
  15. @property (weak, nonatomic) UITableView *tableView;
  16. @property (strong, nonatomic) NSMutableArray *dataArray;
  17. @property (strong, nonatomic) UILabel *emptyLabel;
  18. @end
  19. @implementation YXRoomManagerViewController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.view.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  23. self.navigationItem.title = @"房间管理";
  24. [self addRightItem];
  25. self.dataArray = [[NSMutableArray alloc]initWithCapacity:5];
  26. [self createSubViews];
  27. }
  28. -(void)viewWillAppear:(BOOL)animated
  29. {
  30. [super viewWillAppear:animated];
  31. [self rds_getRoomList];
  32. }
  33. - (void)rds_getRoomList
  34. {
  35. NSString *homeIdStr = self.homeID.length == 0 ? TheDataManager.current_home_id : self.homeID;
  36. RDS_WEAKSELF(weakSelf)
  37. [RDSDemoApiHelper rds_getRoomListWithHomeId:homeIdStr success:^(id responseObject) {
  38. if ([responseObject[@"code"] intValue] == 9999) {
  39. [RDSRootControl shareControl].isLoginSuccess = NO;
  40. TheDataManager.token = @"";
  41. }
  42. if ([responseObject[@"code"] intValue] == 0) {
  43. NSArray *data = responseObject[@"data"];
  44. NSMutableArray *rooms = [YXHomeRoomModel mj_objectArrayWithKeyValuesArray:data];
  45. for (YXHomeRoomModel *model in rooms) {
  46. model.devices = [YXHomeDeviceModel mj_objectArrayWithKeyValuesArray:model.devices];
  47. }
  48. weakSelf.dataArray = rooms;
  49. TheDataManager.rooms = rooms.mutableCopy;
  50. [weakSelf.tableView reloadData];
  51. if (weakSelf.dataArray.count == 0) {
  52. weakSelf.emptyLabel.hidden = NO;
  53. }else{
  54. weakSelf.emptyLabel.hidden = YES;
  55. }
  56. } else{
  57. [RDSHudShower showBottomToast:responseObject[@"message"]];
  58. }
  59. } failure:^(NSError *error) {
  60. }];
  61. }
  62. -(void)p_createRoomWithName:(NSString *)name
  63. {
  64. NSString *homeIdStr = self.homeID.length == 0 ? TheDataManager.current_home_id : self.homeID;
  65. RDS_WEAKSELF(weakSelf)
  66. [RDSDemoApiHelper rds_createRoom:name homeId:homeIdStr success:^(id responseObject) {
  67. if ([responseObject[@"code"] intValue] == 0) {
  68. [RDSHudShower showBottomToast:@"房间添加成功"];
  69. [weakSelf rds_getRoomList];
  70. // [weakSelf popViewControllerAnimated:YES];
  71. } else{
  72. [RDSHudShower showBottomToast:responseObject[@"message"]];
  73. }
  74. } failure:^(NSError *error) {
  75. }];
  76. }
  77. -(void)addRightItem
  78. {
  79. UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  80. [rightBtn setTitle:@"编辑" forState:UIControlStateNormal];
  81. [rightBtn addTarget:self action:@selector(rightBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  82. [rightBtn setTitleColor:[UIColor colorWithHexString:@"#267AFF"] forState:UIControlStateNormal];
  83. rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
  84. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
  85. self.navigationItem.rightBarButtonItem = rightItem;
  86. }
  87. -(void)rightBtnAction:(UIButton *)btn
  88. {
  89. YXEditRoomViewController *editVc = [[YXEditRoomViewController alloc]init];
  90. editVc.homeID = self.homeID;
  91. [self.navigationController pushViewController:editVc animated:YES];
  92. }
  93. -(UILabel *)emptyLabel
  94. {
  95. if (_emptyLabel == nil) {
  96. _emptyLabel = [[UILabel alloc]init];
  97. _emptyLabel.text = @"暂无数据";
  98. _emptyLabel.font = [UIFont systemFontOfSize:20];
  99. _emptyLabel.textColor = [UIColor colorWithHexString:@"333333"];
  100. _emptyLabel.textAlignment = NSTextAlignmentCenter;
  101. _emptyLabel.hidden = YES;
  102. [self.view addSubview:_emptyLabel];
  103. [_emptyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  104. make.width.equalTo(@200);
  105. make.centerX.equalTo(@0);
  106. make.height.equalTo(@50);
  107. make.centerY.equalTo(@-30);
  108. }];
  109. }
  110. return _emptyLabel;
  111. }
  112. -(void)createSubViews
  113. {
  114. UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10) style:UITableViewStyleGrouped];
  115. tableView.delegate = self;
  116. tableView.dataSource = self;
  117. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  118. [tableView registerNib:[UINib nibWithNibName:@"YXRoomManagerTableViewCell" bundle:nil] forCellReuseIdentifier:@"roomManagerCellID"];
  119. tableView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  120. [self.view addSubview:tableView];
  121. self.tableView = tableView;
  122. [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  123. make.top.equalTo(@10);
  124. make.right.equalTo(@0);
  125. make.left.equalTo(@0);
  126. make.bottom.equalTo(@(-kTabbarHeight));
  127. }];
  128. UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  129. [addBtn setTitle:@"+ 添加房间" forState:UIControlStateNormal];
  130. [addBtn addTarget:self action:@selector(addBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  131. [addBtn setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
  132. addBtn.titleLabel.font = [UIFont systemFontOfSize:16];
  133. addBtn.backgroundColor = [UIColor colorWithHexString:@"#267AFF"];
  134. [self.view addSubview:addBtn];
  135. [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  136. make.height.equalTo(@45);
  137. make.left.equalTo(@20);
  138. make.right.equalTo(@-20);
  139. make.bottom.equalTo(@(-kSafeBottomHeight));
  140. }];
  141. addBtn.layer.cornerRadius = 22.5;
  142. }
  143. -(void)addBtnAction:(UIButton *)btn
  144. {
  145. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"添加房间" message:nil preferredStyle:UIAlertControllerStyleAlert];
  146. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"完成" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  147. UITextField *txt = [alertController.textFields objectAtIndex:0];
  148. NSLog(@"%@",txt.text);
  149. if (txt.text.length > 0) {
  150. [self p_createRoomWithName:txt.text];
  151. }
  152. }];
  153. [alertController addAction:sureAction];
  154. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  155. }];
  156. [alertController addAction:cancelAction];
  157. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  158. textField.placeholder = @"请输入房间名称";
  159. }];
  160. [self presentViewController:alertController animated:YES completion:nil];
  161. }
  162. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  163. {
  164. return 1;
  165. }
  166. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  167. {
  168. return self.dataArray.count;
  169. }
  170. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  171. {
  172. YXRoomManagerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"roomManagerCellID" forIndexPath:indexPath];
  173. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  174. YXHomeRoomModel *model = self.dataArray[indexPath.row];
  175. cell.nameLabel.text = model.name;
  176. cell.numberLabel.text = [NSString stringWithFormat:@"%ld个设备",model.devices.count];
  177. cell.picImgView.image = [UIImage imageNamed:@"home_right_arrow"];
  178. return cell;
  179. }
  180. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  181. {
  182. YXHomeRoomModel *model = self.dataArray[indexPath.row];
  183. YXSetRoomViewController *roomVc = [[YXSetRoomViewController alloc]init];
  184. roomVc.model = model;
  185. [self.navigationController pushViewController:roomVc animated:YES];
  186. }
  187. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  188. {
  189. UIView *headerView = [[UIView alloc]init];
  190. return headerView;
  191. }
  192. -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  193. {
  194. UIView *footerView = [[UIView alloc]init];
  195. return footerView;
  196. }
  197. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  198. {
  199. return 0.01;
  200. }
  201. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  202. {
  203. return 0.01;
  204. }
  205. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  206. {
  207. return 65;
  208. }
  209. /*
  210. #pragma mark - Navigation
  211. // In a storyboard-based application, you will often want to do a little preparation before navigation
  212. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  213. // Get the new view controller using [segue destinationViewController].
  214. // Pass the selected object to the new view controller.
  215. }
  216. */
  217. @end