From f58bdb572b43ebd9a36742d16e9db95253be0b0b Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Sat, 16 Nov 2019 08:22:36 +0400 Subject: [PATCH] Donate send message intents for groups and saved messages --- .../AppIntents/Sources/AppIntents.swift | 64 +++++++++++++++---- 1 file changed, 51 insertions(+), 13 deletions(-) diff --git a/submodules/AppIntents/Sources/AppIntents.swift b/submodules/AppIntents/Sources/AppIntents.swift index 676d2373cc..54ecd7c7d5 100644 --- a/submodules/AppIntents/Sources/AppIntents.swift +++ b/submodules/AppIntents/Sources/AppIntents.swift @@ -10,12 +10,32 @@ import TelegramPresentationData import AvatarNode import AccountContext +private let savedMessagesAvatar: UIImage = { + return generateImage(CGSize(width: 60.0, height: 60.0)) { size, context in + var locations: [CGFloat] = [1.0, 0.0] + + let colorSpace = CGColorSpaceCreateDeviceRGB() + let gradient = CGGradient(colorsSpace: colorSpace, colors: [UIColor(rgb: 0x2a9ef1).cgColor, UIColor(rgb: 0x72d5fd).cgColor] as CFArray, locations: &locations)! + + context.drawLinearGradient(gradient, start: CGPoint(), end: CGPoint(x: 0.0, y: size.height), options: CGGradientDrawingOptions()) + + let factor = size.width / 60.0 + context.translateBy(x: size.width / 2.0, y: size.height / 2.0) + context.scaleBy(x: factor, y: -factor) + context.translateBy(x: -size.width / 2.0, y: -size.height / 2.0) + + if let savedMessagesIcon = generateTintedImage(image: UIImage(bundleImageName: "Avatar/SavedMessagesIcon"), color: .white) { + context.draw(savedMessagesIcon.cgImage!, in: CGRect(origin: CGPoint(x: floor((size.width - savedMessagesIcon.size.width) / 2.0), y: floor((size.height - savedMessagesIcon.size.height) / 2.0)), size: savedMessagesIcon.size)) + } + }! +}() + public func donateSendMessageIntent(account: Account, sharedContext: SharedAccountContext, peerIds: [PeerId]) { if #available(iOSApplicationExtension 13.2, iOS 13.2, *) { let _ = (account.postbox.transaction { transaction -> [Peer] in var peers: [Peer] = [] for peerId in peerIds { - if peerId.namespace == Namespaces.Peer.CloudUser && peerId != account.peerId, let peer = transaction.getPeer(peerId) { + if peerId.namespace != Namespaces.Peer.SecretChat, let peer = transaction.getPeer(peerId) { peers.append(peer) } } @@ -24,27 +44,44 @@ public func donateSendMessageIntent(account: Account, sharedContext: SharedAccou |> mapToSignal { peers -> Signal<[(Peer, UIImage?)], NoError> in var signals: [Signal<(Peer, UIImage?), NoError>] = [] for peer in peers { - let peerAndAvatar = (peerAvatarImage(account: account, peer: peer, authorOfMessage: nil, representation: peer.smallProfileImage, round: false) ?? .single(nil)) - |> map { avatarImage in - return (peer, avatarImage) + if peer.id == account.peerId { + signals.append(.single((peer, savedMessagesAvatar))) + } else { + let peerAndAvatar = (peerAvatarImage(account: account, peer: peer, authorOfMessage: nil, representation: peer.smallProfileImage, round: false) ?? .single(nil)) + |> map { avatarImage in + return (peer, avatarImage) + } + signals.append(peerAndAvatar) } - signals.append(peerAndAvatar) } return combineLatest(signals) } |> deliverOnMainQueue).start(next: { peers in + let presentationData = sharedContext.currentPresentationData.with { $0 } + for (peer, avatarImage) in peers { - guard let peer = peer as? TelegramUser, peer.botInfo == nil && !peer.flags.contains(.isSupport) else { - continue - } - let presentationData = sharedContext.currentPresentationData.with { $0 } - let recipientHandle = INPersonHandle(value: "tg\(peer.id.id)", type: .unknown) + let displayTitle: String var nameComponents = PersonNameComponents() - nameComponents.givenName = peer.firstName - nameComponents.familyName = peer.lastName - let displayTitle = peer.displayTitle(strings: presentationData.strings, displayOrder: presentationData.nameDisplayOrder) + if let peer = peer as? TelegramUser { + if peer.botInfo != nil || peer.flags.contains(.isSupport) { + continue + } + + if peer.id == account.peerId { + displayTitle = presentationData.strings.DialogList_SavedMessages + nameComponents.givenName = displayTitle + } else { + displayTitle = peer.displayTitle(strings: presentationData.strings, displayOrder: presentationData.nameDisplayOrder) + nameComponents.givenName = peer.firstName + nameComponents.familyName = peer.lastName + } + } else { + displayTitle = peer.compactDisplayTitle + nameComponents.givenName = displayTitle + } + let recipient = INPerson(personHandle: recipientHandle, nameComponents: nameComponents, displayName: displayTitle, image: nil, contactIdentifier: nil, customIdentifier: "tg\(peer.id.id)") let intent = INSendMessageIntent(recipients: [recipient], content: nil, speakableGroupName: INSpeakableString(spokenPhrase: displayTitle), conversationIdentifier: "tg\(peer.id.id)", serviceName: nil, sender: nil) @@ -53,6 +90,7 @@ public func donateSendMessageIntent(account: Account, sharedContext: SharedAccou } let interaction = INInteraction(intent: intent, response: nil) interaction.direction = .outgoing + interaction.identifier = "sendMessage_\(account.peerId.toInt64())_\(peer.id.toInt64)" interaction.groupIdentifier = "sendMessage_\(account.peerId.toInt64())" interaction.donate() }