重构 GiftComboManager,移除 giftComboQueue,新增 networkQueue 和 uiQueue,优化队列处理逻辑,提升代码可维护性和可读性。同时,更新相关方法以支持新的队列结构,确保连击效果的准确处理。
This commit is contained in:
@@ -32,8 +32,9 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
@property (nonatomic, strong) NSMutableArray *requestQueue;
|
||||
|
||||
// 用来存储 GiftReceiveInfoModel 和 NSDictionary 的元数据队列
|
||||
@property (nonatomic, strong) NSMutableArray *giftComboQueue;
|
||||
@property (nonatomic, strong) NSMutableArray *comboFlagQueue;
|
||||
// @property (nonatomic, strong) NSMutableArray *giftComboQueue;
|
||||
@property (nonatomic, strong) NSMutableArray *networkQueue; // 网络通信队列(AttachmentModel)
|
||||
@property (nonatomic, strong) NSMutableArray *uiQueue; // UI动画队列(GiftReceiveInfoModel)
|
||||
@property (nonatomic, strong) dispatch_source_t comboFlagTimer;
|
||||
@property (nonatomic, strong) UIView *containerView;
|
||||
|
||||
@@ -66,9 +67,9 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
#pragma mark - 单例方法
|
||||
|
||||
- (void)dealloc {
|
||||
[self stopProcessingGiftComboFlagQueue];
|
||||
if (self.comboFlagQueue) {
|
||||
self.comboFlagQueue = NULL;
|
||||
[self stopProcessingUIQueue];
|
||||
if (self.uiQueue) {
|
||||
self.uiQueue = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,11 +78,12 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
sharedInstance = [[self alloc] init];
|
||||
sharedInstance.giftComboQueue = [NSMutableArray array];
|
||||
// sharedInstance.giftComboQueue = [NSMutableArray array];
|
||||
sharedInstance.networkQueue = [NSMutableArray array];
|
||||
sharedInstance.uiQueue = [NSMutableArray array];
|
||||
sharedInstance.activeViews = [NSMutableArray array];
|
||||
sharedInstance.comboFlagQueue = [NSMutableArray array];
|
||||
sharedInstance.requestQueue = [NSMutableArray array];
|
||||
[sharedInstance startProcessingGiftComboFlagQueue];
|
||||
[sharedInstance startProcessingUIQueue];
|
||||
});
|
||||
return sharedInstance;
|
||||
}
|
||||
@@ -92,7 +94,7 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
// 将元数据打包成字典并添加到队列
|
||||
@synchronized (self) {
|
||||
NSDictionary *comboData = @{@"info": info, @"metadata": metadata};
|
||||
[self.giftComboQueue addObject:comboData];
|
||||
[self.networkQueue addObject:comboData];
|
||||
}
|
||||
// 启动定时器
|
||||
[self startProcessingQueue];
|
||||
@@ -103,7 +105,7 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
if (attachment) {
|
||||
// 将元数据打包成字典并添加到队列
|
||||
@synchronized (self) {
|
||||
[self.giftComboQueue addObject:attachment];
|
||||
[self.networkQueue addObject:attachment];
|
||||
}
|
||||
// 启动定时器
|
||||
[self startProcessingQueue];
|
||||
@@ -285,21 +287,15 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
- (void)clearAllQueues {
|
||||
@synchronized (self) {
|
||||
[self.requestQueue removeAllObjects];
|
||||
[self.giftComboQueue removeAllObjects];
|
||||
[self.comboFlagQueue removeAllObjects];
|
||||
[self.networkQueue removeAllObjects];
|
||||
[self.uiQueue removeAllObjects];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
- (NSInteger)loadTotalGiftNum {
|
||||
return self.combo * self.countModel.giftNumber.integerValue * self.sendGiftToUIDs.count;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 新增:检查连击状态是否有效
|
||||
- (BOOL)isComboStateValid {
|
||||
@synchronized (self) {
|
||||
@@ -360,18 +356,18 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
container:(UIView *)container {
|
||||
NSLog(@"[Combo effect] 🎪 收到连击飘屏请求 - combo: %ld, giftId: %ld", (long)receiveInfo.comboCount, (long)receiveInfo.gift.giftId);
|
||||
self.containerView = container;
|
||||
[self.giftComboQueue addObject:receiveInfo];
|
||||
NSLog(@"[Combo effect] 📊 连击飘屏队列数量: %ld", (long)self.giftComboQueue.count);
|
||||
[self startProcessingGiftComboFlagQueue];
|
||||
[self.uiQueue addObject:receiveInfo];
|
||||
NSLog(@"[Combo effect] 📊 连击飘屏队列数量: %ld", (long)self.uiQueue.count);
|
||||
[self startProcessingUIQueue];
|
||||
}
|
||||
|
||||
- (void)removeComboFlag {
|
||||
self.containerView = nil;
|
||||
[self stopProcessingQueue];
|
||||
[self stopProcessingGiftComboFlagQueue];
|
||||
[self stopProcessingUIQueue];
|
||||
}
|
||||
|
||||
- (void)startProcessingGiftComboFlagQueue {
|
||||
- (void)startProcessingUIQueue {
|
||||
if (self.comboFlagTimer) {
|
||||
return;
|
||||
}
|
||||
@@ -394,7 +390,7 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
self.comboFlagTimer = timer;
|
||||
}
|
||||
|
||||
- (void)stopProcessingGiftComboFlagQueue {
|
||||
- (void)stopProcessingUIQueue {
|
||||
if (self.comboFlagTimer) {
|
||||
dispatch_source_cancel(self.comboFlagTimer);
|
||||
|
||||
@@ -407,17 +403,19 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
}
|
||||
|
||||
- (void)processGiftFlagQueue {
|
||||
if (self.giftComboQueue.count == 0) {
|
||||
return;
|
||||
}
|
||||
@synchronized (self) {
|
||||
if (self.uiQueue.count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
GiftReceiveInfoModel *receiveInfo = [self.giftComboQueue firstObject];
|
||||
NSLog(@"[Combo effect] 🎪 处理连击飘屏 - combo: %ld, giftId: %ld", (long)receiveInfo.comboCount, (long)receiveInfo.gift.giftId);
|
||||
[self.giftComboQueue xpSafeRemoveObjectAtIndex:0];
|
||||
NSLog(@"[Combo effect] 📊 移除后连击飘屏队列数量: %ld", (long)self.giftComboQueue.count);
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[self handleGiftInfo:receiveInfo];
|
||||
});
|
||||
GiftReceiveInfoModel *receiveInfo = [self.uiQueue firstObject];
|
||||
NSLog(@"[Combo effect] 🎪 处理连击飘屏 - combo: %ld, giftId: %ld", (long)receiveInfo.comboCount, (long)receiveInfo.gift.giftId);
|
||||
[self.uiQueue xpSafeRemoveObjectAtIndex:0];
|
||||
NSLog(@"[Combo effect] <EFBFBD><EFBFBD> 移除后UI动画队列数量: %ld", (long)self.uiQueue.count);
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[self handleGiftInfo:receiveInfo];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
- (void)handleGiftInfo:(GiftReceiveInfoModel *)receiveInfo {
|
||||
@@ -558,7 +556,7 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
// 停止处理队列
|
||||
- (void)stopProcessingQueue {
|
||||
if (self.timer) {
|
||||
if (self.requestQueue.count == 0 && self.giftComboQueue.count == 0) {
|
||||
if (self.requestQueue.count == 0 && self.networkQueue.count == 0) {
|
||||
// 取消定时器
|
||||
dispatch_source_cancel(self.timer);
|
||||
|
||||
@@ -574,14 +572,23 @@ NSString * const kBoomStateForceResetNotification = @"BoomStateForceResetNotific
|
||||
|
||||
// 处理队列中的第一个请求
|
||||
- (void)processGiftComboQueue {
|
||||
@synchronized (self) {
|
||||
if (self.giftComboQueue.count > 0) {
|
||||
// 获取并移除队列中的第一个元数据
|
||||
AttachmentModel *attachment = [self.giftComboQueue firstObject];
|
||||
[self.giftComboQueue xpSafeRemoveObjectAtIndex:0];
|
||||
@synchronized (self) {
|
||||
if (self.networkQueue.count > 0) {
|
||||
// 获取并移除队列中的第一个网络数据
|
||||
id networkData = [self.networkQueue firstObject];
|
||||
[self.networkQueue xpSafeRemoveObjectAtIndex:0];
|
||||
|
||||
// 处理逻辑
|
||||
[self processGiftComboWith:attachment];
|
||||
if ([networkData isKindOfClass:[AttachmentModel class]]) {
|
||||
[self processGiftComboWith:(AttachmentModel *)networkData];
|
||||
} else if ([networkData isKindOfClass:[NSDictionary class]]) {
|
||||
// 处理包含info和metadata的字典
|
||||
NSDictionary *comboData = (NSDictionary *)networkData;
|
||||
GiftReceiveInfoModel *info = comboData[@"info"];
|
||||
NSDictionary *metadata = comboData[@"metadata"];
|
||||
// 这里可以添加相应的处理逻辑
|
||||
NSLog(@"[Combo effect] <20><> 处理网络数据 - info: %@, metadata: %@", info, metadata);
|
||||
}
|
||||
} else {
|
||||
[self stopProcessingQueue];
|
||||
}
|
||||
|
Reference in New Issue
Block a user