Przeglądaj źródła

ask to chat with selected email address

cyberta 5 lat temu
rodzic
commit
37a54a6538

+ 21 - 0
deltachat-ios/Controller/ChatViewController.swift

@@ -472,6 +472,25 @@ class ChatViewController: MessagesViewController {
             super.collectionView(collectionView, performAction: action, forItemAt: indexPath, withSender: sender)
         }
     }
+
+    private func askToChatWith(email: String) {
+        let alert = UIAlertController(title: String.localizedStringWithFormat(String.localized("ask_start_chat_with"), email),
+                                      message: nil,
+                                      preferredStyle: .actionSheet)
+        alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
+            self.dismiss(animated: true, completion: nil)
+            var contactId = Utils.getContactIdByEmail(email)
+            if contactId == nil {
+                contactId = self.dcContext.createContact(name: "", email: email)
+            }
+            let chatId = self.dcContext.createChat(contactId: contactId!)
+            self.coordinator?.showChat(chatId: chatId)
+        }))
+        alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: { _ in
+            self.dismiss(animated: true, completion: nil)
+        }))
+        present(alert, animated: true, completion: nil)
+    }
 }
 
 // MARK: - MessagesDataSource
@@ -1028,6 +1047,8 @@ extension ChatViewController: MessageLabelDelegate {
     func didSelectURL(_ url: URL) {
         if Utils.isEmail(url: url) {
             print("tapped on contact")
+            let email = Utils.getEmailFrom(url)
+            self.askToChatWith(email: email)
             ///TODO: implement handling
         } else {
             UIApplication.shared.open(url)

+ 10 - 2
deltachat-ios/DC/Wrapper.swift

@@ -18,6 +18,10 @@ class DcContext {
         dc_context_unref(contextPointer)
     }
 
+    func createContact(name: String, email: String) -> Int {
+        return Int(dc_create_contact(contextPointer, name, email))
+    }
+
     func deleteContact(contactId: Int) -> Bool {
         return dc_delete_contact(self.contextPointer, UInt32(contactId)) == 1
     }
@@ -28,12 +32,16 @@ class DcContext {
         return chatlist
     }
 
+    func createChat(contactId: Int) -> Int {
+        return Int(dc_create_chat_by_contact_id(contextPointer, UInt32(contactId)))
+    }
+
     func deleteChat(chatId: Int) {
-        dc_delete_chat(self.contextPointer, UInt32(chatId))
+        dc_delete_chat(contextPointer, UInt32(chatId))
     }
 
     func archiveChat(chatId: Int, archive: Bool) {
-        dc_archive_chat(self.contextPointer, UInt32(chatId), Int32(archive ? 1 : 0))
+        dc_archive_chat(contextPointer, UInt32(chatId), Int32(archive ? 1 : 0))
     }
 
     func marknoticedChat(chatId: Int) {

+ 10 - 0
deltachat-ios/Helper/Utils.swift

@@ -8,6 +8,11 @@ struct Utils {
         return Utils.copyAndFreeArray(inputArray: cContacts)
     }
 
+    static func getContactIdByEmail(_ address: String) -> Int? {
+        let ids = getContactIds()
+        return ids.first(where: { DcContact(id: $0).email == address })
+    }
+
     static func getBlockedContactIds() -> [Int] {
         let cBlockedContacts = dc_get_blocked_contacts(mailboxPointer)
         return Utils.copyAndFreeArray(inputArray: cBlockedContacts)
@@ -94,6 +99,11 @@ struct Utils {
         return false
     }
 
+    static func getEmailFrom(_ url: URL) -> String {
+        let mailScheme = "mailto"
+        return url.absoluteString.substring(mailScheme.count + 1, url.absoluteString.count)
+    }
+
     static func formatAddressForQuery(address: [String: String]) -> String {
         // Open address in Apple Maps app.
         var addressParts = [String]()