diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Payments/StarGifts.swift b/submodules/TelegramCore/Sources/TelegramEngine/Payments/StarGifts.swift index da54971bd6..959d84f92b 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Payments/StarGifts.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Payments/StarGifts.swift @@ -1648,6 +1648,22 @@ private final class ProfileGiftsContextImpl { self.pushState() } + func insertStarGifts(gifts: [ProfileGiftsContext.State.StarGift]) { + self.gifts.insert(contentsOf: gifts, at: 0) + self.pushState() + } + + func removeStarGifts(references: [StarGiftReference]) { + self.gifts.removeAll(where: { + if let reference = $0.reference { + return references.contains(reference) + } else { + return false + } + }) + self.pushState() + } + func upgradeStarGift(formId: Int64?, reference: StarGiftReference, keepOriginalInfo: Bool) -> Signal { return Signal { [weak self] subscriber in guard let self else { @@ -2074,6 +2090,9 @@ public final class ProfileGiftsContext { } } + public let peerId: EnginePeer.Id + public let collectionId: Int32? + public init( account: Account, peerId: EnginePeer.Id, @@ -2081,6 +2100,9 @@ public final class ProfileGiftsContext { sorting: ProfileGiftsContext.Sorting = .date, filter: ProfileGiftsContext.Filters = .All ) { + self.peerId = peerId + self.collectionId = collectionId + let queue = self.queue self.impl = QueueLocalObject(queue: queue, generate: { return ProfileGiftsContextImpl(queue: queue, account: account, peerId: peerId, collectionId: collectionId, sorting: sorting, filter: filter) @@ -2143,6 +2165,18 @@ public final class ProfileGiftsContext { } } + public func insertStarGifts(gifts: [ProfileGiftsContext.State.StarGift]) { + self.impl.with { impl in + impl.insertStarGifts(gifts: gifts) + } + } + + public func removeStarGifts(references: [StarGiftReference]) { + self.impl.with { impl in + impl.removeStarGifts(references: references) + } + } + public func transferStarGift(prepaid: Bool, reference: StarGiftReference, peerId: EnginePeer.Id) -> Signal { return Signal { subscriber in let disposable = MetaDisposable() diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Payments/StarGiftsCollections.swift b/submodules/TelegramCore/Sources/TelegramEngine/Payments/StarGiftsCollections.swift index 441244bde9..bed46ee260 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Payments/StarGiftsCollections.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Payments/StarGiftsCollections.swift @@ -183,7 +183,20 @@ private func _internal_reorderStarGiftCollections(account: Account, peerId: Engi } } -private func _internal_updateStarGiftCollection(account: Account, peerId: EnginePeer.Id, collectionId: Int32, actions: [StarGiftCollectionsContext.UpdateAction]) -> Signal { +private func _internal_updateStarGiftCollection(account: Account, peerId: EnginePeer.Id, collectionId: Int32, giftsContext: ProfileGiftsContext?, actions: [ProfileGiftsCollectionsContext.UpdateAction]) -> Signal { + for action in actions { + switch action { + case let .addGifts(gifts): + giftsContext?.insertStarGifts(gifts: gifts) + case let .removeGifts(gifts): + giftsContext?.removeStarGifts(references: gifts) + case let .reorderGifts(gifts): + let _ = gifts + default: + break + } + } + return account.postbox.transaction { transaction -> (Api.InputPeer, (FunctionDescription, Buffer, DeserializeFunctionResponse))? in guard let inputPeer = transaction.getPeer(peerId).flatMap(apiInputPeer) else { return nil @@ -202,7 +215,7 @@ private func _internal_updateStarGiftCollection(account: Account, peerId: Engine title = newTitle case let .addGifts(gifts): flags |= (1 << 2) - addStarGift.append(contentsOf: gifts.compactMap { $0.apiStarGiftReference(transaction: transaction) }) + addStarGift.append(contentsOf: gifts.compactMap { $0.reference }.compactMap { $0.apiStarGiftReference(transaction: transaction) }) case let .removeGifts(gifts): flags |= (1 << 1) deleteStarGift.append(contentsOf: gifts.compactMap { $0.apiStarGiftReference(transaction: transaction) }) @@ -258,7 +271,7 @@ private func _internal_deleteStarGiftCollection(account: Account, peerId: Engine } } -public final class StarGiftCollectionsContext { +public final class ProfileGiftsCollectionsContext { public struct State: Equatable { public var collections: [StarGiftCollection] public var isLoading: Bool @@ -266,7 +279,7 @@ public final class StarGiftCollectionsContext { public enum UpdateAction { case updateTitle(String) - case addGifts([StarGiftReference]) + case addGifts([ProfileGiftsContext.State.StarGift]) case removeGifts([StarGiftReference]) case reorderGifts([StarGiftReference]) } @@ -278,6 +291,7 @@ public final class StarGiftCollectionsContext { private let disposable = MetaDisposable() private var collections: [StarGiftCollection] = [] + private var giftsContexts: [Int32: ProfileGiftsContext] = [:] private var isLoading: Bool = false private let stateValue = Promise() @@ -296,6 +310,16 @@ public final class StarGiftCollectionsContext { self.disposable.dispose() } + public func giftsContextForCollection(id: Int32) -> ProfileGiftsContext { + if let current = self.giftsContexts[id] { + return current + } else { + let giftsContext = ProfileGiftsContext(account: self.account, peerId: self.peerId, collectionId: id) + self.giftsContexts[id] = giftsContext + return giftsContext + } + } + public func reload() { guard !self.isLoading else { return } @@ -329,7 +353,8 @@ public final class StarGiftCollectionsContext { } private func updateCollection(id: Int32, actions: [UpdateAction]) -> Signal { - return _internal_updateStarGiftCollection(account: self.account, peerId: self.peerId, collectionId: id, actions: actions) + let giftsContext = self.giftsContextForCollection(id: id) + return _internal_updateStarGiftCollection(account: self.account, peerId: self.peerId, collectionId: id, giftsContext: giftsContext, actions: actions) |> deliverOn(self.queue) |> afterNext { [weak self] collection in guard let self else { @@ -345,12 +370,12 @@ public final class StarGiftCollectionsContext { } } - public func addGifts(id: Int32, gifts: [StarGiftReference]) -> Signal { + public func addGifts(id: Int32, gifts: [ProfileGiftsContext.State.StarGift]) -> Signal { return self.updateCollection(id: id, actions: [.addGifts(gifts)]) } public func removeGifts(id: Int32, gifts: [StarGiftReference]) -> Signal { - return self.updateCollection(id: id, actions: [.addGifts(gifts)]) + return self.updateCollection(id: id, actions: [.removeGifts(gifts)]) } public func reorderGifts(id: Int32, gifts: [StarGiftReference]) -> Signal { @@ -391,6 +416,7 @@ public final class StarGiftCollectionsContext { guard let self else { return } + self.giftsContexts.removeValue(forKey: id) self.collections.removeAll(where: { $0.id == id }) self.pushState() self.reload()