refactor(live-room): BIG_HEART 메시지 수신 애니메이션이 여러번 실행되면 버벅거림과 발열이 생기던 문제 수정

- DispatchQueue로 Concurrent 처리
This commit is contained in:
Yu Sung
2025-11-17 18:47:50 +09:00
parent 31319e4292
commit af42fd074f
2 changed files with 128 additions and 64 deletions

View File

@@ -217,7 +217,7 @@ final class LiveRoomViewModel: NSObject, ObservableObject {
// HEART_DONATION/BIG_HEART_DONATION
private let heartDonationDisplayDuration: TimeInterval = 1.5
private let bigHeartDonationDisplayDuration: TimeInterval = 3.0
private let bigHeartDonationDisplayDuration: TimeInterval = 5.0
private var currentHeartMessageDuration: TimeInterval = 1.5
private var menuId = 0
@@ -250,6 +250,12 @@ final class LiveRoomViewModel: NSObject, ObservableObject {
private var shouldSpawnRainAfterExplosionEnds: Bool = false
// () ( 1 + 6 = 7)
private var pendingExplosionBursts: Int = 0
// 퀀 (Dispatch )
private var explosionSequenceTimer: DispatchSourceTimer?
private var lastSequenceStartedAt: Date = .distantPast
private let sequenceMinInterval: TimeInterval = 1.0
// ( 0.5s )
private var rainEligibleAt: Date? = nil
var signatureImageUrls = [String]()
var signatureList = [LiveRoomDonationResponse]()
@@ -2110,11 +2116,18 @@ final class LiveRoomViewModel: NSObject, ObservableObject {
// 1
if suppressNextRemoteWaterFill {
suppressNextRemoteWaterFill = false
scheduleExplosionBursts()
startParticlesTimer()
startExplosionSequence()
return
}
// : 0 0.5
// :
let now = Date()
if now.timeIntervalSince(lastSequenceStartedAt) < sequenceMinInterval {
DEBUG_LOG("BIG_HEART: throttled to protect performance")
return
}
lastSequenceStartedAt = now
// : 0 1.0~1.5
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
self.remoteWaterTimer?.cancel()
@@ -2134,8 +2147,8 @@ final class LiveRoomViewModel: NSObject, ObservableObject {
self.remoteWaterProgress = 0
self.remoteWavePhase = 0
self.remoteHeartScale = 1.0
self.scheduleExplosionBursts()
self.startParticlesTimer()
self.startExplosionSequence()
}
}
}
@@ -2219,36 +2232,64 @@ final class LiveRoomViewModel: NSObject, ObservableObject {
}
}
/// 0.7 7( 1 + 6)
private func scheduleExplosionBursts() {
let bursts = 7
let interval: Double = 0.7
self.pendingExplosionBursts = bursts
self.shouldSpawnRainAfterExplosionEnds = true
let bounds = UIScreen.main.bounds
let margin: CGFloat = 80
for i in 0..<bursts {
let delay = Double(i) * interval
DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [weak self] in
/// 0.7 7( 1 + 6) (Dispatch )
private func startExplosionSequence() {
// 퀀
explosionSequenceTimer?.cancel()
explosionSequenceTimer = nil
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
self.pendingExplosionBursts = 7
let bounds = UIScreen.main.bounds
let margin: CGFloat = 80
var index = 0
let timer = DispatchSource.makeTimerSource(queue: DispatchQueue.main)
timer.schedule(deadline: .now(), repeating: .milliseconds(700)) // 0.7s
timer.setEventHandler { [weak self] in
guard let self = self else { return }
let origin: CGPoint
if i == 0 {
origin = .zero //
} else {
let minX = -bounds.width * 0.5 + margin
let maxX = bounds.width * 0.5 - margin
let minY = -bounds.height * 0.5 + margin
let maxY = bounds.height * 0.5 - margin
origin = CGPoint(x: CGFloat.random(in: minX...maxX),
y: CGFloat.random(in: minY...maxY))
if index >= 7 {
// 퀀
timer.cancel()
self.explosionSequenceTimer = nil
// 0.5
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
guard let self = self else { return }
let rains = self.spawnHeartRainFromLastExplosion()
if !rains.isEmpty {
self.bigHeartParticles.append(contentsOf: rains)
if self.bigHeartParticleTimer == nil {
self.startParticlesTimer()
}
}
}
return
}
// 0 , 6
let origin: CGPoint = {
if index == 0 { return .zero }
let minX = -bounds.width * 0.5 + margin
let maxX = bounds.width * 0.5 - margin
let minY = -bounds.height * 0.5 + margin
let maxY = bounds.height * 0.5 - margin
return CGPoint(x: .random(in: minX...maxX), y: .random(in: minY...maxY))
}()
self.spawnHeartExplosion(origin: origin)
self.pendingExplosionBursts = max(0, self.pendingExplosionBursts - 1)
// ()
if self.bigHeartParticleTimer == nil {
self.startParticlesTimer()
}
self.pendingExplosionBursts = max(0, self.pendingExplosionBursts - 1)
index += 1
}
timer.resume()
self.explosionSequenceTimer = timer
}
}
@@ -2295,57 +2336,77 @@ final class LiveRoomViewModel: NSObject, ObservableObject {
private func startParticlesTimer() {
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
//
guard self.bigHeartParticleTimer == nil else { return }
let timer = DispatchSource.makeTimerSource(queue: DispatchQueue.main)
// , /
let queue = DispatchQueue(label: "bigHeart.particles.loop", qos: .userInitiated)
let timer = DispatchSource.makeTimerSource(queue: queue)
let dt: Double = 1.0 / 60.0
let gravity: CGFloat = 600 // px/s^2
let bounds = UIScreen.main.bounds
let floorY = bounds.height * 0.5 - 60
timer.schedule(deadline: .now(), repeating: dt)
timer.setEventHandler { [weak self] in
guard let self = self else { return }
// ( )
let hadExplosionBefore = self.bigHeartParticles.contains(where: { !$0.isRain })
// ()
var snapshot: [BigHeartParticle] = []
var pending = 0
var hadExplosionBefore = false
let sem = DispatchSemaphore(value: 0)
DispatchQueue.main.async {
snapshot = self.bigHeartParticles
pending = self.pendingExplosionBursts
hadExplosionBefore = snapshot.contains { !$0.isRain }
sem.signal()
}
sem.wait()
// ()
var next: [BigHeartParticle] = []
next.reserveCapacity(self.bigHeartParticles.count)
// ( )
let bounds = UIScreen.main.bounds
let floorY = bounds.height * 0.5 - 60 //
for var p in self.bigHeartParticles {
//
next.reserveCapacity(snapshot.count)
for var p in snapshot {
p.vy += gravity * p.gravityScale * CGFloat(dt)
p.x += p.vx * CGFloat(dt)
p.y += p.vy * CGFloat(dt)
// ( )
p.x += p.vx * CGFloat(dt)
p.y += p.vy * CGFloat(dt)
p.rotation += Double((abs(p.vx) + abs(p.vy)) * 0.02 * CGFloat(dt))
// / (/ )
p.life -= dt
let baseLife: Double = p.isRain ? 4.0 : 0.7
p.opacity = max(0, min(1, p.life / baseLife))
//
p.scale *= 0.995
//
if p.isRain && p.y >= floorY {
continue
}
if p.life > 0 && p.opacity > 0.01 {
next.append(p)
}
if p.isRain && p.y >= floorY { continue }
if p.life > 0 && p.opacity > 0.01 { next.append(p) }
}
// ( )
let hasExplosionAfter = next.contains(where: { !$0.isRain })
// :
if hadExplosionBefore && !hasExplosionAfter && self.shouldSpawnRainAfterExplosionEnds && self.pendingExplosionBursts == 0 {
let rains = self.spawnHeartRainFromLastExplosion()
if !rains.isEmpty {
next.append(contentsOf: rains)
}
self.shouldSpawnRainAfterExplosionEnds = false
// ( )
let maxParticles = 320
if next.count > maxParticles {
next.removeFirst(next.count - maxParticles)
}
self.bigHeartParticles = next
// : ,
if next.isEmpty && self.pendingExplosionBursts == 0 {
self.bigHeartParticleTimer?.cancel()
self.bigHeartParticleTimer = nil
// ( )
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
//
let snapshotIDs = Set(snapshot.map { $0.id })
var merged = next
if !self.bigHeartParticles.isEmpty {
let delta = self.bigHeartParticles.filter { !snapshotIDs.contains($0.id) }
if !delta.isEmpty { merged.append(contentsOf: delta) }
}
//
let maxParticles = 320
if merged.count > maxParticles {
merged.removeFirst(merged.count - maxParticles)
}
self.bigHeartParticles = merged
if merged.isEmpty && self.pendingExplosionBursts == 0 {
self.bigHeartParticleTimer?.cancel()
self.bigHeartParticleTimer = nil
}
}
}
timer.resume()

View File

@@ -767,7 +767,7 @@ struct LiveRoomViewV2: View {
.opacity(showWaterHeart ? 1 : 0)
.animation(.easeInOut(duration: 0.2), value: showWaterHeart)
// ( ) - 01 (0.5s)
// ( ) - 01 (1.0~1.5s )
WaterHeartView(progress: viewModel.remoteWaterProgress,
show: viewModel.isShowRemoteBigHeart,
phase: viewModel.remoteWavePhase)
@@ -790,6 +790,9 @@ struct LiveRoomViewV2: View {
.allowsHitTesting(false)
}
}
// drawingGroup (Rect) ,
.frame(maxWidth: .infinity, maxHeight: .infinity)
.drawingGroup(opaque: false, colorMode: .linear)
}
//
.offset(y: keyboardHandler.keyboardHeight > 0 ? -(keyboardHandler.keyboardHeight / 2 + 60) : 0)