Update API

This commit is contained in:
Ilya Laktyushin 2025-07-15 14:01:58 +01:00
parent 3547bb0f2a
commit ef2d93c5a0
2 changed files with 67 additions and 7 deletions

View File

@ -1648,6 +1648,22 @@ private final class ProfileGiftsContextImpl {
self.pushState() 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<ProfileGiftsContext.State.StarGift, UpgradeStarGiftError> { func upgradeStarGift(formId: Int64?, reference: StarGiftReference, keepOriginalInfo: Bool) -> Signal<ProfileGiftsContext.State.StarGift, UpgradeStarGiftError> {
return Signal { [weak self] subscriber in return Signal { [weak self] subscriber in
guard let self else { guard let self else {
@ -2074,6 +2090,9 @@ public final class ProfileGiftsContext {
} }
} }
public let peerId: EnginePeer.Id
public let collectionId: Int32?
public init( public init(
account: Account, account: Account,
peerId: EnginePeer.Id, peerId: EnginePeer.Id,
@ -2081,6 +2100,9 @@ public final class ProfileGiftsContext {
sorting: ProfileGiftsContext.Sorting = .date, sorting: ProfileGiftsContext.Sorting = .date,
filter: ProfileGiftsContext.Filters = .All filter: ProfileGiftsContext.Filters = .All
) { ) {
self.peerId = peerId
self.collectionId = collectionId
let queue = self.queue let queue = self.queue
self.impl = QueueLocalObject(queue: queue, generate: { self.impl = QueueLocalObject(queue: queue, generate: {
return ProfileGiftsContextImpl(queue: queue, account: account, peerId: peerId, collectionId: collectionId, sorting: sorting, filter: filter) 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<Never, TransferStarGiftError> { public func transferStarGift(prepaid: Bool, reference: StarGiftReference, peerId: EnginePeer.Id) -> Signal<Never, TransferStarGiftError> {
return Signal { subscriber in return Signal { subscriber in
let disposable = MetaDisposable() let disposable = MetaDisposable()

View File

@ -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<StarGiftCollection?, NoError> { private func _internal_updateStarGiftCollection(account: Account, peerId: EnginePeer.Id, collectionId: Int32, giftsContext: ProfileGiftsContext?, actions: [ProfileGiftsCollectionsContext.UpdateAction]) -> Signal<StarGiftCollection?, NoError> {
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<Api.StarGiftCollection>))? in return account.postbox.transaction { transaction -> (Api.InputPeer, (FunctionDescription, Buffer, DeserializeFunctionResponse<Api.StarGiftCollection>))? in
guard let inputPeer = transaction.getPeer(peerId).flatMap(apiInputPeer) else { guard let inputPeer = transaction.getPeer(peerId).flatMap(apiInputPeer) else {
return nil return nil
@ -202,7 +215,7 @@ private func _internal_updateStarGiftCollection(account: Account, peerId: Engine
title = newTitle title = newTitle
case let .addGifts(gifts): case let .addGifts(gifts):
flags |= (1 << 2) 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): case let .removeGifts(gifts):
flags |= (1 << 1) flags |= (1 << 1)
deleteStarGift.append(contentsOf: gifts.compactMap { $0.apiStarGiftReference(transaction: transaction) }) 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 struct State: Equatable {
public var collections: [StarGiftCollection] public var collections: [StarGiftCollection]
public var isLoading: Bool public var isLoading: Bool
@ -266,7 +279,7 @@ public final class StarGiftCollectionsContext {
public enum UpdateAction { public enum UpdateAction {
case updateTitle(String) case updateTitle(String)
case addGifts([StarGiftReference]) case addGifts([ProfileGiftsContext.State.StarGift])
case removeGifts([StarGiftReference]) case removeGifts([StarGiftReference])
case reorderGifts([StarGiftReference]) case reorderGifts([StarGiftReference])
} }
@ -278,6 +291,7 @@ public final class StarGiftCollectionsContext {
private let disposable = MetaDisposable() private let disposable = MetaDisposable()
private var collections: [StarGiftCollection] = [] private var collections: [StarGiftCollection] = []
private var giftsContexts: [Int32: ProfileGiftsContext] = [:]
private var isLoading: Bool = false private var isLoading: Bool = false
private let stateValue = Promise<State>() private let stateValue = Promise<State>()
@ -296,6 +310,16 @@ public final class StarGiftCollectionsContext {
self.disposable.dispose() 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() { public func reload() {
guard !self.isLoading else { return } guard !self.isLoading else { return }
@ -329,7 +353,8 @@ public final class StarGiftCollectionsContext {
} }
private func updateCollection(id: Int32, actions: [UpdateAction]) -> Signal<StarGiftCollection?, NoError> { private func updateCollection(id: Int32, actions: [UpdateAction]) -> Signal<StarGiftCollection?, NoError> {
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) |> deliverOn(self.queue)
|> afterNext { [weak self] collection in |> afterNext { [weak self] collection in
guard let self else { guard let self else {
@ -345,12 +370,12 @@ public final class StarGiftCollectionsContext {
} }
} }
public func addGifts(id: Int32, gifts: [StarGiftReference]) -> Signal<StarGiftCollection?, NoError> { public func addGifts(id: Int32, gifts: [ProfileGiftsContext.State.StarGift]) -> Signal<StarGiftCollection?, NoError> {
return self.updateCollection(id: id, actions: [.addGifts(gifts)]) return self.updateCollection(id: id, actions: [.addGifts(gifts)])
} }
public func removeGifts(id: Int32, gifts: [StarGiftReference]) -> Signal<StarGiftCollection?, NoError> { public func removeGifts(id: Int32, gifts: [StarGiftReference]) -> Signal<StarGiftCollection?, NoError> {
return self.updateCollection(id: id, actions: [.addGifts(gifts)]) return self.updateCollection(id: id, actions: [.removeGifts(gifts)])
} }
public func reorderGifts(id: Int32, gifts: [StarGiftReference]) -> Signal<StarGiftCollection?, NoError> { public func reorderGifts(id: Int32, gifts: [StarGiftReference]) -> Signal<StarGiftCollection?, NoError> {
@ -391,6 +416,7 @@ public final class StarGiftCollectionsContext {
guard let self else { guard let self else {
return return
} }
self.giftsContexts.removeValue(forKey: id)
self.collections.removeAll(where: { $0.id == id }) self.collections.removeAll(where: { $0.id == id })
self.pushState() self.pushState()
self.reload() self.reload()