RDSHomeVC.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. //
  2. // RDSHomeVC.m
  3. // Temperature
  4. //
  5. // Created by RD on 2022/10/24.
  6. //
  7. #import "RDSHomeVC.h"
  8. #import "RDSHomeTopView.h"
  9. #import "RDSHomeSceneView.h"
  10. #import "RDSHomeRoomView.h"
  11. #import "RDSRoomCell.h"
  12. #import "RDSAddRoomView.h"
  13. #import "RDSSelectDeviceMainVC.h"
  14. #import "RDSAirControlVC.h"
  15. #import "RDSSubControlVC.h"
  16. #import <MJRefresh/MJRefresh.h>
  17. #import "RDSMyInfoVC.h"
  18. #import "RDSSetPwdVC.h"
  19. #import "RDSMapHelper.h"
  20. #import "UIImageView+webImage.h"
  21. #import "WeatherModel.h"
  22. #import "RDSMemberModel.h"
  23. #import "RDSRoomModel.h"
  24. #import "GCDTimer.h"// 轮询定时器
  25. static NSString * const RDSRoomCellID = @"RDSRoomCellID";
  26. @interface RDSHomeVC ()<UICollectionViewDelegate, UICollectionViewDataSource>
  27. @property (nonatomic, weak) UICollectionView *collectionView;
  28. @property (nonatomic, strong) UICollectionReusableView *footerView;
  29. @property (nonatomic, strong) UICollectionReusableView *headerView;
  30. @property (nonatomic, strong) RDSHomeTopView *topView;
  31. @property (nonatomic, strong) RDSHomeSceneView *sceneView;
  32. @property (nonatomic, strong) RDSAddRoomView *addView;
  33. @property (nonatomic, strong) GCDTimer *gcdTimer;// 定时轮询
  34. @end
  35. @implementation RDSHomeVC
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. // Do any additional setup after loading the view.
  39. // 隐藏导航栏
  40. self.fd_prefersNavigationBarHidden = YES;
  41. [self p_getCurrentUserInfo];
  42. // [self p_getHomeList];
  43. [self p_setUpTimer];// 初始化轮询
  44. [self p_setupCollectionView];
  45. [kNotificationCenter addObserver:self selector:@selector(p_refreshUserInfo)name:RDSUpdateUserInfoNotice object:nil];
  46. if(TheDataManager.isFirstLogin){
  47. RDSSetPwdVC *vc = [[RDSSetPwdVC alloc] init];
  48. vc.hidesBottomBarWhenPushed = YES;
  49. [self pushViewController:vc animated:YES];
  50. }
  51. }
  52. - (void)viewDidAppear:(BOOL)animated{
  53. [super viewDidAppear:animated];
  54. [_gcdTimer resumeTimer];// 开始轮询
  55. }
  56. - (void)viewDidDisappear:(BOOL)animated{
  57. [super viewDidDisappear:animated];
  58. [_gcdTimer pauseTimer];// 暂停轮询
  59. }
  60. - (void)p_getCurrentUserInfo{
  61. RDS_WEAKSELF(weakSelf)
  62. [RDSUserInfoModel rds_getCurrentUserInfoFinished:^(NSError *error) {
  63. if (!error) {
  64. [weakSelf p_refreshUserInfo];
  65. [weakSelf p_getHomeDetail];
  66. [weakSelf p_getWeather];
  67. [weakSelf p_getWelcomeMessage];
  68. }
  69. }];
  70. }
  71. //- (void)p_getHomeList {
  72. // [RDSHomeModel rds_getHomeListIsAdmin:NO finished:nil];
  73. //}
  74. - (void)p_getWeather{
  75. RDS_WEAKSELF(weakSelf)
  76. [WeatherModel rds_getWeatherFinished:^(WeatherModel * _Nonnull weather, NSError * _Nonnull error) {
  77. weakSelf.topView.model = weather;
  78. }];
  79. }
  80. - (void)p_getWelcomeMessage {
  81. RDS_WEAKSELF(weakSelf)
  82. [RDSSystemParameterModel rds_getWelcomeMessageFinished:^(RDSSystemParameterModel * _Nullable systemParameter, NSError * _Nullable error) {
  83. weakSelf.topView.systemParameter = systemParameter;
  84. }];
  85. }
  86. // 弃用
  87. - (void)p_getRoomList{
  88. RDS_WEAKSELF(weakSelf)
  89. [RDSRoomModel rds_getRoomListFinished:^(NSError * _Nonnull error) {
  90. weakSelf.addView.hidden = (BOOL)TheDataManager.rooms.count;
  91. [weakSelf.collectionView.mj_header endRefreshing];
  92. [weakSelf.collectionView reloadData];
  93. }];
  94. }
  95. - (void)p_getHomeDetail{
  96. RDS_WEAKSELF(weakSelf)
  97. [RDSHomeModel rds_getHomeDetailWithHomeId:TheDataManager.current_home_id finished:^(RDSHomeModel *home, NSError *error) {
  98. weakSelf.addView.hidden = (BOOL)TheDataManager.rooms.count;
  99. if(kNULLString(home.city)){
  100. [weakSelf p_userLocation];
  101. }
  102. [weakSelf.collectionView.mj_header endRefreshing];
  103. [weakSelf.collectionView reloadData];
  104. }];
  105. }
  106. - (void)p_userLocation{
  107. RDS_WEAKSELF(weakSelf)
  108. [TheMapHelper rds_startLocationSevice:^(RDSUserLocation *userLocation) {
  109. [weakSelf p_updateLocation];
  110. }];
  111. }
  112. - (void)p_updateLocation{
  113. RDS_WEAKSELF(weakSelf)
  114. if(kNULLString(TheMapHelper.userLocation.cityString)){
  115. return;
  116. }
  117. [RDSDemoApiHelper rds_updateHomeInfoWithHomeId:TheDataManager.current_home_id name:TheDataManager.current_home_name city:TheMapHelper.userLocation.cityString success:^(id responseObject) {
  118. if ([responseObject[@"code"] intValue] == 0) {
  119. DDLog(@"更新城市成功");
  120. [weakSelf p_getWeather];
  121. } else{
  122. DDLog(@"更新城市失败:%@",responseObject[@"message"]);
  123. }
  124. } failure:^(NSError *error) {
  125. }];
  126. }
  127. - (void)p_refreshUserInfo{
  128. RDS_WEAKSELF(weakSelf)
  129. weakSelf.topView.title = [NSString stringWithFormat:@"欢迎回来,%@!",TheDataManager.currentUser.user_name];
  130. if (!TheDataManager.currentUser.headImg) {
  131. [TheDataManager.currentUser rds_getHeadImgFinished:^(NSError *error) {
  132. [weakSelf.topView.headBtn setImage:TheDataManager.currentUser.headImg forState:UIControlStateNormal];
  133. }];
  134. }else{
  135. [weakSelf.topView.headBtn setImage:TheDataManager.currentUser.headImg forState:UIControlStateNormal];
  136. }
  137. }
  138. // 定时轮询
  139. - (void)p_setUpTimer{
  140. self.gcdTimer = [[GCDTimer alloc] init];
  141. RDS_WEAKSELF(weakSelf)
  142. [_gcdTimer scheduledTimerWithTimeInterval:3 afterTime:3 repeats:YES block:^{
  143. [weakSelf p_getHomeDetail];
  144. }];
  145. }
  146. #pragma mark - UI
  147. // 顶部视图
  148. - (void)p_setupTopView{
  149. // 375 217.5
  150. CGFloat viewH = 206;//kSCALE_WIDTH_6*222;
  151. RDSHomeTopView *view = [RDSHomeTopView rds_loadViewFromNib];
  152. view.frame = CGRectMake(0, 0, SCREEN_WIDTH, viewH);
  153. [self.footerView addSubview:view];
  154. [view mas_makeConstraints:^(MASConstraintMaker *make) {
  155. make.top.equalTo(self.footerView);
  156. make.height.equalTo(@(viewH));
  157. make.left.equalTo(self.footerView);
  158. make.right.equalTo(self.footerView);
  159. }];
  160. self.topView = view;
  161. RDS_WEAKSELF(weakSelf)
  162. view.onHeadClick = ^{
  163. RDSMyInfoVC *vc = [[RDSMyInfoVC alloc] init];
  164. vc.hidesBottomBarWhenPushed = YES;
  165. [weakSelf pushViewController:vc animated:YES];
  166. };
  167. }
  168. // 场景先不做
  169. - (void)p_setupSceneView{
  170. RDSHomeSceneView *scenesView = [[RDSHomeSceneView alloc] init];
  171. [self.footerView addSubview:scenesView];
  172. self.sceneView = scenesView;
  173. //scenesView.delegate = self;
  174. [scenesView mas_makeConstraints:^(MASConstraintMaker *make) {
  175. make.top.equalTo(self.topView.mas_bottom).offset(10);
  176. make.height.equalTo(@90);
  177. make.left.equalTo(self.footerView);//.offset(10);
  178. make.right.equalTo(self.footerView);//.offset(-10);
  179. }];
  180. NSArray *scenes = @[@1,@2,@3,@4,@5,@6];
  181. scenesView.scenes = scenes;
  182. }
  183. - (RDSAddRoomView *)addView{
  184. if(_addView == nil){
  185. _addView = [RDSAddRoomView rds_loadViewFromNib];
  186. _addView.hidden = YES;
  187. [self.collectionView addSubview:_addView];
  188. [_addView mas_makeConstraints:^(MASConstraintMaker *make) {
  189. make.left.equalTo(self.view);//.offset(20);
  190. make.right.equalTo(self.view);//.offset(-20);
  191. make.height.equalTo(@300);
  192. make.top.equalTo(@(SCREEN_HEIGHT*0.4));
  193. }];
  194. RDS_WEAKSELF(weakSelf)
  195. _addView.onAddClick = ^{
  196. RDSSelectDeviceMainVC *vc = [[RDSSelectDeviceMainVC alloc] init];
  197. vc.hidesBottomBarWhenPushed = YES;
  198. [weakSelf pushViewController:vc animated:YES];
  199. };
  200. }
  201. return _addView;
  202. }
  203. - (void)p_setupCollectionView {
  204. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  205. CGFloat viewW = SCREEN_WIDTH - 16 * 2;
  206. layout.itemSize = CGSizeMake(viewW, 80);
  207. layout.minimumLineSpacing = 8;
  208. layout.sectionInset = UIEdgeInsetsMake(0, 16, 16, 16);
  209. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  210. layout.sectionHeadersPinToVisibleBounds = YES;
  211. UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
  212. collectionView.showsHorizontalScrollIndicator = NO;
  213. collectionView.showsVerticalScrollIndicator = NO;
  214. collectionView.backgroundColor = RDSViewBgColor;
  215. [collectionView registerClass:[RDSRoomCell class] forCellWithReuseIdentifier:RDSRoomCellID];
  216. collectionView.delegate = self;
  217. collectionView.dataSource = self;
  218. collectionView.pagingEnabled = NO;
  219. [self.view addSubview:collectionView];
  220. self.collectionView = collectionView;
  221. [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  222. make.left.equalTo(self.view);//.offset(20);
  223. make.right.equalTo(self.view);//.offset(-20);
  224. make.bottom.equalTo(self.view.mas_bottom);
  225. if (@available(iOS 11.0, *)) {
  226. make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
  227. // make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
  228. } else {
  229. make.top.equalTo(self.view);
  230. }
  231. }];
  232. //注册行头
  233. // [self.collectView registerNib:[UINib nibWithNibName:@"NibName" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReuseIdentifier"];
  234. //或者
  235. [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
  236. [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];
  237. MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(p_getCurrentUserInfo)];
  238. header.automaticallyChangeAlpha = YES;
  239. header.lastUpdatedTimeLabel.hidden = YES;
  240. self.collectionView.mj_header = header;
  241. // UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(p_longPressMethod:)];
  242. // longPress.minimumPressDuration = 0.3f;
  243. // [collectionView addGestureRecognizer:longPress];
  244. }
  245. #pragma mark - UICollectionViewDelegate
  246. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  247. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  248. RDSRoomModel *roomModel = TheDataManager.rooms[indexPath.row];
  249. if(roomModel.isMasterController){
  250. RDSAirControlVC *vc = [[RDSAirControlVC alloc] init];
  251. vc.hidesBottomBarWhenPushed = YES;
  252. vc.roomModel = roomModel;
  253. [self pushViewController:vc animated:YES];
  254. }
  255. else{
  256. RDSSubControlVC *vc = [[RDSSubControlVC alloc] init];
  257. vc.hidesBottomBarWhenPushed = YES;
  258. vc.roomModel = roomModel;
  259. [self pushViewController:vc animated:YES];
  260. }
  261. }
  262. #pragma mark - UICollectionViewDataSource
  263. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  264. return 2;
  265. }
  266. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  267. if(section == 0){// 第一组用来做悬停顶部视图
  268. return 0;
  269. }
  270. return TheDataManager.rooms.count;
  271. }
  272. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  273. RDSRoomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RDSRoomCellID forIndexPath:indexPath];
  274. RDSRoomModel *model;
  275. if(TheDataManager.rooms.count > indexPath.row){
  276. model = TheDataManager.rooms[indexPath.row];
  277. }
  278. cell.model = model;
  279. cell.onPowerClick = ^(BOOL onOff) {
  280. // 开关
  281. if(model.is_master){
  282. [RDSDemoApiHelper rds_setPower:onOff control_number:model.control_number success:^(id responseObject) {
  283. if ([responseObject[@"code"] intValue] != 0) {
  284. [RDSHudShower showBottomToast:responseObject[@"message"]];
  285. }
  286. } failure:^(NSError *error) {
  287. [RDSHudShower showBottomToast:@"连接服务器失败"];
  288. }];
  289. }
  290. else{
  291. [RDSDemoApiHelper rds_setSubDevPower:onOff control_number:model.control_number success:^(id responseObject) {
  292. if ([responseObject[@"code"] intValue] != 0) {
  293. [RDSHudShower showBottomToast:responseObject[@"message"]];
  294. }
  295. } failure:^(NSError *error) {
  296. [RDSHudShower showBottomToast:@"连接服务器失败"];
  297. }];
  298. }
  299. };
  300. return cell;
  301. }
  302. // 不允许高亮
  303. - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
  304. RDSRoomCell *cell = (RDSRoomCell*)[self.collectionView cellForItemAtIndexPath:indexPath];
  305. cell.highlighted = NO;
  306. }
  307. // 设置header尺寸
  308. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  309. if(section == 0){
  310. return CGSizeZero;
  311. }
  312. return CGSizeMake(SCREEN_WIDTH, 50);
  313. }
  314. // 设置footer尺寸
  315. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
  316. if(section == 0){
  317. return CGSizeMake(SCREEN_WIDTH, 306-60-30);// -60是场景的高度,场景先不做
  318. }
  319. return CGSizeZero;
  320. }
  321. //sectionheader
  322. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  323. if (kind == UICollectionElementKindSectionFooter) {
  324. UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer" forIndexPath:indexPath];
  325. footerView.backgroundColor = UIColor.clearColor;
  326. if(self.topView == nil){
  327. self.footerView = footerView;
  328. [self p_setupTopView];
  329. //[self p_setupSceneView]; //先不做场景功能
  330. }
  331. return footerView;
  332. }
  333. UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];
  334. headView.backgroundColor = RDSViewBgColor;
  335. UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 100, 20)];
  336. title.text = @"我的房间";
  337. title.font = [UIFont boldSystemFontOfSize:18];
  338. [headView addSubview:title];
  339. return headView;
  340. }
  341. - (void)dealloc{
  342. DDLog(@"dealloc~~");
  343. [_gcdTimer stopTimer];// 取消轮询
  344. [kNotificationCenter removeObserver:self];
  345. }
  346. @end