mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-06-16 05:55:20 +00:00
Subtract stars balance automatically
This commit is contained in:
parent
af4d13febe
commit
2fee6401b3
@ -292,6 +292,11 @@ public final class AccountStateManager {
|
|||||||
return self.botPreviewUpdatesPipe.signal()
|
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 updatedWebpageContexts: [MediaId: UpdatedWebpageSubscriberContext] = [:]
|
||||||
private var updatedPeersNearbyContext = UpdatedPeersNearbySubscriberContext()
|
private var updatedPeersNearbyContext = UpdatedPeersNearbySubscriberContext()
|
||||||
private var updatedRevenueBalancesContext = UpdatedRevenueBalancesSubscriberContext()
|
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 updateConfigRequested: (() -> Void)?
|
||||||
var isPremiumUpdated: (() -> Void)?
|
var isPremiumUpdated: (() -> Void)?
|
||||||
|
|
||||||
let messagesRemovedContext = MessagesRemovedContext()
|
let messagesRemovedContext = MessagesRemovedContext()
|
||||||
|
|
||||||
|
public weak var starsContext: StarsContext?
|
||||||
|
|
||||||
init(
|
init(
|
||||||
accountPeerId: PeerId,
|
accountPeerId: PeerId,
|
||||||
accountManager: AccountManager<TelegramAccountManagerTypes>,
|
accountManager: AccountManager<TelegramAccountManagerTypes>,
|
||||||
|
@ -220,6 +220,12 @@ func cancelPendingSendStarsReactionInteractively(account: Account, messageId: Me
|
|||||||
|> ignoreValues
|
|> 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> {
|
func _internal_updateStarsReactionIsAnonymous(account: Account, messageId: MessageId, isAnonymous: Bool) -> Signal<Never, NoError> {
|
||||||
return account.postbox.transaction { transaction -> Api.InputPeer? in
|
return account.postbox.transaction { transaction -> Api.InputPeer? in
|
||||||
transaction.updateMessage(messageId, update: { currentMessage in
|
transaction.updateMessage(messageId, update: { currentMessage in
|
||||||
@ -388,6 +394,9 @@ private func requestSendStarsReaction(postbox: Postbox, network: Network, stateM
|
|||||||
return .generic
|
return .generic
|
||||||
}
|
}
|
||||||
|> mapToSignal { result -> Signal<Never, RequestUpdateMessageReactionError> in
|
|> 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
|
return postbox.transaction { transaction -> Void in
|
||||||
transaction.setPendingMessageAction(type: .sendStarsReaction, id: messageId, action: UpdateMessageReactionsAction())
|
transaction.setPendingMessageAction(type: .sendStarsReaction, id: messageId, action: UpdateMessageReactionsAction())
|
||||||
transaction.updateMessage(messageId, update: { currentMessage in
|
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
|
let signal = withTakenStarsAction(postbox: postbox, type: .sendStarsReaction, id: entry.id, { transaction, entry -> Signal<Never, NoError> in
|
||||||
if let entry = entry {
|
if let entry = entry {
|
||||||
if let _ = entry.action as? SendStarsReactionsAction {
|
if let _ = entry.action as? SendStarsReactionsAction {
|
||||||
return synchronizeMessageStarsReactions(transaction: transaction, postbox: postbox, network: network, stateManager: stateManager, id: entry.id)
|
let triggerSignal: Signal<Void, NoError> = stateManager.forceSendPendingStarsReaction
|
||||||
|> delay(5.0, queue: .mainQueue())
|
|> 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 {
|
} else {
|
||||||
assertionFailure()
|
assertionFailure()
|
||||||
}
|
}
|
||||||
|
@ -342,6 +342,10 @@ public extension TelegramEngine {
|
|||||||
let _ = cancelPendingSendStarsReactionInteractively(account: self.account, messageId: id).startStandalone()
|
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> {
|
public func updateStarsReactionIsAnonymous(id: EngineMessage.Id, isAnonymous: Bool) -> Signal<Never, NoError> {
|
||||||
return _internal_updateStarsReactionIsAnonymous(account: self.account, messageId: id, isAnonymous: isAnonymous)
|
return _internal_updateStarsReactionIsAnonymous(account: self.account, messageId: id, isAnonymous: isAnonymous)
|
||||||
}
|
}
|
||||||
|
@ -338,14 +338,16 @@ private final class StarsContextImpl {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func add(balance: Int64) {
|
func add(balance: Int64, addTransaction: Bool) {
|
||||||
guard let state = self._state else {
|
guard let state = self._state else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var transactions = state.transactions
|
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]?) {
|
fileprivate func updateBalance(_ balance: Int64, transactions: [StarsContext.State.Transaction]?) {
|
||||||
@ -696,9 +698,9 @@ public final class StarsContext {
|
|||||||
return state
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
public func add(balance: Int64) {
|
public func add(balance: Int64, addTransaction: Bool = true) {
|
||||||
self.impl.with {
|
self.impl.with {
|
||||||
$0.add(balance: balance)
|
$0.add(balance: balance, addTransaction: addTransaction)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -957,6 +957,7 @@ private final class ChatSendStarsScreenComponent: Component {
|
|||||||
|
|
||||||
private var topOffsetDistance: CGFloat?
|
private var topOffsetDistance: CGFloat?
|
||||||
|
|
||||||
|
private var balance: Int64?
|
||||||
private var amount: Int64 = 1
|
private var amount: Int64 = 1
|
||||||
private var isAnonymous: Bool = false
|
private var isAnonymous: Bool = false
|
||||||
private var cachedStarImage: (UIImage, PresentationTheme)?
|
private var cachedStarImage: (UIImage, PresentationTheme)?
|
||||||
@ -964,6 +965,8 @@ private final class ChatSendStarsScreenComponent: Component {
|
|||||||
|
|
||||||
private var isPastTopCutoff: Bool?
|
private var isPastTopCutoff: Bool?
|
||||||
|
|
||||||
|
private var balanceDisposable: Disposable?
|
||||||
|
|
||||||
override init(frame: CGRect) {
|
override init(frame: CGRect) {
|
||||||
self.bottomOverscrollLimit = 200.0
|
self.bottomOverscrollLimit = 200.0
|
||||||
|
|
||||||
@ -1018,6 +1021,10 @@ private final class ChatSendStarsScreenComponent: Component {
|
|||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
self.balanceDisposable?.dispose()
|
||||||
|
}
|
||||||
|
|
||||||
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
||||||
if !self.ignoreScrolling {
|
if !self.ignoreScrolling {
|
||||||
self.updateScrolling(transition: .immediate)
|
self.updateScrolling(transition: .immediate)
|
||||||
@ -1126,10 +1133,26 @@ private final class ChatSendStarsScreenComponent: Component {
|
|||||||
let sideInset: CGFloat = 16.0
|
let sideInset: CGFloat = 16.0
|
||||||
|
|
||||||
if self.component == nil {
|
if self.component == nil {
|
||||||
|
self.balance = component.balance
|
||||||
self.amount = 50
|
self.amount = 50
|
||||||
if let myTopPeer = component.myTopPeer {
|
if let myTopPeer = component.myTopPeer {
|
||||||
self.isAnonymous = myTopPeer.isAnonymous
|
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
|
self.component = component
|
||||||
@ -1334,7 +1357,7 @@ private final class ChatSendStarsScreenComponent: Component {
|
|||||||
context: component.context,
|
context: component.context,
|
||||||
theme: environment.theme,
|
theme: environment.theme,
|
||||||
strings: environment.strings,
|
strings: environment.strings,
|
||||||
balance: component.balance
|
balance: self.balance
|
||||||
)),
|
)),
|
||||||
environment: {},
|
environment: {},
|
||||||
containerSize: CGSize(width: 120.0, height: 100.0)
|
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 {
|
guard let self, let component = self.component else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
guard let balance = component.balance else {
|
guard let balance = self.balance else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -304,6 +304,8 @@ public final class AccountContextImpl: AccountContext {
|
|||||||
self.starsContext = nil
|
self.starsContext = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.account.stateManager.starsContext = self.starsContext
|
||||||
|
|
||||||
if let locationManager = self.sharedContextImpl.locationManager, sharedContext.applicationBindings.isMainApp && !temp {
|
if let locationManager = self.sharedContextImpl.locationManager, sharedContext.applicationBindings.isMainApp && !temp {
|
||||||
self.peersNearbyManager = PeersNearbyManagerImpl(account: account, engine: self.engine, locationManager: locationManager, inForeground: sharedContext.applicationBindings.applicationInForeground)
|
self.peersNearbyManager = PeersNearbyManagerImpl(account: account, engine: self.engine, locationManager: locationManager, inForeground: sharedContext.applicationBindings.applicationInForeground)
|
||||||
} else {
|
} else {
|
||||||
|
@ -369,6 +369,12 @@ extension ChatControllerImpl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func openMessageSendStarsScreen(message: Message) {
|
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 reactionsAttribute = mergedMessageReactions(attributes: message.attributes, isTags: false)
|
||||||
let _ = (ChatSendStarsScreen.initialData(context: self.context, peerId: message.id.peerId, messageId: message.id, topPeers: reactionsAttribute?.topPeers ?? [])
|
let _ = (ChatSendStarsScreen.initialData(context: self.context, peerId: message.id.peerId, messageId: message.id, topPeers: reactionsAttribute?.topPeers ?? [])
|
||||||
|> deliverOnMainQueue).start(next: { [weak self] initialData in
|
|> deliverOnMainQueue).start(next: { [weak self] initialData in
|
||||||
|
Loading…
x
Reference in New Issue
Block a user