@@ -138,11 +138,11 @@ public final class PeerInfoGiftsCoverComponent: Component {
138138
139139 private var giftsDisposable : Disposable ?
140140 private var gifts : [ ProfileGiftsContext . State . StarGift ] = [ ]
141+ private var appliedGiftIds : [ Int64 ] = [ ]
141142
142143 private var iconLayers : [ AnyHashable : GiftIconLayer ] = [ : ]
143144
144145 private var iconPositions : [ PositionGenerator . Position ] = [ ]
145- private let seed = UInt ( Date ( ) . timeIntervalSince1970)
146146
147147 override public init ( frame: CGRect ) {
148148 self . avatarBackgroundGradientLayer = SimpleGradientLayer ( )
@@ -185,6 +185,7 @@ public final class PeerInfoGiftsCoverComponent: Component {
185185 self . isUpdating = false
186186 }
187187
188+ let previousComponent = self . component
188189 self . component = component
189190 self . state = state
190191
@@ -193,7 +194,15 @@ public final class PeerInfoGiftsCoverComponent: Component {
193194
194195 let iconSize = CGSize ( width: 32.0 , height: 32.0 )
195196
196- if previousCurrentSize? . width != availableSize. width {
197+ let giftIds = self . gifts. map { gift in
198+ if case let . unique( gift) = gift. gift {
199+ return gift. id
200+ } else {
201+ return 0
202+ }
203+ }
204+
205+ if previousCurrentSize? . width != availableSize. width || ( previousComponent != nil && previousComponent? . hasBackground != component. hasBackground) || self . appliedGiftIds != giftIds {
197206 var excludeRects : [ CGRect ] = [ ]
198207 excludeRects. append ( CGRect ( origin: . zero, size: CGSize ( width: 50.0 , height: 90.0 ) ) )
199208 excludeRects. append ( CGRect ( origin: CGPoint ( x: availableSize. width - 105.0 , y: 0.0 ) , size: CGSize ( width: 105.0 , height: 90.0 ) ) )
@@ -209,11 +218,12 @@ public final class PeerInfoGiftsCoverComponent: Component {
209218 minDistance: 75.0 ,
210219 maxDistance: availableSize. width / 2.0 ,
211220 padding: 12.0 ,
212- seed: self . seed ,
221+ seed: UInt ( Date ( ) . timeIntervalSince1970 ) ,
213222 excludeRects: excludeRects
214223 )
215224 self . iconPositions = positionGenerator. generatePositions ( count: 9 , viewSize: iconSize)
216225 }
226+ self . appliedGiftIds = giftIds
217227
218228 if self . giftsDisposable == nil {
219229 self . giftsDisposable = combineLatest (
@@ -243,7 +253,7 @@ public final class PeerInfoGiftsCoverComponent: Component {
243253 self . gifts = pinnedGifts
244254
245255 if !self . isUpdating {
246- self . state? . updated ( transition: . immediate )
256+ self . state? . updated ( transition: . spring ( duration : 0.4 ) )
247257 }
248258 } )
249259 }
@@ -271,11 +281,13 @@ public final class PeerInfoGiftsCoverComponent: Component {
271281 }
272282 validIds. insert ( id)
273283
284+ var iconTransition = transition
274285 let iconPosition = self . iconPositions [ index]
275286 let iconLayer : GiftIconLayer
276287 if let current = self . iconLayers [ id] {
277288 iconLayer = current
278289 } else {
290+ iconTransition = . immediate
279291 iconLayer = GiftIconLayer ( context: component. context, gift: gift, size: iconSize, glowing: component. hasBackground)
280292 iconLayer. startHovering ( )
281293 self . iconLayers [ id] = iconLayer
@@ -284,6 +296,7 @@ public final class PeerInfoGiftsCoverComponent: Component {
284296 iconLayer. animateAlpha ( from: 0.0 , to: 1.0 , duration: 0.2 )
285297 iconLayer. animateScale ( from: 0.01 , to: 1.0 , duration: 0.2 )
286298 }
299+ iconLayer. glowing = component. hasBackground
287300
288301 let centerPosition = component. avatarCenter
289302 let finalPosition = iconPosition. center. offsetBy ( dx: component. avatarCenter. x, dy: component. avatarCenter. y)
@@ -303,10 +316,10 @@ public final class PeerInfoGiftsCoverComponent: Component {
303316
304317 let effectivePosition = interpolateRect ( from: finalPosition, to: centerPosition, t: itemScaleFraction)
305318
306- transition . setBounds ( layer: iconLayer, bounds: CGRect ( origin: . zero, size: iconSize) )
307- transition . setPosition ( layer: iconLayer, position: effectivePosition)
308- transition . setScale ( layer: iconLayer, scale: iconPosition. scale * ( 1.0 - itemScaleFraction) )
309- transition . setAlpha ( layer: iconLayer, alpha: 1.0 - itemScaleFraction)
319+ iconTransition . setBounds ( layer: iconLayer, bounds: CGRect ( origin: . zero, size: iconSize) )
320+ iconTransition . setPosition ( layer: iconLayer, position: effectivePosition)
321+ iconTransition . setScale ( layer: iconLayer, scale: iconPosition. scale * ( 1.0 - itemScaleFraction) )
322+ iconTransition . setAlpha ( layer: iconLayer, alpha: 1.0 - itemScaleFraction)
310323
311324 index += 1
312325 }
@@ -434,7 +447,7 @@ private class PositionGenerator {
434447 let distance = hypot ( result. x - self . avatarFrame. center. x, result. y - self . avatarFrame. center. y)
435448 let baseScale = min ( 1.0 , max ( 0.77 , 1.0 - ( distance - 75.0 ) / 75.0 ) )
436449
437- let randomFactor = 0.05 + ( 1.0 - baseScale) * 0.1
450+ let randomFactor = 0.14 + ( 1.0 - baseScale) * 0.2
438451 let randomValue = - randomFactor + CGFloat( self . rng. next ( ) ) * 2.0 * randomFactor
439452
440453 let finalScale = min ( 1.2 , max ( baseScale * 0.65 , baseScale + randomValue) )
@@ -563,6 +576,8 @@ private final class StarsEffectLayer: SimpleLayer {
563576 }
564577
565578 func setup( color: UIColor , size: CGSize ) {
579+ self . color = color
580+
566581 let emitter = CAEmitterCell ( )
567582 emitter. name = " emitter "
568583 emitter. contents = UIImage ( bundleImageName: " Premium/Stars/Particle " ) ? . cgImage
@@ -586,8 +601,10 @@ private final class StarsEffectLayer: SimpleLayer {
586601 self . emitterLayer. emitterCells = [ emitter]
587602 }
588603
604+ private var color : UIColor ?
605+
589606 func update( color: UIColor , size: CGSize ) {
590- if self . emitterLayer . emitterCells == nil {
607+ if self . color != color {
591608 self . setup ( color: color, size: size)
592609 }
593610 self . emitterLayer. seed = UInt32 . random ( in: . min ..< . max)
@@ -604,7 +621,25 @@ private class GiftIconLayer: SimpleLayer {
604621 private let context : AccountContext
605622 private let gift : ProfileGiftsContext . State . StarGift
606623 private let size : CGSize
607- private let glowing : Bool
624+ var glowing : Bool {
625+ didSet {
626+ self . shadowLayer. opacity = self . glowing ? 1.0 : 0.0
627+
628+ let color : UIColor
629+ if self . glowing {
630+ color = . white
631+ } else if let layerTintColor = self . shadowLayer. layerTintColor {
632+ color = UIColor ( cgColor: layerTintColor)
633+ } else {
634+ color = . white
635+ }
636+
637+ let side = floor ( self . size. width * 1.25 )
638+ let starsFrame = CGSize ( width: side, height: side) . centered ( in: CGRect ( origin: . zero, size: self . size) )
639+ self . starsLayer. frame = starsFrame
640+ self . starsLayer. update ( color: color, size: starsFrame. size)
641+ }
642+ }
608643
609644 let shadowLayer = SimpleLayer ( )
610645 let starsLayer = StarsEffectLayer ( )
@@ -656,6 +691,7 @@ private class GiftIconLayer: SimpleLayer {
656691
657692 self . shadowLayer. contents = shadowImage? . cgImage
658693 self . shadowLayer. layerTintColor = color. cgColor
694+ self . shadowLayer. opacity = glowing ? 1.0 : 0.0
659695
660696 self . context = context
661697 self . gift = gift
@@ -669,9 +705,7 @@ private class GiftIconLayer: SimpleLayer {
669705 self . starsLayer. frame = starsFrame
670706 self . starsLayer. update ( color: glowing ? . white : color, size: starsFrame. size)
671707
672- if glowing {
673- self . addSublayer ( self . shadowLayer)
674- }
708+ self . addSublayer ( self . shadowLayer)
675709 self . addSublayer ( self . starsLayer)
676710 self . addSublayer ( self . animationLayer)
677711 }
@@ -723,6 +757,7 @@ private class GiftIconLayer: SimpleLayer {
723757
724758 self . shadowLayer. contents = shadowImage? . cgImage
725759 self . shadowLayer. layerTintColor = color. cgColor
760+ self . shadowLayer. opacity = glowing ? 1.0 : 0.0
726761
727762 super. init ( )
728763
@@ -731,9 +766,7 @@ private class GiftIconLayer: SimpleLayer {
731766 self . starsLayer. frame = starsFrame
732767 self . starsLayer. update ( color: glowing ? . white : color, size: starsFrame. size)
733768
734- if glowing {
735- self . addSublayer ( self . shadowLayer)
736- }
769+ self . addSublayer ( self . shadowLayer)
737770 self . addSublayer ( self . starsLayer)
738771 self . addSublayer ( self . animationLayer)
739772 }
0 commit comments