Files
peko-ios/YuMi/Modules/YMRoom/View/AnimationView/GiftAnimationManager.m

332 lines
9.8 KiB
Mathematica
Raw Normal View History

//
// GiftAnimationManager.m
// YuMi
//
// Created by P on 2024/12/9.
//
#import "GiftAnimationManager.h"
#import "GiftComboManager.h"
#import "GiftAnimationHelper.h"
#import "GiftReceiveInfoModel.h"
@interface GiftAnimationManager ()
@property (nonatomic, strong) dispatch_source_t giftTimer;
2025-01-13 15:29:58 +08:00
@property (nonatomic, strong) dispatch_queue_t queue;
@property (nonatomic, strong) NSMutableArray<GiftReceiveInfoModel *> *giftQueue;
2025-01-13 15:29:58 +08:00
@property (nonatomic, strong) GiftAnimationHelper *animationHelper;
// 🔧 Combo
@property (nonatomic, strong) NSMutableDictionary<NSString *, NSNumber *> *userComboStates;
@property (nonatomic, strong) NSMutableDictionary<NSString *, NSDate *> *userLastGiftTime;
@property (nonatomic, assign) NSTimeInterval comboTimeWindow;
@end
@implementation GiftAnimationManager
2025-01-13 15:29:58 +08:00
- (void)dealloc {
if (_queue) {
_queue = NULL;
}
2025-01-15 19:02:58 +08:00
if (_giftTimer) {
dispatch_source_cancel(_giftTimer);
_giftTimer = nil;
}
// 🔧 combo
[self cleanupExpiredStates];
[self.userComboStates removeAllObjects];
[self.userLastGiftTime removeAllObjects];
2025-01-13 15:29:58 +08:00
}
- (instancetype)initWithContainerView:(UIView *)containerView {
self = [super init];
if (self) {
_containerView = containerView;
_giftQueue = [NSMutableArray array];
_animationHelper = [[GiftAnimationHelper alloc] init];
2024-12-11 10:48:01 +08:00
_animationInterval = 0.2;
_comboAnimationDelay = 0.2;
_standardAnimationDelay = 0.3;
2025-01-13 15:29:58 +08:00
_queue = dispatch_queue_create("com.GiftAnimationManager.queue", DISPATCH_QUEUE_SERIAL);
// 🔧 Combo
_userComboStates = [NSMutableDictionary dictionary];
_userLastGiftTime = [NSMutableDictionary dictionary];
_comboTimeWindow = 2.0; // 2combo
}
return self;
}
- (void)startGiftQueue {
2024-12-11 10:48:01 +08:00
if (self.giftTimer) {
return;
}
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
dispatch_source_set_timer(timer,
DISPATCH_TIME_NOW,
self.animationInterval * NSEC_PER_SEC,
2024-12-11 10:48:01 +08:00
0.01 * NSEC_PER_SEC);
@kWeakify(self);
dispatch_source_set_event_handler(timer, ^{
2024-12-11 10:48:01 +08:00
@kStrongify(self);
[self processNextGift];
});
2025-01-15 19:02:58 +08:00
// dispatch_async(dispatch_get_main_queue(), ^{
// @kStrongify(self);
// [self processNextGift];
// });
2024-12-11 10:48:01 +08:00
dispatch_resume(timer);
self.giftTimer = timer;
}
- (void)processNextGift {
2025-01-15 19:02:58 +08:00
dispatch_async(self.queue, ^{
if (self.giftQueue.count == 0) {
[self stopGiftQueue];
return;
2025-01-13 15:29:58 +08:00
}
GiftReceiveInfoModel *giftInfo = self.giftQueue.firstObject;
if (!giftInfo) {
return;
}
//
if (giftInfo.comboCount < 1) {
giftInfo.comboCount = 1;
2025-01-15 19:02:58 +08:00
}
// 线
[self.giftQueue xpSafeRemoveObjectAtIndex:0];
@kWeakify(self);
dispatch_async(dispatch_get_main_queue(), ^{
@kStrongify(self);
if (self) {
[self distributeGiftAnimation:giftInfo];
} else {
}
});
2025-01-15 19:02:58 +08:00
});
}
- (void)distributeGiftAnimation:(GiftReceiveInfoModel *)giftInfo {
NSArray<NSString *> *targetUids = [self resolveTargetUids:giftInfo];
2025-01-13 15:29:58 +08:00
CGPoint startPoint = CGPointZero;
BOOL isComboAnimation = [self shouldUseComboAnimationForSender:giftInfo.uid];
2025-01-13 15:29:58 +08:00
if (isComboAnimation) {
startPoint = [self comboAnimationStartPoint];
} else {
2025-01-15 19:02:58 +08:00
startPoint = [self calculateAnimationPoint:giftInfo.uid isEndPoint:NO];
2025-01-13 15:29:58 +08:00
}
NSTimeInterval delay = isComboAnimation ? self.comboAnimationDelay : self.standardAnimationDelay;
for (NSString *targetUid in targetUids) {
CGPoint endPoint = [self calculateAnimationPoint:targetUid isEndPoint:YES];
[self scheduleAnimationWithDelay:delay
giftInfo:giftInfo.gift
startPoint:startPoint
endPoint:endPoint
isComboAnimation:isComboAnimation];
}
}
- (NSArray<NSString *> *)resolveTargetUids:(GiftReceiveInfoModel *)giftInfo {
2025-01-13 15:29:58 +08:00
if (!giftInfo) {
return @[];
}
if (giftInfo.isLuckyBagGift) {
return @[giftInfo.targetUid];
}
if (giftInfo.targetUids.count > 0) {
return [giftInfo.targetUids valueForKey:@"stringValue"];
}
if (giftInfo.targetUsers) {
NSArray *uidDatas = [self ensureArrayContainsOnlyStrings:[giftInfo.targetUsers valueForKeyPath:@"uid"]];
return uidDatas;//[giftInfo.targetUsers valueForKeyPath:@"uid"];
}
return [NSString isEmpty:giftInfo.targetUid] ? @[] : @[giftInfo.targetUid];
}
- (NSArray<NSString *> *)ensureArrayContainsOnlyStrings:(NSArray *)inputArray {
//
NSMutableArray<NSString *> *resultArray = [NSMutableArray array];
for (id item in inputArray) {
if ([item isKindOfClass:[NSString class]]) {
// NSString
[resultArray addObject:item];
} else if ([item isKindOfClass:[NSNumber class]]) {
// NSNumber NSString
[resultArray addObject:[item stringValue]];
} else {
// NSString NSNumber
2025-04-02 11:04:07 +08:00
// NSLog(@"Warning: Unsupported item type: %@", [item class]);
}
}
return [resultArray copy]; //
}
- (CGPoint)calculateAnimationPoint:(NSString *)uid isEndPoint:(BOOL)isEndPoint {
2025-01-13 15:29:58 +08:00
if (uid.length == 0) {
return [self fallbackPointForEndPoint:isEndPoint];
}
2025-01-13 15:29:58 +08:00
CGPoint point = [self.delegate animationPointAtStageViewByUid:uid];
if (!CGPointEqualToPoint(point, CGPointZero)) {
return point;
}
2025-01-13 15:29:58 +08:00
return [self fallbackPointForEndPoint:isEndPoint];
}
- (void)scheduleAnimationWithDelay:(NSTimeInterval)delay
giftInfo:(GiftInfoModel *)giftInfo
startPoint:(CGPoint)startPoint
endPoint:(CGPoint)endPoint
isComboAnimation:(BOOL)isComboAnimation {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC),
dispatch_get_main_queue(), ^{
[self.animationHelper beginGiftAnimation:giftInfo.giftUrl
startPoint:startPoint
endPoint:endPoint
isGiftCombing:isComboAnimation
toTargetView:self.containerView];
});
}
- (void)stopGiftQueue {
if (self.giftTimer) {
2024-12-11 10:48:01 +08:00
//
dispatch_source_cancel(self.giftTimer);
// timer nil
dispatch_source_set_cancel_handler(self.giftTimer, ^{
self.giftTimer = nil;
});
}
}
- (void)enqueueGift:(GiftReceiveInfoModel *)giftInfo {
2025-01-13 15:29:58 +08:00
if (!giftInfo) {
return;
}
2025-01-13 15:29:58 +08:00
dispatch_async(self.queue, ^{
[self.giftQueue addObject:giftInfo];
[self startGiftQueue];
});
}
// Helper methods
- (BOOL)shouldUseComboAnimationForSender:(NSString *)uid {
if (!uid || uid.length == 0) {
return NO;
}
// 使
BOOL isUserInCombo = [self.userComboStates[uid] boolValue];
if (isUserInCombo) {
BOOL isCurrentUser = [uid isEqualToString:[AccountInfoStorage instance].getUid];
return isCurrentUser;
}
//
NSDate *lastGiftTime = self.userLastGiftTime[uid];
if (lastGiftTime) {
NSTimeInterval timeSinceLastGift = [[NSDate date] timeIntervalSinceDate:lastGiftTime];
if (timeSinceLastGift <= self.comboTimeWindow) {
BOOL isCurrentUser = [uid isEqualToString:[AccountInfoStorage instance].getUid];
return isCurrentUser;
}
}
return NO;
}
- (CGPoint)fallbackPointForEndPoint:(BOOL)isEndPoint {
CGFloat x = [UIScreen mainScreen].bounds.size.width / 2;
if (isEndPoint) {
x += 30;
}
return CGPointMake(x, 44 + kSafeAreaTopHeight);
}
- (CGPoint)comboAnimationStartPoint {
2024-12-11 10:48:01 +08:00
CGFloat x = 0;
if (isMSRTL()) {
2024-12-11 10:48:01 +08:00
x = kGetScaleWidth(90);
} else {
x = KScreenWidth - kGetScaleWidth(90);
}
return CGPointMake(x,
2024-12-11 10:48:01 +08:00
KScreenHeight - kSafeAreaBottomHeight - kGetScaleWidth(140));
}
// 🔧 Combo
- (void)setUserComboState:(BOOL)isCombo forUser:(NSString *)uid {
if (!uid || uid.length == 0) {
return;
}
if (isCombo) {
self.userComboStates[uid] = @(YES);
} else {
[self.userComboStates removeObjectForKey:uid];
}
}
- (void)clearUserComboState:(NSString *)uid {
[self setUserComboState:NO forUser:uid];
}
- (void)updateUserGiftTime:(NSString *)uid {
if (!uid || uid.length == 0) {
return;
}
self.userLastGiftTime[uid] = [NSDate date];
}
- (void)cleanupExpiredStates {
NSDate *now = [NSDate date];
NSMutableArray *expiredUsers = [NSMutableArray array];
[self.userLastGiftTime enumerateKeysAndObjectsUsingBlock:^(NSString *uid, NSDate *lastTime, BOOL *stop) {
if ([now timeIntervalSinceDate:lastTime] > self.comboTimeWindow * 2) {
[expiredUsers addObject:uid];
}
}];
for (NSString *uid in expiredUsers) {
[self.userLastGiftTime removeObjectForKey:uid];
[self.userComboStates removeObjectForKey:uid];
}
}
@end