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)