瀏覽代碼

Merge pull request #363 from deltachat/link_detection

Link detection
björn petersen 5 年之前
父節點
當前提交
173a0c8a81

+ 31 - 27
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
@@ -790,19 +809,13 @@ extension ChatViewController: MessagesDisplayDelegate {
     }
 
     func enabledDetectors(for _: MessageType, at _: IndexPath, in _: MessagesCollectionView) -> [DetectorType] {
-        return [.url, .date, .phoneNumber, .address]
+        return [.url, .phoneNumber]
     }
 
     func detectorAttributes(for detector: DetectorType, and message: MessageType, at indexPath: IndexPath) -> [NSAttributedString.Key: Any] {
-        switch detector {
-        case .url:
-            return  [NSAttributedString.Key.foregroundColor: DcColors.defaultTextColor,
-                     NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue,
-                     NSAttributedString.Key.underlineColor: DcColors.defaultTextColor]
-        default:
-            return MessageLabel.defaultAttributes
-        }
-
+        return  [ NSAttributedString.Key.foregroundColor: DcColors.defaultTextColor,
+                  NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue,
+                  NSAttributedString.Key.underlineColor: DcColors.defaultTextColor ]
     }
 
 }
@@ -1015,22 +1028,6 @@ extension ChatViewController: MessageCellDelegate {
 
 // MARK: - MessageLabelDelegate
 extension ChatViewController: MessageLabelDelegate {
-    func didSelectAddress(_ addressComponents: [String: String]) {
-        let mapAddress = Utils.formatAddressForQuery(address: addressComponents)
-        if let escapedMapAddress = mapAddress.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
-            // Use query, to handle malformed addresses
-            if let url = URL(string: "http://maps.apple.com/?q=\(escapedMapAddress)") {
-                UIApplication.shared.open(url as URL)
-            }
-        }
-    }
-
-    func didSelectDate(_ date: Date) {
-        let interval = date.timeIntervalSinceReferenceDate
-        if let url = NSURL(string: "calshow:\(interval)") {
-            UIApplication.shared.open(url as URL)
-        }
-    }
 
     func didSelectPhoneNumber(_ phoneNumber: String) {
         logger.info("phone open", phoneNumber)
@@ -1042,7 +1039,14 @@ extension ChatViewController: MessageLabelDelegate {
     }
 
     func didSelectURL(_ url: URL) {
-        UIApplication.shared.open(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)
+        }
     }
 }
 

+ 1 - 1
deltachat-ios/Controller/NewContactController.swift

@@ -11,7 +11,7 @@ class NewContactController: UITableViewController {
     var cancelButton: UIBarButtonItem?
 
     func contactIsValid() -> Bool {
-        return Utils.isValid(model.email)
+        return Utils.isValid(email: model.email)
     }
 
     var model: (name: String, email: String) = ("", "") {

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

+ 20 - 1
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)
@@ -72,7 +77,7 @@ struct Utils {
         return acc
     }
 
-    static func isValid(_ email: String) -> Bool {
+    static func isValid(email: String) -> Bool {
         let emailRegEx = "(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
             + "~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
             + "x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"
@@ -85,6 +90,20 @@ struct Utils {
         return emailTest.evaluate(with: email)
     }
 
+
+    static func isEmail(url: URL) -> Bool {
+        let mailScheme = "mailto"
+        if let scheme = url.scheme {
+            return mailScheme == scheme && isValid(email: url.absoluteString.substring(mailScheme.count + 1, url.absoluteString.count))
+        }
+        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]()