Files
yinmeng-ios/xplan-ios/Main/Home/View/SubViews/XPHomeRecommendHeaderView.m
2022-09-27 10:14:40 +08:00

333 lines
12 KiB
Objective-C

//
// XPHomeRecommendHeaderView.m
// xplan-ios
//
// Created by 冯硕 on 2022/2/25.
//
#import "XPHomeRecommendHeaderView.h"
///Third
#import <Masonry/Masonry.h>
///Tool
#import "XPMacro.h"
#import "XPConstant.h"
#import "XCHUDTool.h"
#import "AccountInfoStorage.h"
#import "TTPopup.h"
#import "Api+Home.h"
///View
#import "XPHomeBannerTableViewCell.h"
#import "XPHomeMenuTableViewCell.h"
#import "XPHomeRecommendTableViewCell.h"
#import "XPHomeHotRoomTableViewCell.h"
#import "XPHomeSectionView.h"
#import "XPHomeRecommendOtherRoomView.h"
///Model
#import "HomeMenuInfoModel.h"
#import "HomeBannerInfoModel.h"
#import "HomeRecommendRoomModel.h"
#import "HomeMenuSourceModel.h"
///VC
#import "XPWebViewController.h"
#import "XPRoomViewController.h"
#define bannerHeight 140
#define menuHeight ((KScreenWidth - 30 - 18) / 3 * 74 / 111)
#define recommendHeight 100 * kScreenScale
#define hotRoomHeight 117 * kScreenScale
#define sectionHeight 50
UIKIT_EXTERN NSString * kHomeMoreScrollPageKey;
@interface XPHomeRecommendHeaderView ()<UITableViewDelegate, UITableViewDataSource, XPHomeMenuTableViewCellDelegate, XPHomeBannerTableViewCellDelegate, XPHomeHotRoomTableViewCellDelegate, XPHomeRecommendTableViewCellDelegate, XPHomeSectionViewDelegate, XPHomeRecommendOtherRoomViewDelegate>
///列表
@property (nonatomic,strong) UITableView *tableView;
@end
@implementation XPHomeRecommendHeaderView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Public Method
+ (CGFloat)getHomeRecommendHeaderViewHeight:(NSArray *)bannerList menuList:(NSArray *)menuList recommendList:(NSArray *)recommendList hotRoomList:(NSArray *)hotRoomList{
CGFloat totaHeight = 0;
totaHeight = bannerHeight + menuHeight + sectionHeight + recommendHeight + sectionHeight + hotRoomHeight + 10;
if (bannerList.count <= 0) {
totaHeight-= bannerHeight;
}
if (menuList.count <= 0) {
totaHeight-= menuHeight;
}
if (recommendList.count <= 0) {
totaHeight-= recommendHeight;
totaHeight-= sectionHeight;
}
if (hotRoomList.count <= 0) {
totaHeight-= hotRoomHeight;
totaHeight-= sectionHeight;
}
return totaHeight;
}
#pragma mark - Private Method
- (void)initSubViews {
[self addSubview:self.tableView];
}
- (void)initSubViewConstraints {
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self).offset(10);
make.left.right.bottom.mas_equalTo(self);
}];
}
#pragma mark - UITableViewDelegate And UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return self.bannerList.count >0 ? 1 : 0;
} else if(section == 1) {
return self.menuList.count > 0 ? 1 : 0;
} else if(section == 2) {
return self.recommendList.count > 0 ? 1 : 0;
} else if(section == 3) {
return self.hotRoomList.count > 0 ? 1 : 0;
}
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
return bannerHeight;
} else if(indexPath.section == 1) {
return menuHeight;
} else if(indexPath.section == 2) {
return recommendHeight;
} else if(indexPath.section == 3) {
return hotRoomHeight;
}
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section == 2) {
return self.recommendList.count > 0 ? sectionHeight : 0;
} else if(section == 3) {
return self.hotRoomList.count > 0 ? sectionHeight : 0;
}
return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 2) {
XPHomeSectionView * sectionView = [[XPHomeSectionView alloc] init];
sectionView.frame = CGRectMake(0, 0, KScreenWidth, sectionHeight);
sectionView.tag = 1000 + section;
sectionView.imageName = @"home_recommend_section_logo";
sectionView.title = @"为你推荐";
sectionView.delegate= self;
return sectionView;
} else if (section == 3) {
XPHomeSectionView * sectionView = [[XPHomeSectionView alloc] init];
sectionView.frame = CGRectMake(0, 0, KScreenWidth, sectionHeight);
sectionView.imageName = @"home_hot_section_logo";
sectionView.title = @"人气直播";
sectionView.tag = 1000 + section;
sectionView.delegate= self;
return sectionView;
}
return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
return [[UIView alloc]init];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
XPHomeBannerTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPHomeBannerTableViewCell class])];
if (cell == nil) {
cell = [[XPHomeBannerTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPHomeBannerTableViewCell class])];
}
cell.delegate = self;
cell.bannerList = self.bannerList;
return cell;
} else if(indexPath.section == 1) {
XPHomeMenuTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPHomeMenuTableViewCell class])];
if (cell == nil) {
cell = [[XPHomeMenuTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPHomeMenuTableViewCell class])];
}
cell.delegate = self;
cell.menuList = self.menuList;
return cell;
} else if(indexPath.section == 2) {
XPHomeRecommendTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPHomeRecommendTableViewCell class])];
if (cell == nil) {
cell = [[XPHomeRecommendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPHomeRecommendTableViewCell class])];
}
cell.recommendList = self.recommendList;
cell.delegate = self;
return cell;
} else {
XPHomeHotRoomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([XPHomeHotRoomTableViewCell class])];
if (cell == nil) {
cell = [[XPHomeHotRoomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XPHomeHotRoomTableViewCell class])];
}
cell.hotAnchorList = self.hotRoomList;
cell.delegate = self;
return cell;
}
}
//去掉UItableview headerview黏性
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat sectionHeaderHeight = sectionHeight;
if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
} else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
}
#pragma mark - XPHomeMenuTableViewCellDelegate
- (void)xPHomeMenuTableViewCell:(XPHomeMenuTableViewCell *)view didClickMenuInfo:(HomeMenuInfoModel *)info {
if (info.resourceType == HomeMenuResourceType_H5) {
XPWebViewController * webVC = [[ XPWebViewController alloc] init];
webVC.url = info.resourceContent;
[self.currenViewController.navigationController pushViewController:webVC animated:YES];
} else {
NSString * uid = [AccountInfoStorage instance].getUid;
if (info.hid.length > 0 && uid.length > 0) {
[Api homePickResource:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
if (code == 200) {
HomeMenuSourceModel * sourceModel = [HomeMenuSourceModel modelWithDictionary:data.data];
if (sourceModel.isPick) {
if (sourceModel.uid.integerValue > 0) {
[XPRoomViewController openRoom:sourceModel.uid fromNick:nil fromType:UserEnterRoomFromType_Home_Recommend fromUid:nil viewController:self.currenViewController];
}
} else {
XPHomeRecommendOtherRoomView * recommendView = [[XPHomeRecommendOtherRoomView alloc] init];
recommendView.delegate = self;
recommendView.roomInfo = sourceModel;
[TTPopup popupView:recommendView style:TTPopupStyleAlert];
}
} else {
[XCHUDTool showErrorWithMessage:msg];
}
} uid:uid rid:info.hid];
}
}
}
#pragma mark - XPHomeBannerTableViewCellDelegate
- (void)xPHomeBannerTableViewCell:(XPHomeBannerTableViewCell *)view didClickBanner:(HomeBannerInfoModel *)info {
switch (info.skipType) {
case HomeBannerInfoSkipType_Room:
{
if (info.skipUri.length > 0) {
[XPRoomViewController openRoom:info.skipUri viewController:self.currenViewController];
}
}
break;
case HomeBannerInfoSkipType_Web:
{
XPWebViewController *vc = [[XPWebViewController alloc]init];
vc.url = info.skipUri;
[self.currenViewController.navigationController pushViewController:vc animated:YES];
}
break;
default:
break;
}
}
#pragma mark - XPHomeHotRoomTableViewCellDelegate
- (void)xPHomeHotRoomTableViewCell:(XPHomeHotRoomTableViewCell *)view didClickItem:(HomeRecommendRoomModel *)roomInfo {
if (roomInfo.uid.length > 0) {
[XPRoomViewController openRoom:roomInfo.uid fromNick:nil fromType:UserEnterRoomFromType_Home_Recommend fromUid:nil viewController:self.currenViewController];
}
}
#pragma mark - XPHomeRecommendTableViewCellDelegate
- (void)xPHomeRecommendTableViewCell:(XPHomeRecommendTableViewCell *)view didClickItem:(HomeRecommendRoomModel *)roomInfo {
if (roomInfo.roomUid.length > 0) {
[XPRoomViewController openRoom:roomInfo.roomUid fromNick:nil fromType:UserEnterRoomFromType_Home_Recommend fromUid:nil viewController:self.currenViewController];
}
}
#pragma mark - XPHomeSectionViewDelegate
- (void)didClickXPHomeSectionView:(XPHomeSectionView *)view {
NSInteger section = view.tag - 1000;
NSString * sectionStr = [NSString stringWithFormat:@"%ld",section];
NSDictionary * dic = @{@"section": sectionStr};
[[NSNotificationCenter defaultCenter] postNotificationName:kHomeMoreScrollPageKey object:dic];
}
#pragma mark - XPHomeRecommendOtherRoomViewDelegate
- (void)xPHomeRecommendOtherRoomView:(XPHomeRecommendOtherRoomView *)view didClickEnterRoom:(HomeMenuSourceModel *)model {
if (model.uid.integerValue > 0) {
[XPRoomViewController openRoom:model.uid fromNick:nil fromType:UserEnterRoomFromType_Home_Recommend fromUid:nil viewController:self.currenViewController];
}
}
#pragma mark - Getters And Setters
- (void)setBannerList:(NSArray<HomeBannerInfoModel *> *)bannerList {
_bannerList = bannerList;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
}
- (void)setMenuList:(NSArray<HomeMenuInfoModel *> *)menuList {
_menuList = menuList;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
}
- (void)setRecommendList:(NSArray<HomeRecommendRoomModel *> *)recommendList {
_recommendList = recommendList;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
}
- (void)setHotRoomList:(NSArray<HomeRecommendRoomModel *> *)hotRoomList {
_hotRoomList = hotRoomList;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:3] withRowAnimation:UITableViewRowAnimationNone];
}
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.tableFooterView = [UIView new];
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.1)];
_tableView.tableHeaderView = view;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor clearColor];
if (@available(iOS 11.0, *)) {
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
_tableView.bounces = NO;
[_tableView registerClass:[XPHomeBannerTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPHomeBannerTableViewCell class])];
[_tableView registerClass:[XPHomeMenuTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPHomeMenuTableViewCell class])];
[_tableView registerClass:[XPHomeRecommendTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPHomeRecommendTableViewCell class])];
[_tableView registerClass:[XPHomeHotRoomTableViewCell class] forCellReuseIdentifier:NSStringFromClass([XPHomeHotRoomTableViewCell class])];
_tableView.scrollEnabled = NO;
_tableView.bounces = NO;
}
return _tableView;
}
@end