RDSHomeVC.m 15 KB

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