Browse Source

make use of dcAccounts in share extension

cyberta 4 years ago
parent
commit
11777abc97
1 changed files with 26 additions and 30 deletions
  1. 26 30
      DcShare/Controller/ShareViewController.swift

+ 26 - 30
DcShare/Controller/ShareViewController.swift

@@ -31,12 +31,12 @@ class ShareViewController: SLComposeServiceViewController {
         }
     }
 
-    lazy var dbHelper: DatabaseHelper = {
-       return DatabaseHelper(dcContext: dcContext)
+    let logger = SimpleLogger()
+    let dcAccounts: DcAccounts = DcAccounts()
+    lazy var dcContext: DcContext = {
+        return dcAccounts.get()
     }()
 
-    let logger = SimpleLogger()
-    let dcContext: DcContext = DcContext()
     var selectedChatId: Int?
     var selectedChat: DcChat?
     var shareAttachment: ShareAttachment?
@@ -77,27 +77,25 @@ class ShareViewController: SLComposeServiceViewController {
     }
 
     override func presentationAnimationDidFinish() {
-        if dbHelper.currentDatabaseLocation == dbHelper.sharedDbFile {
-            dcContext.logger = self.logger
-            dcContext.openDatabase(dbFile: dbHelper.sharedDbFile)
-            isAccountConfigured = dcContext.isConfigured()
-            if isAccountConfigured {
-                if #available(iOSApplicationExtension 13.0, *) {
-                   if let intent = self.extensionContext?.intent as? INSendMessageIntent, let chatId = Int(intent.conversationIdentifier ?? "") {
-                       selectedChatId = chatId
-                   }
-                }
+        dcAccounts.openDatabase()
+        dcContext.logger = self.logger
+        isAccountConfigured = dcContext.isConfigured()
+        if isAccountConfigured {
+            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)
-                }
-                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)
-                }
+            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.dcAccounts.get(), inputItems: self.extensionContext?.inputItems, delegate: self)
             }
             reloadConfigurationItems()
             validateContent()
@@ -134,21 +132,19 @@ class ShareViewController: SLComposeServiceViewController {
                 if messages.count == 1 {
                     messages[0].text?.append(self.contentText)
                 } else {
-                    let message = dcContext.newMessage(viewType: DC_MSG_TEXT)
+                    let message = dcAccounts.get().newMessage(viewType: DC_MSG_TEXT)
                     message.text = self.contentText
                     messages.insert(message, at: 0)
                 }
             }
-            let chatListController = SendingController(chatId: chatId, dcMsgs: messages, dcContext: dcContext)
+            let chatListController = SendingController(chatId: chatId, dcMsgs: messages, dcContext: dcAccounts.get())
             chatListController.delegate = self
             self.pushConfigurationViewController(chatListController)
         }
     }
 
     func quit() {
-        if dbHelper.currentDatabaseLocation == dbHelper.sharedDbFile {
-            dcContext.closeDatabase()
-        }
+        dcAccounts.closeDatabase()
 
         // Inform the host that we're done, so it un-blocks its UI.
         self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
@@ -165,7 +161,7 @@ class ShareViewController: SLComposeServiceViewController {
             item?.value = selectedChat?.name
             logger.debug("configurationItems chat name: \(String(describing: selectedChat?.name))")
             item?.tapHandler = {
-                let chatListController = ChatListController(dcContext: self.dcContext, chatListDelegate: self)
+                let chatListController = ChatListController(dcContext: self.dcAccounts.get(), chatListDelegate: self)
                 self.pushConfigurationViewController(chatListController)
             }
         } else {