Forráskód Böngészése

Merge pull request #763 from deltachat/share_suggestions

add last chats to share suggestions
cyBerta 5 éve
szülő
commit
d4d45487c9

+ 29 - 0
DcCore/DcCore/Helper/DcUtils.swift

@@ -2,6 +2,7 @@ import Foundation
 import UIKit
 import MobileCoreServices
 import AVFoundation
+import Intents
 
 public struct DcUtils {
 
@@ -13,6 +14,34 @@ public struct DcUtils {
         }
     }
 
+    public static func donateSendMessageIntent(chatId: Int) {
+        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 {
+                    DcContext.shared.logger?.error(error.debugDescription)
+                }
+            })
+        }
+    }
+
     static func copyAndFreeArray(inputArray: OpaquePointer?) -> [Int] {
         var acc: [Int] = []
         let len = dc_array_get_cnt(inputArray)

+ 4 - 0
DcShare/Controller/SendingController.swift

@@ -72,6 +72,10 @@ class SendingController: UIViewController {
             for dcMsg in self.dcMsgs {
                 self.dcContext.sendMsgSync(chatId: self.chatId, msg: dcMsg)
             }
+
+            if !self.dcContext.getChat(chatId: self.chatId).isSelfTalk {
+                DcUtils.donateSendMessageIntent(chatId: self.chatId)
+            }
             self.delegate?.onSendingAttemptFinished()
         }
     }

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

+ 4 - 0
DcShare/Info.plist

@@ -24,6 +24,10 @@
 	<dict>
 		<key>NSExtensionAttributes</key>
 		<dict>
+			<key>IntentsSupported</key>
+			<array>
+				<string>INSendMessageIntent</string>
+			</array>
 			<key>NSExtensionActivationRule</key>
 			<string>SUBQUERY (
 extensionItems,

+ 4 - 0
deltachat-ios/Info.plist

@@ -2,6 +2,10 @@
 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 <plist version="1.0">
 <dict>
+	<key>NSUserActivityTypes</key>
+	<array>
+		<string>INSendMessageIntent</string>
+	</array>
 	<key>CFBundleDevelopmentRegion</key>
 	<string>$(DEVELOPMENT_LANGUAGE)</string>
 	<key>CFBundleDisplayName</key>