Birthday gifts

This commit is contained in:
Ilya Laktyushin 2024-11-15 16:18:32 +04:00
parent 10984d7ce4
commit 30a85d1fe1
3 changed files with 45 additions and 26 deletions

View File

@ -35,6 +35,7 @@ final class GiftOptionsScreenComponent: Component {
let starsContext: StarsContext
let peerId: EnginePeer.Id
let premiumOptions: [CachedPremiumGiftOption]
let hasBirthday: Bool
let completion: (() -> Void)?
init(
@ -42,12 +43,14 @@ final class GiftOptionsScreenComponent: Component {
starsContext: StarsContext,
peerId: EnginePeer.Id,
premiumOptions: [CachedPremiumGiftOption],
hasBirthday: Bool,
completion: (() -> Void)?
) {
self.context = context
self.starsContext = starsContext
self.peerId = peerId
self.premiumOptions = premiumOptions
self.hasBirthday = hasBirthday
self.completion = completion
}
@ -61,6 +64,9 @@ final class GiftOptionsScreenComponent: Component {
if lhs.premiumOptions != rhs.premiumOptions {
return false
}
if lhs.hasBirthday != rhs.hasBirthday {
return false
}
return true
}
@ -127,32 +133,43 @@ final class GiftOptionsScreenComponent: Component {
private var _effectiveStarGifts: ([StarGift], StarsFilter)?
private var effectiveStarGifts: [StarGift]? {
get {
if case .all = self.starsFilter {
return self.state?.starGifts
} else {
if let (currentGifts, currentFilter) = self._effectiveStarGifts, currentFilter == self.starsFilter {
return currentGifts
} else if let allGifts = self.state?.starGifts {
let filteredGifts: [StarGift] = allGifts.filter {
switch self.starsFilter {
case .all:
return true
case .limited:
if $0.availability != nil {
return true
}
case let .stars(stars):
if $0.price == stars {
return true
}
if let (currentGifts, currentFilter) = self._effectiveStarGifts, currentFilter == self.starsFilter {
return currentGifts
} else if let allGifts = self.state?.starGifts {
var sortedGifts = allGifts
if self.component?.hasBirthday == true {
var updatedGifts: [StarGift] = []
for gift in allGifts {
if gift.flags.contains(.isBirthdayGift) {
updatedGifts.append(gift)
}
return false
}
self._effectiveStarGifts = (filteredGifts, self.starsFilter)
return filteredGifts
} else {
return nil
for gift in allGifts {
if !gift.flags.contains(.isBirthdayGift) {
updatedGifts.append(gift)
}
}
sortedGifts = updatedGifts
}
let filteredGifts: [StarGift] = sortedGifts.filter {
switch self.starsFilter {
case .all:
return true
case .limited:
if $0.availability != nil {
return true
}
case let .stars(stars):
if $0.price == stars {
return true
}
}
return false
}
self._effectiveStarGifts = (filteredGifts, self.starsFilter)
return filteredGifts
} else {
return nil
}
}
}
@ -1073,6 +1090,7 @@ open class GiftOptionsScreen: ViewControllerComponentContainer, GiftOptionsScree
starsContext: StarsContext,
peerId: EnginePeer.Id,
premiumOptions: [CachedPremiumGiftOption],
hasBirthday: Bool,
completion: (() -> Void)? = nil
) {
self.context = context
@ -1082,6 +1100,7 @@ open class GiftOptionsScreen: ViewControllerComponentContainer, GiftOptionsScree
starsContext: starsContext,
peerId: peerId,
premiumOptions: premiumOptions,
hasBirthday: hasBirthday,
completion: completion
), navigationBarAppearance: .none, theme: .default, updatedPresentationData: nil)

View File

@ -591,7 +591,7 @@ extension ChatControllerImpl {
if let peer = strongSelf.presentationInterfaceState.renderedPeer?.peer, let starsContext = context.starsContext {
let premiumGiftOptions = strongSelf.presentationInterfaceState.premiumGiftOptions
if !premiumGiftOptions.isEmpty {
let controller = PremiumGiftAttachmentScreen(context: context, starsContext: starsContext, peerId: peer.id, premiumOptions: premiumGiftOptions, completion: { [weak self] in
let controller = PremiumGiftAttachmentScreen(context: context, starsContext: starsContext, peerId: peer.id, premiumOptions: premiumGiftOptions, hasBirthday: true, completion: { [weak self] in
guard let self else {
return
}

View File

@ -2336,7 +2336,7 @@ public final class SharedAccountContextImpl: SharedAccountContext {
.startStandalone(next: { [weak controller] result, options in
if let (peers, _, _, _, _, _) = result, let contactPeer = peers.first, case let .peer(peer, _, _) = contactPeer, let starsContext = context.starsContext {
let premiumOptions = options.filter { $0.users == 1 }.map { CachedPremiumGiftOption(months: $0.months, currency: $0.currency, amount: $0.amount, botUrl: "", storeProductId: $0.storeProductId) }
let giftController = GiftOptionsScreen(context: context, starsContext: starsContext, peerId: peer.id, premiumOptions: premiumOptions)
let giftController = GiftOptionsScreen(context: context, starsContext: starsContext, peerId: peer.id, premiumOptions: premiumOptions, hasBirthday: true)
giftController.navigationPresentation = .modal
controller?.push(giftController)
}
@ -2406,7 +2406,7 @@ public final class SharedAccountContextImpl: SharedAccountContext {
guard let starsContext = context.starsContext else {
fatalError()
}
let controller = GiftOptionsScreen(context: context, starsContext: starsContext, peerId: peerId, premiumOptions: premiumOptions)
let controller = GiftOptionsScreen(context: context, starsContext: starsContext, peerId: peerId, premiumOptions: premiumOptions, hasBirthday: false)
controller.navigationPresentation = .modal
return controller
}