123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- //
- // HomePageBodyView.m
- // KulexiuForStudent
- //
- // Created by 王智 on 2022/8/29.
- //
- #import "HomePageBodyView.h"
- #import "HomeHotStyleCell.h"
- #import "HomeTeacherLiveModel.h"
- #import "TeacherStyleModel.h"
- #import "KSBaseWKWebViewController.h"
- #import "KSEnterLiveroomManager.h"
- #import "HomeTempLiveCell.h"
- #import "UserInfoManager.h"
- #import "TeacherStyleFlowLayout.h"
- @interface HomePageBodyView ()<UICollectionViewDelegate,UICollectionViewDataSource,WaterFlowLayoutDelegate>
- @property (nonatomic, strong) NSMutableArray *sourceArray;
- @property (nonatomic, assign) BOOL isFirstLoad;
- @end
- @implementation HomePageBodyView
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- self.isFirstLoad = YES;
- self.backgroundColor = HexRGB(0xf6f8f9);
- TeacherStyleFlowLayout *layout = [[TeacherStyleFlowLayout alloc] init];
- layout.sectionInset = UIEdgeInsetsMake(0, 14, 12, 14);
- layout.scrollDirection = UICollectionViewScrollDirectionVertical;
- layout.delegate = self;
- self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:layout];
- self.collectionView.backgroundColor = HexRGB(0xf6f8f9);
- self.collectionView.delegate = self;
- self.collectionView.dataSource = self;
- self.collectionView.showsVerticalScrollIndicator = NO;
- self.collectionView.showsHorizontalScrollIndicator = NO;
- [self.collectionView registerNib:[UINib nibWithNibName:@"HomeHotStyleCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"HomeHotStyleCell"];
- [self.collectionView registerNib:[UINib nibWithNibName:@"HomeTempLiveCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"HomeTempLiveCell"];
- [self addSubview:self.collectionView];
-
- MJWeakSelf;
- self.collectionView.mj_footer = [KSGifRefreshFooter footerWithRefreshingBlock:^{
- if (weakSelf.isLoadMore) {
- weakSelf.pages += 1;
- [weakSelf requestData];
- }
- else {
- [weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
- }
- }];
- }
- return self;
- }
- - (void)configDefaultRefresh {
- self.isLoadMore = YES;
- self.pages = 1;
- self.rows = 10;
- [self.collectionView.mj_footer resetNoMoreData];
- }
- - (void)resetParamenter {
- [self configDefaultRefresh];
- self.styleArray = [NSMutableArray array];
- self.sourceArray = [NSMutableArray arrayWithArray:self.liveArray];
- [self.collectionView reloadData];
- }
- - (void)endRefresh {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [self.collectionView.mj_footer endRefreshing];
- });
- }
- - (void)requestData {
- [KSNetworkingManager homeQueryTeacherStyle:KS_POST page:self.pages rows:self.rows version:[USER_MANAGER getCurrentVersion] success:^(NSDictionary * _Nonnull dic) {
- [self endRefresh];
- if ([dic ks_integerValueForKey:@"code"] == 200 && [dic ks_boolValueForKey:@"status"]) {
- NSArray *sourceArray = [[dic ks_dictionaryValueForKey:@"data"] ks_arrayValueForKey:@"rows"];
- NSMutableArray *styleArray = [NSMutableArray array];
- for (NSDictionary *parm in sourceArray) {
- TeacherStyleModel *model = [[TeacherStyleModel alloc] initWithDictionary:parm];
- [styleArray addObject:model];
- }
- if (styleArray.count < self.rows) {
- self.isLoadMore = NO;
- }
- [self.sourceArray addObjectsFromArray:styleArray];
- }
- else {
- [LOADING_MANAGER MBShowAUTOHidingInWindow:MESSAGEKEY];
- }
- [self.collectionView reloadData];
- // 回调
- } faliure:^(NSError * _Nonnull error) {
- [self endRefresh];
- }];
- }
- - (void)resetAndRefresh {
- [self configDefaultRefresh];
- self.sourceArray = [NSMutableArray arrayWithArray:self.liveArray];
- [self.sourceArray addObjectsFromArray:self.styleArray];
- [self.collectionView reloadData];
- }
- - (void)beginFirstRefresh {
- if (self.isFirstLoad) {
- self.isFirstLoad = NO;
- [self configDefaultRefresh];
- self.sourceArray = [NSMutableArray arrayWithArray:self.liveArray];
- [self.sourceArray addObjectsFromArray:self.styleArray];
- }
-
- [self.collectionView reloadData];
- }
- - (void)selectCellAtIndexPath:(NSIndexPath *)indexPath {
-
- if (self.lastSelectedIndexPath == indexPath) {
- return;
- }
- if (self.lastSelectedIndexPath != nil) {
- UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:self.lastSelectedIndexPath];
- [cell setSelected:NO];
- }
- UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
- [cell setSelected:YES];
- self.lastSelectedIndexPath = indexPath;
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
- self.collectionView.frame = self.bounds;
- }
- #pragma mark ----- collection view
- - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
- return 1;
- }
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return self.sourceArray.count;
- }
- - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- id source = self.sourceArray[indexPath.item];
- if ([source isKindOfClass:[TeacherStyleModel class]]) {
- HomeHotStyleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeHotStyleCell" forIndexPath:indexPath];
- [cell configWithSource:source];
- return cell;
- }
- else {
- HomeTempLiveCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeTempLiveCell" forIndexPath:indexPath];
- [cell configWithSource:source];
- return cell;
- }
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- id source = self.sourceArray[indexPath.item];
- if ([source isKindOfClass:[HomeTeacherLiveModel class]]) { // 进入直播间
- HomeTeacherLiveModel *model = (HomeTeacherLiveModel *)source;
- [self joinLiveRoom:model.roomUid];
- }
- else if ([source isKindOfClass:[TeacherStyleModel class]]) {
- TeacherStyleModel *model = (TeacherStyleModel *)source;
- [self displayTeacherStyle:model.userId];
- }
- }
- - (void)joinLiveRoom:(NSString *)liveRoomId {
- if ([NSString isEmptyString:liveRoomId]) {
- return;
- }
- [LOADING_MANAGER showCustomLoading:@"加载中..."];
- [KSEnterLiveroomManager joinLiveWithRoomId:liveRoomId inController:(CustomNavViewController *)self.naviController callback:^{
- [LOADING_MANAGER removeCustomLoading];
- }];
- }
- - (void)displayTeacherStyle:(NSString *)teacherId {
- KSBaseWKWebViewController *ctrl = [[KSBaseWKWebViewController alloc] init];
- ctrl.url = [NSString stringWithFormat:@"%@%@%@", WEBHOST, @"/#/teacherHome?teacherId=",teacherId];
- [self.naviController pushViewController:ctrl animated:YES];
- }
- - (CGFloat)waterFlowLayout:(TeacherStyleFlowLayout *)waterFlowLayout heightForWidth:(CGFloat)width andIndexPath:(NSIndexPath *)indexPath {
- id source = self.sourceArray[indexPath.item];
- if (IS_IPAD) {
- if (indexPath.row == 0) { // 第一排
- if ([source isKindOfClass:[HomeTeacherLiveModel class]]) {
- return 302;
- }
- else {
- if (indexPath.item % 3 == 1) {
- return 211.0f;
- }
- return 265.0f;
- }
- }
- else {
- if ([source isKindOfClass:[HomeTeacherLiveModel class]]) { // 进入直播间
- return 248.0f;
- }
- else {
- if (indexPath.item % 3 == 1) {
- return 211.0f;
- }
- return 248.0f;
- }
- }
- }
- else {
- if (indexPath.row == 0) { // 第一排
- if ([source isKindOfClass:[HomeTeacherLiveModel class]]) {
- return 302;
- }
- else {
- return 265.0f;
- }
- }
- else {
- if ([source isKindOfClass:[HomeTeacherLiveModel class]]) { // 进入直播间
- return 248.0f;
- }
- else {
- return 211.0f;
- }
- }
- }
-
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|