From 7db42376bdd4b0a609c91856cb459f06ed108c93 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Tue, 29 Oct 2024 19:31:50 +0400 Subject: [PATCH] Varios fixes --- .../TelegramEngine/Payments/Stars.swift | 58 +++++++++++++++++-- .../Sources/StarsStatisticsScreen.swift | 4 +- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Payments/Stars.swift b/submodules/TelegramCore/Sources/TelegramEngine/Payments/Stars.swift index ecd4e581fb..48ea7ace5f 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Payments/Stars.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Payments/Stars.swift @@ -878,10 +878,10 @@ public final class StarsContext { private final class StarsTransactionsContextImpl { private let account: Account private weak var starsContext: StarsContext? - private let peerId: EnginePeer.Id + fileprivate let peerId: EnginePeer.Id private let mode: StarsTransactionsContext.Mode - private var _state: StarsTransactionsContext.State + fileprivate var _state: StarsTransactionsContext.State private let _statePromise = Promise() var state: Signal { return self._statePromise.get() @@ -894,17 +894,24 @@ private final class StarsTransactionsContextImpl { init(account: Account, subject: StarsTransactionsContext.Subject, mode: StarsTransactionsContext.Mode) { assert(Queue.mainQueue().isCurrent()) + + let currentTransactions: [StarsContext.State.Transaction] + self.account = account switch subject { + case let .starsTransactionsContext(transactionsContext): + self.peerId = transactionsContext.peerId + currentTransactions = transactionsContext.currentState?.transactions ?? [] case let .starsContext(starsContext): self.starsContext = starsContext self.peerId = starsContext.peerId + currentTransactions = starsContext.currentState?.transactions ?? [] case let .peer(peerId): self.peerId = peerId + currentTransactions = [] } self.mode = mode - let currentTransactions = self.starsContext?.currentState?.transactions ?? [] let initialTransactions: [StarsContext.State.Transaction] switch mode { case .all: @@ -918,7 +925,33 @@ private final class StarsTransactionsContextImpl { self._state = StarsTransactionsContext.State(transactions: initialTransactions, canLoadMore: true, isLoading: false) self._statePromise.set(.single(self._state)) - if let starsContext = self.starsContext { + if case let .starsTransactionsContext(transactionsContext) = subject { + self.stateDisposable = (transactionsContext.state + |> deliverOnMainQueue).start(next: { [weak self] state in + guard let self else { + return + } + let currentTransactions = state.transactions + let filteredTransactions: [StarsContext.State.Transaction] + switch mode { + case .all: + filteredTransactions = currentTransactions + case .incoming: + filteredTransactions = currentTransactions.filter { $0.count > 0 } + case .outgoing: + filteredTransactions = currentTransactions.filter { $0.count < 0 } + } + + if !filteredTransactions.isEmpty && self._state.transactions.isEmpty && filteredTransactions != initialTransactions { + var updatedState = self._state + updatedState.transactions.removeAll(where: { $0.flags.contains(.isLocal) }) + for transaction in filteredTransactions.reversed() { + updatedState.transactions.insert(transaction, at: 0) + } + self.updateState(updatedState) + } + }) + } else if case let .starsContext(starsContext) = subject { self.stateDisposable = (starsContext.state |> deliverOnMainQueue).start(next: { [weak self] state in guard let self, let state else { @@ -1021,6 +1054,7 @@ public final class StarsTransactionsContext { fileprivate let impl: QueueLocalObject public enum Subject { + case starsTransactionsContext(StarsTransactionsContext) case starsContext(StarsContext) case peer(EnginePeer.Id) } @@ -1043,6 +1077,14 @@ public final class StarsTransactionsContext { } } + public var currentState: StarsTransactionsContext.State? { + var state: StarsTransactionsContext.State? + self.impl.syncWith { impl in + state = impl._state + } + return state + } + public func reload() { self.impl.with { $0.loadMore(reload: true) @@ -1060,6 +1102,14 @@ public final class StarsTransactionsContext { return StarsTransactionsContextImpl(account: account, subject: subject, mode: mode) }) } + + var peerId: EnginePeer.Id { + var peerId: EnginePeer.Id? + self.impl.syncWith { impl in + peerId = impl.peerId + } + return peerId! + } } private final class StarsSubscriptionsContextImpl { diff --git a/submodules/TelegramUI/Components/Stars/StarsTransactionsScreen/Sources/StarsStatisticsScreen.swift b/submodules/TelegramUI/Components/Stars/StarsTransactionsScreen/Sources/StarsStatisticsScreen.swift index e3df2ee96a..6a883c8df1 100644 --- a/submodules/TelegramUI/Components/Stars/StarsTransactionsScreen/Sources/StarsStatisticsScreen.swift +++ b/submodules/TelegramUI/Components/Stars/StarsTransactionsScreen/Sources/StarsStatisticsScreen.swift @@ -663,7 +663,7 @@ final class StarsStatisticsScreenComponent: Component { if let current = self.incomingTransactionsContext { incomingTransactionsContext = current } else { - incomingTransactionsContext = component.context.engine.payments.peerStarsTransactionsContext(subject: .peer(component.peerId), mode: .incoming) + incomingTransactionsContext = component.context.engine.payments.peerStarsTransactionsContext(subject: .starsTransactionsContext(allTransactionsContext), mode: .incoming) self.incomingTransactionsContext = incomingTransactionsContext } @@ -671,7 +671,7 @@ final class StarsStatisticsScreenComponent: Component { if let current = self.outgoingTransactionsContext { outgoingTransactionsContext = current } else { - outgoingTransactionsContext = component.context.engine.payments.peerStarsTransactionsContext(subject: .peer(component.peerId), mode: .outgoing) + outgoingTransactionsContext = component.context.engine.payments.peerStarsTransactionsContext(subject: .starsTransactionsContext(allTransactionsContext), mode: .outgoing) self.outgoingTransactionsContext = outgoingTransactionsContext }