Files
peko-ios/YuMi/Modules/YMRoom/View/MoreView/Manager/XPTurboModeTipsManager.m

169 lines
4.7 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// XPTurboModeTipsManager.m
// YuMi
//
// Created by AI Assistant
// Copyright © 2024 YuMi. All rights reserved.
//
#import "XPTurboModeTipsManager.h"
#import "XPTurboModeTipsView.h"
@interface XPTurboModeTipsManager ()
@property (nonatomic, strong) XPTurboModeTipsView *currentTipsView;
@property (nonatomic, assign) BOOL isMonitoring;
@property (nonatomic, assign) BOOL hasShownTipsInCurrentSession;
@end
@implementation XPTurboModeTipsManager
#pragma mark - Singleton
+ (instancetype)sharedManager {
static XPTurboModeTipsManager *instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[XPTurboModeTipsManager alloc] init];
});
return instance;
}
- (instancetype)init {
self = [super init];
if (self) {
_isMonitoring = NO;
_hasShownTipsInCurrentSession = NO;
[self setupNotifications];
}
return self;
}
#pragma mark - Public Methods
- (void)startTipsMonitoringInRoom {
if (self.isMonitoring) {
NSLog(@"🎮 XPTurboModeTipsManager 已经在监听中");
return;
}
self.isMonitoring = YES;
self.hasShownTipsInCurrentSession = NO;
NSLog(@"🎮 XPTurboModeTipsManager 开始在房间中监听卡顿");
}
- (void)stopTipsMonitoring {
if (!self.isMonitoring) {
return;
}
self.isMonitoring = NO;
NSLog(@"🎮 XPTurboModeTipsManager 停止监听卡顿");
}
- (void)showTipsManually {
[self showTipsWithReason:@"手动触发"];
}
#pragma mark - Private Methods
- (void)setupNotifications {
// 监听卡顿检测通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleLagDetection:)
name:@"BuglyManagerDidDetectLag"
object:nil];
// 监听房间退出通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleRoomExit:)
name:@"RoomDidExit"
object:nil];
}
- (void)handleLagDetection:(NSNotification *)notification {
if (!self.isMonitoring) {
NSLog(@"🎮 XPTurboModeTipsManager 未在监听状态,跳过卡顿处理");
return;
}
if (self.hasShownTipsInCurrentSession) {
NSLog(@"🎮 XPTurboModeTipsManager 当前会话已显示过 Tips跳过");
return;
}
NSDictionary *userInfo = notification.userInfo;
NSTimeInterval duration = [userInfo[@"duration"] doubleValue];
NSString *stackTrace = userInfo[@"stackTrace"];
NSLog(@"🎮 XPTurboModeTipsManager 收到卡顿通知 - 持续时间: %.2f秒", duration);
// 延迟显示 Tips避免在卡顿期间立即显示
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self showTipsWithReason:[NSString stringWithFormat:@"检测到卡顿 (%.1fs)", duration]];
});
}
- (void)handleRoomExit:(NSNotification *)notification {
NSLog(@"🎮 XPTurboModeTipsManager 房间退出,停止监听");
[self stopTipsMonitoring];
// 关闭当前 Tips 弹窗
if (self.currentTipsView) {
[self.currentTipsView dismiss];
self.currentTipsView = nil;
}
}
- (void)showTipsWithReason:(NSString *)reason {
NSLog(@"🎮 XPTurboModeTipsManager 显示 Tips - 原因: %@", reason);
// 获取当前窗口来显示弹窗
UIWindow *window = [UIApplication sharedApplication].keyWindow;
if (!window) {
// 尝试获取其他窗口
NSArray *windows = [UIApplication sharedApplication].windows;
for (UIWindow *w in windows) {
if (w.isKeyWindow) {
window = w;
break;
}
}
}
if (!window) {
NSLog(@"🎮 XPTurboModeTipsManager 无法获取窗口Tips 弹窗显示失败");
return;
}
// 创建并显示 Tips
self.currentTipsView = [XPTurboModeTipsView showInView:window];
self.currentTipsView.delegate = self;
// 标记已显示
self.hasShownTipsInCurrentSession = YES;
NSLog(@"🎮 Tips 弹窗创建成功,已添加到窗口");
}
#pragma mark - XPTurboModeTipsViewDelegate
- (void)turboModeTipsViewDidTapUnderstand {
NSLog(@"🎮 XPTurboModeTipsManager 用户点击了 'I understand' 按钮");
// 关闭 Tips 弹窗
if (self.currentTipsView) {
[self.currentTipsView dismiss];
self.currentTipsView = nil;
}
}
#pragma mark - Dealloc
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end