瀏覽代碼

switch accounts if nececssary for direct share shortcuts

cyberta 2 年之前
父節點
當前提交
fdbf8a285a
共有 1 個文件被更改,包括 50 次插入35 次删除
  1. 50 35
      DcShare/Controller/ShareViewController.swift

+ 50 - 35
DcShare/Controller/ShareViewController.swift

@@ -84,47 +84,63 @@ class ShareViewController: SLComposeServiceViewController {
     override func presentationAnimationDidFinish() {
         dcAccounts.logger = logger
         dcAccounts.openDatabase()
-        if !dcContext.isOpen() {
-            do {
-                let secret = try KeychainManager.getAccountSecret(accountID: dcContext.id)
-                if !dcContext.open(passphrase: secret) {
-                    logger.error("Failed to open database.")
+        let accountIds = dcAccounts.getAll()
+        for accountId in accountIds {
+            let dcContext = dcAccounts.get(id: accountId)
+            if !dcContext.isOpen() {
+                do {
+                    let secret = try KeychainManager.getAccountSecret(accountID: dcContext.id)
+                    if !dcContext.open(passphrase: secret) {
+                        logger.error("Failed to open database.")
+                    }
+                } catch KeychainError.unhandledError(let message, let status), KeychainError.accessError(let message, let status) {
+                    logger.error("KeychainError. \(message). Error status: \(status)")
+                } catch {
+                    logger.error("\(error)")
                 }
-            } catch KeychainError.unhandledError(let message, let status), KeychainError.accessError(let message, let status) {
-                logger.error("KeychainError. \(message). Error status: \(status)")
-            } catch {
-                logger.error("\(error)")
             }
         }
-        isAccountConfigured = dcContext.isOpen() && dcContext.isConfigured()
 
-        if isAccountConfigured {
-            if #available(iOSApplicationExtension 13.0, *) {
-                if let intent = self.extensionContext?.intent as? INSendMessageIntent,
-                   let identifiers = intent.conversationIdentifier?.split(separator: ".") {
-                    let contextId = Int(identifiers[0])
-                    let chatId = Int(identifiers[1])
-                    if dcContext.id == contextId {
-                        selectedChatId = chatId
-                    }
-               }
+        if #available(iOSApplicationExtension 13.0, *) {
+            if let intent = self.extensionContext?.intent as? INSendMessageIntent,
+               let identifiers = intent.conversationIdentifier?.split(separator: "."),
+               let contextId = Int(identifiers[0]),
+               let chatId = Int(identifiers[1]) {
+                if accountIds.contains(contextId) {
+                    dcContext = dcAccounts.get(id: contextId)
+                    selectedChatId = chatId
+                } else {
+                    logger.error("invalid INSendMessageIntent \(contextId) doesn't exist")
+                    cancel()
+                    return
+                }
             }
+        }
 
-            if selectedChatId == nil {
-                selectedChatId = dcContext.getChatIdByContactId(contactId: Int(DC_CONTACT_ID_SELF))
-            }
-            if let chatId = selectedChatId {
-                selectedChat = dcContext.getChat(chatId: chatId)
-            }
-            DispatchQueue.global(qos: .userInitiated).async { [weak self] in
-                guard let self = self else { return }
-                self.shareAttachment = ShareAttachment(dcContext: self.dcContext, inputItems: self.extensionContext?.inputItems, delegate: self)
-            }
-            reloadConfigurationItems()
-            validateContent()
-        } else {
+        isAccountConfigured = dcContext.isOpen() && dcContext.isConfigured()
+        if !isAccountConfigured {
+            logger.error("selected context \(dcContext.id) is not configured")
             cancel()
+            return
         }
+
+        if selectedChatId == nil {
+            selectedChatId = dcContext.getChatIdByContactId(contactId: Int(DC_CONTACT_ID_SELF))
+            logger.debug("selected chatID: \(String(describing: selectedChatId))")
+        }
+
+        guard let chatId = selectedChatId else {
+            cancel()
+            return
+        }
+
+        selectedChat = dcContext.getChat(chatId: chatId)
+        DispatchQueue.global(qos: .userInitiated).async { [weak self] in
+            guard let self = self else { return }
+            self.shareAttachment = ShareAttachment(dcContext: self.dcContext, inputItems: self.extensionContext?.inputItems, delegate: self)
+        }
+        reloadConfigurationItems()
+        validateContent()
     }
 
     override func loadPreviewView() -> UIView! {
@@ -176,9 +192,8 @@ class ShareViewController: SLComposeServiceViewController {
     override func configurationItems() -> [Any]! {
         let item = SLComposeSheetConfigurationItem()
         if isAccountConfigured {
-            logger.debug("configurationItems")
             // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
-
+            // TODO: discuss if we want to add an item for the account selection.
 
             item?.title = String.localized("forward_to")
             item?.value = selectedChat?.name