Subtract stars balance automatically

This commit is contained in:
Isaac 2024-08-13 00:03:03 +08:00
parent af4d13febe
commit 2fee6401b3
7 changed files with 86 additions and 9 deletions

View File

@ -292,6 +292,11 @@ public final class AccountStateManager {
return self.botPreviewUpdatesPipe.signal()
}
fileprivate let forceSendPendingStarsReactionPipe = ValuePipe<MessageId>()
public var forceSendPendingStarsReaction: Signal<MessageId, NoError> {
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<MessageId, NoError> {
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<TelegramAccountManagerTypes>,

View File

@ -220,6 +220,12 @@ func cancelPendingSendStarsReactionInteractively(account: Account, messageId: Me
|> ignoreValues
}
func _internal_forceSendPendingSendStarsReaction(account: Account, messageId: MessageId) -> Signal<Never, NoError> {
account.stateManager.forceSendPendingStarsReaction(messageId: messageId)
return .complete()
}
func _internal_updateStarsReactionIsAnonymous(account: Account, messageId: MessageId, isAnonymous: Bool) -> Signal<Never, NoError> {
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<Never, RequestUpdateMessageReactionError> 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<Never, NoError> 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<Void, NoError> = 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<Never, NoError> in
return synchronizeMessageStarsReactions(transaction: transaction, postbox: postbox, network: network, stateManager: stateManager, id: entry.id)
}
} else {
assertionFailure()
}

View File

@ -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<Never, NoError> {
return _internal_updateStarsReactionIsAnonymous(account: self.account, messageId: id, isAnonymous: isAnonymous)
}

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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 {

View File

@ -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