Files
peko-ios/YuMi/Modules/YMNewHome/View/XPHomePagingViewController.m

353 lines
14 KiB
Objective-C

//
// XPHomePagingViewController.m
// YuMi
//
// Created by P on 2024/6/16.
//
#import "XPHomePagingViewController.h"
#import "XPNewHomeViewController.h"
#import "XPHomeMineViewController.h"
#import "XPRoomSearchContainerViewController.h"
#import "XPWebViewController.h"
#import "Api+Gift.h"
#import "XPGiftStorage.h"
#import "FirstRechargeManager.h"
#import "FirstRechargeModel.h"
#import "YUMIHtmlUrl.h"
#import "YUMIMacroUitls.h"
@interface XPHomePagingViewController () <UIPageViewControllerDelegate, UIPageViewControllerDataSource, FirstRechargeManagerDelegate>
@property (nonatomic, strong) UIView *topControlView;
@property (nonatomic, strong) UIButton *mineButton;
@property (nonatomic, strong) UIButton *recommendButton;
@property (nonatomic, strong) UIButton *layoutButton;
@property (nonatomic, strong) UIPageViewController *pageContainer;
@property (nonatomic, strong) NSArray *viewControllers; // 存储子视图控制器的数组
@property (nonatomic, assign) NSUInteger currentIndex; // 当前显示的页面索引
@property (nonatomic, strong) XPNewHomeViewController *recommendVC;
@property (nonatomic, strong) XPHomeMineViewController *mineVC;
// 首充弹窗引用
@property (nonatomic, strong) XPWebViewController *firstChargeWebVC;
// 首充弹窗背景视图
@property (nonatomic, strong) UIView *firstChargeBackgroundView;
@end
@implementation XPHomePagingViewController
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[FirstRechargeManager sharedManager].delegate = nil;
}
- (BOOL)isHiddenNavBar {
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setup];
// 注册首充管理器监听
[FirstRechargeManager sharedManager].delegate = self;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// 启动首充监控
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[FirstRechargeManager sharedManager] startMonitoring];
});
}
- (void)setup {
self.view.backgroundColor = [[ClientConfig shareConfig] bgColor];
[self setupTopTheme];
[self setupTopControl];
_pageContainer = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:nil];
_pageContainer.delegate = self;
_pageContainer.dataSource = self;
_recommendVC = [[XPNewHomeViewController alloc] init];
_mineVC = [[XPHomeMineViewController alloc] init];
_viewControllers = @[_recommendVC, _mineVC];
[self didTapRecommendButton];
[self addChildViewController:_pageContainer];
[self.view addSubview:_pageContainer.view];
[self.pageContainer.view mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.bottom.trailing.mas_equalTo(self.view);
make.top.mas_equalTo(self.topControlView.mas_bottom);
}];
[self.pageContainer didMoveToParentViewController:self];
for (UIView *view in self.pageContainer.view.subviews) {
if ([view isKindOfClass:[UIScrollView class]]) {
UIScrollView *scrollView = (UIScrollView *)view;
scrollView.scrollEnabled = NO; // 禁用滚动
}
}
}
- (void)setupTopTheme {
__block UIImageView *theme = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kGetScaleWidth(140))];
theme.image = [[ClientConfig shareConfig] navigationAreaBG];
theme.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:theme];
[[NSNotificationCenter defaultCenter] addObserverForName:[ClientConfig shareConfig].reloadNavigationAreaImageKey
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification * _Nonnull notification) {
if ([notification.object isKindOfClass:[UIImage class]]) {
theme.image = (UIImage *)notification.object;
}
}];
}
- (void)setupTopControl {
_topControlView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,
KScreenWidth,
44+[UIApplication sharedApplication].keyWindow.safeAreaInsets.top)];
_topControlView.backgroundColor = [UIColor clearColor];
[self.view addSubview:_topControlView];
_recommendButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_recommendButton setTitle:YMLocalizedString(@"XPMonentsViewController2") forState:UIControlStateNormal];
[_recommendButton addTarget:self
action:@selector(didTapRecommendButton)
forControlEvents:UIControlEventTouchUpInside];
[_topControlView addSubview:_recommendButton];
[_recommendButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.topControlView).offset(-6);
make.width.mas_greaterThanOrEqualTo(40);
make.leading.mas_equalTo(self.topControlView).offset(12);
}];
_mineButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_mineButton setTitle:YMLocalizedString(@"TabbarViewController5") forState:UIControlStateNormal];
[_mineButton addTarget:self
action:@selector(didTapMineButton)
forControlEvents:UIControlEventTouchUpInside];
[_topControlView addSubview:_mineButton];
[_mineButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.topControlView).offset(-6);
make.width.mas_greaterThanOrEqualTo(40);
make.leading.mas_equalTo(_recommendButton.mas_trailing).offset(32);
}];
UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
[searchButton setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
[searchButton setBackgroundImage:[UIImage imageNamed:@"home_nav_search"] forState:UIControlStateNormal];
[searchButton addTarget:self
action:@selector(didTapSearchButton)
forControlEvents:UIControlEventTouchUpInside];
[_topControlView addSubview:searchButton];
[searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.mineButton);
make.trailing.mas_equalTo(self.topControlView).offset(-16);
make.width.height.mas_equalTo(28);
}];
_layoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_layoutButton setImage:kImage(@"room_layout_type_1") forState:UIControlStateNormal];
[_layoutButton setImage:kImage(@"room_layout_type_2") forState:UIControlStateSelected];
[_topControlView addSubview:_layoutButton];
[_layoutButton addTarget:self
action:@selector(didTapLayoutButton)
forControlEvents:UIControlEventTouchUpInside];
[_layoutButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(searchButton);
make.trailing.mas_equalTo(searchButton.mas_leading).offset(-16);
make.width.height.mas_equalTo(28);
}];
}
- (void)displayMineTab {
[_mineButton.titleLabel setFont:kFontBold(20)];
[_recommendButton.titleLabel setFont:kFontRegular(16)];
[_mineButton setTitleColor:UIColorRGBAlpha(0x313131, 1) forState:UIControlStateNormal];
[_recommendButton setTitleColor:UIColorRGBAlpha(0x313131, 1) forState:UIControlStateNormal];
}
- (void)displayRecommendTab {
[_mineButton.titleLabel setFont:kFontRegular(16)];
[_recommendButton.titleLabel setFont:kFontBold(20)];
[_recommendButton setTitleColor:UIColorRGBAlpha(0x313131, 1) forState:UIControlStateNormal];
[_mineButton setTitleColor:UIColorRGBAlpha(0x313131, 1) forState:UIControlStateNormal];
}
#pragma mark -
- (void)didTapMineButton {
[self.pageContainer setViewControllers:@[self.viewControllers[1]]
direction:UIPageViewControllerNavigationDirectionReverse
animated:NO
completion:nil];
[self displayMineTab];
}
- (void)didTapRecommendButton {
[self.pageContainer setViewControllers:@[self.viewControllers[0]]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
[self displayRecommendTab];
}
- (void)didTapSearchButton {
XPRoomSearchContainerViewController * searchVC = [[XPRoomSearchContainerViewController alloc] init];
searchVC.modalPresentationStyle = UIModalPresentationFullScreen;
[self.navigationController presentViewController:searchVC
animated:YES
completion:nil];
}
- (void)didTapLayoutButton {
self.layoutButton.selected = !self.layoutButton.isSelected;
// 发送布局切换通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"kHomeLayoutToggle" object:nil];
}
#pragma mark - UIPageViewController Delegate & DataSource
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
return nil;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
return nil;
}
#pragma mark - UIPageViewControllerDelegate
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers {
// 更新当前索引
self.currentIndex = [self.viewControllers indexOfObject:pendingViewControllers.firstObject];
if (self.currentIndex == 0) {
[self displayMineTab];
} else {
[self displayRecommendTab];
}
}
- (void)tokenInvalid {
// 停止首充监控
[[FirstRechargeManager sharedManager] stopMonitoring];
// [super tokenInvalid];
}
#pragma mark - FirstRechargeManagerDelegate
- (void)firstRechargeManager:(FirstRechargeManager *)manager didCheckFirstRecharge:(FirstRechargeModel *)model shouldShow:(BOOL)shouldShow {
if (shouldShow && model.chargeStatus == NO) {
// 展示首充弹窗
[self showFirstRechargePopup:model];
// 标记今天已展示过(在展示后标记)
[[FirstRechargeManager sharedManager] markTodayShown];
}
}
- (void)showFirstRechargePopup:(FirstRechargeModel *)model {
if (model.chargeStatus) {
return;
}
// 如果已经有弹窗在显示,先移除它
[self removeFirstChargePopup];
// 创建背景视图
self.firstChargeBackgroundView = [[UIView alloc] initWithFrame:self.view.bounds];
self.firstChargeBackgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
self.firstChargeBackgroundView.alpha = 0;
// 添加点击手势
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleBackgroundTap:)];
[self.firstChargeBackgroundView addGestureRecognizer:tapGesture];
// 添加到当前视图
[self.view addSubview:self.firstChargeBackgroundView];
// 创建并配置 web 视图
self.firstChargeWebVC = [[XPWebViewController alloc] initWithRoomUID:@""];
self.firstChargeWebVC.isPush = NO;
self.firstChargeWebVC.url = URLWithType(kFirstChargeHomeIndex);
[self addChildViewController:self.firstChargeWebVC];
[self.firstChargeBackgroundView addSubview:self.firstChargeWebVC.view];
[self.firstChargeWebVC didMoveToParentViewController:self];
self.firstChargeWebVC.view.backgroundColor = [UIColor clearColor];
self.firstChargeWebVC.webview.backgroundColor = [UIColor clearColor];
self.firstChargeWebVC.webview.opaque = NO;
self.firstChargeWebVC.webview.scrollView.backgroundColor = [UIColor clearColor];
self.firstChargeWebVC.view.frame = CGRectMake(0,
0,
kGetScaleWidth(305),
kGetScaleWidth(403));
self.firstChargeWebVC.view.center = self.firstChargeBackgroundView.center;
@kWeakify(self);
[self.firstChargeWebVC setUrlLoadCompleted:^(BOOL result, NSError * _Nullable error) {
@kStrongify(self);
[UIView animateWithDuration:0.5 animations:^{
self.firstChargeBackgroundView.alpha = 1;
} completion:^(BOOL finished) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self removeFirstChargePopup];
});
}];
}];
[self.firstChargeWebVC setDidTapCharge:^{
@kStrongify(self);
[self removeFirstChargePopup];
}];
}
// 处理背景点击事件
- (void)handleBackgroundTap:(UITapGestureRecognizer *)gesture {
// 获取点击位置
CGPoint location = [gesture locationInView:self.firstChargeBackgroundView];
// 检查是否点击在 webView 区域外
if (self.firstChargeWebVC && self.firstChargeWebVC.view) {
CGRect webViewFrame = self.firstChargeWebVC.view.frame;
if (!CGRectContainsPoint(webViewFrame, location)) {
// 点击在 webView 外部,移除弹窗
[self removeFirstChargePopup];
}
} else {
// 如果 webView 不存在,直接移除弹窗
[self removeFirstChargePopup];
}
}
// 移除弹窗
- (void)removeFirstChargePopup {
if (self.firstChargeWebVC) {
[self.firstChargeWebVC willMoveToParentViewController:nil];
[self.firstChargeWebVC.view removeFromSuperview];
[self.firstChargeWebVC removeFromParentViewController];
self.firstChargeWebVC = nil;
}
if (self.firstChargeBackgroundView) {
[self.firstChargeBackgroundView removeFromSuperview];
self.firstChargeBackgroundView = nil;
}
}
@end