// // GlobalEventManager.m // YuMi // // Created by AI on 2025-10-09. // Copyright © 2025 YuMi. All rights reserved. // #import "GlobalEventManager.h" #import "XPMiniRoomView.h" #import "RoomBoomManager.h" #import "PublicRoomManager.h" #import "XPSkillCardPlayerManager.h" #import "SocialShareManager.h" #import "YUMIConstant.h" #import @interface GlobalEventManager () // MARK: - Private Properties /// 房间最小化视图 @property (nonatomic, strong) XPMiniRoomView *miniRoomView; /// 配置重载回调 @property (nonatomic, copy) void(^configReloadCallback)(void); /// 新用户充值回调 @property (nonatomic, copy) void(^newUserRechargeCallback)(void); @end @implementation GlobalEventManager // MARK: - Lifecycle + (instancetype)shared { static GlobalEventManager *instance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[GlobalEventManager alloc] init]; }); return instance; } - (instancetype)init { if (self = [super init]) { [self setupNotificationObservers]; } return self; } - (void)dealloc { [self removeAllDelegates]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } // MARK: - SDK Delegates Setup - (void)setupSDKDelegates { // NIMSDK 代理设置 [[NIMSDK sharedSDK].loginManager addDelegate:self]; [[NIMSDK sharedSDK].chatManager addDelegate:self]; [[NIMSDK sharedSDK].systemNotificationManager addDelegate:self]; [[NIMSDK sharedSDK].broadcastManager addDelegate:self]; // RoomBoomManager 回调注册 __weak typeof(self) weakSelf = self; [[RoomBoomManager sharedManager] registerBoomBanner:^(id sth) { __strong typeof(weakSelf) strongSelf = weakSelf; if (!strongSelf) return; dispatch_async(dispatch_get_main_queue(), ^{ // 检查用户是否在房间中 if ([XPSkillCardPlayerManager shareInstance].isInRoom) { NSLog(@"[GlobalEventManager] 收到 RoomBoom 通知"); // TODO: 显示 Boom Banner // [RoomBoomBannerAnimation display:window with:sth tapToRoom:YES complete:^{}]; } }); } target:self]; NSLog(@"[GlobalEventManager] SDK 代理设置完成"); } - (void)removeAllDelegates { [[NIMSDK sharedSDK].loginManager removeDelegate:self]; [[NIMSDK sharedSDK].chatManager removeDelegate:self]; [[NIMSDK sharedSDK].systemNotificationManager removeDelegate:self]; [[NIMSDK sharedSDK].broadcastManager removeDelegate:self]; [[RoomBoomManager sharedManager] removeEventListenerForTarget:self]; NSLog(@"[GlobalEventManager] 所有代理已移除"); } // MARK: - Room Mini View - (void)setupRoomMiniViewOn:(UIView *)containerView { if (!self.miniRoomView) { self.miniRoomView = [[XPMiniRoomView alloc] init]; } [containerView addSubview:self.miniRoomView]; NSLog(@"[GlobalEventManager] 房间最小化视图已添加"); } - (void)handleRoomMini:(NSDictionary *)userInfo { if (self.miniRoomView) { // TODO: 处理房间最小化逻辑 NSLog(@"[GlobalEventManager] 处理房间最小化: %@", userInfo); } } - (void)hideRoomMiniView { if (self.miniRoomView) { [self.miniRoomView hiddenRoomMiniView]; NSLog(@"[GlobalEventManager] 房间最小化视图已隐藏"); } } // MARK: - Notification Observers - (void)setupNotificationObservers { NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; // 房间最小化通知 [center addObserver:self selector:@selector(onRoomMiniNotification:) name:kRoomMiniNotificationKey object:nil]; // 配置重载通知 [center addObserver:self selector:@selector(onConfigReloadNotification:) name:@"reloadAfterLoadConfig" object:nil]; // 语言切换通知 [center addObserver:self selector:@selector(onLanguageSwitchNotification:) name:@"kSwitchLanguage" object:nil]; NSLog(@"[GlobalEventManager] 通知监听已设置"); } - (void)onRoomMiniNotification:(NSNotification *)notification { [self handleRoomMini:notification.userInfo]; } - (void)onConfigReloadNotification:(NSNotification *)notification { [self handleConfigReload]; } - (void)onLanguageSwitchNotification:(NSNotification *)notification { [self handleLanguageSwitch:notification]; } // MARK: - Global Notifications Handler - (void)handleConfigReload { NSLog(@"[GlobalEventManager] 配置重载"); if (self.configReloadCallback) { self.configReloadCallback(); } } - (void)handleNewUserRecharge { NSLog(@"[GlobalEventManager] 新用户充值"); if (self.newUserRechargeCallback) { self.newUserRechargeCallback(); } } - (void)handleAnchorCard:(NSNotification *)notification { NSLog(@"[GlobalEventManager] 主播卡片通知: %@", notification.userInfo); // TODO: 实现主播卡片逻辑 } - (void)handleLanguageSwitch:(NSNotification *)notification { NSLog(@"[GlobalEventManager] 语言切换: %@", notification.userInfo); // TODO: 实现语言切换逻辑 } // MARK: - User Info - (void)handleUserInfoSuccess:(id)userInfo { NSLog(@"[GlobalEventManager] 用户信息获取成功"); // 更新各个 Manager 的用户信息 if ([userInfo respondsToSelector:@selector(uid)]) { [[PublicRoomManager sharedManager] initialize]; [[PublicRoomManager sharedManager] updateUserInfo:userInfo]; [[RoomBoomManager sharedManager] saveUserInfo:userInfo]; [[XPSkillCardPlayerManager shareInstance] setUserInfoModel:userInfo]; } } // MARK: - Social Share - (void)registerSocialShareCallback { // 延迟 2 秒检查社交分享 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [[SocialShareManager sharedManager] checkSocialShareItem]; NSLog(@"[GlobalEventManager] 社交分享回调已注册"); }); } // MARK: - NIMSDK Delegate Methods #pragma mark - NIMLoginManagerDelegate - (void)onLogin:(NIMLoginStep)step { NSLog(@"[GlobalEventManager] NIMSDK 登录步骤: %ld", (long)step); } - (void)onKickout:(NIMKickReason)code clientType:(NIMLoginClientType)clientType { NSLog(@"[GlobalEventManager] NIMSDK 被踢出: reason=%ld, clientType=%ld", (long)code, (long)clientType); } - (void)onAutoLoginFailed:(NSError *)error { NSLog(@"[GlobalEventManager] NIMSDK 自动登录失败: %@", error); } #pragma mark - NIMChatManagerDelegate - (void)onRecvMessages:(NSArray *)messages { NSLog(@"[GlobalEventManager] 收到 %lu 条消息", (unsigned long)messages.count); } #pragma mark - NIMSystemNotificationManagerDelegate - (void)onReceiveSystemNotification:(NIMSystemNotification *)notification { NSLog(@"[GlobalEventManager] 收到系统通知: %@", notification.notificationId); } #pragma mark - NIMBroadcastManagerDelegate - (void)onReceiveBroadcastMessage:(NIMBroadcastMessage *)message { NSLog(@"[GlobalEventManager] 收到广播消息: %@", message.content); } @end