RDSHomeVC.m 14 KB

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