-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathStickerPreviewPeekContent.swift
More file actions
583 lines (491 loc) Β· 24.4 KB
/
Copy pathStickerPreviewPeekContent.swift
File metadata and controls
583 lines (491 loc) Β· 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import Foundation
import UIKit
import Display
import AsyncDisplayKit
import TelegramCore
import SwiftSignalKit
import StickerResources
import AnimatedStickerNode
import TelegramAnimatedStickerNode
import ContextUI
import SolidRoundedButtonNode
import TelegramPresentationData
import AccountContext
import AppBundle
import ReactionSelectionNode
import EntityKeyboard
public enum StickerPreviewPeekItem: Equatable {
public static func == (lhs: StickerPreviewPeekItem, rhs: StickerPreviewPeekItem) -> Bool {
switch lhs {
case let .pack(lhsPack):
if case let .pack(rhsPack) = rhs, lhsPack == rhsPack {
return true
} else {
return false
}
case let .found(lhsItem):
if case let .found(rhsItem) = rhs, lhsItem == rhsItem {
return true
} else {
return false
}
case let .portal(lhsPortal):
if case let .portal(rhsPortal) = rhs, lhsPortal === rhsPortal {
return true
} else {
return false
}
}
}
case pack(TelegramMediaFile)
case found(FoundStickerItem)
case portal(PortalView)
public var file: TelegramMediaFile? {
switch self {
case let .pack(file):
return file
case let .found(item):
return item.file
case .portal:
return nil
}
}
}
public final class StickerPreviewPeekContent: PeekControllerContent {
let context: AccountContext
let theme: PresentationTheme
let strings: PresentationStrings
public let item: StickerPreviewPeekItem
let isLocked: Bool
let isCreating: Bool
let selectedEmoji: [String]
let selectedEmojiUpdated: ([String]) -> Void
let recommendedEmoji: [String]
let menu: [ContextMenuItem]
let openPremiumIntro: () -> Void
public init(context: AccountContext, theme: PresentationTheme, strings: PresentationStrings, item: StickerPreviewPeekItem, isLocked: Bool = false, isCreating: Bool = false, selectedEmoji: [String] = [], selectedEmojiUpdated: @escaping ([String]) -> Void = { _ in }, recommendedEmoji: [String] = [], menu: [ContextMenuItem], openPremiumIntro: @escaping () -> Void) {
self.context = context
self.theme = theme
self.strings = strings
self.item = item
self.isLocked = isLocked
self.isCreating = isCreating
self.selectedEmoji = selectedEmoji
self.selectedEmojiUpdated = selectedEmojiUpdated
self.recommendedEmoji = recommendedEmoji
if isLocked {
self.menu = []
} else {
self.menu = menu
}
self.openPremiumIntro = openPremiumIntro
}
public func presentation() -> PeekControllerContentPresentation {
return .freeform
}
public func menuActivation() -> PeerControllerMenuActivation {
return .press
}
public func menuItems() -> [ContextMenuItem] {
return self.menu
}
public func node() -> PeekControllerContentNode & ASDisplayNode {
return StickerPreviewPeekContentNode(context: self.context, item: self.item, theme: self.theme)
}
public func topAccessoryNode() -> ASDisplayNode? {
return nil
}
public func fullScreenAccessoryNode(blurView: UIVisualEffectView) -> (PeekControllerAccessoryNode & ASDisplayNode)? {
if self.isCreating {
return EmojiStickerAccessoryNode(context: self.context, theme: self.theme, recommendedEmoji: self.recommendedEmoji, selectedEmoji: self.selectedEmoji, selectedEmojiUpdated: self.selectedEmojiUpdated)
}
if self.isLocked {
return PremiumStickerPackAccessoryNode(theme: self.theme, strings: self.strings, isEmoji: self.item.file?.isCustomEmoji ?? false, proceed: self.openPremiumIntro)
} else {
return nil
}
}
public func isEqual(to: PeekControllerContent) -> Bool {
if let to = to as? StickerPreviewPeekContent {
return self.item == to.item
} else {
return false
}
}
}
public final class StickerPreviewPeekContentNode: ASDisplayNode, PeekControllerContentNode {
private let context: AccountContext
private let item: StickerPreviewPeekItem
private var textNode: ASTextNode
public var imageNode: TransformImageNode
public var animationNode: AnimatedStickerNode?
public var additionalAnimationNode: AnimatedStickerNode?
private let portalWrapperNode: ASDisplayNode
private let effectDisposable = MetaDisposable()
private var containerLayout: (ContainerViewLayout, CGFloat)?
private let _ready = Promise<Bool>()
init(context: AccountContext, item: StickerPreviewPeekItem, theme: PresentationTheme) {
self.context = context
self.item = item
self.textNode = ASTextNode()
self.imageNode = TransformImageNode()
var isPremiumSticker = false
if let file = item.file {
for case let .Sticker(text, _, _) in file.attributes {
self.textNode.attributedText = NSAttributedString(string: text, font: Font.regular(32.0), textColor: .black)
break
}
isPremiumSticker = file.isPremiumSticker
if file.isAnimatedSticker || file.isVideoSticker {
let animationNode = DefaultAnimatedStickerNodeImpl()
animationNode.overrideVisibility = true
self.animationNode = animationNode
let dimensions = file.dimensions ?? PixelDimensions(width: 512, height: 512)
// Decode at native sticker size (capped 512) for peek/enlarged presentation.
// Previously custom emoji used a ~200px buffer while displayed on Retina,
// which upscaled a soft raster β same class of bug as Desktop media viewer.
let fitSize = CGSize(width: 512.0, height: 512.0)
let fittedDimensions = dimensions.cgSize.aspectFitted(fitSize)
if file.isCustomTemplateEmoji {
animationNode.dynamicColor = theme.list.itemPrimaryTextColor
}
animationNode.setup(source: AnimatedStickerResourceSource(account: context.account, resource: file.resource, isVideo: file.isVideoSticker), width: Int(fittedDimensions.width), height: Int(fittedDimensions.height), playbackMode: isPremiumSticker ? .once : .loop, mode: .direct(cachePathPrefix: nil))
animationNode.visibility = true
animationNode.addSubnode(self.textNode)
if isPremiumSticker, let effect = file.videoThumbnails.first {
self.effectDisposable.set(freeMediaFileResourceInteractiveFetched(account: context.account, userLocation: .other, fileReference: .standalone(media: file), resource: effect.resource).start())
let source = AnimatedStickerResourceSource(account: context.account, resource: effect.resource, fitzModifier: nil)
let additionalAnimationNode = DefaultAnimatedStickerNodeImpl()
additionalAnimationNode.setup(source: source, width: Int(fittedDimensions.width * 2.0), height: Int(fittedDimensions.height * 2.0), playbackMode: .once, mode: .direct(cachePathPrefix: nil))
additionalAnimationNode.visibility = true
self.additionalAnimationNode = additionalAnimationNode
}
} else {
self.imageNode.addSubnode(self.textNode)
self.animationNode = nil
}
self.imageNode.setSignal(chatMessageSticker(account: context.account, userLocation: .other, file: file, small: false, fetched: true, onlyFullSize: false, blurThumbnail: false))
} else if case .portal = item {
self._ready.set(.single(true))
}
self.portalWrapperNode = ASDisplayNode()
self.portalWrapperNode.clipsToBounds = true
self.portalWrapperNode.isUserInteractionEnabled = false
super.init()
self.isUserInteractionEnabled = false
if let animationNode = self.animationNode {
self.addSubnode(animationNode)
if isPremiumSticker {
animationNode.completed = { [weak self] _ in
if let strongSelf = self, let animationNode = strongSelf.animationNode, let additionalAnimationNode = strongSelf.additionalAnimationNode {
Queue.mainQueue().async {
animationNode.play(firstFrame: false, fromIndex: nil)
additionalAnimationNode.play(firstFrame: false, fromIndex: nil)
}
}
}
}
} else {
self.addSubnode(self.imageNode)
}
if let additionalAnimationNode = self.additionalAnimationNode {
self.addSubnode(additionalAnimationNode)
}
self.addSubnode(self.portalWrapperNode)
if let animationNode = self.animationNode {
animationNode.started = { [weak self] in
guard let strongSelf = self else {
return
}
strongSelf._ready.set(.single(true))
}
} else {
self.imageNode.imageUpdated = { [weak self] _ in
guard let strongSelf = self else {
return
}
strongSelf._ready.set(.single(true))
}
}
}
deinit {
self.effectDisposable.dispose()
}
public override func didLoad() {
super.didLoad()
if case let .portal(portalView) = self.item {
self.portalWrapperNode.view.addSubview(portalView.view)
}
}
public func ready() -> Signal<Bool, NoError> {
return self._ready.get()
}
public func updateLayout(size: CGSize, transition: ContainedViewLayoutTransition) -> CGSize {
let boundingSize: CGSize
if self.item.file?.isCustomEmoji == true {
boundingSize = CGSize(width: 180.0, height: 180.0).fitted(size)
} else if let _ = self.additionalAnimationNode {
boundingSize = CGSize(width: 240.0, height: 240.0).fitted(size)
} else {
boundingSize = CGSize(width: 180.0, height: 180.0).fitted(size)
}
let dimensions: PixelDimensions
if let dimensionsValue = self.item.file?.dimensions {
dimensions = dimensionsValue
} else {
dimensions = PixelDimensions(width: 512, height: 512)
}
var topOffset: CGFloat = 0.0
var textSpacing: CGFloat = 50.0
if size.width == 292.0 {
topOffset = 60.0
textSpacing -= 10.0
} else if size.width == 347.0 && size.height == 577.0 {
topOffset = 60.0
textSpacing -= 10.0
}
let textSize = self.textNode.measure(CGSize(width: 100.0, height: 100.0))
if textSize.height.isZero {
topOffset = 0.0
textSpacing = 0.0
}
let imageSize = dimensions.cgSize.aspectFitted(boundingSize)
self.imageNode.asyncLayout()(TransformImageArguments(corners: ImageCorners(), imageSize: imageSize, boundingSize: imageSize, intrinsicInsets: UIEdgeInsets()))()
var imageFrame = CGRect(origin: CGPoint(x: floor((boundingSize.width - imageSize.width) / 2.0), y: textSize.height + textSpacing - topOffset), size: imageSize)
var centerOffset: CGFloat = 0.0
if self.item.file?.isPremiumSticker == true {
let originalImageFrame = imageFrame
imageFrame.origin.x = min(imageFrame.minX + imageFrame.width * 0.1, size.width - imageFrame.width - 18.0)
centerOffset = imageFrame.minX - originalImageFrame.minX
}
self.imageNode.frame = imageFrame
if let animationNode = self.animationNode {
animationNode.frame = imageFrame
animationNode.updateLayout(size: imageSize)
if let additionalAnimationNode = self.additionalAnimationNode {
additionalAnimationNode.frame = imageFrame.offsetBy(dx: -imageFrame.width * 0.245 + 21.0, dy: -1.0).insetBy(dx: -imageFrame.width * 0.245, dy: -imageFrame.height * 0.245)
additionalAnimationNode.updateLayout(size: additionalAnimationNode.frame.size)
}
}
if case let .portal(portalView) = self.item {
self.portalWrapperNode.bounds = CGRect(origin: .zero, size: imageFrame.size)
self.portalWrapperNode.position = imageFrame.center
self.portalWrapperNode.cornerRadius = imageFrame.size.width / 8.0
portalView.view.center = CGPoint(x: imageFrame.size.width / 2.0, y: imageFrame.size.height / 2.0)
let scale = 180.0 / (size.width * 1.04)
portalView.view.transform = CGAffineTransformMakeScale(scale, scale)
}
self.textNode.frame = CGRect(origin: CGPoint(x: floor((imageFrame.size.width - textSize.width) / 2.0) - centerOffset, y: -textSize.height - textSpacing), size: textSize)
if self.item.file?.isCustomEmoji == true || textSize.height.isZero {
return CGSize(width: boundingSize.width, height: imageFrame.height)
} else {
return CGSize(width: boundingSize.width, height: imageFrame.height + textSize.height + textSpacing)
}
}
}
final class PremiumStickerPackAccessoryNode: SparseNode, PeekControllerAccessoryNode {
var dismiss: () -> Void = {}
let proceed: () -> Void
let textNode: ImmediateTextNode
let proceedButton: SolidRoundedButtonNode
let cancelButton: HighlightableButtonNode
init(theme: PresentationTheme, strings: PresentationStrings, isEmoji: Bool, proceed: @escaping () -> Void) {
self.proceed = proceed
self.textNode = ImmediateTextNode()
self.textNode.displaysAsynchronously = false
self.textNode.textAlignment = .center
self.textNode.maximumNumberOfLines = 0
self.textNode.attributedText = NSAttributedString(string: isEmoji ? strings.Premium_Emoji_Description : strings.Premium_Stickers_Description, font: Font.regular(17.0), textColor: theme.actionSheet.secondaryTextColor)
self.textNode.lineSpacing = 0.1
self.proceedButton = SolidRoundedButtonNode(title: isEmoji ? strings.Premium_Emoji_Proceed: strings.Premium_Stickers_Proceed, theme: SolidRoundedButtonTheme(
backgroundColor: .white,
backgroundColors: [
UIColor(rgb: 0x0077ff),
UIColor(rgb: 0x6b93ff),
UIColor(rgb: 0x8878ff),
UIColor(rgb: 0xe46ace)
], foregroundColor: .white), height: 50.0, cornerRadius: 11.0, isShimmering: true)
self.proceedButton.iconPosition = .right
self.proceedButton.iconSpacing = 4.0
self.proceedButton.animation = "premium_unlock"
self.cancelButton = HighlightableButtonNode()
self.cancelButton.setTitle(strings.Common_Cancel, with: Font.regular(17.0), with: theme.list.itemAccentColor, for: .normal)
super.init()
self.addSubnode(self.textNode)
self.addSubnode(self.proceedButton)
self.addSubnode(self.cancelButton)
self.proceedButton.pressed = { [weak self] in
self?.dismiss()
self?.proceed()
}
self.cancelButton.addTarget(self, action: #selector(self.cancelPressed), forControlEvents: .touchUpInside)
}
@objc func cancelPressed() {
self.dismiss()
}
func updateLayout(size: CGSize, transition: ContainedViewLayoutTransition) {
let sideInset: CGFloat = 16.0
var bottomOffset: CGFloat = 0.0
if size.width == 320.0 {
bottomOffset = 30.0
} else if size.width == 375.0 && size.height == 667.0 {
bottomOffset = 30.0
}
let cancelSize = self.cancelButton.measure(size)
self.cancelButton.frame = CGRect(origin: CGPoint(x: floorToScreenPixels((size.width - cancelSize.width) / 2.0), y: size.height - cancelSize.height - 49.0 + bottomOffset), size: cancelSize)
let buttonWidth = size.width - sideInset * 2.0
let buttonHeight = self.proceedButton.updateLayout(width: buttonWidth, transition: transition)
self.proceedButton.frame = CGRect(origin: CGPoint(x: floorToScreenPixels((size.width - buttonWidth) / 2.0), y: size.height - cancelSize.height - 49.0 - buttonHeight - 23.0 + bottomOffset), size: CGSize(width: buttonWidth, height: buttonHeight))
let textSideInset = size.width == 320.0 ? sideInset : sideInset * 2.0
let textSize = self.textNode.updateLayout(CGSize(width: size.width - textSideInset * 2.0, height: CGFloat.greatestFiniteMagnitude))
self.textNode.frame = CGRect(origin: CGPoint(x: floorToScreenPixels((size.width - textSize.width) / 2.0), y: size.height - cancelSize.height - 48.0 - buttonHeight - 20.0 - textSize.height - 31.0 + bottomOffset), size: textSize)
}
}
private func topItems(selectedEmoji: [String] = [], recommendedEmoji: [String], count: Int) -> [String] {
var defaultItems: [String] = [
"π", "π", "β€", "π₯", "π₯°", "π", "π", "π"
]
if !recommendedEmoji.isEmpty, let firstEmoji = recommendedEmoji.first {
defaultItems.remove(at: defaultItems.count - 2)
defaultItems.insert(firstEmoji, at: defaultItems.count - 1)
}
var result = selectedEmoji.filter { !defaultItems.contains($0) }
result.append(contentsOf: defaultItems)
return Array(result.prefix(count))
}
final class EmojiStickerAccessoryNode: SparseNode, PeekControllerAccessoryNode {
let context: AccountContext
let reactionContextNode: ReactionContextNode
var selectedItemsDisposable: Disposable?
var dismiss: () -> Void = {}
private var scheduledCollapse = false
init(context: AccountContext, theme: PresentationTheme, recommendedEmoji: [String], selectedEmoji: [String], selectedEmojiUpdated: @escaping ([String]) -> Void) {
self.context = context
var layoutImpl: ((ContainedViewLayoutTransition) -> Void)?
let items = topItems(selectedEmoji: selectedEmoji, recommendedEmoji: recommendedEmoji, count: 7)
let selectedItems = ValuePromise<[String]>(selectedEmoji)
let presentationData = self.context.sharedContext.currentPresentationData.with({ $0 }).withUpdated(theme: defaultDarkPresentationTheme)
let reactionContextNode = ReactionContextNode(
context: self.context,
animationCache: self.context.animationCache,
presentationData: presentationData,
items: items.map { .staticEmoji($0) },
selectedItems: Set(selectedEmoji),
title: presentationData.strings.MediaEditor_SetStickerEmoji,
reactionsLocked: false,
alwaysAllowPremiumReactions: true,
allPresetReactionsAreAvailable: true,
getEmojiContent: { animationCache, animationRenderer in
return selectedItems.get()
|> mapToSignal { selectedItems in
return EmojiPagerContentComponent.emojiInputData(
context: context,
animationCache: animationCache,
animationRenderer: animationRenderer,
isStandalone: false,
subject: .stickerAlt,
hasTrending: false,
topReactionItems: [],
topEmojiItems: topItems(selectedEmoji: selectedItems, recommendedEmoji: recommendedEmoji, count: 8),
areUnicodeEmojiEnabled: true,
areCustomEmojiEnabled: false,
chatPeerId: context.account.peerId,
selectedItems: Set(selectedItems),
hasRecent: false,
premiumIfSavedMessages: false
)
}
},
isExpandedUpdated: { transition in
layoutImpl?(transition)
},
requestLayout: { transition in
layoutImpl?(transition)
},
requestUpdateOverlayWantsToBeBelowKeyboard: { transition in
layoutImpl?(transition)
}
)
reactionContextNode.hideBackground = true
reactionContextNode.displayTail = true
reactionContextNode.forceTailToRight = true
reactionContextNode.forceDark = true
reactionContextNode.isEmojiOnly = true
self.reactionContextNode = reactionContextNode
super.init()
layoutImpl = { [weak self] transition in
self?.requestLayout(forceUpdate: true, transition: transition)
}
reactionContextNode.emojiSelected = { [weak self] emoji in
guard let self else {
return
}
let _ = (selectedItems.get()
|> take(1)
|> deliverOnMainQueue).startStandalone(next: { [weak self] items in
guard let self else {
return
}
var items = items
if let index = items.firstIndex(where: { $0 == emoji }) {
items.remove(at: index)
} else {
items.append(emoji)
}
selectedItems.set(items)
selectedEmojiUpdated(items)
self.reactionContextNode.collapse()
})
}
self.addSubnode(reactionContextNode)
self.selectedItemsDisposable = (selectedItems.get()
|> deliverOnMainQueue).start(next: { [weak self] items in
guard let self else {
return
}
self.reactionContextNode.selectedItems = Set(items)
self.reactionContextNode.items = topItems(selectedEmoji: items, recommendedEmoji: recommendedEmoji, count: 7).map { .staticEmoji($0) }
})
}
deinit {
self.selectedItemsDisposable?.dispose()
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let result = super.hitTest(point, with: event)
if let result, result.isDescendant(of: self.reactionContextNode.view) {
return result
} else if self.reactionContextNode.isExpanded {
if !self.scheduledCollapse {
self.scheduledCollapse = true
Queue.mainQueue().after(0.01, {
self.scheduledCollapse = false
self.reactionContextNode.collapse()
})
}
return self.reactionContextNode.view
}
return nil
}
func requestLayout(forceUpdate: Bool = false, transition: ContainedViewLayoutTransition) {
guard let size = self.currentLayout else {
return
}
self.updateLayout(size: size, forceUpdate: forceUpdate, transition: transition)
}
private var currentLayout: CGSize?
func updateLayout(size: CGSize, transition: ContainedViewLayoutTransition) {
self.updateLayout(size: size, forceUpdate: false, transition: transition)
}
func updateLayout(size: CGSize, forceUpdate: Bool, transition: ContainedViewLayoutTransition) {
let isFirstTime = self.currentLayout == nil
self.currentLayout = size
let anchorRect = CGRect(x: size.width / 2.0, y: size.height / 3.0 - 50.0, width: 0.0, height: 0.0)
transition.updateFrame(view: self.reactionContextNode.view, frame: CGRect(origin: CGPoint(), size: size))
self.reactionContextNode.updateLayout(size: size, insets: UIEdgeInsets(top: 64.0, left: 0.0, bottom: 0.0, right: 0.0), anchorRect: anchorRect, centerAligned: true, isCoveredByInput: false, isAnimatingOut: false, forceUpdate: forceUpdate, transition: transition)
if isFirstTime {
Queue.mainQueue().justDispatch {
self.reactionContextNode.animateIn(from: anchorRect)
}
}
}
}