-
Notifications
You must be signed in to change notification settings - Fork 895
/
Copy pathBoostChannelModalController.swift
1229 lines (982 loc) · 47.4 KB
/
BoostChannelModalController.swift
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// BoostChannelModalController.swift
// Telegram
//
// Created by Mike Renoir on 03.09.2023.
// Copyright © 2023 Telegram. All rights reserved.
//
import Foundation
import Cocoa
import TGUIKit
import SwiftSignalKit
import TelegramCore
import Postbox
import TelegramMedia
func requiredBoostSubjectLevel(subject: BoostSubject, group: Bool, context: AccountContext, configuration: PremiumConfiguration) -> Int32 {
switch subject {
case .stories:
return 1
case let .channelReactions(reactionCount):
return reactionCount
case let .nameColors(colors):
if group {
if let value = context.peerNameColors.nameColorsGroupMinRequiredBoostLevel[colors.rawValue] {
return value
}
} else {
if let value = context.peerNameColors.nameColorsChannelMinRequiredBoostLevel[colors.rawValue] {
return value
}
}
return 1
case .nameIcon:
return configuration.minChannelNameIconLevel
case .profileColors:
return configuration.minChannelProfileColorLevel
case .profileIcon:
return group ? configuration.minGroupProfileIconLevel : configuration.minChannelProfileIconLevel
case .emojiStatus:
return group ? configuration.minGroupEmojiStatusLevel : configuration.minChannelEmojiStatusLevel
case .wallpaper:
return group ? configuration.minGroupWallpaperLevel : configuration.minChannelWallpaperLevel
case .customWallpaper:
return group ? configuration.minGroupCustomWallpaperLevel : configuration.minChannelCustomWallpaperLevel
case .audioTranscription:
return configuration.minGroupAudioTranscriptionLevel
case .emojiPack:
return configuration.minGroupEmojiPackLevel
case .noAds:
return configuration.minChannelRestrictAdsLevel
}
}
enum BoostSubject: Equatable {
case stories
case channelReactions(reactionCount: Int32)
case nameColors(colors: PeerNameColor)
case nameIcon
case profileColors
case profileIcon
case emojiStatus
case wallpaper
case customWallpaper
case audioTranscription
case emojiPack
case noAds
func requiredLevel(context: AccountContext, group: Bool, configuration: PremiumConfiguration) -> Int32 {
return requiredBoostSubjectLevel(subject: self, group: group, context: context, configuration: configuration)
}
}
private final class Arguments {
let context: AccountContext
let presentation: TelegramPresentationTheme
let isGroup: Bool
let onlyFeatures: Bool
let boost:()->Void
let openChannel:()->Void
let shareLink:(String)->Void
let copyLink:(String)->Void
let openGiveaway:()->Void
init(context: AccountContext, presentation: TelegramPresentationTheme, isGroup: Bool, onlyFeatures: Bool, boost:@escaping()->Void, openChannel:@escaping()->Void, shareLink: @escaping(String)->Void, copyLink: @escaping(String)->Void, openGiveaway:@escaping()->Void) {
self.context = context
self.presentation = presentation
self.isGroup = isGroup
self.onlyFeatures = onlyFeatures
self.boost = boost
self.copyLink = copyLink
self.shareLink = shareLink
self.openChannel = openChannel
self.openGiveaway = openGiveaway
}
}
extension ChannelBoostStatus {
func increment() -> ChannelBoostStatus {
return .init(level: self.level, boosts: self.boosts + 1, giftBoosts: self.giftBoosts, currentLevelBoosts: self.currentLevelBoosts, nextLevelBoosts: self.nextLevelBoosts, premiumAudience: self.premiumAudience, url: self.url, prepaidGiveaways: self.prepaidGiveaways, boostedByMe: self.boostedByMe)
}
}
private struct State : Equatable {
var peer: PeerEquatable
var status: ChannelBoostStatus
var myStatus: MyBoostStatus?
var canApplyStatus: Bool {
return true
}
var samePeer: Bool
var infoOnly: Bool
var source: BoostChannelSource
var percentToNext: CGFloat {
if let nextLevelBoosts = status.nextLevelBoosts {
return CGFloat(status.boosts - status.currentLevelBoosts) / CGFloat(nextLevelBoosts - status.currentLevelBoosts)
} else {
return 1.0
}
}
var isAdmin: Bool {
return infoOnly && peer.peer.groupAccess.isCreator //&& self.boosted
}
var boosted: Bool {
return status.boostedByMe
}
var boostedByMe: Int32 {
return Int32(myStatus?.boosts.filter { $0.peer?.id == self.peer.peer.id }.count ?? 0)
}
var link: String {
if let address = peer.peer.addressName {
return "https://t.me/\(address)?boost"
} else {
return "https://t.me/c/\(peer.peer.id.id._internalGetInt64Value())?boost"
}
}
var isGroup: Bool {
return self.peer.peer.isGroup || self.peer.peer.isSupergroup
}
var currentLevelBoosts: Int {
return status.boosts - status.currentLevelBoosts
}
var title: String {
var title: String = ""
var remaining: Int?
if let nextLevelBoosts = self.status.nextLevelBoosts {
remaining = nextLevelBoosts - self.status.boosts
}
let level = self.status.level
if isAdmin {
if let _ = remaining {
switch source {
case .nameColor:
title = strings().channelBoostEnableColors
case .nameIcon:
title = strings().channelBoostNameIcon
case .profileColor:
title = strings().channelBoostProfileColor
case .profileIcon:
title = strings().channelBoostProfileIcon
case .emojiStatus:
title = strings().channelBoostEmojiStatus
case .emojiPack:
title = strings().channelBoostEmojiPack
case .reactions:
title = strings().channelBoostEnableReactions
case .wallpaper:
title = strings().channelBoostEnableWallpapers
default:
if self.isGroup {
title = strings().channelBoostTitleGroup
} else {
title = strings().channelBoostTitleChannel
}
}
} else {
title = strings().channelBoostMaxLevelReached
}
} else {
if let _ = remaining {
if level == 0 {
if isGroup {
title = strings().channelBoostTitleGroup
} else {
title = strings().channelBoostTitleChannel
}
} else {
if isGroup {
title = strings().channelBoostHelpUpgradeGroup
} else {
title = strings().channelBoostHelpUpgradeChannel
}
}
} else {
title = strings().channelBoostMaxLevelReached
}
}
if self.boosted {
if let _ = remaining {
title = samePeer ? strings().channelBoostYouBoostedChannel(peer.peer.compactDisplayTitle) : strings().channelBoostYouBoostedOtherChannel
} else {
title = strings().channelBoostMaxLevelReached
}
}
return title
}
}
private final class BoostRowItem : TableRowItem {
fileprivate let context: AccountContext
fileprivate let state: State
fileprivate let text: TextViewLayout
fileprivate let presentation: TelegramPresentationTheme
fileprivate let boost:()->Void
fileprivate let openChannel:()->Void
init(_ initialSize: NSSize, presentation: TelegramPresentationTheme, state: State, context: AccountContext, boost:@escaping()->Void, openChannel:@escaping()->Void) {
self.context = context
self.state = state
self.boost = boost
self.presentation = presentation
self.openChannel = openChannel
var remaining: Int?
if let nextLevelBoosts = state.status.nextLevelBoosts {
remaining = nextLevelBoosts - state.status.boosts
}
let level = state.status.level
var string: String
if state.status.nextLevelBoosts != nil {
if state.infoOnly {
if let remaining = remaining {
let valueString: String = strings().channelBoostMoreBoostsCountable(remaining)
switch state.source {
case let .nameColor(level):
string = strings().channelBoostEnableColorsText("\(level)")
case let .nameIcon(level):
string = strings().channelBoostEnableNameIconLevelText("\(level)")
case let .profileIcon(level):
string = strings().channelBoostEnableProfileIconLevelText("\(level)")
case let .profileColor(level):
string = strings().channelBoostEnableProfileColorLevelText("\(level)")
case let .emojiStatus(level):
if state.isGroup {
string = strings().channelBoostEnableEmojiStatusLevelTextGroup("\(level)")
} else {
string = strings().channelBoostEnableEmojiStatusLevelText("\(level)")
}
case let .wallpaper(level):
if state.isGroup {
string = strings().channelBoostEnableWallpapersTextGroup("\(level)")
} else {
string = strings().channelBoostEnableWallpapersText("\(level)")
}
case .reactions:
if state.isGroup {
string = strings().channelBoostEnableReactionsTextGroup("\(level + 1)", "\(level)")
} else {
string = strings().channelBoostEnableReactionsText("\(level + 1)", "\(level)")
}
case let .noAds(level):
string = strings().channelBoostEnableNoAdsLevelText("\(level)")
default:
if level == 0 {
if state.isGroup {
string = strings().channelBoostZeroLevelTextGroup(valueString)
} else {
string = strings().channelBoostZeroLevelTextChannel(valueString)
}
} else {
if state.isGroup {
string = strings().channelBoostIncreaseLimitTextGroup(valueString)
} else {
string = strings().channelBoostIncreaseLimitTextChannel(valueString)
}
}
}
} else {
string = ""
}
} else {
if let remaining = remaining {
let valueString: String = strings().channelBoostMoreBoostsCountable(remaining)
if remaining == 0 {
if state.isGroup {
string = strings().channelBoostBoostedChannelReachedLevelGroup("\(level)")
} else {
string = strings().channelBoostBoostedChannelReachedLevelChannel("\(level)")
}
} else {
string = strings().channelBoostBoostedChannelMoreRequiredNew(valueString)
}
} else {
string = ""
}
}
if state.boosted {
if let remaining = remaining {
let valueString: String = strings().channelBoostMoreBoostsCountable(remaining)
if level == 0 {
if remaining == 0 {
string = strings().channelBoostEnabledStoriesForChannelText
} else {
string = strings().channelBoostEnableStoriesMoreRequired(valueString)
}
} else {
if state.isGroup {
string = strings().channelBoostBoostedChannelReachedLevelGroup("\(level)")
} else {
string = strings().channelBoostBoostedChannelReachedLevelChannel("\(level)")
}
}
} else {
if state.isGroup {
string = strings().channelBoostBoostedChannelReachedLevelGroup("\(level)")
} else {
string = strings().channelBoostBoostedChannelReachedLevelChannel("\(level)")
}
}
}
} else {
if state.isGroup {
string = strings().channelBoostMaxLevelReachedTextGroup("\(level)")
} else {
string = strings().channelBoostMaxLevelReachedTextChannel("\(level)")
}
}
switch state.source {
case let .unblockText(count):
if count > state.boostedByMe {
if state.status.nextLevelBoosts == nil {
string = strings().channelBoostUnblockTextGroupFullCountable(Int(count - state.boostedByMe))
} else {
string = strings().channelBoostUnblockTextGroupCountable(Int(count - state.boostedByMe), state.peer.peer.displayTitle)
}
}
case let .unblockSlowmode(count):
if count > state.boostedByMe {
if state.status.nextLevelBoosts == nil {
string = strings().channelBoostUnblockSlowmodeGroupFullCountable(Int(state.boostedByMe))
} else {
string = strings().channelBoostUnblockSlowmodeGroupCountable(Int(count - state.boostedByMe), state.peer.peer.displayTitle)
}
}
default:
break
}
let textString = NSMutableAttributedString()
textString.append(string: string, color: presentation.colors.text, font: .normal(.text))
textString.detectBoldColorInString(with: .medium(.text))
self.text = .init(textString, alignment: .center)
super.init(initialSize)
_ = makeSize(initialSize.width)
}
override var stableId: AnyHashable {
return 0
}
override func makeSize(_ width: CGFloat, oldWidth: CGFloat = 0) -> Bool {
_ = super.makeSize(width, oldWidth: oldWidth)
text.measure(width: width - 40)
return true
}
override var height: CGFloat {
var height: CGFloat = 0
height += 100
if !state.samePeer {
height += 60
} else {
height += 10
}
height += text.layoutSize.height
return height
}
override func viewClass() -> AnyClass {
return BoostRowItemView.self
}
}
private final class BoostRowItemView : TableRowView {
private class ChannelView : Control {
private let avatar = AvatarControl(font: .avatar(12))
private let textView = TextView()
required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
addSubview(avatar)
addSubview(textView)
textView.userInteractionEnabled = false
textView.isSelectable = false
avatar.setFrameSize(NSMakeSize(30, 30))
scaleOnClick = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func update(_ peer: Peer, context: AccountContext, presentation: TelegramPresentationTheme, maxWidth: CGFloat) {
self.avatar.setPeer(account: context.account, peer: peer)
let layout = TextViewLayout(.initialize(string: peer.displayTitle, color: presentation.colors.text, font: .medium(.text)))
layout.measure(width: maxWidth - 40)
textView.update(layout)
self.backgroundColor = presentation.colors.background
self.setFrameSize(NSMakeSize(layout.layoutSize.width + 10 + avatar.frame.width + 10, 30))
self.layer?.cornerRadius = frame.height / 2
}
override func layout() {
super.layout()
textView.centerY(x: avatar.frame.maxX + 10)
}
}
private class LineView: View {
private let currentLevel = TextView()
private let nextLevel = TextView()
private let nextLevel_background = View()
private let currentLevel_background = PremiumGradientView(frame: .zero)
private var state: State?
required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
addSubview(nextLevel_background)
addSubview(currentLevel_background)
addSubview(nextLevel)
addSubview(currentLevel)
nextLevel.userInteractionEnabled = false
currentLevel.userInteractionEnabled = false
nextLevel.isSelectable = false
currentLevel.isSelectable = false
}
func update(_ state: State, context: AccountContext, presentation: TelegramPresentationTheme, transition: ContainedViewLayoutTransition) {
self.state = state
let width = frame.width * state.percentToNext
var normalCountLayout = TextViewLayout(.initialize(string: strings().channelBoostLevel("\(state.status.level)"), color: presentation.colors.text, font: .medium(13)))
normalCountLayout.measure(width: .greatestFiniteMagnitude)
if width >= 10 + normalCountLayout.layoutSize.width {
normalCountLayout = TextViewLayout(.initialize(string: normalCountLayout.attributedString.string, color: .white, font: .medium(13)))
normalCountLayout.measure(width: .greatestFiniteMagnitude)
}
currentLevel.update(normalCountLayout)
var premiumCountLayout = TextViewLayout(.initialize(string: strings().channelBoostLevel("\(state.status.level + 1)"), color: presentation.colors.text, font: .medium(13)))
premiumCountLayout.measure(width: .greatestFiniteMagnitude)
if width >= frame.width - 10 {
premiumCountLayout = TextViewLayout(.initialize(string: premiumCountLayout.attributedString.string, color: .white, font: .medium(13)))
premiumCountLayout.measure(width: .greatestFiniteMagnitude)
}
nextLevel.update(premiumCountLayout)
nextLevel.isHidden = state.status.nextLevelBoosts == nil
nextLevel_background.backgroundColor = presentation.colors.background
self.updateLayout(size: self.frame.size, transition: transition)
}
func updateLayout(size: NSSize, transition: ContainedViewLayoutTransition) {
guard let state = self.state else {
return
}
let width = frame.width * state.percentToNext
transition.updateFrame(view: currentLevel, frame: currentLevel.centerFrameY(x: 10))
transition.updateFrame(view: nextLevel, frame: nextLevel.centerFrameY(x: bounds.width - 10 - nextLevel.frame.width))
transition.updateFrame(view: nextLevel_background, frame: NSMakeRect(width, 0, size.width - width, frame.height))
transition.updateFrame(view: currentLevel_background, frame: NSMakeRect(0, 0, width, frame.height))
}
override func layout() {
super.layout()
self.updateLayout(size: self.frame.size, transition: .immediate)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
private class TypeView : View {
private let backgrounView = ImageView()
private let textView = DynamicCounterTextView(frame: .zero)
private let imageView = ImageView()
private let container = View()
required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
addSubview(backgrounView)
container.addSubview(textView)
container.addSubview(imageView)
addSubview(container)
textView.userInteractionEnabled = false
}
override func layout() {
super.layout()
backgrounView.frame = bounds
container.centerX()
imageView.centerY(x: -3)
textView.centerY(x: imageView.frame.maxX)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func update(state: State, context: AccountContext, transition: ContainedViewLayoutTransition) -> NSSize {
let dynamicValue = DynamicCounterTextView.make(for: "\(state.status.boosts)", count: "\(state.status.boosts)", font: .avatar(20), textColor: .white, width: .greatestFiniteMagnitude)
textView.update(dynamicValue, animated: transition.isAnimated)
transition.updateFrame(view: textView, frame: CGRect(origin: textView.frame.origin, size: dynamicValue.size))
imageView.image = NSImage(named: "Icon_Boost_Lighting")?.precomposed()
imageView.sizeToFit()
container.setFrameSize(NSMakeSize(dynamicValue.size.width + imageView.frame.width, 40))
let size = NSMakeSize(container.frame.width + 20, 50)
let image = generateImage(NSMakeSize(size.width, size.height - 10), contextGenerator: { size, ctx in
ctx.clear(size.bounds)
let path = CGMutablePath()
path.addRoundedRect(in: NSMakeRect(0, 0, size.width, size.height), cornerWidth: size.height / 2, cornerHeight: size.height / 2)
ctx.addPath(path)
ctx.setFillColor(NSColor.black.cgColor)
ctx.fillPath()
})!
let corner = generateImage(NSMakeSize(30, 10), contextGenerator: { size, context in
context.clear(CGRect(origin: CGPoint(), size: size))
context.setFillColor(NSColor.black.cgColor)
context.scaleBy(x: 0.333, y: 0.333)
let _ = try? drawSvgPath(context, path: "M85.882251,0 C79.5170552,0 73.4125613,2.52817247 68.9116882,7.02834833 L51.4264069,24.5109211 C46.7401154,29.1964866 39.1421356,29.1964866 34.4558441,24.5109211 L16.9705627,7.02834833 C12.4696897,2.52817247 6.36519576,0 0,0 L85.882251,0 ")
context.fillPath()
})!
let clipImage = generateImage(size, rotatedContext: { size, ctx in
ctx.clear(size.bounds)
ctx.draw(image, in: NSMakeRect(0, 0, image.backingSize.width, image.backingSize.height))
ctx.draw(corner, in: NSMakeRect(size.bounds.focus(corner.backingSize).minX, image.backingSize.height, corner.backingSize.width, corner.backingSize.height))
})!
let fullImage = generateImage(size, contextGenerator: { size, ctx in
ctx.clear(size.bounds)
ctx.clip(to: size.bounds, mask: clipImage)
let colors = premiumGradient.compactMap { $0.cgColor } as NSArray
let delta: CGFloat = 1.0 / (CGFloat(colors.count) - 1.0)
var locations: [CGFloat] = []
for i in 0 ..< colors.count {
locations.append(delta * CGFloat(i))
}
let colorSpace = deviceColorSpace
let gradient = CGGradient(colorsSpace: colorSpace, colors: colors, locations: &locations)!
ctx.drawLinearGradient(gradient, start: CGPoint(x: 0, y: size.height), end: CGPoint(x: size.width, y: size.height), options: CGGradientDrawingOptions())
})!
self.backgrounView.image = fullImage
needsLayout = true
return size
}
}
private let headerBg = View()
private let lineView = LineView(frame: .zero)
private let top = TypeView(frame: .zero)
private let channel = ChannelView(frame: .zero)
private var text: TextView?
required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
addSubview(channel)
addSubview(headerBg)
headerBg.addSubview(top)
headerBg.addSubview(lineView)
channel.set(handler: { [weak self] _ in
if let item = self?.item as? BoostRowItem {
item.openChannel()
}
}, for: .Click)
}
override var backdorColor: NSColor {
return .clear
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func set(item: TableRowItem, animated: Bool = false) {
super.set(item: item, animated: animated)
guard let item = item as? BoostRowItem else {
return
}
if self.text?.textLayout?.attributedString.string != item.text.attributedString.string {
if let view = self.text {
performSubviewRemoval(view, animated: animated, scale: false)
self.text = nil
}
let text: TextView = TextView()
text.userInteractionEnabled = false
text.isSelectable = false
self.text = text
addSubview(text)
text.frame = text.centerFrameX(y: frame.height - text.frame.height)
text.update(item.text)
if animated {
text.layer?.animateAlpha(from: 0, to: 1, duration: 0.2)
}
}
let transition: ContainedViewLayoutTransition
if animated {
transition = .animated(duration: 0.2, curve: .easeOut)
} else {
transition = .immediate
}
channel.update(item.state.peer.peer, context: item.context, presentation: item.presentation, maxWidth: frame.width - 40)
channel.isHidden = item.state.samePeer
lineView.setFrameSize(NSMakeSize(frame.width - 40, 30))
lineView.update(item.state, context: item.context, presentation: item.presentation, transition: transition)
lineView.layer?.cornerRadius = 10
let size = top.update(state: item.state, context: item.context, transition: transition)
top.setFrameSize(size)
headerBg.setFrameSize(NSMakeSize(lineView.frame.width, top.frame.height + lineView.frame.height + 10))
updateLayout(size: self.frame.size, transition: transition)
}
override func updateLayout(size: NSSize, transition: ContainedViewLayoutTransition) {
super.updateLayout(size: size, transition: transition)
guard let item = self.item as? BoostRowItem else {
return
}
transition.updateFrame(view: channel, frame: channel.centerFrameX(y: 10))
if channel.isHidden {
transition.updateFrame(view: headerBg, frame: headerBg.centerFrameX(y: 10))
} else {
transition.updateFrame(view: headerBg, frame: headerBg.centerFrameX(y: channel.frame.maxY + 20))
}
transition.updateFrame(view: lineView, frame: lineView.centerFrameX(y: headerBg.frame.height - lineView.frame.height))
transition.updateFrame(view: top, frame: CGRect.init(origin: NSMakePoint(max(min(headerBg.frame.width * item.state.percentToNext - top.frame.width / 2, headerBg.frame.width - top.frame.width), 0), lineView.frame.minY - top.frame.height - 10), size: top.frame.size))
if let text = text {
transition.updateFrame(view: text, frame: text.centerFrameX(y: size.height - text.frame.height))
}
}
}
private final class AcceptRowItem : TableRowItem {
fileprivate let boost:()->Void
fileprivate let state: State
fileprivate let context: AccountContext
fileprivate let presentation: TelegramPresentationTheme
init(_ initialSize: NSSize, state: State, context: AccountContext, presentation: TelegramPresentationTheme, boost:@escaping()->Void) {
self.boost = boost
self.presentation = presentation
self.state = state
self.context = context
super.init(initialSize)
}
override var height: CGFloat {
return 80
}
override var stableId: AnyHashable {
return _id_accept
}
override func viewClass() -> AnyClass {
return AcceptRowView.self
}
}
private final class AcceptRowView : TableRowView {
private final class AcceptView : Control {
private let gradient: PremiumGradientView = PremiumGradientView(frame: .zero)
private let textView = TextView()
private let imageView = LottiePlayerView(frame: NSMakeRect(0, 0, 24, 24))
private let container = View()
required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
addSubview(gradient)
container.addSubview(textView)
container.addSubview(imageView)
addSubview(container)
scaleOnClick = true
textView.userInteractionEnabled = false
textView.isSelectable = false
}
override func layout() {
super.layout()
gradient.frame = bounds
container.center()
if imageView.isHidden {
textView.center()
} else {
imageView.centerY(x: 0)
textView.centerY(x: imageView.frame.maxX)
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func update(state: State, presentation: TelegramPresentationTheme, lottie: LocalAnimatedSticker) {
let title: String
var gradient: Bool = false
if state.status.nextLevelBoosts == nil {
title = strings().modalOK
} else {
if state.isAdmin {
title = strings().modalCopyLink
} else {
if state.isGroup {
title = strings().channelBoostBoostGroup
} else {
title = strings().channelBoostBoostChannel
}
gradient = true
}
}
set(background: presentation.colors.accent, for: .Normal)
//self.gradient.isHidden = !gradient
let layout = TextViewLayout(.initialize(string: title, color: NSColor.white, font: .medium(.text)))
layout.measure(width: .greatestFiniteMagnitude)
textView.update(layout)
if let data = lottie.data, gradient {
let colors:[LottieColor] = [.init(keyPath: "", color: NSColor(0xffffff))]
imageView.set(LottieAnimation(compressed: data, key: .init(key: .bundle("bundle_\(lottie.rawValue)"), size: NSMakeSize(24, 24), colors: colors), cachePurpose: .temporaryLZ4(.thumb), playPolicy: .onceEnd, maximumFps: 60, colors: colors, runOnQueue: .mainQueue()))
}
imageView.isHidden = !gradient
if imageView.isHidden {
container.setFrameSize(NSMakeSize(layout.layoutSize.width, max(layout.layoutSize.height, imageView.frame.height)))
} else {
container.setFrameSize(NSMakeSize(layout.layoutSize.width + imageView.frame.width, max(layout.layoutSize.height, imageView.frame.height)))
}
needsLayout = true
}
}
private let button: AcceptView = AcceptView(frame: .zero)
required init(frame frameRect: NSRect) {
super.init(frame: frameRect)
addSubview(button)
button.set(handler: { [weak self] _ in
if let item = self?.item as? AcceptRowItem {
item.boost()
}
}, for: .Click)
button.scaleOnClick = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func set(item: TableRowItem, animated: Bool = false) {
super.set(item: item, animated: animated)
guard let item = item as? AcceptRowItem else {
return
}
button.update(state: item.state, presentation: item.presentation, lottie: .menu_lighting)
button.setFrameSize(NSMakeSize(frame.width - 40, 40))
button.layer?.cornerRadius = 10
}
override var backdorColor: NSColor {
return .clear
}
override func updateLayout(size: NSSize, transition: ContainedViewLayoutTransition) {
super.updateLayout(size: size, transition: transition)
transition.updateFrame(view: button, frame: button.centerFrameX(y: 20))
}
}
private let _id_accept = InputDataIdentifier("accept")
private func _id_perk(_ perk: BoostChannelPerk, _ level: Int32, _ isGroup: Bool) -> InputDataIdentifier {
return .init("perk_\(perk.title(isGroup: isGroup).hashValue)_\(level)")
}
private func _id_perk_level(_ level: Int32) -> InputDataIdentifier {
return .init("perk_level_\(level)")
}
private func entries(_ state: State, arguments: Arguments) -> [InputDataEntry] {
var entries:[InputDataEntry] = []
var index: Int32 = 0
var sectionId: Int32 = 0
if arguments.onlyFeatures {
let text = NSMutableAttributedString()
//.initialize(string: "Additional Features\nBy gaining boosts, your group levels and unlocks more features.")
text.append(string: strings().channelBoostAdditionalFeaturesTitle, color: theme.colors.text, font: .medium(.header))
text.append(string: "\n")
text.append(string: strings().channelBoostAdditionalFeaturesText, color: theme.colors.text, font: .normal(.text))
entries.append(.custom(sectionId: sectionId, index: index, value: .none, identifier: .init("whole"), equatable: .init(state), comparable: nil, item: { initialSize, stableId in
return AnimatedStickerHeaderItem(initialSize, stableId: stableId, context: arguments.context, sticker: .menu_lighting, text: text, stickerSize: NSMakeSize(60, 60))
}))
index += 1
} else {
entries.append(.custom(sectionId: sectionId, index: index, value: .none, identifier: .init("whole"), equatable: .init(state), comparable: nil, item: { initialSize, stableId in
return BoostRowItem(initialSize, presentation: arguments.presentation, state: state, context: arguments.context, boost: arguments.boost, openChannel: arguments.openChannel)
}))
index += 1
}
var noLastSection = false
if !arguments.onlyFeatures {
if state.isAdmin, state.status.nextLevelBoosts != nil {
entries.append(.sectionId(sectionId, type: .customModern(20)))
sectionId += 1
entries.append(.custom(sectionId: sectionId, index: index, value: .none, identifier: InputDataIdentifier("link"), equatable: InputDataEquatable(state.link), comparable: nil, item: { initialSize, stableId in
return GeneralBlockTextRowItem(initialSize, stableId: stableId, viewType: .singleItem, text: state.link, font: .normal(.text), insets: NSEdgeInsets(left: 20, right: 20), rightAction: .init(image: arguments.presentation.icons.fast_copy_link, action: {
arguments.copyLink(state.link)
}), customTheme: .initialize(arguments.presentation))
}))
index += 1
entries.append(.sectionId(sectionId, type: .customModern(10)))
sectionId += 1
entries.append(.desc(sectionId: sectionId, index: index, text: .markdown(strings().boostGetBoosts, linkHandler: { _ in
arguments.openGiveaway()
}), data: .init(color: arguments.presentation.colors.text, viewType: .textBottomItem, fontSize: 13, centerViewAlignment: true, alignment: .center, linkColor: arguments.presentation.colors.link)))
} else {
entries.append(.custom(sectionId: sectionId, index: index, value: .none, identifier: _id_accept, equatable: .init(state), comparable: nil, item: { initialSize, stableId in
return AcceptRowItem(initialSize, state: state, context: arguments.context, presentation: arguments.presentation, boost: arguments.boost)
}))
index += 1
noLastSection = true
}
}
var nextLevels: ClosedRange<Int32>?
if arguments.onlyFeatures {
nextLevels = 1 ... 10
} else {
if state.status.level < 10 {
nextLevels = Int32(state.status.level) + 1 ... 10
}
}
let premiumConfiguration = PremiumConfiguration.with(appConfiguration: arguments.context.appConfiguration)
var nameColorsAtLevel: [(Int32, Int32)] = []
var nameColorsCountMap: [Int32: Int32] = [:]
for color in arguments.context.peerNameColors.displayOrder {
if let level = arguments.context.peerNameColors.nameColorsChannelMinRequiredBoostLevel[color] {
if let current = nameColorsCountMap[level] {
nameColorsCountMap[level] = current + 1
} else {
nameColorsCountMap[level] = 1
}
}
}
for (key, value) in nameColorsCountMap {
nameColorsAtLevel.append((key, value))
}
let isGroup = arguments.isGroup
if let nextLevels = nextLevels {
var levels: [Int32] = []
for level in nextLevels {
levels.append(level)
}