YXDeviceViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. //
  2. // YXDeviceViewController.m
  3. // Temperature
  4. //
  5. // Created by z on 2025/1/17.
  6. //
  7. #import "YXDeviceViewController.h"
  8. #import "YXDeviceCollectionViewCell.h"
  9. #import "YXDeviceListHeaderView.h"
  10. #import "LJCollectionViewFlowLayout.h"
  11. #import "YXLeftTableViewCell.h"
  12. #import <SDWebImage/SDWebImage.h>
  13. #import "NSObject+Property.h"
  14. //#import "RDSResetDeviceVC.h"
  15. #import "YXStepViewController.h"
  16. #import "YXDeviceListModel.h"
  17. #import "RDSRootControl.h"
  18. @interface YXDeviceViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegateFlowLayout,
  19. UICollectionViewDataSource>
  20. @property (nonatomic, strong) UITableView *tableView;
  21. @property (nonatomic, strong) UICollectionView *collectionView;
  22. @property (nonatomic, strong) NSMutableArray *dataSource;
  23. @property (nonatomic, strong) LJCollectionViewFlowLayout *flowLayout;
  24. @end
  25. static float kLeftTableViewWidth = 90.f;
  26. static float kCollectionViewMargin = 3.f;
  27. @implementation YXDeviceViewController
  28. {
  29. NSInteger _selectIndex;
  30. BOOL _isScrollDown;
  31. }
  32. - (void)viewDidLoad
  33. {
  34. [super viewDidLoad];
  35. // Do any additional setup after loading the view.
  36. self.navigationItem.title = @"添加设备";
  37. self.view.backgroundColor = [UIColor whiteColor];
  38. _selectIndex = 0;
  39. _isScrollDown = YES;
  40. self.edgesForExtendedLayout = UIRectEdgeNone;
  41. self.extendedLayoutIncludesOpaqueBars = NO;
  42. self.tableView.contentInsetAdjustmentBehavior = NO;
  43. self.collectionView.contentInsetAdjustmentBehavior = NO;
  44. self.dataSource = [[NSMutableArray alloc]initWithCapacity:5];
  45. [self getDeviceList];
  46. [self.view addSubview:self.tableView];
  47. [self.view addSubview:self.collectionView];
  48. }
  49. -(void)getDeviceList
  50. {
  51. [RDSDemoApiHelper rds_getDeviceTypeListSuccess:^(id responseObject) {
  52. NSLog(@"responseObject is %@", responseObject);
  53. if ([responseObject[@"code"] intValue] == 9999) {
  54. [RDSRootControl shareControl].isLoginSuccess = NO;
  55. TheDataManager.token = @"";
  56. }
  57. if ([responseObject[@"code"] integerValue] == 0) {
  58. NSArray *data = responseObject[@"data"];
  59. for (int i = 0; i < data.count; i++) {
  60. NSDictionary *listDic = data[i];
  61. YXDeviceListModel *listModel = [YXDeviceListModel mj_objectWithKeyValues:listDic];
  62. NSArray *itemArray = listDic[@"children"];
  63. NSMutableArray *itemModelArray = [[NSMutableArray alloc]initWithCapacity:5];
  64. for (int j = 0; j < itemArray.count; j++) {
  65. NSDictionary *itemDic = itemArray[j];
  66. YXDeviceItemModel *itemModel = [YXDeviceItemModel mj_objectWithKeyValues:itemDic];
  67. NSArray *stepArray = itemDic[@"steps"];
  68. NSArray *stepModelArray = [YXDeviceStepModel mj_objectArrayWithKeyValuesArray:stepArray];
  69. itemModel.steps = stepModelArray;
  70. [itemModelArray addObject:itemModel];
  71. }
  72. listModel.children = itemModelArray;
  73. [self->_dataSource addObject:listModel];
  74. }
  75. [self.tableView reloadData];
  76. [self.collectionView reloadData];
  77. [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
  78. }
  79. } failure:^(NSError *error) {
  80. }];
  81. }
  82. #pragma mark - Getters
  83. - (UITableView *)tableView
  84. {
  85. if (!_tableView)
  86. {
  87. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kLeftTableViewWidth, SCREEN_HEIGHT)];
  88. _tableView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  89. _tableView.delegate = self;
  90. _tableView.dataSource = self;
  91. _tableView.tableFooterView = [UIView new];
  92. _tableView.rowHeight = 55;
  93. _tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  94. _tableView.showsVerticalScrollIndicator = NO;
  95. _tableView.separatorColor = [UIColor clearColor];
  96. [_tableView registerClass:[YXLeftTableViewCell class] forCellReuseIdentifier:@"leftTableViewCellID"];
  97. }
  98. return _tableView;
  99. }
  100. - (LJCollectionViewFlowLayout *)flowLayout
  101. {
  102. if (!_flowLayout)
  103. {
  104. _flowLayout = [[LJCollectionViewFlowLayout alloc] init];
  105. _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
  106. _flowLayout.minimumInteritemSpacing = 2;
  107. _flowLayout.minimumLineSpacing = 2;
  108. }
  109. return _flowLayout;
  110. }
  111. - (UICollectionView *)collectionView
  112. {
  113. if (!_collectionView)
  114. {
  115. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(kCollectionViewMargin + kLeftTableViewWidth, kCollectionViewMargin, SCREEN_WIDTH - kLeftTableViewWidth - 2 * kCollectionViewMargin, SCREEN_HEIGHT - 2 * kCollectionViewMargin) collectionViewLayout:self.flowLayout];
  116. _collectionView.delegate = self;
  117. _collectionView.dataSource = self;
  118. _collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  119. _collectionView.showsVerticalScrollIndicator = NO;
  120. _collectionView.showsHorizontalScrollIndicator = NO;
  121. [_collectionView setBackgroundColor:[UIColor clearColor]];
  122. //注册cell
  123. [_collectionView registerClass:[YXDeviceCollectionViewCell class] forCellWithReuseIdentifier:@"deviceCollectionViewCellID"];
  124. //注册分区头标题
  125. [_collectionView registerClass:[YXDeviceListHeaderView class]
  126. forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
  127. withReuseIdentifier:@"headerViewID"];
  128. }
  129. return _collectionView;
  130. }
  131. #pragma mark - UITableView DataSource Delegate
  132. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  133. {
  134. return self.dataSource.count;
  135. }
  136. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  137. {
  138. YXLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"leftTableViewCellID" forIndexPath:indexPath];
  139. YXDeviceListModel *model = self.dataSource[indexPath.row];
  140. cell.titleLabel.text = model.name;
  141. return cell;
  142. }
  143. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  144. {
  145. _selectIndex = indexPath.row;
  146. // 解决点击 TableView 后 CollectionView 的 Header 遮挡问题。
  147. [self scrollToTopOfSection:_selectIndex animated:YES];
  148. // [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:_selectIndex] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
  149. [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_selectIndex inSection:0]
  150. atScrollPosition:UITableViewScrollPositionTop animated:YES];
  151. }
  152. #pragma mark - 解决点击 TableView 后 CollectionView 的 Header 遮挡问题
  153. - (void)scrollToTopOfSection:(NSInteger)section animated:(BOOL)animated
  154. {
  155. CGRect headerRect = [self frameForHeaderForSection:section];
  156. CGPoint topOfHeader = CGPointMake(0, headerRect.origin.y - _collectionView.contentInset.top);
  157. [self.collectionView setContentOffset:topOfHeader animated:animated];
  158. }
  159. - (CGRect)frameForHeaderForSection:(NSInteger)section
  160. {
  161. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:section];
  162. UICollectionViewLayoutAttributes *attributes = [self.collectionView.collectionViewLayout layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
  163. return attributes.frame;
  164. }
  165. #pragma mark - UICollectionView DataSource Delegate
  166. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  167. {
  168. return self.dataSource.count;
  169. }
  170. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  171. {
  172. YXDeviceListModel *listModel = self.dataSource[section];
  173. return listModel.children.count;
  174. }
  175. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  176. {
  177. YXDeviceCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"deviceCollectionViewCellID" forIndexPath:indexPath];
  178. YXDeviceListModel *listModel = self.dataSource[indexPath.section];
  179. YXDeviceItemModel *itemModel = listModel.children[indexPath.row];
  180. NSString *urlStr = [NSString stringWithFormat:@"%@%@",kBaseUrlHeadImg,itemModel.icon];
  181. [cell.picImgView sd_setImageWithURL:[NSURL URLWithString:urlStr]];
  182. cell.textLabel.text = itemModel.name;
  183. return cell;
  184. }
  185. - (CGSize)collectionView:(UICollectionView *)collectionView
  186. layout:(UICollectionViewLayout *)collectionViewLayout
  187. sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  188. {
  189. return CGSizeMake((SCREEN_WIDTH - kLeftTableViewWidth - 4 * kCollectionViewMargin) / 3,
  190. (SCREEN_WIDTH - kLeftTableViewWidth - 4 * kCollectionViewMargin) / 3 + 30);
  191. }
  192. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
  193. viewForSupplementaryElementOfKind:(NSString *)kind
  194. atIndexPath:(NSIndexPath *)indexPath
  195. {
  196. NSString *reuseIdentifier;
  197. if ([kind isEqualToString:UICollectionElementKindSectionHeader])
  198. { // header
  199. reuseIdentifier = @"CollectionViewHeaderView";
  200. }
  201. YXDeviceListHeaderView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerViewID" forIndexPath:indexPath];
  202. if ([kind isEqualToString:UICollectionElementKindSectionHeader])
  203. {
  204. YXDeviceItemModel *model = self.dataSource[indexPath.section];
  205. view.titleLabel.text = model.name;
  206. }
  207. return view;
  208. }
  209. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  210. {
  211. return CGSizeMake(SCREEN_WIDTH, 30);
  212. }
  213. // CollectionView分区标题即将展示
  214. - (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
  215. {
  216. // 当前CollectionView滚动的方向向上,CollectionView是用户拖拽而产生滚动的(主要是判断CollectionView是用户拖拽而滚动的,还是点击TableView而滚动的)
  217. if (!_isScrollDown && (collectionView.dragging || collectionView.decelerating))
  218. {
  219. [self selectRowAtIndexPath:indexPath.section];
  220. }
  221. }
  222. // CollectionView分区标题展示结束
  223. - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(nonnull UICollectionReusableView *)view forElementOfKind:(nonnull NSString *)elementKind atIndexPath:(nonnull NSIndexPath *)indexPath
  224. {
  225. // 当前CollectionView滚动的方向向下,CollectionView是用户拖拽而产生滚动的(主要是判断CollectionView是用户拖拽而滚动的,还是点击TableView而滚动的)
  226. if (_isScrollDown && (collectionView.dragging || collectionView.decelerating))
  227. {
  228. [self selectRowAtIndexPath:indexPath.section + 1];
  229. }
  230. }
  231. // 当拖动CollectionView的时候,处理TableView
  232. - (void)selectRowAtIndexPath:(NSInteger)index
  233. {
  234. [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
  235. }
  236. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  237. {
  238. YXDeviceListModel *listModel = self.dataSource[indexPath.section];
  239. YXDeviceItemModel *itemModel = listModel.children[indexPath.row];
  240. YXStepViewController *stepVC = [[YXStepViewController alloc] init];
  241. stepVC.itemModel = itemModel;
  242. [self pushViewController:stepVC animated:YES];
  243. // RDSResetDeviceVC *resetDeviceVC = [[RDSResetDeviceVC alloc] init];
  244. // resetDeviceVC.itemModel = itemModel;
  245. // [self pushViewController:resetDeviceVC animated:YES];
  246. }
  247. #pragma mark - UIScrollView Delegate
  248. // 标记一下CollectionView的滚动方向,是向上还是向下
  249. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  250. {
  251. static float lastOffsetY = 0;
  252. if (self.collectionView == scrollView)
  253. {
  254. _isScrollDown = lastOffsetY < scrollView.contentOffset.y;
  255. lastOffsetY = scrollView.contentOffset.y;
  256. }
  257. }
  258. - (void)didReceiveMemoryWarning
  259. {
  260. [super didReceiveMemoryWarning];
  261. // Dispose of any resources that can be recreated.
  262. }
  263. /*
  264. #pragma mark - Navigation
  265. // In a storyboard-based application, you will often want to do a little preparation before navigation
  266. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  267. // Get the new view controller using [segue destinationViewController].
  268. // Pass the selected object to the new view controller.
  269. }
  270. */
  271. @end