HomePageBodyView.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // HomePageBodyView.m
  3. // KulexiuForStudent
  4. //
  5. // Created by 王智 on 2022/8/29.
  6. //
  7. #import "HomePageBodyView.h"
  8. #import "HomeHotStyleCell.h"
  9. #import "HomeTeacherLiveModel.h"
  10. #import "TeacherStyleModel.h"
  11. #import "KSBaseWKWebViewController.h"
  12. #import "KSEnterLiveroomManager.h"
  13. #import "HomeTempLiveCell.h"
  14. #import "UserInfoManager.h"
  15. #import "TeacherStyleFlowLayout.h"
  16. @interface HomePageBodyView ()<UICollectionViewDelegate,UICollectionViewDataSource,WaterFlowLayoutDelegate>
  17. @property (nonatomic, strong) NSMutableArray *sourceArray;
  18. @property (nonatomic, assign) BOOL isFirstLoad;
  19. @end
  20. @implementation HomePageBodyView
  21. - (instancetype)initWithFrame:(CGRect)frame {
  22. self = [super initWithFrame:frame];
  23. if (self) {
  24. self.isFirstLoad = YES;
  25. self.backgroundColor = HexRGB(0xf6f8f9);
  26. TeacherStyleFlowLayout *layout = [[TeacherStyleFlowLayout alloc] init];
  27. layout.sectionInset = UIEdgeInsetsMake(0, 14, 12, 14);
  28. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  29. layout.delegate = self;
  30. self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:layout];
  31. self.collectionView.backgroundColor = HexRGB(0xf6f8f9);
  32. self.collectionView.delegate = self;
  33. self.collectionView.dataSource = self;
  34. self.collectionView.showsVerticalScrollIndicator = NO;
  35. self.collectionView.showsHorizontalScrollIndicator = NO;
  36. [self.collectionView registerNib:[UINib nibWithNibName:@"HomeHotStyleCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"HomeHotStyleCell"];
  37. [self.collectionView registerNib:[UINib nibWithNibName:@"HomeTempLiveCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"HomeTempLiveCell"];
  38. [self addSubview:self.collectionView];
  39. MJWeakSelf;
  40. self.collectionView.mj_footer = [KSGifRefreshFooter footerWithRefreshingBlock:^{
  41. if (weakSelf.isLoadMore) {
  42. weakSelf.pages += 1;
  43. [weakSelf requestData];
  44. }
  45. else {
  46. [weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
  47. }
  48. }];
  49. }
  50. return self;
  51. }
  52. - (void)configDefaultRefresh {
  53. self.isLoadMore = YES;
  54. self.pages = 1;
  55. self.rows = 10;
  56. [self.collectionView.mj_footer resetNoMoreData];
  57. }
  58. - (void)resetParamenter {
  59. [self configDefaultRefresh];
  60. self.styleArray = [NSMutableArray array];
  61. self.sourceArray = [NSMutableArray arrayWithArray:self.liveArray];
  62. [self.collectionView reloadData];
  63. }
  64. - (void)endRefresh {
  65. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  66. [self.collectionView.mj_footer endRefreshing];
  67. });
  68. }
  69. - (void)requestData {
  70. [KSNetworkingManager homeQueryTeacherStyle:KS_POST page:self.pages rows:self.rows version:[USER_MANAGER getCurrentVersion] success:^(NSDictionary * _Nonnull dic) {
  71. [self endRefresh];
  72. if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
  73. NSArray *sourceArray = [[dic ks_dictionaryValueForKey:@"data"] ks_arrayValueForKey:@"rows"];
  74. NSMutableArray *styleArray = [NSMutableArray array];
  75. for (NSDictionary *parm in sourceArray) {
  76. TeacherStyleModel *model = [[TeacherStyleModel alloc] initWithDictionary:parm];
  77. [styleArray addObject:model];
  78. }
  79. if (styleArray.count < self.rows) {
  80. self.isLoadMore = NO;
  81. }
  82. [self.sourceArray addObjectsFromArray:styleArray];
  83. }
  84. else {
  85. [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
  86. }
  87. [self.collectionView reloadData];
  88. // 回调
  89. } faliure:^(NSError * _Nonnull error) {
  90. [self endRefresh];
  91. }];
  92. }
  93. - (void)resetAndRefresh {
  94. [self configDefaultRefresh];
  95. self.sourceArray = [NSMutableArray arrayWithArray:self.liveArray];
  96. [self.sourceArray addObjectsFromArray:self.styleArray];
  97. [self.collectionView reloadData];
  98. }
  99. - (void)beginFirstRefresh {
  100. if (self.isFirstLoad) {
  101. self.isFirstLoad = NO;
  102. [self configDefaultRefresh];
  103. self.sourceArray = [NSMutableArray arrayWithArray:self.liveArray];
  104. [self.sourceArray addObjectsFromArray:self.styleArray];
  105. }
  106. [self.collectionView reloadData];
  107. }
  108. - (void)selectCellAtIndexPath:(NSIndexPath *)indexPath {
  109. if (self.lastSelectedIndexPath == indexPath) {
  110. return;
  111. }
  112. if (self.lastSelectedIndexPath != nil) {
  113. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:self.lastSelectedIndexPath];
  114. [cell setSelected:NO];
  115. }
  116. UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
  117. [cell setSelected:YES];
  118. self.lastSelectedIndexPath = indexPath;
  119. }
  120. - (void)layoutSubviews {
  121. [super layoutSubviews];
  122. self.collectionView.frame = self.bounds;
  123. }
  124. #pragma mark ----- collection view
  125. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  126. return 1;
  127. }
  128. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  129. return self.sourceArray.count;
  130. }
  131. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  132. id source = self.sourceArray[indexPath.item];
  133. if ([source isKindOfClass:[TeacherStyleModel class]]) {
  134. HomeHotStyleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeHotStyleCell" forIndexPath:indexPath];
  135. [cell configWithSource:source];
  136. return cell;
  137. }
  138. else {
  139. HomeTempLiveCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeTempLiveCell" forIndexPath:indexPath];
  140. [cell configWithSource:source];
  141. return cell;
  142. }
  143. }
  144. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  145. id source = self.sourceArray[indexPath.item];
  146. if ([source isKindOfClass:[HomeTeacherLiveModel class]]) { // 进入直播间
  147. HomeTeacherLiveModel *model = (HomeTeacherLiveModel *)source;
  148. [self joinLiveRoom:model.roomUid];
  149. }
  150. else if ([source isKindOfClass:[TeacherStyleModel class]]) {
  151. TeacherStyleModel *model = (TeacherStyleModel *)source;
  152. [self displayTeacherStyle:model.userId];
  153. }
  154. }
  155. - (void)joinLiveRoom:(NSString *)liveRoomId {
  156. if ([NSString isEmptyString:liveRoomId]) {
  157. return;
  158. }
  159. [LOADING_MANAGER showCustomLoading:@"加载中..."];
  160. [KSEnterLiveroomManager joinLiveWithRoomId:liveRoomId inController:(CustomNavViewController *)self.naviController callback:^{
  161. [LOADING_MANAGER removeCustomLoading];
  162. }];
  163. }
  164. - (void)displayTeacherStyle:(NSString *)teacherId {
  165. KSBaseWKWebViewController *ctrl = [[KSBaseWKWebViewController alloc] init];
  166. ctrl.url = [NSString stringWithFormat:@"%@%@%@", WEBHOST, @"/#/teacherHome?teacherId=",teacherId];
  167. [self.naviController pushViewController:ctrl animated:YES];
  168. }
  169. - (CGFloat)waterFlowLayout:(TeacherStyleFlowLayout *)waterFlowLayout heightForWidth:(CGFloat)width andIndexPath:(NSIndexPath *)indexPath {
  170. id source = self.sourceArray[indexPath.item];
  171. if (IS_IPAD) {
  172. if (indexPath.row == 0) { // 第一排
  173. if ([source isKindOfClass:[HomeTeacherLiveModel class]]) {
  174. return 302;
  175. }
  176. else {
  177. if (indexPath.item % 3 == 1) {
  178. return 211.0f;
  179. }
  180. return 265.0f;
  181. }
  182. }
  183. else {
  184. if ([source isKindOfClass:[HomeTeacherLiveModel class]]) { // 进入直播间
  185. return 248.0f;
  186. }
  187. else {
  188. if (indexPath.item % 3 == 1) {
  189. return 211.0f;
  190. }
  191. return 248.0f;
  192. }
  193. }
  194. }
  195. else {
  196. if (indexPath.row == 0) { // 第一排
  197. if ([source isKindOfClass:[HomeTeacherLiveModel class]]) {
  198. return 302;
  199. }
  200. else {
  201. return 265.0f;
  202. }
  203. }
  204. else {
  205. if ([source isKindOfClass:[HomeTeacherLiveModel class]]) { // 进入直播间
  206. return 248.0f;
  207. }
  208. else {
  209. return 211.0f;
  210. }
  211. }
  212. }
  213. }
  214. /*
  215. // Only override drawRect: if you perform custom drawing.
  216. // An empty implementation adversely affects performance during animation.
  217. - (void)drawRect:(CGRect)rect {
  218. // Drawing code
  219. }
  220. */
  221. @end