Skip to content

Commit

Permalink
Merge branch 'master' of gitlab.com:peter-iakovlev/telegram-ios
Browse files Browse the repository at this point in the history
  • Loading branch information
laktyushin committed Apr 11, 2021
2 parents e0ffac5 + 86f7d78 commit 7382d41
Show file tree
Hide file tree
Showing 11 changed files with 3,598 additions and 3,469 deletions.
2 changes: 2 additions & 0 deletions Telegram/Telegram-iOS/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -6444,3 +6444,5 @@ Sorry for the inconvenience.";
"Checkout.OptionalTipItemPlaceholder" = "Enter Custom";

"VoiceChat.ReminderNotify" = "We will notify you when it starts.";

"Checkout.SuccessfulTooltip" = "You paid %1$@ for %2$@.";
6 changes: 4 additions & 2 deletions submodules/BotPaymentsUI/Sources/BotCheckoutController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,20 @@ public final class BotCheckoutController: ViewController {
private let context: AccountContext
private let invoice: TelegramMediaInvoice
private let messageId: MessageId
private let completed: (String, MessageId?) -> Void

private var presentationData: PresentationData

private var didPlayPresentationAnimation = false

private let inputData: Promise<BotCheckoutController.InputData?>

public init(context: AccountContext, invoice: TelegramMediaInvoice, messageId: MessageId, inputData: Promise<BotCheckoutController.InputData?>) {
public init(context: AccountContext, invoice: TelegramMediaInvoice, messageId: MessageId, inputData: Promise<BotCheckoutController.InputData?>, completed: @escaping (String, MessageId?) -> Void) {
self.context = context
self.invoice = invoice
self.messageId = messageId
self.inputData = inputData
self.completed = completed

self.presentationData = context.sharedContext.currentPresentationData.with { $0 }

Expand Down Expand Up @@ -121,7 +123,7 @@ public final class BotCheckoutController: ViewController {
self?.present(c, in: .window(.root), with: a)
}, dismissAnimated: { [weak self] in
self?.dismiss()
})
}, completed: self.completed)

//displayNode.enableInteractiveDismiss = true

Expand Down
34 changes: 26 additions & 8 deletions submodules/BotPaymentsUI/Sources/BotCheckoutControllerNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ final class BotCheckoutControllerNode: ItemListControllerNode, PKPaymentAuthoriz
private let messageId: MessageId
private let present: (ViewController, Any?) -> Void
private let dismissAnimated: () -> Void
private let completed: (String, MessageId?) -> Void

private var stateValue = BotCheckoutControllerState()
private let state = ValuePromise(BotCheckoutControllerState(), ignoreRepeated: true)
Expand Down Expand Up @@ -536,12 +537,13 @@ final class BotCheckoutControllerNode: ItemListControllerNode, PKPaymentAuthoriz
private var passwordTip: String?
private var passwordTipDisposable: Disposable?

init(controller: BotCheckoutController?, navigationBar: NavigationBar, updateNavigationOffset: @escaping (CGFloat) -> Void, context: AccountContext, invoice: TelegramMediaInvoice, messageId: MessageId, inputData: Promise<BotCheckoutController.InputData?>, present: @escaping (ViewController, Any?) -> Void, dismissAnimated: @escaping () -> Void) {
init(controller: BotCheckoutController?, navigationBar: NavigationBar, updateNavigationOffset: @escaping (CGFloat) -> Void, context: AccountContext, invoice: TelegramMediaInvoice, messageId: MessageId, inputData: Promise<BotCheckoutController.InputData?>, present: @escaping (ViewController, Any?) -> Void, dismissAnimated: @escaping () -> Void, completed: @escaping (String, MessageId?) -> Void) {
self.controller = controller
self.context = context
self.messageId = messageId
self.present = present
self.dismissAnimated = dismissAnimated
self.completed = completed

self.presentationData = context.sharedContext.currentPresentationData.with { $0 }

Expand Down Expand Up @@ -1213,6 +1215,9 @@ final class BotCheckoutControllerNode: ItemListControllerNode, PKPaymentAuthoriz
tipAmount = 0
}

let totalAmount = currentTotalPrice(paymentForm: paymentForm, validatedFormInfo: self.currentValidatedFormInfo, currentShippingOptionId: self.currentShippingOptionId, currentTip: self.currentTipAmount)
let currencyValue = formatCurrencyAmount(totalAmount, currency: paymentForm.invoice.currency)

self.payDisposable.set((sendBotPaymentForm(account: self.context.account, messageId: self.messageId, formId: paymentForm.id, validatedInfoId: self.currentValidatedFormInfo?.id, shippingOptionId: self.currentShippingOptionId, tipAmount: tipAmount, credentials: credentials) |> deliverOnMainQueue).start(next: { [weak self] result in
if let strongSelf = self {
strongSelf.inProgressDimNode.isUserInteractionEnabled = false
Expand All @@ -1227,19 +1232,32 @@ final class BotCheckoutControllerNode: ItemListControllerNode, PKPaymentAuthoriz
strongSelf.applePayController = nil
applePayController.presentingViewController?.dismiss(animated: true, completion: nil)
}

let proceedWithCompletion: (Bool, MessageId?) -> Void = { success, receiptMessageId in
guard let strongSelf = self else {
return
}

if success {
strongSelf.dismissAnimated()
strongSelf.completed(currencyValue, receiptMessageId)
} else {
strongSelf.dismissAnimated()
}
}

switch result {
case .done:
strongSelf.dismissAnimated()
case let .done(receiptMessageId):
proceedWithCompletion(true, receiptMessageId)
case let .externalVerificationRequired(url):
strongSelf.updateActionButton()
var dismissImpl: (() -> Void)?
let controller = BotCheckoutWebInteractionController(context: strongSelf.context, url: url, intent: .externalVerification({ _ in
dismissImpl?()
var dismissImpl: ((Bool) -> Void)?
let controller = BotCheckoutWebInteractionController(context: strongSelf.context, url: url, intent: .externalVerification({ success in
dismissImpl?(success)
}))
dismissImpl = { [weak controller] in
dismissImpl = { [weak controller] success in
controller?.dismiss()
self?.dismissAnimated()
proceedWithCompletion(success, nil)
}
strongSelf.present(controller, ViewControllerPresentationArguments(presentationAnimation: .modalSheet))
}
Expand Down
25 changes: 25 additions & 0 deletions submodules/Postbox/Sources/Postbox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,11 @@ public final class Transaction {
assert(!self.disposed)
self.postbox?.scanMessages(peerId: peerId, namespace: namespace, tag: tag, f)
}

public func scanTopMessages(peerId: PeerId, namespace: MessageId.Namespace, limit: Int, _ f: (Message) -> Bool) {
assert(!self.disposed)
self.postbox?.scanTopMessages(peerId: peerId, namespace: namespace, limit: limit, f)
}

public func scanMessageAttributes(peerId: PeerId, namespace: MessageId.Namespace, limit: Int, _ f: (MessageId, [MessageAttribute]) -> Bool) {
self.postbox?.scanMessageAttributes(peerId: peerId, namespace: namespace, limit: limit, f)
Expand Down Expand Up @@ -3411,6 +3416,26 @@ public final class Postbox {
}
}
}

fileprivate func scanTopMessages(peerId: PeerId, namespace: MessageId.Namespace, limit: Int, _ f: (Message) -> Bool) {
let lowerBound = MessageIndex.lowerBound(peerId: peerId, namespace: namespace)
var index = MessageIndex.upperBound(peerId: peerId, namespace: namespace)
var remainingLimit = limit
while remainingLimit > 0 {
let messages = self.messageHistoryTable.fetch(peerId: peerId, namespace: namespace, tag: nil, threadId: nil, from: index, includeFrom: false, to: lowerBound, limit: 10)
remainingLimit -= 10
for message in messages {
if !f(self.renderIntermediateMessage(message)) {
break
}
}
if let last = messages.last {
index = last.index
} else {
break
}
}
}

fileprivate func scanMessageAttributes(peerId: PeerId, namespace: MessageId.Namespace, limit: Int, _ f: (MessageId, [MessageAttribute]) -> Bool) {
var remainingLimit = limit
Expand Down
48 changes: 45 additions & 3 deletions submodules/TelegramCore/Sources/BotPaymentForm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public enum SendBotPaymentFormError {
}

public enum SendBotPaymentResult {
case done
case done(receiptMessageId: MessageId?)
case externalVerificationRequired(url: String)
}

Expand Down Expand Up @@ -384,7 +384,27 @@ public func sendBotPaymentForm(account: Account, messageId: MessageId, formId: I
switch result {
case let .paymentResult(updates):
account.stateManager.addUpdates(updates)
return .done
var receiptMessageId: MessageId?
for apiMessage in updates.messages {
if let message = StoreMessage(apiMessage: apiMessage) {
for media in message.media {
if let action = media as? TelegramMediaAction {
if case .paymentSent = action.action {
for attribute in message.attributes {
if let reply = attribute as? ReplyMessageAttribute {
if reply.messageId == messageId {
if case let .Id(id) = message.id {
receiptMessageId = id
}
}
}
}
}
}
}
}
}
return .done(receiptMessageId: receiptMessageId)
case let .paymentVerificationNeeded(url):
return .externalVerificationRequired(url: url)
}
Expand All @@ -402,13 +422,35 @@ public func sendBotPaymentForm(account: Account, messageId: MessageId, formId: I
}
}

public struct BotPaymentReceipt {
public struct BotPaymentReceipt : Equatable {
public let invoice: BotPaymentInvoice
public let info: BotPaymentRequestedInfo?
public let shippingOption: BotPaymentShippingOption?
public let credentialsTitle: String
public let invoiceMedia: TelegramMediaInvoice
public let tipAmount: Int64?

public static func ==(lhs: BotPaymentReceipt, rhs: BotPaymentReceipt) -> Bool {
if lhs.invoice != rhs.invoice {
return false
}
if lhs.info != rhs.info {
return false
}
if lhs.shippingOption != rhs.shippingOption {
return false
}
if lhs.credentialsTitle != rhs.credentialsTitle {
return false
}
if !lhs.invoiceMedia.isEqual(to: rhs.invoiceMedia) {
return false
}
if lhs.tipAmount != rhs.tipAmount {
return false
}
return true
}
}

public enum RequestBotPaymentReceiptError {
Expand Down
Loading

1 comment on commit 7382d41

@nitsuaaisblink
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q

Please sign in to comment.