浏览代码

implement very basic chat selection for share extension

cyberta 5 年之前
父节点
当前提交
c1e17b5d2a
共有 3 个文件被更改,包括 130 次插入6 次删除
  1. 53 0
      DcShare/ChatListController.swift
  2. 73 6
      DcShare/ShareViewController.swift
  3. 4 0
      deltachat-ios.xcodeproj/project.pbxproj

+ 53 - 0
DcShare/ChatListController.swift

@@ -0,0 +1,53 @@
+import Foundation
+import UIKit
+import DcCore
+
+protocol ChatListDelegate: class {
+    func onChatSelected(chatId: Int)
+}
+
+class ChatListController: UITableViewController {
+    let dcContext: DcContext
+    var chatList: DcChatlist?
+    let contactCellReuseIdentifier = "contactCellReuseIdentifier"
+    weak var chatListDelegate: ChatListDelegate?
+
+    init(dcContext: DcContext, chatListDelegate: ChatListDelegate) {
+        self.dcContext = dcContext
+        self.chatListDelegate = chatListDelegate
+        super.init(style: .grouped)
+    }
+
+    required init?(coder: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+
+    override func viewDidLoad() {
+        super.viewDidLoad()
+        chatList = dcContext.getChatlist(flags: DC_GCL_ADD_ALLDONE_HINT | DC_GCL_FOR_FORWARDING | DC_GCL_NO_SPECIALS, queryString: nil, queryId: 0)
+        tableView.register(UITableViewCell.self, forCellReuseIdentifier: contactCellReuseIdentifier)
+    }
+
+    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
+        return chatList?.length ?? 0
+    }
+
+    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
+        guard let cell = tableView.dequeueReusableCell(withIdentifier: contactCellReuseIdentifier) else {
+            fatalError("could not deque TableViewCell")
+        }
+        if let chatList = chatList {
+            let chat = dcContext.getChat(chatId: chatList.getChatId(index: indexPath.row))
+            cell.textLabel?.text = chat.name
+        }
+
+        return cell
+    }
+
+    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
+        if let chatList = chatList {
+            chatListDelegate?.onChatSelected(chatId: chatList.getChatId(index: indexPath.row))
+        }
+    }
+
+}

+ 73 - 6
DcShare/ShareViewController.swift

@@ -2,14 +2,60 @@ import UIKit
 import Social
 import DcCore
 
+
 class ShareViewController: SLComposeServiceViewController {
 
+    class SimpleLogger: Logger {
+        func verbose(_ message: String) {
+            print("ShareViewController", "verbose", message)
+        }
+
+        func debug(_ message: String) {
+            print("ShareViewController", "debug", message)
+        }
+
+        func info(_ message: String) {
+            print("ShareViewController", "info", message)
+        }
+
+        func warning(_ message: String) {
+            print("ShareViewController", "warning", message)
+        }
+
+        func error(_ message: String) {
+            print("ShareViewController", "error", message)
+        }
+    }
+
+    let logger = SimpleLogger()
     let dcContext = DcContext.shared
+    var selectedChatId: Int?
+    var selectedChat: DcChat?
+
+
+    override func viewDidLoad() {
+        super.viewDidLoad()
+        // workaround for iOS13 bug
+        if #available(iOS 13.0, *) {
+            _ = NotificationCenter.default.addObserver(forName: UIResponder.keyboardDidShowNotification, object: nil, queue: .main) { (_) in
+                if let layoutContainerView = self.view.subviews.last {
+                    layoutContainerView.frame.size.height += 10
+                }
+            }
+        }
+    }
 
     override func presentationAnimationDidFinish() {
+
         let dbHelper = DatabaseHelper()
         if dbHelper.currentDatabaseLocation == dbHelper.sharedDbFile {
+            dcContext.logger = self.logger
             dcContext.openDatabase(dbFile: dbHelper.sharedDbFile)
+            selectedChatId = dcContext.getChatIdByContactId(contactId: Int(DC_CONTACT_ID_SELF))
+            if let chatId = selectedChatId {
+                selectedChat = dcContext.getChat(chatId: chatId)
+            }
+            reloadConfigurationItems()
         } else {
             cancel()
         }
@@ -22,20 +68,41 @@ class ShareViewController: SLComposeServiceViewController {
 
     override func didSelectPost() {
         // This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
+        logger.debug("did select post - \(String(describing: selectedChatId))")
+        if let chatId = selectedChatId {
+            let message = DcMsg(viewType: DC_MSG_TEXT)
+            message.text = self.contentText
+            message.sendInChat(id: chatId)
+        }
 
-        let selfchatId = dcContext.getChatIdByContactId(contactId: Int(DC_CONTACT_ID_SELF))
-        let message = DcMsg(viewType: DC_MSG_TEXT)
-        message.text = self.contentText
-        message.sendInChat(id: selfchatId)
-
+        logger.debug("did select post - closeDatabase")
         dcContext.closeDatabase()
         // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
         self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
     }
 
     override func configurationItems() -> [Any]! {
+         logger.debug("configurationItems")
         // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
-        return []
+
+        let item = SLComposeSheetConfigurationItem()
+        item?.title = String.localized("send_message")
+        item?.value = selectedChat?.name ?? "unknown"
+        logger.debug("configurationItems chat name: \(String(describing: selectedChat?.name))")
+        item?.tapHandler = {
+            let chatListController = ChatListController(dcContext: self.dcContext, chatListDelegate: self)
+            self.pushConfigurationViewController(chatListController)
+        }
+
+        return [item as Any]
     }
+}
 
+extension ShareViewController: ChatListDelegate {
+    func onChatSelected(chatId: Int) {
+        selectedChatId = chatId
+        selectedChat = DcChat(id: chatId)
+        reloadConfigurationItems()
+        popConfigurationViewController()
+    }
 }

+ 4 - 0
deltachat-ios.xcodeproj/project.pbxproj

@@ -99,6 +99,7 @@
 		30E8F21A2447285600CE2C90 /* DcShare.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 30E8F2102447285600CE2C90 /* DcShare.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
 		30E8F2252447622300CE2C90 /* DcCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 304219D1243F588500516852 /* DcCore.framework */; };
 		30E8F2262447622300CE2C90 /* DcCore.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 304219D1243F588500516852 /* DcCore.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
+		30E8F2422448B77600CE2C90 /* ChatListController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30E8F2412448B77600CE2C90 /* ChatListController.swift */; };
 		30F9B9EC235F2116006E7ACF /* MessageCounter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30F9B9EB235F2116006E7ACF /* MessageCounter.swift */; };
 		6795F63A82E94FF7CD2CEC0F /* Pods_deltachat_iosTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2F7009234DB9408201A6CDCB /* Pods_deltachat_iosTests.framework */; };
 		7070FB9B2101ECBB000DC258 /* NewGroupController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7070FB9A2101ECBB000DC258 /* NewGroupController.swift */; };
@@ -376,6 +377,7 @@
 		30E8F2152447285600CE2C90 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/MainInterface.storyboard; sourceTree = "<group>"; };
 		30E8F2172447285600CE2C90 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
 		30E8F21F24472AAE00CE2C90 /* DcShare.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = DcShare.entitlements; sourceTree = "<group>"; };
+		30E8F2412448B77600CE2C90 /* ChatListController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatListController.swift; sourceTree = "<group>"; };
 		30F9B9EB235F2116006E7ACF /* MessageCounter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageCounter.swift; sourceTree = "<group>"; };
 		6241BE1534A653E79AD5D01D /* Pods_deltachat_ios.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_deltachat_ios.framework; sourceTree = BUILT_PRODUCTS_DIR; };
 		7070FB9A2101ECBB000DC258 /* NewGroupController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewGroupController.swift; sourceTree = "<group>"; };
@@ -685,6 +687,7 @@
 			children = (
 				30E8F21F24472AAE00CE2C90 /* DcShare.entitlements */,
 				30E8F2122447285600CE2C90 /* ShareViewController.swift */,
+				30E8F2412448B77600CE2C90 /* ChatListController.swift */,
 				30E8F2142447285600CE2C90 /* MainInterface.storyboard */,
 				30E8F2172447285600CE2C90 /* Info.plist */,
 			);
@@ -1246,6 +1249,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				30E8F2132447285600CE2C90 /* ShareViewController.swift in Sources */,
+				30E8F2422448B77600CE2C90 /* ChatListController.swift in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};