Limit emoji avatars looping

This commit is contained in:
Ilya Laktyushin 2023-01-29 18:44:00 +04:00
parent 1a37943e89
commit a6009be37c
2 changed files with 19 additions and 4 deletions

View File

@ -17,7 +17,7 @@ import AnimatedStickerNode
import TelegramAnimatedStickerNode import TelegramAnimatedStickerNode
import StickerResources import StickerResources
private let maxVideoLoopCount = 3 private let maxVideoLoopCount = 2
public final class AvatarVideoNode: ASDisplayNode { public final class AvatarVideoNode: ASDisplayNode {
private let context: AccountContext private let context: AccountContext
@ -133,6 +133,14 @@ public final class AvatarVideoNode: ASDisplayNode {
} }
} }
} }
itemLayer.onLoop = { [weak self] in
if let self {
self.videoLoopCount += 1
if self.videoLoopCount >= maxVideoLoopCount {
self.itemLayer?.isVisibleForAnimations = false
}
}
}
itemLayer.layerTintColor = UIColor.white.cgColor itemLayer.layerTintColor = UIColor.white.cgColor
self.itemLayer = itemLayer self.itemLayer = itemLayer
@ -223,7 +231,7 @@ public final class AvatarVideoNode: ASDisplayNode {
} }
} }
self.animationNode?.visibility = isVisible self.animationNode?.visibility = isVisible
if isVisible, let videoContent = self.videoContent, self.videoLoopCount != maxVideoLoopCount { if isVisible, let videoContent = self.videoContent, self.videoLoopCount < maxVideoLoopCount {
if self.videoNode == nil { if self.videoNode == nil {
let context = self.context let context = self.context
let mediaManager = context.sharedContext.mediaManager let mediaManager = context.sharedContext.mediaManager
@ -234,7 +242,7 @@ public final class AvatarVideoNode: ASDisplayNode {
videoNode.playbackCompleted = { [weak self] in videoNode.playbackCompleted = { [weak self] in
if let strongSelf = self { if let strongSelf = self {
strongSelf.videoLoopCount += 1 strongSelf.videoLoopCount += 1
if strongSelf.videoLoopCount == maxVideoLoopCount { if strongSelf.videoLoopCount >= maxVideoLoopCount {
if let videoNode = strongSelf.videoNode { if let videoNode = strongSelf.videoNode {
strongSelf.videoNode = nil strongSelf.videoNode = nil
videoNode.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.2, removeOnCompletion: false, completion: { [weak videoNode] _ in videoNode.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.2, removeOnCompletion: false, completion: { [weak videoNode] _ in
@ -279,7 +287,9 @@ public final class AvatarVideoNode: ASDisplayNode {
self.videoNode = nil self.videoNode = nil
videoNode.removeFromSupernode() videoNode.removeFromSupernode()
} }
self.itemLayer?.isVisibleForAnimations = isVisible if self.videoLoopCount < maxVideoLoopCount {
self.itemLayer?.isVisibleForAnimations = isVisible
}
} }
public func updateLayout(size: CGSize, cornerRadius: CGFloat, transition: ContainedViewLayoutTransition) { public func updateLayout(size: CGSize, cornerRadius: CGFloat, transition: ContainedViewLayoutTransition) {

View File

@ -3157,6 +3157,7 @@ public final class EmojiPagerContentComponent: Component {
} }
public var onContentsUpdate: () -> Void = {} public var onContentsUpdate: () -> Void = {}
public var onLoop: () -> Void = {}
public init( public init(
item: Item, item: Item,
@ -3413,6 +3414,10 @@ public final class EmojiPagerContentComponent: Component {
self.onUpdateDisplayPlaceholder(false, 0.2) self.onUpdateDisplayPlaceholder(false, 0.2)
self.animateAlpha(from: 0.0, to: 1.0, duration: 0.18) self.animateAlpha(from: 0.0, to: 1.0, duration: 0.18)
} }
if didLoop {
self.onLoop()
}
} }
} }