// // YXSceneViewController.m // Temperature // // Created by TC on 2025/2/9. // #import "YXSceneViewController.h" #import "YXSceneTableViewCell.h" #import "YXHomeSceneModel.h" #import #import "YXMenuItemView.h" #import #import "RDSAddRoomView.h" #import "RDSControlWebVC.h" @interface YXSceneViewController () @property (weak, nonatomic) UIScrollView *menuScrollView; @property (weak, nonatomic) UITableView *tableView; @property (nonatomic, strong) RDSAddRoomView *addView; @property (strong, nonatomic) NSMutableArray *sceneArray; @property (weak, nonatomic) UIButton *familyBtn; @property (weak, nonatomic) UIButton *menuBtn; @property (weak, nonatomic) UIButton *addBtn; @property (nonatomic, copy) NSString *homeID; //1一键执行 0自动化 @property (nonatomic, assign) NSInteger selectedIndex; @end @implementation YXSceneViewController - (void)viewDidLoad { [super viewDidLoad]; self.fd_prefersNavigationBarHidden = YES; self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F5FA"]; self.sceneArray = [[NSMutableArray alloc]initWithCapacity:5]; self.selectedIndex = 0; [self createTableView]; self.homeID = TheDataManager.current_home_id; self.selectedIndex = 0; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.familyBtn setTitle:TheDataManager.current_home_name forState:UIControlStateNormal]; self.homeID = TheDataManager.current_home_id; if (self.selectedIndex == 0) { [self p_getSceneWithHomeId:self.homeID homepage:@0 type:@"automation"]; }else{ [self p_getSceneWithHomeId:self.homeID homepage:@0 type:@"oneKey"]; } } - (void)p_changeSelectWithHomeID:(NSString *)homeId { [RDSHudShower showWithStatus:@"切换中..."]; RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_setDefaultHome:homeId success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 0) { [RDSHudShower dismissHUD]; for (int i = 0; i < TheDataManager.homes.count; i++) { RDSHomeModel *model = TheDataManager.homes[i]; if ([model.record_id isEqualToString:homeId]) { TheDataManager.current_home_name = model.name; TheDataManager.current_home_index = i; TheDataManager.current_home_id = model.record_id; [weakSelf.familyBtn setTitle:model.name forState:UIControlStateNormal]; if (weakSelf.selectedIndex == 0) { [weakSelf p_getSceneWithHomeId:model.record_id homepage:@0 type:@"automation"]; }else{ [weakSelf p_getSceneWithHomeId:model.record_id homepage:@0 type:@"oneKey"]; } weakSelf.homeID = model.record_id; } } } else{ [RDSHudShower showErrorWithStatus:responseObject[@"message"]]; } } failure:^(NSError *error) { [RDSHudShower showBottomToast:@"连接服务器失败"]; }]; } - (void)p_getSceneWithHomeId:(NSString *)homeId homepage:(NSNumber *)page type:(NSString *)type{ [self.sceneArray removeAllObjects]; //联动类型 1、oneKey:一键执行 2、automation:自动化 RDS_WEAKSELF(weakSelf) [YXHomeSceneModel rds_getHomeScenesWithHomeId:homeId homepage:page type:type finished:^(NSArray * _Nonnull scenes, NSError * _Nonnull error) { [weakSelf.sceneArray addObjectsFromArray:scenes]; if (weakSelf.sceneArray.count == 0) { weakSelf.addView.hidden = NO; }else{ weakSelf.addView.hidden = YES; } [weakSelf.tableView reloadData]; }]; } -(void)p_putScenesWithID:(NSString *)recordId { RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_putScenes:recordId success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 0) { if (weakSelf.selectedIndex == 0) { [weakSelf p_getSceneWithHomeId:weakSelf.homeID homepage:@0 type:@"automation"]; }else{ [weakSelf p_getSceneWithHomeId:weakSelf.homeID homepage:@0 type:@"oneKey"]; } [RDSHudShower showBottomToast:@"操作成功"]; }else{ [RDSHudShower showBottomToast:responseObject[@"message"]]; } } failure:^(NSError *error) { [RDSHudShower showBottomToast:@"连接服务器失败"]; }]; } -(void)createTableView { UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped]; tableView.delegate = self; tableView.dataSource = self; tableView.separatorStyle = UITableViewCellSeparatorStyleNone; tableView.backgroundColor = [UIColor colorWithHexString:@"#F2F5FA"]; [tableView registerNib:[UINib nibWithNibName:@"YXSceneTableViewCell" bundle:nil] forCellReuseIdentifier:@"sceneCell"]; tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; tableView.insetsContentViewsToSafeArea = false; [self.view addSubview:tableView]; [tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@0); make.right.equalTo(@0); make.left.equalTo(@0); make.bottom.equalTo(@0); }]; self.tableView = tableView; [self createTableHeaderView]; } -(void)createTableHeaderView { UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 170)]; // headerView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; headerView.backgroundColor = [UIColor redColor]; self.tableView.tableHeaderView = headerView; UIImageView *bgImgView = [[UIImageView alloc]init]; bgImgView.image = [UIImage imageNamed:@"my_top_bg"]; [headerView addSubview:bgImgView]; [bgImgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.equalTo(@0); make.left.right.equalTo(@0); }]; UIButton *familyBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [familyBtn setTitle:TheDataManager.current_home_name forState:UIControlStateNormal]; familyBtn.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium]; [familyBtn setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:UIControlStateNormal]; [familyBtn addTarget:self action:@selector(familyBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [headerView addSubview:familyBtn]; [familyBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(@20); make.top.equalTo(@75); // make.width.equalTo(@150); make.height.equalTo(@30); }]; self.familyBtn = familyBtn; UIImageView *imgView = [[UIImageView alloc]init]; imgView.image = [UIImage imageNamed:@"home_top_arrow"]; imgView.contentMode = UIViewContentModeScaleAspectFit; [headerView addSubview:imgView]; [imgView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(familyBtn.mas_centerY); make.left.equalTo(familyBtn.mas_right).offset(5); make.width.equalTo(@20); make.height.equalTo(@20); }]; // UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom]; // [addBtn setImage:[UIImage imageNamed:@"home_top_add"] forState:UIControlStateNormal]; // [addBtn addTarget:self action:@selector(addBtnAction:) forControlEvents:UIControlEventTouchUpInside]; // [headerView addSubview:addBtn]; // [addBtn mas_makeConstraints:^(MASConstraintMaker *make) { // make.right.equalTo(@-15); // make.top.equalTo(@50); // make.width.equalTo(@30); // make.height.equalTo(@30); // }]; // self.addBtn = addBtn; // // UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom]; // [menuBtn setImage:[UIImage imageNamed:@"scene_menu"] forState:UIControlStateNormal]; // [menuBtn addTarget:self action:@selector(menuBtnAction:) forControlEvents:UIControlEventTouchUpInside]; // [headerView addSubview:menuBtn]; // [menuBtn mas_makeConstraints:^(MASConstraintMaker *make) { // make.right.equalTo(self.addBtn.mas_left).offset(-15); // make.top.equalTo(@50); // make.width.equalTo(@30); // make.height.equalTo(@30); // }]; // self.menuBtn = menuBtn; UIScrollView *menuScrollView = [[UIScrollView alloc]init]; menuScrollView.showsVerticalScrollIndicator = NO; menuScrollView.showsHorizontalScrollIndicator = NO; [headerView addSubview:menuScrollView]; [menuScrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(familyBtn.mas_bottom).offset(20); make.left.equalTo(@15); make.right.equalTo(@-15); make.height.equalTo(@30); }]; self.menuScrollView = menuScrollView; CGFloat itemWidth = 80; CGFloat itemSpace = 10; NSArray *titleArray = @[@"自动化",@"一键执行"]; for (int i = 0; i < titleArray.count; i++) { NSString *text = titleArray[i]; YXMenuItemView *menuItemView = [[YXMenuItemView alloc]init]; menuItemView.frame = CGRectMake(i*(itemWidth + itemSpace), 0, itemWidth, 30); menuItemView.textLabel.text = text; NSDictionary *dict = @{NSFontAttributeName: menuItemView.textLabel.font}; CGSize textSize = [text boundingRectWithSize:CGSizeMake(itemWidth, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size; CGFloat width = textSize.width == 0 ? 80 : textSize.width; [menuItemView.colorView mas_updateConstraints:^(MASConstraintMaker *make) { make.width.equalTo(@(width)); }]; if (i == 0) { menuItemView.colorView.hidden = NO; menuItemView.textLabel.textColor = [UIColor colorWithHexString:@"#333333"]; }else{ menuItemView.colorView.hidden = YES; menuItemView.textLabel.textColor = [UIColor colorWithHexString:@"#999999"]; } [self.menuScrollView addSubview:menuItemView]; menuItemView.tag = i; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(menuItemTapAction:)]; [menuItemView addGestureRecognizer:tap]; } } -(void)menuItemTapAction:(UITapGestureRecognizer *)tap{ NSInteger tag = tap.view.tag; self.selectedIndex = tag; for (int i = 0; i < self.menuScrollView.subviews.count; i++) { YXMenuItemView *menuItemView = self.menuScrollView.subviews[i]; if (i == tag) { menuItemView.colorView.hidden = NO; menuItemView.textLabel.textColor = [UIColor colorWithHexString:@"#333333"]; }else{ menuItemView.colorView.hidden = YES; menuItemView.textLabel.textColor = [UIColor colorWithHexString:@"#999999"]; } } //联动类型 1、oneKey:一键执行 2、automation:自动化 if (self.selectedIndex == 0) { [self p_getSceneWithHomeId:self.homeID homepage:@0 type:@"automation"]; }else{ [self p_getSceneWithHomeId:self.homeID homepage:@0 type:@"oneKey"]; } } #pragma mark - 菜单 - (void)familyBtnAction:(UIButton *)button { NSMutableArray *titleArray = [[NSMutableArray alloc]initWithCapacity:5]; for (int i = 0; i < TheDataManager.homes.count; i++) { RDSHomeModel *model = TheDataManager.homes[i]; [titleArray addObject:model.name]; } [YBPopupMenu showRelyOnView:button titles:titleArray icons:nil menuWidth:130 otherSettings:^(YBPopupMenu *popupMenu) { popupMenu.delegate = self; popupMenu.animationManager.duration = 0.25; popupMenu.textColor = [UIColor colorWithHexString:@"#1F1F1F"]; }]; } - (void)menuBtnAction:(UIButton *)button { } - (void)addBtnAction:(UIButton *)button { } #pragma mark - YPPopupMenuDelegate - (void)ybPopupMenu:(YBPopupMenu *)ybPopupMenu didSelectedAtIndex:(NSInteger)index { RDSHomeModel *model = TheDataManager.homes[index]; [self p_changeSelectWithHomeID:model.record_id]; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.sceneArray.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { YXSceneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sceneCell" forIndexPath:indexPath]; cell.selectionStyle = UITableViewCellSelectionStyleNone; YXHomeSceneModel *sceneModel = self.sceneArray[indexPath.section]; [cell.picImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", TheApiHelper.baseUrlHeadImg, sceneModel.icon]] placeholderImage:[UIImage imageNamed:@"scene_placeholder_image"]]; [cell.iconImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", TheApiHelper.baseUrlHeadImg, sceneModel.small_icon]]]; cell.titleLabel.text = sceneModel.name; BOOL isShow = YES; if (sceneModel.device_infos) { for (int i = 0; i < sceneModel.device_infos.count; i++) { YXHomeSceneDeviceModel *model = sceneModel.device_infos[i]; if ([model.online integerValue] == 0) { isShow = NO; } } }else{ isShow = NO; } if (isShow) { cell.iconBgView.hidden = NO; cell.iconBgWidthConstraint.constant = 40; cell.statusLabel.hidden = YES; cell.bgWidthConstraint.constant = sceneModel.device_infos.count * 20; cell.imgBgView.hidden = NO; for (int i = 0; i < sceneModel.device_infos.count; i++) { YXHomeSceneDeviceModel *model = sceneModel.device_infos[i]; UIImageView *imgView = [[UIImageView alloc]init]; [cell.imgBgView addSubview:imgView]; [imgView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(cell.imgBgView.mas_centerY); make.left.equalTo(@0); make.width.equalTo(@20); make.height.equalTo(@20); }]; [imgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseUrlHeadImg,model.device_icon]]]; } }else{ cell.statusLabel.hidden = NO; cell.iconBgView.hidden = YES; cell.iconBgWidthConstraint.constant = 0; cell.imgBgView.hidden = YES; cell.bgWidthConstraint.constant = 0; } //联动类型 1、oneKey:一键执行 2、automation:自动化 if (self.selectedIndex == 0) { cell.eventBtn.backgroundColor = [UIColor clearColor]; [cell.eventBtn setTitle:@"" forState:UIControlStateNormal]; if ([sceneModel.status integerValue] == 1) { [cell.eventBtn setBackgroundImage:[UIImage imageNamed:@"home_switch_on"] forState:UIControlStateNormal]; }else{ [cell.eventBtn setBackgroundImage:[UIImage imageNamed:@"home_switch_off"] forState:UIControlStateNormal]; } }else{ cell.eventBtn.backgroundColor = [UIColor colorWithHexString:@"#E9F1FF"]; [cell.eventBtn setTitle:@"执行" forState:UIControlStateNormal]; [cell.eventBtn setTitleColor:[UIColor colorWithHexString:@"#267AFF"] forState:UIControlStateNormal]; [cell.eventBtn setBackgroundImage:nil forState:UIControlStateNormal]; } cell.onPowerClick = ^{ [self p_putScenesWithID:sceneModel.record_id]; }; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { YXHomeSceneModel *sceneModel = self.sceneArray[indexPath.section]; RDSControlWebVC *webVc = [[RDSControlWebVC alloc]init]; webVc.requestURL = sceneModel.detail_url; webVc.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:webVc animated:YES]; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 0.01)]; return headerBgView; } -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 10)]; return footerView; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0.01; } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 10; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 80; } - (RDSAddRoomView *)addView{ if(_addView == nil){ _addView = [RDSAddRoomView rds_loadViewFromNib]; _addView.hidden = YES; _addView.titleLabel.text = @"暂未添加场景"; _addView.detailLabel.text = @"添加场景,开启智能生活吧"; [self.tableView addSubview:_addView]; [_addView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view); make.right.equalTo(self.view); make.height.equalTo(@380); make.top.equalTo(@200); }]; RDS_WEAKSELF(weakSelf) _addView.onAddClick = ^{ RDSControlWebVC *webVc = [[RDSControlWebVC alloc]init]; webVc.requestURL = [NSString stringWithFormat:@"%@scene/%@",kBaseUrlH5,TheDataManager.current_home_id]; webVc.hidesBottomBarWhenPushed = YES; [weakSelf.navigationController pushViewController:webVc animated:YES]; }; } return _addView; } /* #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