Jelajahi Sumber

populate suggestion intent data in share view controller

cyberta 5 tahun lalu
induk
melakukan
7936369295

+ 20 - 24
DcCore/DcCore/Helper/DcUtils.swift

@@ -15,35 +15,31 @@ public struct DcUtils {
     }
 
     public static func donateSendMessageIntent(chatId: Int) {
-       let chat = DcContext.shared.getChat(chatId: chatId)
-       let groupName = INSpeakableString(spokenPhrase: chat.name)
-
-       let sendMessageIntent = INSendMessageIntent(recipients: nil,
-                                                   content: nil,
-                                                   speakableGroupName: groupName,
-                                                   conversationIdentifier: "\(chat.id)",
-                                                   serviceName: nil,
-                                                   sender: nil)
-
-       // Add the user's avatar to the intent.
-        if #available(iOS 12.0, *) {
+        if #available(iOS 13.0, *) {
+            let chat = DcContext.shared.getChat(chatId: chatId)
+            let groupName = INSpeakableString(spokenPhrase: chat.name)
+
+            let sendMessageIntent = INSendMessageIntent(recipients: nil,
+                                                        content: nil,
+                                                        speakableGroupName: groupName,
+                                                        conversationIdentifier: "\(chat.id)",
+                serviceName: nil,
+                sender: nil)
+
+            // Add the user's avatar to the intent.
             if let imageData = chat.profileImage?.pngData() {
                 let image = INImage(imageData: imageData)
                 sendMessageIntent.setImage(image, forParameterNamed: \.speakableGroupName)
             }
-        }
 
-       // Donate the intent.
-       let interaction = INInteraction(intent: sendMessageIntent, response: nil)
-       interaction.donate(completion: { error in
-           if error != nil {
-               // Add error handling here.
-                DcContext.shared.logger?.error(error.debugDescription)
-           } else {
-               // Do something, e.g. send the content to a contact.
-                DcContext.shared.logger?.debug("donated message intent")
-           }
-       })
+            // Donate the intent.
+            let interaction = INInteraction(intent: sendMessageIntent, response: nil)
+            interaction.donate(completion: { error in
+                if error != nil {
+                    DcContext.shared.logger?.error(error.debugDescription)
+                }
+            })
+        }
     }
 
     static func copyAndFreeArray(inputArray: OpaquePointer?) -> [Int] {

+ 10 - 1
DcShare/Controller/ShareViewController.swift

@@ -2,6 +2,7 @@ import UIKit
 import Social
 import DcCore
 import MobileCoreServices
+import Intents
 
 
 class ShareViewController: SLComposeServiceViewController {
@@ -65,7 +66,15 @@ class ShareViewController: SLComposeServiceViewController {
             dcContext.openDatabase(dbFile: dbHelper.sharedDbFile)
             isAccountConfigured = dcContext.isConfigured()
             if isAccountConfigured {
-                selectedChatId = dcContext.getChatIdByContactId(contactId: Int(DC_CONTACT_ID_SELF))
+                if #available(iOSApplicationExtension 13.0, *) {
+                   if let intent = self.extensionContext?.intent as? INSendMessageIntent, let chatId = Int(intent.conversationIdentifier ?? "") {
+                       selectedChatId = chatId
+                   }
+                }
+
+                if selectedChatId == nil {
+                    selectedChatId = dcContext.getChatIdByContactId(contactId: Int(DC_CONTACT_ID_SELF))
+                }
                 if let chatId = selectedChatId {
                     selectedChat = dcContext.getChat(chatId: chatId)
                 }