@@ -10,6 +10,7 @@ import Foundation
1010import SwiftSignalKit
1111import TGUIKit
1212import TelegramCore
13+ import TelegramMedia
1314
1415extension StarGift {
1516 var id : Int64 {
@@ -81,7 +82,27 @@ struct PeerStarGift : Equatable {
8182
8283final class GiftOptionsRowItem : GeneralRowItem {
8384
84- struct Option {
85+ struct Option : Equatable {
86+
87+ static func == ( lhs: Option , rhs: Option ) -> Bool {
88+ if lhs. nativePayment != rhs. nativePayment {
89+ return false
90+ }
91+ if lhs. nativeProfileGift != rhs. nativeProfileGift {
92+ return false
93+ }
94+ if lhs. nativeStarGift != rhs. nativeStarGift {
95+ return false
96+ }
97+ if lhs. nativeStarUniqueGift != rhs. nativeStarUniqueGift {
98+ return false
99+ }
100+ if lhs. file != rhs. file {
101+ return false
102+ }
103+ return true
104+ }
105+
85106 enum TypeValue {
86107 case price( String )
87108 case stars( Int64 , Bool , Bool )
@@ -93,7 +114,9 @@ final class GiftOptionsRowItem : GeneralRowItem {
93114 let textColor : NSColor
94115 let outline : Bool
95116 }
96- let file : TelegramMediaFile
117+ let file : TelegramMediaFile ?
118+ let image : CGImage ?
119+ let colors : [ LottieColor ]
97120 let text : NSAttributedString ?
98121 let type : TypeValue
99122 let badge : Badge ?
@@ -137,8 +160,15 @@ final class GiftOptionsRowItem : GeneralRowItem {
137160 starsPrice = nil
138161 }
139162
140- return . init( file: option. media. file, text: option. text, type: . price( option. total) , badge: option. discount. flatMap { . init( text: $0, colors: colors, textColor: . white, outline: false ) } , peer: nil , invisible: false , pinned: false , toggleSelect: nil , priceBadge: nil , starsPrice: starsPrice, nativePayment: option)
163+ return . init( file: option. media. file, image : nil , colors : [ ] , text: option. text, type: . price( option. total) , badge: option. discount. flatMap { . init( text: $0, colors: colors, textColor: . white, outline: false ) } , peer: nil , invisible: false , pinned: false , toggleSelect: nil , priceBadge: nil , starsPrice: starsPrice, nativePayment: option)
141164 }
165+
166+
167+ static func initialize( _ image: CGImage , text: NSAttributedString ) -> Option {
168+ return . init( file: nil , image: image, colors: [ ] , text: text, type: . none, badge: nil , peer: nil , invisible: false , pinned: false , toggleSelect: nil , priceBadge: nil , starsPrice: nil , nativePayment: nil )
169+ }
170+
171+
142172 static func initialize( _ option: PeerStarGift ) -> Option {
143173 let badge : Badge ?
144174
@@ -183,7 +213,7 @@ final class GiftOptionsRowItem : GeneralRowItem {
183213 resale = false
184214 }
185215
186- return . init( file: option. media, text: nil , type: . stars( price, false , resale) , badge: badge, peer: nil , invisible: false , pinned: false , toggleSelect: nil , priceBadge: nil , nativeStarGift: option)
216+ return . init( file: option. media, image : nil , colors : [ ] , text: nil , type: . stars( price, false , resale) , badge: badge, peer: nil , invisible: false , pinned: false , toggleSelect: nil , priceBadge: nil , nativeStarGift: option)
187217 }
188218
189219 static func initialize( _ option: StarGift . UniqueGift , resale: Bool = false , showNumber: Bool = false ) -> Option {
@@ -205,7 +235,7 @@ final class GiftOptionsRowItem : GeneralRowItem {
205235 badge = nil
206236 }
207237
208- return . init( file: option. file!, text: nil , type: option. resellStars != nil ? . stars( option. resellStars!, true , resale) : . none, badge: badge, peer: nil , invisible: false , pinned: false , toggleSelect: nil , priceBadge: nil , nativeStarUniqueGift: option)
238+ return . init( file: option. file!, image : nil , colors : [ ] , text: nil , type: option. resellStars != nil ? . stars( option. resellStars!, true , resale) : . none, badge: badge, peer: nil , invisible: false , pinned: false , toggleSelect: nil , priceBadge: nil , nativeStarUniqueGift: option)
209239 }
210240
211241
@@ -246,7 +276,7 @@ final class GiftOptionsRowItem : GeneralRowItem {
246276 case . unique( let uniqueGift) :
247277 file = uniqueGift. file!
248278 }
249- return . init( file: file, text: nil , type: transfrarable ? . price( strings ( ) . starNftTransfer) : . none, badge: badge, peer: selected != nil ? nil : option. fromPeer, invisible: selected != nil ? false : !option. savedToProfile, pinned: selected != nil ? false : option. pinnedToTop, toggleSelect: selected, priceBadge: selected != nil ? nil : priceBadge, nativeProfileGift: option)
279+ return . init( file: file, image : nil , colors : [ ] , text: nil , type: transfrarable ? . price( strings ( ) . starNftTransfer) : . none, badge: badge, peer: selected != nil ? nil : option. fromPeer, invisible: selected != nil ? false : !option. savedToProfile, pinned: selected != nil ? false : option. pinnedToTop, toggleSelect: selected, priceBadge: selected != nil ? nil : priceBadge, nativeProfileGift: option)
250280 }
251281
252282 var height : CGFloat {
@@ -293,7 +323,7 @@ final class GiftOptionsRowItem : GeneralRowItem {
293323 itemSize = NSMakeSize ( width - self . inset. right - self . inset. left, self . height)
294324 } else {
295325 let insets = self . inset
296- let count = self . fitToSize ? CGFloat ( self . options. count) : 3
326+ let count = self . fitToSize ? CGFloat ( self . options. count) : CGFloat ( self . perRowCount )
297327 let space = self . width - insets. left - insets. right - ( 10 * CGFloat( count - 1 ) )
298328 itemSize = NSMakeSize ( floorToScreenPixels ( space / count) , self . height)
299329 }
@@ -381,6 +411,7 @@ private final class GiftOptionsRowView: GeneralContainableRowView {
381411 }
382412
383413 private let sticker = MediaAnimatedStickerView ( frame: NSMakeRect ( 0 , 0 , 80 , 80 ) )
414+ private var imageView : ImageView ?
384415 private var textView : TextView ?
385416 private var badgeView : ImageView ?
386417 private var badgeOutlineView : ImageView ?
@@ -488,8 +519,29 @@ private final class GiftOptionsRowView: GeneralContainableRowView {
488519 callback ( option)
489520 } , for: . Click)
490521
491- let parameters = ChatAnimatedStickerMediaLayoutParameters ( playPolicy: . framesCount( 1 ) , alwaysAccept: true , cache: . temporaryLZ4( . thumb) , hidePlayer: false , media: option. file, shimmer: false , thumbAtFrame: 1 )
492- self . sticker. update ( with: option. file, size: self . sticker. frame. size, context: context, table: nil , parameters: parameters, animated: false )
522+ if let file = option. file {
523+ let parameters = ChatAnimatedStickerMediaLayoutParameters ( playPolicy: . framesCount( 2 ) , alwaysAccept: true , cache: . temporaryLZ4( . thumb) , hidePlayer: false , media: file, colors: option. colors, shimmer: false , thumbAtFrame: 1 )
524+ self . sticker. update ( with: file, size: self . sticker. frame. size, context: context, table: nil , parameters: parameters, animated: false )
525+ }
526+
527+ self . sticker. isHidden = option. file == nil
528+
529+ if let image = option. image {
530+ let current : ImageView
531+ if let view = self . imageView {
532+ current = view
533+ } else {
534+ current = ImageView ( )
535+ self . addSubview ( current)
536+ self . imageView = current
537+ current. isEventLess = true
538+ }
539+ current. image = image
540+ current. sizeToFit ( )
541+ } else if let view = self . imageView {
542+ performSubviewRemoval ( view, animated: false )
543+ self . imageView = nil
544+ }
493545
494546 if let text = option. text {
495547 let current : TextView
@@ -756,7 +808,7 @@ private final class GiftOptionsRowView: GeneralContainableRowView {
756808 self . selectionView = nil
757809 }
758810
759- if option. gift? . generic? . flags. contains ( . requiresPremium) == true , let badge = option. badge {
811+ if option. gift? . generic? . flags. contains ( . requiresPremium) == true , option . nativeStarGift != nil , let badge = option. badge {
760812 let current : View
761813 if let view = premiumSelectView {
762814 current = view
@@ -769,8 +821,8 @@ private final class GiftOptionsRowView: GeneralContainableRowView {
769821 current. layer? . borderColor = badge. colors [ 0 ] . cgColor
770822 current. layer? . borderWidth = 1
771823
772- } else if let selectionView {
773- performSubviewRemoval ( selectionView , animated: animated)
824+ } else if let premiumSelectView {
825+ performSubviewRemoval ( premiumSelectView , animated: animated)
774826 self . premiumSelectView = nil
775827 }
776828
@@ -854,7 +906,7 @@ private final class GiftOptionsRowView: GeneralContainableRowView {
854906 backgroundView. frame = NSMakeRect ( 1 , 1 , bounds. width - 2 , bounds. height - 1 )
855907 }
856908 if let emoji {
857- emoji. frame = bounds. insetBy ( dx: 1 , dy: 1 ) . offsetBy ( dx: 0 , dy: 25 )
909+ emoji. frame = bounds. insetBy ( dx: 1 , dy: 1 ) . offsetBy ( dx: 0 , dy: 33 )
858910 }
859911
860912 surfaceView. frame = bounds. insetBy ( dx: 1 , dy: 1 )
@@ -872,6 +924,15 @@ private final class GiftOptionsRowView: GeneralContainableRowView {
872924 premiumSelectView. frame = bounds. insetBy ( dx: 1 , dy: 1 )
873925 }
874926
927+ if let imageView {
928+ imageView. centerX ( y: 20 )
929+ if let textView {
930+ textView. centerX ( y: frame. height - textView. frame. height - 20 )
931+ }
932+ } else {
933+ textView? . centerX ( y: sticker. frame. maxY + 5 )
934+ }
935+
875936
876937 if let priceBadgeView {
877938 priceBadgeView. centerX ( y: frame. height - priceBadgeView. frame. height - 10 )
@@ -952,7 +1013,7 @@ private final class GiftOptionsRowView: GeneralContainableRowView {
9521013 itemSize = NSMakeSize ( content. frame. width - item. inset. right - item. inset. left, content. frame. height)
9531014 } else {
9541015 let insets = item. inset
955- let count = item. fitToSize ? CGFloat ( item. options. count) : 3
1016+ let count = item. fitToSize ? CGFloat ( item. options. count) : CGFloat ( item . perRowCount )
9561017 let space = content. frame. width - insets. left - insets. right - ( 10 * CGFloat( count - 1 ) )
9571018 itemSize = NSMakeSize ( floor ( space / count) , content. frame. height)
9581019 }
0 commit comments