YXSceneViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. //
  2. // YXSceneViewController.m
  3. // Temperature
  4. //
  5. // Created by TC on 2025/2/9.
  6. //
  7. #import "YXSceneViewController.h"
  8. #import "YXSceneTableViewCell.h"
  9. #import "YXHomeSceneModel.h"
  10. #import <YBPopupMenu/YBPopupMenu.h>
  11. #import "YXMenuItemView.h"
  12. #import <SDWebImage/SDWebImage.h>
  13. #import "RDSAddRoomView.h"
  14. #import "RDSControlWebVC.h"
  15. @interface YXSceneViewController ()<UITableViewDataSource,UITableViewDelegate,YBPopupMenuDelegate>
  16. @property (weak, nonatomic) UIScrollView *menuScrollView;
  17. @property (weak, nonatomic) UITableView *tableView;
  18. @property (nonatomic, strong) RDSAddRoomView *addView;
  19. @property (strong, nonatomic) NSMutableArray *sceneArray;
  20. @property (weak, nonatomic) UIButton *familyBtn;
  21. @property (weak, nonatomic) UIButton *menuBtn;
  22. @property (weak, nonatomic) UIButton *addBtn;
  23. @property (nonatomic, copy) NSString *homeID;
  24. //1一键执行 0自动化
  25. @property (nonatomic, assign) NSInteger selectedIndex;
  26. @end
  27. @implementation YXSceneViewController
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. self.fd_prefersNavigationBarHidden = YES;
  31. self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F5FA"];
  32. self.sceneArray = [[NSMutableArray alloc]initWithCapacity:5];
  33. self.selectedIndex = 0;
  34. [self createTableView];
  35. self.homeID = TheDataManager.current_home_id;
  36. self.selectedIndex = 0;
  37. }
  38. -(void)viewWillAppear:(BOOL)animated
  39. {
  40. [super viewWillAppear:animated];
  41. [self.familyBtn setTitle:TheDataManager.current_home_name forState:UIControlStateNormal];
  42. self.homeID = TheDataManager.current_home_id;
  43. if (self.selectedIndex == 0) {
  44. [self p_getSceneWithHomeId:self.homeID homepage:@0 type:@"automation"];
  45. }else{
  46. [self p_getSceneWithHomeId:self.homeID homepage:@0 type:@"oneKey"];
  47. }
  48. }
  49. - (void)p_changeSelectWithHomeID:(NSString *)homeId
  50. {
  51. [RDSHudShower showWithStatus:@"切换中..."];
  52. RDS_WEAKSELF(weakSelf)
  53. [RDSDemoApiHelper rds_setDefaultHome:homeId success:^(id responseObject) {
  54. if ([responseObject[@"code"] intValue] == 0) {
  55. [RDSHudShower dismissHUD];
  56. for (int i = 0; i < TheDataManager.homes.count; i++) {
  57. RDSHomeModel *model = TheDataManager.homes[i];
  58. if ([model.record_id isEqualToString:homeId]) {
  59. TheDataManager.current_home_name = model.name;
  60. TheDataManager.current_home_index = i;
  61. TheDataManager.current_home_id = model.record_id;
  62. [weakSelf.familyBtn setTitle:model.name forState:UIControlStateNormal];
  63. if (weakSelf.selectedIndex == 0) {
  64. [weakSelf p_getSceneWithHomeId:model.record_id homepage:@0 type:@"automation"];
  65. }else{
  66. [weakSelf p_getSceneWithHomeId:model.record_id homepage:@0 type:@"oneKey"];
  67. }
  68. weakSelf.homeID = model.record_id;
  69. }
  70. }
  71. } else{
  72. [RDSHudShower showErrorWithStatus:responseObject[@"message"]];
  73. }
  74. } failure:^(NSError *error) {
  75. [RDSHudShower showBottomToast:@"连接服务器失败"];
  76. }];
  77. }
  78. - (void)p_getSceneWithHomeId:(NSString *)homeId homepage:(NSNumber *)page type:(NSString *)type{
  79. [self.sceneArray removeAllObjects];
  80. //联动类型 1、oneKey:一键执行 2、automation:自动化
  81. RDS_WEAKSELF(weakSelf)
  82. [YXHomeSceneModel rds_getHomeScenesWithHomeId:homeId homepage:page type:type finished:^(NSArray * _Nonnull scenes, NSError * _Nonnull error) {
  83. [weakSelf.sceneArray addObjectsFromArray:scenes];
  84. if (weakSelf.sceneArray.count == 0) {
  85. weakSelf.addView.hidden = NO;
  86. }else{
  87. weakSelf.addView.hidden = YES;
  88. }
  89. [weakSelf.tableView reloadData];
  90. }];
  91. }
  92. -(void)p_putScenesWithID:(NSString *)recordId
  93. {
  94. RDS_WEAKSELF(weakSelf)
  95. [RDSDemoApiHelper rds_putScenes:recordId success:^(id responseObject) {
  96. if ([responseObject[@"code"] intValue] == 0) {
  97. if (weakSelf.selectedIndex == 0) {
  98. [weakSelf p_getSceneWithHomeId:weakSelf.homeID homepage:@0 type:@"automation"];
  99. }else{
  100. [weakSelf p_getSceneWithHomeId:weakSelf.homeID homepage:@0 type:@"oneKey"];
  101. }
  102. [RDSHudShower showBottomToast:@"操作成功"];
  103. }else{
  104. [RDSHudShower showBottomToast:responseObject[@"message"]];
  105. }
  106. } failure:^(NSError *error) {
  107. [RDSHudShower showBottomToast:@"连接服务器失败"];
  108. }];
  109. }
  110. -(void)createTableView
  111. {
  112. UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
  113. tableView.delegate = self;
  114. tableView.dataSource = self;
  115. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  116. tableView.backgroundColor = [UIColor colorWithHexString:@"#F2F5FA"];
  117. [tableView registerNib:[UINib nibWithNibName:@"YXSceneTableViewCell" bundle:nil] forCellReuseIdentifier:@"sceneCell"];
  118. tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  119. tableView.insetsContentViewsToSafeArea = false;
  120. [self.view addSubview:tableView];
  121. [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  122. make.top.equalTo(@0);
  123. make.right.equalTo(@0);
  124. make.left.equalTo(@0);
  125. make.bottom.equalTo(@0);
  126. }];
  127. self.tableView = tableView;
  128. [self createTableHeaderView];
  129. }
  130. -(void)createTableHeaderView
  131. {
  132. UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 170)];
  133. // headerView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  134. headerView.backgroundColor = [UIColor redColor];
  135. self.tableView.tableHeaderView = headerView;
  136. UIImageView *bgImgView = [[UIImageView alloc]init];
  137. bgImgView.image = [UIImage imageNamed:@"my_top_bg"];
  138. [headerView addSubview:bgImgView];
  139. [bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  140. make.top.bottom.equalTo(@0);
  141. make.left.right.equalTo(@0);
  142. }];
  143. UIButton *familyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  144. [familyBtn setTitle:TheDataManager.current_home_name forState:UIControlStateNormal];
  145. familyBtn.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
  146. [familyBtn setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:UIControlStateNormal];
  147. [familyBtn addTarget:self action:@selector(familyBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  148. [headerView addSubview:familyBtn];
  149. [familyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  150. make.left.equalTo(@20);
  151. make.top.equalTo(@75);
  152. // make.width.equalTo(@150);
  153. make.height.equalTo(@30);
  154. }];
  155. self.familyBtn = familyBtn;
  156. UIImageView *imgView = [[UIImageView alloc]init];
  157. imgView.image = [UIImage imageNamed:@"home_top_arrow"];
  158. imgView.contentMode = UIViewContentModeScaleAspectFit;
  159. [headerView addSubview:imgView];
  160. [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
  161. make.centerY.equalTo(familyBtn.mas_centerY);
  162. make.left.equalTo(familyBtn.mas_right).offset(5);
  163. make.width.equalTo(@20);
  164. make.height.equalTo(@20);
  165. }];
  166. // UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  167. // [addBtn setImage:[UIImage imageNamed:@"home_top_add"] forState:UIControlStateNormal];
  168. // [addBtn addTarget:self action:@selector(addBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  169. // [headerView addSubview:addBtn];
  170. // [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  171. // make.right.equalTo(@-15);
  172. // make.top.equalTo(@50);
  173. // make.width.equalTo(@30);
  174. // make.height.equalTo(@30);
  175. // }];
  176. // self.addBtn = addBtn;
  177. //
  178. // UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  179. // [menuBtn setImage:[UIImage imageNamed:@"scene_menu"] forState:UIControlStateNormal];
  180. // [menuBtn addTarget:self action:@selector(menuBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  181. // [headerView addSubview:menuBtn];
  182. // [menuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  183. // make.right.equalTo(self.addBtn.mas_left).offset(-15);
  184. // make.top.equalTo(@50);
  185. // make.width.equalTo(@30);
  186. // make.height.equalTo(@30);
  187. // }];
  188. // self.menuBtn = menuBtn;
  189. UIScrollView *menuScrollView = [[UIScrollView alloc]init];
  190. menuScrollView.showsVerticalScrollIndicator = NO;
  191. menuScrollView.showsHorizontalScrollIndicator = NO;
  192. [headerView addSubview:menuScrollView];
  193. [menuScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
  194. make.top.equalTo(familyBtn.mas_bottom).offset(20);
  195. make.left.equalTo(@15);
  196. make.right.equalTo(@-15);
  197. make.height.equalTo(@30);
  198. }];
  199. self.menuScrollView = menuScrollView;
  200. CGFloat itemWidth = 80;
  201. CGFloat itemSpace = 10;
  202. NSArray *titleArray = @[@"自动化",@"一键执行"];
  203. for (int i = 0; i < titleArray.count; i++) {
  204. NSString *text = titleArray[i];
  205. YXMenuItemView *menuItemView = [[YXMenuItemView alloc]init];
  206. menuItemView.frame = CGRectMake(i*(itemWidth + itemSpace), 0, itemWidth, 30);
  207. menuItemView.textLabel.text = text;
  208. NSDictionary *dict = @{NSFontAttributeName: menuItemView.textLabel.font};
  209. CGSize textSize = [text boundingRectWithSize:CGSizeMake(itemWidth, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
  210. CGFloat width = textSize.width == 0 ? 80 : textSize.width;
  211. [menuItemView.colorView mas_updateConstraints:^(MASConstraintMaker *make) {
  212. make.width.equalTo(@(width));
  213. }];
  214. if (i == 0) {
  215. menuItemView.colorView.hidden = NO;
  216. menuItemView.textLabel.textColor = [UIColor colorWithHexString:@"#333333"];
  217. }else{
  218. menuItemView.colorView.hidden = YES;
  219. menuItemView.textLabel.textColor = [UIColor colorWithHexString:@"#999999"];
  220. }
  221. [self.menuScrollView addSubview:menuItemView];
  222. menuItemView.tag = i;
  223. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(menuItemTapAction:)];
  224. [menuItemView addGestureRecognizer:tap];
  225. }
  226. }
  227. -(void)menuItemTapAction:(UITapGestureRecognizer *)tap{
  228. NSInteger tag = tap.view.tag;
  229. self.selectedIndex = tag;
  230. for (int i = 0; i < self.menuScrollView.subviews.count; i++) {
  231. YXMenuItemView *menuItemView = self.menuScrollView.subviews[i];
  232. if (i == tag) {
  233. menuItemView.colorView.hidden = NO;
  234. menuItemView.textLabel.textColor = [UIColor colorWithHexString:@"#333333"];
  235. }else{
  236. menuItemView.colorView.hidden = YES;
  237. menuItemView.textLabel.textColor = [UIColor colorWithHexString:@"#999999"];
  238. }
  239. }
  240. //联动类型 1、oneKey:一键执行 2、automation:自动化
  241. if (self.selectedIndex == 0) {
  242. [self p_getSceneWithHomeId:self.homeID homepage:@0 type:@"automation"];
  243. }else{
  244. [self p_getSceneWithHomeId:self.homeID homepage:@0 type:@"oneKey"];
  245. }
  246. }
  247. #pragma mark - 菜单
  248. - (void)familyBtnAction:(UIButton *)button {
  249. NSMutableArray *titleArray = [[NSMutableArray alloc]initWithCapacity:5];
  250. for (int i = 0; i < TheDataManager.homes.count; i++) {
  251. RDSHomeModel *model = TheDataManager.homes[i];
  252. [titleArray addObject:model.name];
  253. }
  254. [YBPopupMenu showRelyOnView:button titles:titleArray icons:nil menuWidth:130 otherSettings:^(YBPopupMenu *popupMenu) {
  255. popupMenu.delegate = self;
  256. popupMenu.animationManager.duration = 0.25;
  257. popupMenu.textColor = [UIColor colorWithHexString:@"#1F1F1F"];
  258. }];
  259. }
  260. - (void)menuBtnAction:(UIButton *)button {
  261. }
  262. - (void)addBtnAction:(UIButton *)button {
  263. }
  264. #pragma mark - YPPopupMenuDelegate
  265. - (void)ybPopupMenu:(YBPopupMenu *)ybPopupMenu didSelectedAtIndex:(NSInteger)index
  266. {
  267. RDSHomeModel *model = TheDataManager.homes[index];
  268. [self p_changeSelectWithHomeID:model.record_id];
  269. }
  270. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  271. {
  272. return self.sceneArray.count;
  273. }
  274. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  275. {
  276. return 1;
  277. }
  278. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  279. {
  280. YXSceneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sceneCell" forIndexPath:indexPath];
  281. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  282. YXHomeSceneModel *sceneModel = self.sceneArray[indexPath.section];
  283. [cell.picImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", TheApiHelper.baseUrlHeadImg, sceneModel.icon]] placeholderImage:[UIImage imageNamed:@"scene_placeholder_image"]];
  284. [cell.iconImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", TheApiHelper.baseUrlHeadImg, sceneModel.small_icon]]];
  285. cell.titleLabel.text = sceneModel.name;
  286. BOOL isShow = YES;
  287. if (sceneModel.device_infos) {
  288. for (int i = 0; i < sceneModel.device_infos.count; i++) {
  289. YXHomeSceneDeviceModel *model = sceneModel.device_infos[i];
  290. if ([model.online integerValue] == 0) {
  291. isShow = NO;
  292. }
  293. }
  294. }else{
  295. isShow = NO;
  296. }
  297. if (isShow) {
  298. cell.iconBgView.hidden = NO;
  299. cell.iconBgWidthConstraint.constant = 40;
  300. cell.statusLabel.hidden = YES;
  301. cell.bgWidthConstraint.constant = sceneModel.device_infos.count * 20;
  302. cell.imgBgView.hidden = NO;
  303. for (int i = 0; i < sceneModel.device_infos.count; i++) {
  304. YXHomeSceneDeviceModel *model = sceneModel.device_infos[i];
  305. UIImageView *imgView = [[UIImageView alloc]init];
  306. [cell.imgBgView addSubview:imgView];
  307. [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
  308. make.centerY.equalTo(cell.imgBgView.mas_centerY);
  309. make.left.equalTo(@0);
  310. make.width.equalTo(@20);
  311. make.height.equalTo(@20);
  312. }];
  313. [imgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseUrlHeadImg,model.device_icon]]];
  314. }
  315. }else{
  316. cell.statusLabel.hidden = NO;
  317. cell.iconBgView.hidden = YES;
  318. cell.iconBgWidthConstraint.constant = 0;
  319. cell.imgBgView.hidden = YES;
  320. cell.bgWidthConstraint.constant = 0;
  321. }
  322. //联动类型 1、oneKey:一键执行 2、automation:自动化
  323. if (self.selectedIndex == 0) {
  324. cell.eventBtn.backgroundColor = [UIColor clearColor];
  325. [cell.eventBtn setTitle:@"" forState:UIControlStateNormal];
  326. if ([sceneModel.status integerValue] == 1) {
  327. [cell.eventBtn setBackgroundImage:[UIImage imageNamed:@"home_switch_on"] forState:UIControlStateNormal];
  328. }else{
  329. [cell.eventBtn setBackgroundImage:[UIImage imageNamed:@"home_switch_off"] forState:UIControlStateNormal];
  330. }
  331. }else{
  332. cell.eventBtn.backgroundColor = [UIColor colorWithHexString:@"#E9F1FF"];
  333. [cell.eventBtn setTitle:@"执行" forState:UIControlStateNormal];
  334. [cell.eventBtn setTitleColor:[UIColor colorWithHexString:@"#267AFF"] forState:UIControlStateNormal];
  335. [cell.eventBtn setBackgroundImage:nil forState:UIControlStateNormal];
  336. }
  337. cell.onPowerClick = ^{
  338. [self p_putScenesWithID:sceneModel.record_id];
  339. };
  340. return cell;
  341. }
  342. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  343. {
  344. YXHomeSceneModel *sceneModel = self.sceneArray[indexPath.section];
  345. RDSControlWebVC *webVc = [[RDSControlWebVC alloc]init];
  346. webVc.requestURL = sceneModel.detail_url;
  347. webVc.hidesBottomBarWhenPushed = YES;
  348. [self.navigationController pushViewController:webVc animated:YES];
  349. }
  350. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  351. {
  352. UIView *headerBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 0.01)];
  353. return headerBgView;
  354. }
  355. -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  356. {
  357. UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 10)];
  358. return footerView;
  359. }
  360. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  361. {
  362. return 0.01;
  363. }
  364. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  365. {
  366. return 10;
  367. }
  368. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  369. {
  370. return 80;
  371. }
  372. - (RDSAddRoomView *)addView{
  373. if(_addView == nil){
  374. _addView = [RDSAddRoomView rds_loadViewFromNib];
  375. _addView.hidden = YES;
  376. _addView.titleLabel.text = @"暂未添加场景";
  377. _addView.detailLabel.text = @"添加场景,开启智能生活吧";
  378. [self.tableView addSubview:_addView];
  379. [_addView mas_makeConstraints:^(MASConstraintMaker *make) {
  380. make.left.equalTo(self.view);
  381. make.right.equalTo(self.view);
  382. make.height.equalTo(@380);
  383. make.top.equalTo(@200);
  384. }];
  385. RDS_WEAKSELF(weakSelf)
  386. _addView.onAddClick = ^{
  387. RDSControlWebVC *webVc = [[RDSControlWebVC alloc]init];
  388. webVc.requestURL = [NSString stringWithFormat:@"%@scene/%@",kBaseUrlH5,TheDataManager.current_home_id];
  389. webVc.hidesBottomBarWhenPushed = YES;
  390. [weakSelf.navigationController pushViewController:webVc animated:YES];
  391. };
  392. }
  393. return _addView;
  394. }
  395. /*
  396. #pragma mark - Navigation
  397. // In a storyboard-based application, you will often want to do a little preparation before navigation
  398. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  399. // Get the new view controller using [segue destinationViewController].
  400. // Pass the selected object to the new view controller.
  401. }
  402. */
  403. @end