Various Fixes

This commit is contained in:
Ilya Laktyushin 2022-01-31 23:55:18 +03:00
parent 851f106afd
commit e2ca4e989c

View File

@ -60,13 +60,16 @@ final class StickerPackPreviewGridItemNode: GridItemNode {
private var isEmpty: Bool? private var isEmpty: Bool?
private let imageNode: TransformImageNode private let imageNode: TransformImageNode
private var animationNode: AnimatedStickerNode? private var animationNode: AnimatedStickerNode?
private var placeholderNode: StickerShimmerEffectNode? private var placeholderNode: StickerShimmerEffectNode
private var theme: PresentationTheme? private var theme: PresentationTheme?
override var isVisibleInGrid: Bool { override var isVisibleInGrid: Bool {
didSet { didSet {
let visibility = self.isVisibleInGrid && (self.interaction?.playAnimatedStickers ?? true) let visibility = self.isVisibleInGrid && (self.interaction?.playAnimatedStickers ?? true)
if visibility && self.setupTimestamp == nil {
self.setupTimestamp = CACurrentMediaTime()
}
if let animationNode = self.animationNode { if let animationNode = self.animationNode {
animationNode.visibility = visibility animationNode.visibility = visibility
} }
@ -89,24 +92,34 @@ final class StickerPackPreviewGridItemNode: GridItemNode {
self.imageNode = TransformImageNode() self.imageNode = TransformImageNode()
self.imageNode.isLayerBacked = !smartInvertColorsEnabled() self.imageNode.isLayerBacked = !smartInvertColorsEnabled()
self.placeholderNode = StickerShimmerEffectNode() self.placeholderNode = StickerShimmerEffectNode()
self.placeholderNode?.isUserInteractionEnabled = false self.placeholderNode.isUserInteractionEnabled = false
super.init() super.init()
self.addSubnode(self.imageNode) self.addSubnode(self.imageNode)
if let placeholderNode = self.placeholderNode { self.addSubnode(self.placeholderNode)
self.addSubnode(placeholderNode)
}
var firstTime = true var firstTime = true
self.imageNode.imageUpdated = { [weak self] image in self.imageNode.imageUpdated = { [weak self] image in
guard let strongSelf = self else { guard let strongSelf = self else {
return return
} }
if image != nil { if image != nil {
strongSelf.removePlaceholder(animated: !firstTime) if let stickerItem = strongSelf.currentState?.1 {
if stickerItem.file.isVideoSticker || stickerItem.file.isAnimatedSticker {
strongSelf.removePlaceholder(animated: !firstTime)
} else {
let current = CACurrentMediaTime()
if let setupTimestamp = strongSelf.setupTimestamp, current - setupTimestamp > 0.3 {
strongSelf.removePlaceholder(animated: true)
} else {
strongSelf.removePlaceholder(animated: false)
}
}
}
firstTime = false
} }
firstTime = false
} }
} }
@ -115,18 +128,18 @@ final class StickerPackPreviewGridItemNode: GridItemNode {
} }
private func removePlaceholder(animated: Bool) { private func removePlaceholder(animated: Bool) {
if let placeholderNode = self.placeholderNode { guard self.placeholderNode.alpha != 0 else {
self.placeholderNode = nil return
if !animated { }
placeholderNode.removeFromSupernode() if !animated {
} else { self.placeholderNode.removeFromSupernode()
placeholderNode.allowsGroupOpacity = true } else {
placeholderNode.alpha = 0.0 self.placeholderNode.alpha = 0.0
placeholderNode.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.2, completion: { [weak placeholderNode] _ in self.placeholderNode.allowsGroupOpacity = true
placeholderNode?.removeFromSupernode() self.placeholderNode.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.2, completion: { [weak self] _ in
placeholderNode?.allowsGroupOpacity = false self?.placeholderNode.removeFromSupernode()
}) self?.placeholderNode.allowsGroupOpacity = false
} })
} }
} }
@ -136,12 +149,18 @@ final class StickerPackPreviewGridItemNode: GridItemNode {
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.imageNodeTap(_:)))) self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.imageNodeTap(_:))))
} }
private var setupTimestamp: Double?
func setup(account: Account, stickerItem: StickerPackItem?, interaction: StickerPackPreviewInteraction, theme: PresentationTheme, isEmpty: Bool) { func setup(account: Account, stickerItem: StickerPackItem?, interaction: StickerPackPreviewInteraction, theme: PresentationTheme, isEmpty: Bool) {
self.interaction = interaction self.interaction = interaction
self.theme = theme self.theme = theme
if self.currentState == nil || self.currentState!.0 !== account || self.currentState!.1 != stickerItem || self.isEmpty != isEmpty { if self.currentState == nil || self.currentState!.0 !== account || self.currentState!.1 != stickerItem || self.isEmpty != isEmpty {
if let stickerItem = stickerItem { if let stickerItem = stickerItem {
let visibility = self.isVisibleInGrid && self.interaction?.playAnimatedStickers ?? true
if visibility && self.setupTimestamp == nil {
self.setupTimestamp = CACurrentMediaTime()
}
if stickerItem.file.isAnimatedSticker || stickerItem.file.isVideoSticker { if stickerItem.file.isAnimatedSticker || stickerItem.file.isVideoSticker {
let dimensions = stickerItem.file.dimensions ?? PixelDimensions(width: 512, height: 512) let dimensions = stickerItem.file.dimensions ?? PixelDimensions(width: 512, height: 512)
if stickerItem.file.isVideoSticker { if stickerItem.file.isVideoSticker {
@ -155,13 +174,26 @@ final class StickerPackPreviewGridItemNode: GridItemNode {
self.animationNode = animationNode self.animationNode = animationNode
self.insertSubnode(animationNode, aboveSubnode: self.imageNode) self.insertSubnode(animationNode, aboveSubnode: self.imageNode)
animationNode.started = { [weak self] in animationNode.started = { [weak self] in
guard let strongSelf = self else {
return
}
self?.imageNode.isHidden = true self?.imageNode.isHidden = true
self?.removePlaceholder(animated: false)
let current = CACurrentMediaTime()
if let setupTimestamp = strongSelf.setupTimestamp, current - setupTimestamp > 0.3 {
if !strongSelf.placeholderNode.alpha.isZero {
strongSelf.removePlaceholder(animated: true)
}
} else {
strongSelf.removePlaceholder(animated: false)
}
} }
} }
let fittedDimensions = dimensions.cgSize.aspectFitted(CGSize(width: 160.0, height: 160.0)) let fittedDimensions = dimensions.cgSize.aspectFitted(CGSize(width: 160.0, height: 160.0))
self.animationNode?.setup(source: AnimatedStickerResourceSource(account: account, resource: stickerItem.file.resource, isVideo: stickerItem.file.isVideoSticker), width: Int(fittedDimensions.width), height: Int(fittedDimensions.height), mode: .cached) self.animationNode?.setup(source: AnimatedStickerResourceSource(account: account, resource: stickerItem.file.resource, isVideo: stickerItem.file.isVideoSticker), width: Int(fittedDimensions.width), height: Int(fittedDimensions.height), mode: .cached)
self.animationNode?.visibility = self.isVisibleInGrid && self.interaction?.playAnimatedStickers ?? true
self.animationNode?.visibility = visibility
self.stickerFetchedDisposable.set(freeMediaFileResourceInteractiveFetched(account: account, fileReference: stickerPackFileReference(stickerItem.file), resource: stickerItem.file.resource).start()) self.stickerFetchedDisposable.set(freeMediaFileResourceInteractiveFetched(account: account, fileReference: stickerPackFileReference(stickerItem.file), resource: stickerItem.file.resource).start())
} else { } else {
if let animationNode = self.animationNode { if let animationNode = self.animationNode {
@ -173,15 +205,13 @@ final class StickerPackPreviewGridItemNode: GridItemNode {
self.stickerFetchedDisposable.set(freeMediaFileResourceInteractiveFetched(account: account, fileReference: stickerPackFileReference(stickerItem.file), resource: chatMessageStickerResource(file: stickerItem.file, small: true)).start()) self.stickerFetchedDisposable.set(freeMediaFileResourceInteractiveFetched(account: account, fileReference: stickerPackFileReference(stickerItem.file), resource: chatMessageStickerResource(file: stickerItem.file, small: true)).start())
} }
} else { } else {
if let placeholderNode = self.placeholderNode { if isEmpty {
if isEmpty { if !self.placeholderNode.alpha.isZero {
if !placeholderNode.alpha.isZero { self.placeholderNode.alpha = 0.0
placeholderNode.alpha = 0.0 self.placeholderNode.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.2)
placeholderNode.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.2)
}
} else {
placeholderNode.alpha = 1.0
} }
} else {
self.placeholderNode.alpha = 1.0
} }
} }
self.currentState = (account, stickerItem) self.currentState = (account, stickerItem)
@ -210,23 +240,18 @@ final class StickerPackPreviewGridItemNode: GridItemNode {
} }
} }
if let placeholderNode = self.placeholderNode { let imageFrame = self.imageNode.frame
let imageFrame = self.imageNode.frame
// let placeholderFrame = CGRect(origin: CGPoint(x: floor((bounds.width - boundingSize.width) / 2.0), y: floor((bounds.height - boundingSize.height) / 2.0)), size: boundingSize) let placeholderFrame = imageFrame
let placeholderFrame = imageFrame self.placeholderNode.frame = imageFrame
placeholderNode.frame = imageFrame
if let theme = self.theme, let (_, stickerItem) = self.currentState, let item = stickerItem {
if let theme = self.theme, let (_, stickerItem) = self.currentState, let item = stickerItem { self.placeholderNode.update(backgroundColor: theme.list.itemBlocksBackgroundColor, foregroundColor: theme.list.mediaPlaceholderColor, shimmeringColor: theme.list.itemBlocksBackgroundColor.withAlphaComponent(0.4), data: item.file.immediateThumbnailData, size: placeholderFrame.size)
placeholderNode.update(backgroundColor: theme.list.itemBlocksBackgroundColor, foregroundColor: theme.list.mediaPlaceholderColor, shimmeringColor: theme.list.itemBlocksBackgroundColor.withAlphaComponent(0.4), data: item.file.immediateThumbnailData, size: placeholderFrame.size)
}
} }
} }
override func updateAbsoluteRect(_ absoluteRect: CGRect, within containerSize: CGSize) { override func updateAbsoluteRect(_ absoluteRect: CGRect, within containerSize: CGSize) {
if let placeholderNode = self.placeholderNode { self.placeholderNode.updateAbsoluteRect(absoluteRect, within: containerSize)
placeholderNode.updateAbsoluteRect(absoluteRect, within: containerSize)
}
} }
func transitionNode() -> ASDisplayNode? { func transitionNode() -> ASDisplayNode? {