RDSHomeVC.m 14 KB

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