diff --git a/submodules/TelegramCore/Sources/State/AccountStateManager.swift b/submodules/TelegramCore/Sources/State/AccountStateManager.swift index f2f7f5321f..1ae6baf6ea 100644 --- a/submodules/TelegramCore/Sources/State/AccountStateManager.swift +++ b/submodules/TelegramCore/Sources/State/AccountStateManager.swift @@ -292,6 +292,11 @@ public final class AccountStateManager { return self.botPreviewUpdatesPipe.signal() } + fileprivate let forceSendPendingStarsReactionPipe = ValuePipe() + public var forceSendPendingStarsReaction: Signal { + return self.forceSendPendingStarsReactionPipe.signal() + } + private var updatedWebpageContexts: [MediaId: UpdatedWebpageSubscriberContext] = [:] private var updatedPeersNearbyContext = UpdatedPeersNearbySubscriberContext() private var updatedRevenueBalancesContext = UpdatedRevenueBalancesSubscriberContext() @@ -1873,11 +1878,25 @@ public final class AccountStateManager { } } + var forceSendPendingStarsReaction: Signal { + return self.impl.signalWith { impl, subscriber in + return impl.forceSendPendingStarsReaction.start(next: subscriber.putNext, error: subscriber.putError, completed: subscriber.putCompletion) + } + } + + func forceSendPendingStarsReaction(messageId: MessageId) { + self.impl.with { impl in + impl.forceSendPendingStarsReactionPipe.putNext(messageId) + } + } + var updateConfigRequested: (() -> Void)? var isPremiumUpdated: (() -> Void)? let messagesRemovedContext = MessagesRemovedContext() + public weak var starsContext: StarsContext? + init( accountPeerId: PeerId, accountManager: AccountManager, diff --git a/submodules/TelegramCore/Sources/State/MessageReactions.swift b/submodules/TelegramCore/Sources/State/MessageReactions.swift index 97ea3f2d53..9aa3c8f31a 100644 --- a/submodules/TelegramCore/Sources/State/MessageReactions.swift +++ b/submodules/TelegramCore/Sources/State/MessageReactions.swift @@ -220,6 +220,12 @@ func cancelPendingSendStarsReactionInteractively(account: Account, messageId: Me |> ignoreValues } +func _internal_forceSendPendingSendStarsReaction(account: Account, messageId: MessageId) -> Signal { + account.stateManager.forceSendPendingStarsReaction(messageId: messageId) + + return .complete() +} + func _internal_updateStarsReactionIsAnonymous(account: Account, messageId: MessageId, isAnonymous: Bool) -> Signal { return account.postbox.transaction { transaction -> Api.InputPeer? in transaction.updateMessage(messageId, update: { currentMessage in @@ -388,6 +394,9 @@ private func requestSendStarsReaction(postbox: Postbox, network: Network, stateM return .generic } |> mapToSignal { result -> Signal in + stateManager.starsContext?.add(balance: Int64(-count), addTransaction: false) + //stateManager.starsContext?.load(force: true) + return postbox.transaction { transaction -> Void in transaction.setPendingMessageAction(type: .sendStarsReaction, id: messageId, action: UpdateMessageReactionsAction()) transaction.updateMessage(messageId, update: { currentMessage in @@ -568,8 +577,20 @@ func managedApplyPendingMessageStarsReactionsActions(postbox: Postbox, network: let signal = withTakenStarsAction(postbox: postbox, type: .sendStarsReaction, id: entry.id, { transaction, entry -> Signal in if let entry = entry { if let _ = entry.action as? SendStarsReactionsAction { - return synchronizeMessageStarsReactions(transaction: transaction, postbox: postbox, network: network, stateManager: stateManager, id: entry.id) - |> delay(5.0, queue: .mainQueue()) + let triggerSignal: Signal = stateManager.forceSendPendingStarsReaction + |> filter { + $0 == entry.id + } + |> map { _ -> Void in + return Void() + } + |> take(1) + |> timeout(5.0, queue: .mainQueue(), alternate: .single(Void())) + + return triggerSignal + |> mapToSignal { _ -> Signal in + return synchronizeMessageStarsReactions(transaction: transaction, postbox: postbox, network: network, stateManager: stateManager, id: entry.id) + } } else { assertionFailure() } diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift b/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift index 3031c0b69a..8979290338 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Messages/TelegramEngineMessages.swift @@ -342,6 +342,10 @@ public extension TelegramEngine { let _ = cancelPendingSendStarsReactionInteractively(account: self.account, messageId: id).startStandalone() } + public func forceSendPendingSendStarsReaction(id: EngineMessage.Id) { + let _ = _internal_forceSendPendingSendStarsReaction(account: self.account, messageId: id).startStandalone() + } + public func updateStarsReactionIsAnonymous(id: EngineMessage.Id, isAnonymous: Bool) -> Signal { return _internal_updateStarsReactionIsAnonymous(account: self.account, messageId: id, isAnonymous: isAnonymous) } diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Payments/Stars.swift b/submodules/TelegramCore/Sources/TelegramEngine/Payments/Stars.swift index 714e87683e..3a8fabd8e2 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Payments/Stars.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Payments/Stars.swift @@ -338,14 +338,16 @@ private final class StarsContextImpl { })) } - func add(balance: Int64) { + func add(balance: Int64, addTransaction: Bool) { guard let state = self._state else { return } var transactions = state.transactions - transactions.insert(.init(flags: [.isLocal], id: "\(arc4random())", count: balance, date: Int32(Date().timeIntervalSince1970), peer: .appStore, title: nil, description: nil, photo: nil, transactionDate: nil, transactionUrl: nil, paidMessageId: nil, media: [], subscriptionPeriod: nil), at: 0) + if addTransaction { + transactions.insert(.init(flags: [.isLocal], id: "\(arc4random())", count: balance, date: Int32(Date().timeIntervalSince1970), peer: .appStore, title: nil, description: nil, photo: nil, transactionDate: nil, transactionUrl: nil, paidMessageId: nil, media: [], subscriptionPeriod: nil), at: 0) + } - self.updateState(StarsContext.State(flags: [.isPendingBalance], balance: state.balance + balance, subscriptions: state.subscriptions, canLoadMoreSubscriptions: state.canLoadMoreSubscriptions, transactions: transactions, canLoadMoreTransactions: state.canLoadMoreTransactions, isLoading: state.isLoading)) + self.updateState(StarsContext.State(flags: [.isPendingBalance], balance: max(0, state.balance + balance), subscriptions: state.subscriptions, canLoadMoreSubscriptions: state.canLoadMoreSubscriptions, transactions: transactions, canLoadMoreTransactions: state.canLoadMoreTransactions, isLoading: state.isLoading)) } fileprivate func updateBalance(_ balance: Int64, transactions: [StarsContext.State.Transaction]?) { @@ -696,9 +698,9 @@ public final class StarsContext { return state } - public func add(balance: Int64) { + public func add(balance: Int64, addTransaction: Bool = true) { self.impl.with { - $0.add(balance: balance) + $0.add(balance: balance, addTransaction: addTransaction) } } diff --git a/submodules/TelegramUI/Components/Chat/ChatSendStarsScreen/Sources/ChatSendStarsScreen.swift b/submodules/TelegramUI/Components/Chat/ChatSendStarsScreen/Sources/ChatSendStarsScreen.swift index 456f1e8a3f..9ea30deeea 100644 --- a/submodules/TelegramUI/Components/Chat/ChatSendStarsScreen/Sources/ChatSendStarsScreen.swift +++ b/submodules/TelegramUI/Components/Chat/ChatSendStarsScreen/Sources/ChatSendStarsScreen.swift @@ -957,6 +957,7 @@ private final class ChatSendStarsScreenComponent: Component { private var topOffsetDistance: CGFloat? + private var balance: Int64? private var amount: Int64 = 1 private var isAnonymous: Bool = false private var cachedStarImage: (UIImage, PresentationTheme)? @@ -964,6 +965,8 @@ private final class ChatSendStarsScreenComponent: Component { private var isPastTopCutoff: Bool? + private var balanceDisposable: Disposable? + override init(frame: CGRect) { self.bottomOverscrollLimit = 200.0 @@ -1018,6 +1021,10 @@ private final class ChatSendStarsScreenComponent: Component { fatalError("init(coder:) has not been implemented") } + deinit { + self.balanceDisposable?.dispose() + } + func scrollViewDidScroll(_ scrollView: UIScrollView) { if !self.ignoreScrolling { self.updateScrolling(transition: .immediate) @@ -1126,10 +1133,26 @@ private final class ChatSendStarsScreenComponent: Component { let sideInset: CGFloat = 16.0 if self.component == nil { + self.balance = component.balance self.amount = 50 if let myTopPeer = component.myTopPeer { self.isAnonymous = myTopPeer.isAnonymous } + + if let starsContext = component.context.starsContext { + self.balanceDisposable = (starsContext.state + |> deliverOnMainQueue).startStrict(next: { [weak self] state in + guard let self else { + return + } + if let state { + if self.balance != state.balance { + self.balance = state.balance + self.state?.updated(transition: .immediate) + } + } + }) + } } self.component = component @@ -1334,7 +1357,7 @@ private final class ChatSendStarsScreenComponent: Component { context: component.context, theme: environment.theme, strings: environment.strings, - balance: component.balance + balance: self.balance )), environment: {}, containerSize: CGSize(width: 120.0, height: 100.0) @@ -1750,7 +1773,7 @@ private final class ChatSendStarsScreenComponent: Component { guard let self, let component = self.component else { return } - guard let balance = component.balance else { + guard let balance = self.balance else { return } diff --git a/submodules/TelegramUI/Sources/AccountContext.swift b/submodules/TelegramUI/Sources/AccountContext.swift index 57272623f8..25f96e9251 100644 --- a/submodules/TelegramUI/Sources/AccountContext.swift +++ b/submodules/TelegramUI/Sources/AccountContext.swift @@ -304,6 +304,8 @@ public final class AccountContextImpl: AccountContext { self.starsContext = nil } + self.account.stateManager.starsContext = self.starsContext + if let locationManager = self.sharedContextImpl.locationManager, sharedContext.applicationBindings.isMainApp && !temp { self.peersNearbyManager = PeersNearbyManagerImpl(account: account, engine: self.engine, locationManager: locationManager, inForeground: sharedContext.applicationBindings.applicationInForeground) } else { diff --git a/submodules/TelegramUI/Sources/ChatControllerOpenMessageReactionContextMenu.swift b/submodules/TelegramUI/Sources/ChatControllerOpenMessageReactionContextMenu.swift index ed92d13607..310ce9af8e 100644 --- a/submodules/TelegramUI/Sources/ChatControllerOpenMessageReactionContextMenu.swift +++ b/submodules/TelegramUI/Sources/ChatControllerOpenMessageReactionContextMenu.swift @@ -369,6 +369,12 @@ extension ChatControllerImpl { } func openMessageSendStarsScreen(message: Message) { + if let current = self.currentSendStarsUndoController { + self.currentSendStarsUndoController = nil + current.dismiss() + } + self.context.engine.messages.forceSendPendingSendStarsReaction(id: message.id) + let reactionsAttribute = mergedMessageReactions(attributes: message.attributes, isTags: false) let _ = (ChatSendStarsScreen.initialData(context: self.context, peerId: message.id.peerId, messageId: message.id, topPeers: reactionsAttribute?.topPeers ?? []) |> deliverOnMainQueue).start(next: { [weak self] initialData in