// // RDSHomeVC.m // Temperature // // Created by RD on 2022/10/24. // #import "RDSHomeVC.h" #import "RDSHomeTopView.h" #import "RDSHomeSceneView.h" #import "RDSHomeRoomView.h" #import "RDSRoomCell.h" #import "RDSAddRoomView.h" #import "RDSSelectDeviceMainVC.h" #import "RDSAirControlVC.h" #import "RDSSubControlVC.h" #import #import "RDSMyInfoVC.h" #import "RDSSetPwdVC.h" #import "RDSMapHelper.h" #import "UIImageView+webImage.h" #import "GCDTimer.h"// 轮询定时器 #import "YXMenuItemView.h" #import #import "YXDeviceViewController.h" #import "YXDeviceManagerViewController.h" #import "YXRoomManagerViewController.h" #import "YXInviteViewController.h" #import "OPQRCodeViewController.h" #import "YXMessageListViewController.h" #import "RDSControlWebVC.h" #import "YXAddressViewController.h" #import "RDSRootControl.h" #import "YXTopImageModel.h" #import "WeatherModel.h" #import "RDSRoomModel.h" #import "YXHomeSceneModel.h" static NSString * const RDSRoomCellID = @"RDSRoomCellID"; @interface RDSHomeVC () @property (nonatomic, weak) UICollectionView *collectionView; @property (nonatomic, strong) UICollectionReusableView *footerView; @property (nonatomic, strong) UICollectionReusableView *headerView; @property (nonatomic, weak) RDSHomeTopView *topView; @property (nonatomic, strong) UIView *menuView; @property (nonatomic, strong) UIScrollView *menuScrollView; @property (nonatomic, strong) RDSHomeSceneView *sceneView; @property (nonatomic, strong) RDSAddRoomView *addView; @property (nonatomic, strong) GCDTimer *gcdTimer;// 定时轮询 @property (nonatomic, strong) NSMutableArray *dataArray; @property (nonatomic, assign) NSInteger selectedIndex; @property (nonatomic, copy) NSArray *sceneArray; @property (nonatomic, assign) RoomCellStyle cellStyle; @end @implementation RDSHomeVC - (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F5FA"]; // 隐藏导航栏 self.fd_prefersNavigationBarHidden = YES; // [self setNeedsStatusBarAppearanceUpdate]; self.selectedIndex = 0; self.dataArray = [[NSMutableArray alloc]initWithCapacity:5]; [self p_setupCollectionView]; [kNotificationCenter addObserver:self selector:@selector(p_refreshUserInfo)name:RDSUpdateUserInfoNotice object:nil]; [kNotificationCenter addObserver:self selector:@selector(applicationDidEnterBackground)name:@"kApplicationDidEnterBackground" object:nil]; [kNotificationCenter addObserver:self selector:@selector(applicationWillEnterForeground)name:@"kApplicationWillEnterForeground" object:nil]; if(TheDataManager.isFirstLogin){ RDSSetPwdVC *vc = [[RDSSetPwdVC alloc] init]; // vc.hidesBottomBarWhenPushed = YES; vc.modalPresentationStyle = UIModalPresentationFullScreen; [self presentViewController:vc animated:YES completion:nil]; } // [self showReason]; } -(void)showReason { NSString *reason = [[NSUserDefaults standardUserDefaults] objectForKey:@"exceptionReason"]; NSString *trace = [[NSUserDefaults standardUserDefaults] objectForKey:@"exceptionStackTrace"]; if (reason.length == 0 && trace.length == 0) { return; } NSString *msgText = [NSString stringWithFormat:@"%@---%@",reason,trace]; UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"崩溃原因" message:msgText preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]; [alert addAction:okAction]; [self presentViewController:alert animated:YES completion:nil]; } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self p_setUpTimer];// 初始化轮询 [self p_getBgImage]; [self p_getCurrentUserInfo]; } - (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [_gcdTimer resumeTimer];// 开始轮询 } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; } - (void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:animated]; [_gcdTimer pauseTimer];// 暂停轮询 [_gcdTimer stopTimer];// 取消轮询 } -(void)applicationDidEnterBackground { [_gcdTimer pauseTimer];// 暂停轮询 } -(void)applicationWillEnterForeground { [_gcdTimer resumeTimer];// 开始轮询 } - (void)p_getCurrentUserInfo{ RDS_WEAKSELF(weakSelf) [RDSUserInfoModel rds_getCurrentUserInfoFinished:^(NSError *error) { if (!error) { [weakSelf p_refreshUserInfo]; [weakSelf p_getHomeList]; [weakSelf p_getHomeDetail]; [weakSelf p_getWeather]; [weakSelf p_getScene]; [weakSelf p_getUnreadList]; }else{ [RDSRootControl shareControl].isLoginSuccess = NO; TheDataManager.token = @""; } }]; } -(void)p_getUnreadList { RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_getUnreadMessageSuccess:^(id responseObject) { if ([responseObject[@"code"] intValue] == 0) { NSDictionary *dataDic = responseObject[@"data"]; NSDictionary *listDic = dataDic[@"list"]; if ([listDic isKindOfClass:[NSDictionary class]]) { NSNumber *num = listDic[@"unread_total"]; if ([num integerValue] > 0) { weakSelf.topView.redView.hidden = NO; }else{ weakSelf.topView.redView.hidden = YES; } } } else{ [RDSHudShower showBottomToast:responseObject[@"message"]]; } } failure:^(NSError *error) { }]; } - (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.topView.title = model.name; [weakSelf p_getHomeDetail]; [weakSelf p_getWeather]; [weakSelf p_getScene]; } } } else{ [RDSHudShower showErrorWithStatus:responseObject[@"message"]]; } } failure:^(NSError *error) { [RDSHudShower showBottomToast:@"连接服务器失败"]; }]; } - (void)p_getHomeList { [RDSHomeModel rds_getHomeListIsAdmin:NO finished:nil]; } - (void)p_getWeather{ RDS_WEAKSELF(weakSelf) [WeatherModel rds_getWeatherFinished:^(WeatherModel * _Nonnull weather, NSError * _Nonnull error) { weakSelf.topView.model = weather; }]; } - (void)p_getBgImage{ RDS_WEAKSELF(weakSelf) [YXTopImageModel rds_getImageWithFinished:^(YXTopImageModel * _Nonnull model, NSError * _Nonnull error) { [weakSelf.topView sendSubviewToBack:weakSelf.topView.topImageView]; [weakSelf.topView.topImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", TheApiHelper.baseUrlHeadImg, model.url]]]; }]; } - (void)p_getScene{ RDS_WEAKSELF(weakSelf) [YXHomeSceneModel rds_getHomeScenesWithHomeId:TheDataManager.current_home_id homepage:@1 type:@"" finished:^(NSArray * _Nonnull scenes, NSError * _Nonnull error) { weakSelf.topView.scenesArray = scenes; weakSelf.sceneArray = scenes; [weakSelf.collectionView reloadData]; }]; } //- (void)p_getWelcomeMessage { // RDS_WEAKSELF(weakSelf) // [RDSSystemParameterModel rds_getWelcomeMessageFinished:^(RDSSystemParameterModel * _Nullable systemParameter, NSError * _Nullable error) { // weakSelf.topView.systemParameter = systemParameter; // }]; //} //// 弃用 //- (void)p_getRoomList{ // RDS_WEAKSELF(weakSelf) // [RDSRoomModel rds_getRoomListFinished:^(NSError * _Nonnull error) { // // weakSelf.addView.hidden = (BOOL)TheDataManager.rooms.count; // // [weakSelf.collectionView.mj_header endRefreshing]; // [weakSelf.collectionView reloadData]; // }]; //} //获取设备 - (void)p_getHomeDetail{ RDS_WEAKSELF(weakSelf) [RDSHomeModel rds_getHomeDetailWithFinished:^(RDSHomeModel *home, NSError *error) { if(kNULLString(home.city)){ [weakSelf p_userLocation]; } //数据处理 [weakSelf.dataArray removeAllObjects]; for (YXHomeRoomModel *room in home.room) { NSArray *devicesArray = [[NSArray alloc]init]; if (room.devices != nil) { devicesArray = room.devices; } NSDictionary *roomDic = @{ @"title":room.name, @"devices":devicesArray }; [weakSelf.dataArray addObject:roomDic]; } if (weakSelf.dataArray.count > weakSelf.selectedIndex) { NSDictionary *dic = weakSelf.dataArray[weakSelf.selectedIndex]; NSArray *devicesArray = dic[@"devices"]; weakSelf.addView.hidden = devicesArray.count == 0 ? NO : YES; } [weakSelf setRoomItemView]; [weakSelf.collectionView.mj_header endRefreshing]; [weakSelf.collectionView reloadData]; }]; } - (void)p_userLocation{ RDS_WEAKSELF(weakSelf) [TheMapHelper rds_startLocationSevice:^(RDSUserLocation *userLocation){ // [weakSelf p_updateLocation]; }]; } //- (void)p_updateLocation{ // RDS_WEAKSELF(weakSelf) // if(kNULLString(TheMapHelper.userLocation.cityString)){ // return; // } // // [RDSDemoApiHelper rds_updateHomeInfoWithHomeId:TheDataManager.current_home_id name:TheDataManager.current_home_name city:TheMapHelper.userLocation.cityString success:^(id responseObject) { // if ([responseObject[@"code"] intValue] == 0) { // DDLog(@"更新城市成功"); // // [weakSelf p_getWeather]; // } else{ // DDLog(@"更新城市失败:%@",responseObject[@"message"]); // } // } failure:^(NSError *error) { // // }]; //} -(void)p_changeScene:(NSString *)sceneId { [RDSDemoApiHelper rds_putScenes:sceneId success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 0) { [RDSHudShower showBottomToast:@"设置成功"]; } else { [RDSHudShower showBottomToast:responseObject[@"message"]]; } } failure:^(NSError *error) { }]; } - (void)p_refreshUserInfo{ RDS_WEAKSELF(weakSelf) weakSelf.topView.title = TheDataManager.current_home_name; } // 定时轮询 - (void)p_setUpTimer{ if (self.gcdTimer == nil) { self.gcdTimer = [[GCDTimer alloc] init]; } RDS_WEAKSELF(weakSelf) [_gcdTimer scheduledTimerWithTimeInterval:3 afterTime:3 repeats:YES block:^{ [weakSelf p_getCurrentUserInfo]; }]; } #pragma mark - 右上角按钮 - (void)topRightAction:(UIButton *)button { [YBPopupMenu showRelyOnView:button titles:@[@"添加设备", @"创建场景",@"邀请家人"] icons:@[@"home_pop_add", @"home_pop_scene",@"home_pop_invitation"] menuWidth:130 otherSettings:^(YBPopupMenu *popupMenu) { popupMenu.delegate = self; popupMenu.animationManager.duration = 0.25; popupMenu.textColor = [UIColor colorWithHexString:@"#1F1F1F"]; popupMenu.tag = 1; }]; } #pragma mark - YPPopupMenuDelegate - (void)ybPopupMenu:(YBPopupMenu *)ybPopupMenu didSelectedAtIndex:(NSInteger)index { NSInteger tag = ybPopupMenu.tag; if (tag == 1) { if (index == 0) { YXDeviceViewController *deviceVc = [[YXDeviceViewController alloc]init]; deviceVc.hidesBottomBarWhenPushed = YES; [self pushViewController:deviceVc animated:YES]; }else if(index == 1){ RDSControlWebVC *webVc = [[RDSControlWebVC alloc]init]; webVc.requestURL = [NSString stringWithFormat:@"%@scene/%@",kBaseUrlH5,TheDataManager.current_home_id]; webVc.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:webVc animated:YES]; } // else if(index == 2){ // OPQRCodeViewController *vc = [[OPQRCodeViewController alloc] init]; // vc.delegate = self; // vc.topOffset = -kNavHeight; // vc.hidesBottomBarWhenPushed = YES; // [self pushViewController:vc animated:YES]; // } else if(index == 2){ YXInviteViewController *inviteVc = [[YXInviteViewController alloc]init]; inviteVc.homeName = TheDataManager.current_home_name; inviteVc.homeId = TheDataManager.current_home_id; inviteVc.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:inviteVc animated:YES]; }else{ } } if (tag == 2) { if (index == 0) { if (self.cellStyle == RoomCellStyleDefault) { self.cellStyle = RoomCellStyleList; }else{ self.cellStyle = RoomCellStyleDefault; } NSNumber *style = @(self.cellStyle); [[NSUserDefaults standardUserDefaults] setObject:style forKey:@"kHomeCellStyle"]; [self.collectionView reloadData]; }else if(index == 1){ YXDeviceManagerViewController *deviceVc = [[YXDeviceManagerViewController alloc]init]; deviceVc.hidesBottomBarWhenPushed = YES; [self pushViewController:deviceVc animated:YES]; }else if(index == 2){ YXRoomManagerViewController *roomVc = [[YXRoomManagerViewController alloc]init]; roomVc.hidesBottomBarWhenPushed = YES; [self pushViewController:roomVc animated:YES]; }else{ } } if (tag == 3) { if (index < TheDataManager.homes.count) { RDSHomeModel *home = TheDataManager.homes[index]; NSString *homeId = home.record_id; [self p_changeSelectWithHomeID:homeId]; } } } #pragma mark - 房间名称 -(void)setRoomItemView { [self.menuScrollView removeAllSubviews]; NSMutableArray *titleArray = [[NSMutableArray alloc]initWithCapacity:5]; for (int i = 0; i < self.dataArray.count; i++) { NSDictionary *dic = self.dataArray[i]; NSString *title = dic[@"title"]; [titleArray addObject:title]; } CGFloat itemSpace = 20; CGFloat allWidth = 0; for (int i = 0; i < titleArray.count; i++) { NSString *text = titleArray[i]; YXMenuItemView *menuItemView = [[YXMenuItemView alloc]init]; NSDictionary *dict = @{NSFontAttributeName: menuItemView.textLabel.font}; CGSize textSize = [text boundingRectWithSize:CGSizeMake(200, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size; CGFloat width = textSize.width+2; [menuItemView.colorView mas_updateConstraints:^(MASConstraintMaker *make) { make.width.equalTo(@(width)); }]; menuItemView.frame = CGRectMake(i*itemSpace + allWidth, 0, width, 30); allWidth = allWidth + width; menuItemView.textLabel.text = text; if (i == self.selectedIndex) { 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]; } self.menuScrollView.contentSize = CGSizeMake(titleArray.count * itemSpace + allWidth, 30); } -(void)menuItemTapAction:(UITapGestureRecognizer *)tap{ NSInteger tag = tap.view.tag; self.selectedIndex = tag; for (int i = 0; i < self.menuView.subviews.count; i++) { UIView *view = self.menuView.subviews[i]; if ([view isKindOfClass:[UIScrollView class]]) { UIScrollView *menuScrollView = (UIScrollView *)view; NSArray *views = menuScrollView.subviews; for (int i = 0; i < views.count; i++) { YXMenuItemView *menuItemView = views[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"]; } } } } if (self.selectedIndex < self.dataArray.count) { NSDictionary *roomDic = self.dataArray[self.selectedIndex]; NSArray *devicesArray = roomDic[@"devices"]; self.addView.hidden = devicesArray.count == 0 ? NO : YES; } [self.collectionView reloadData]; } #pragma mark - UI // 顶部视图 - (void)p_setupTopView{ if(self.topView == nil){ RDSHomeTopView *view = [RDSHomeTopView rds_loadViewFromNib]; // view.frame = CGRectMake(0, 0, SCREEN_WIDTH, viewH); [self.footerView addSubview:view]; [view mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.footerView); make.bottom.equalTo(self.footerView); make.left.equalTo(self.footerView); make.right.equalTo(self.footerView); }]; self.topView = view; RDS_WEAKSELF(weakSelf) view.headTitleClick = ^(UILabel * _Nonnull label) { NSMutableArray *nameArray = [[NSMutableArray alloc]initWithCapacity:5]; for (int i = 0; i < TheDataManager.homes.count; i++) { RDSHomeModel *model = TheDataManager.homes[i]; [nameArray addObject:model.name]; } if (nameArray.count == 0) { return; } [YBPopupMenu showRelyOnView:label titles:nameArray icons:nil menuWidth:130 otherSettings:^(YBPopupMenu *popupMenu) { popupMenu.delegate = self; popupMenu.animationManager.duration = 0.25; popupMenu.textColor = [UIColor colorWithHexString:@"#1F1F1F"]; popupMenu.tag = 3; }]; }; view.locationClick = ^{ YXAddressViewController *addressVc = [[YXAddressViewController alloc]init]; addressVc.hidesBottomBarWhenPushed = YES; [weakSelf.navigationController pushViewController:addressVc animated:YES]; addressVc.sureBtnClick = ^(NSString * _Nonnull province, NSString * _Nonnull city, NSString * _Nonnull area, NSString * _Nonnull address) { [weakSelf p_updateHomeInfoWithProvince:province city:city area:area address:address]; }; }; view.addBtnClick = ^(UIButton *btn) { [weakSelf topRightAction:btn]; }; view.messageClick = ^(UIButton *btn) { YXMessageListViewController *msgVc = [[YXMessageListViewController alloc]init]; msgVc.hidesBottomBarWhenPushed = YES; [weakSelf.navigationController pushViewController:msgVc animated:YES]; }; view.itemClick = ^(NSInteger tag) { if (tag < weakSelf.sceneArray.count) { YXHomeSceneModel *model = weakSelf.sceneArray[tag]; [weakSelf p_changeScene:model.record_id]; } }; } } -(void)p_updateHomeInfoWithProvince:(NSString *)province city:(NSString *)city area:(NSString *)area address:(NSString *)address { RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_updateHomeInfoWithHomeId:TheDataManager.current_home_id name:TheDataManager.current_home_name province:province city:city district:area address:address rooms:nil success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 0) { [weakSelf.collectionView reloadData]; } else{ [RDSHudShower showBottomToast:responseObject[@"message"]]; } } failure:^(NSError *error) { }]; } - (void)p_setupMenuView{ if (self.menuView == nil) { self.menuView = [[UIView alloc]init]; self.menuView.frame = CGRectMake(20, 0, SCREEN_WIDTH-40, 30); [self.headerView addSubview:self.menuView]; UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom]; menuBtn.frame = CGRectMake(self.menuView.frame.size.width - 25, 2.5, 25, 25); [menuBtn setImage:[UIImage imageNamed:@"home_menu"] forState:UIControlStateNormal]; [menuBtn addTarget:self action:@selector(menuBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [self.menuView addSubview:menuBtn]; UIScrollView *menuScrollView = [[UIScrollView alloc]init]; menuScrollView.frame = CGRectMake(0, 0, self.menuView.frame.size.width - 40, self.menuView.frame.size.height); menuScrollView.showsVerticalScrollIndicator = NO; menuScrollView.showsHorizontalScrollIndicator = NO; [self.menuView addSubview:menuScrollView]; self.menuScrollView = menuScrollView; } } -(void)menuBtnAction:(UIButton *)btn { NSArray *titleArray; if (self.cellStyle == RoomCellStyleDefault) { titleArray = @[@"列表显示", @"设备管理",@"房间管理"]; }else{ titleArray = @[@"宫格显示", @"设备管理",@"房间管理"]; } [YBPopupMenu showRelyOnView:btn titles:titleArray icons:@[@"home_pop_list", @"home_pop_device",@"home_pop_room"] menuWidth:130 otherSettings:^(YBPopupMenu *popupMenu) { popupMenu.delegate = self; popupMenu.animationManager.duration = 0.25; popupMenu.textColor = [UIColor colorWithHexString:@"#1F1F1F"]; popupMenu.tag = 2; }]; } - (RDSAddRoomView *)addView{ if(_addView == nil){ _addView = [RDSAddRoomView rds_loadViewFromNib]; _addView.hidden = YES; [self.collectionView addSubview:_addView]; [_addView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view); make.right.equalTo(self.view); make.height.equalTo(@380); make.top.equalTo(self.topView.mas_bottom).offset(40); }]; RDS_WEAKSELF(weakSelf) _addView.onAddClick = ^{ YXDeviceViewController *deviceVc = [[YXDeviceViewController alloc]init]; deviceVc.hidesBottomBarWhenPushed = YES; [weakSelf pushViewController:deviceVc animated:YES]; }; } return _addView; } - (void)p_setupCollectionView { NSNumber *style = [[NSUserDefaults standardUserDefaults] objectForKey:@"kHomeCellStyle"]; if (style) { self.cellStyle = [style intValue]; }else{ self.cellStyle = RoomCellStyleList; } if (self.collectionView == nil) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.minimumLineSpacing = 10; layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15); layout.scrollDirection = UICollectionViewScrollDirectionVertical; layout.sectionHeadersPinToVisibleBounds = YES; UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout]; collectionView.showsHorizontalScrollIndicator = NO; collectionView.showsVerticalScrollIndicator = NO; collectionView.backgroundColor = [UIColor colorWithHexString:@"#F2F5FA"]; [collectionView registerClass:[RDSRoomCell class] forCellWithReuseIdentifier:RDSRoomCellID]; collectionView.delegate = self; collectionView.dataSource = self; collectionView.pagingEnabled = NO; [self.view addSubview:collectionView]; self.collectionView = collectionView; [collectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(@0); make.right.equalTo(@0); make.bottom.equalTo(@0); make.top.equalTo(@0); }]; collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; //注册行头 [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"]; [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"]; MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(p_getCurrentUserInfo)]; header.automaticallyChangeAlpha = YES; header.lastUpdatedTimeLabel.hidden = YES; self.collectionView.mj_header = header; } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGSize size = CGSizeZero; if (self.cellStyle == RoomCellStyleDefault) { CGFloat width = SCREEN_WIDTH / 2 - 20; size = CGSizeMake(width, 125); }else{ size = CGSizeMake(self.view.bounds.size.width-30, 75); } return size; } #pragma mark - UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *dic = self.dataArray[self.selectedIndex]; NSArray *devicesArray = dic[@"devices"]; YXHomeDeviceModel *model; if(devicesArray.count > indexPath.row){ model = devicesArray[indexPath.row]; RDSControlWebVC *webVc = [[RDSControlWebVC alloc]init]; webVc.requestURL = model.detail_url; webVc.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:webVc animated:YES]; } } #pragma mark - UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 2; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if(section == 0){// 第一组用来做悬停顶部视图 return 0; } if (self.dataArray.count > self.selectedIndex) { NSDictionary *dic = self.dataArray[self.selectedIndex]; NSArray *devicesArray = dic[@"devices"]; return devicesArray.count; } return 0; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { RDSRoomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RDSRoomCellID forIndexPath:indexPath]; cell.style = self.cellStyle; NSDictionary *dic = self.dataArray[self.selectedIndex]; NSArray *devicesArray = dic[@"devices"]; YXHomeDeviceModel *model; if(devicesArray.count > indexPath.row){ model = devicesArray[indexPath.row]; } cell.model = model; cell.onPowerClick = ^(BOOL onOff) { int state = 0; if ([model.power intValue] == 0) { state = 1; } // 开关 if([model.is_master boolValue]){ [RDSDemoApiHelper rds_setPower:state control_number:model.device_code sub_code:@"" success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 0) { [RDSHudShower showBottomToast:@"操作成功"]; [self p_getHomeDetail]; }else{ [RDSHudShower showBottomToast:responseObject[@"message"]]; } } failure:^(NSError *error) { [RDSHudShower showBottomToast:@"连接服务器失败"]; }]; } else { [RDSDemoApiHelper rds_setPower:state control_number:model.device_code sub_code:model.sub_code success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 0) { [RDSHudShower showBottomToast:@"操作成功"]; }else{ [RDSHudShower showBottomToast:responseObject[@"message"]]; } } failure:^(NSError *error) { [RDSHudShower showBottomToast:@"连接服务器失败"]; }]; } }; return cell; } // 不允许高亮 - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{ RDSRoomCell *cell = (RDSRoomCell*)[self.collectionView cellForItemAtIndexPath:indexPath]; cell.highlighted = NO; } // 设置header尺寸 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { if(section == 0){ return CGSizeZero; } return CGSizeMake(SCREEN_WIDTH, 50); } // 设置footer尺寸 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{ if(section == 0){ if (self.sceneArray.count == 0) { return CGSizeMake(SCREEN_WIDTH, 215); } return CGSizeMake(SCREEN_WIDTH, 260); } return CGSizeZero; } //sectionheader - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if (kind == UICollectionElementKindSectionFooter) { UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer" forIndexPath:indexPath]; footerView.backgroundColor = UIColor.clearColor; self.footerView = footerView; [self p_setupTopView]; return footerView; } if (kind == UICollectionElementKindSectionHeader) { UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath]; self.headerView = headView; [self p_setupMenuView]; return headView; } return nil; } #pragma mark - OPQRCodeViewControllerDelegate // 扫码返回字符串 - (void)rdy_didFinishPickingMediaWithInfo:(NSString *)stringValue{ DDLog(@"二维码:%@",stringValue); if(!kNULLString(stringValue)){ } } - (void)dealloc{ DDLog(@"dealloc~~"); [kNotificationCenter removeObserver:self]; } @end