Browse Source

open poi in organic maps app

Simon Laux 2 years ago
parent
commit
306bf9bd86

+ 16 - 0
DcCore/DcCore/DC/Wrapper.swift

@@ -600,6 +600,22 @@ public class DcContext {
     public func setLocation(latitude: Double, longitude: Double, accuracy: Double) {
         dc_set_location(contextPointer, latitude, longitude, accuracy)
     }
+    
+    public func getLocationStringForMessage(chatId: Int, msgId: Int) -> String? {
+        let locationsArray = dc_get_locations(contextPointer, UInt32(chatId), 0, 0, 0)
+        let len = dc_array_get_cnt(locationsArray)
+        var result: String?
+        for i in 0 ..< len {
+            if dc_array_get_msg_id(locationsArray, i) == msgId {
+                let lat = Double(dc_array_get_latitude(locationsArray, i))
+                let lng = Double(dc_array_get_longitude(locationsArray, i))
+                result = "\(lat),\(lng)"
+                break
+            }
+        }
+        dc_array_unref(locationsArray)
+        return result
+    }
 
     public func searchMessages(chatId: Int = 0, searchText: String) -> [Int] {
         let start = CFAbsoluteTimeGetCurrent()

+ 35 - 8
deltachat-ios/Chat/ChatViewController.swift

@@ -177,15 +177,16 @@ class ChatViewController: UITableViewController, UITableViewDropDelegate {
         let config = ContextMenuProvider()
         if #available(iOS 13.0, *) {
             if dcChat.canSend {
-                let mainMenu = ContextMenuProvider.ContextMenuItem(submenuitems: [replyItem, replyPrivatelyItem, forwardItem, infoItem, copyItem, deleteItem])
+                let mainMenu = ContextMenuProvider.ContextMenuItem(
+                    submenuitems: [replyItem, replyPrivatelyItem, openLocationItem, forwardItem, infoItem, copyItem, deleteItem])
                 config.setMenu([mainMenu, selectMoreItem])
             } else {
-                config.setMenu([replyPrivatelyItem, forwardItem, infoItem, copyItem, deleteItem])
+                config.setMenu([replyPrivatelyItem, forwardItem, openLocationItem, infoItem, copyItem, deleteItem])
             }
         } else if dcChat.canSend { // skips some options on iOS <13 because of limited horizontal space (reply is still available by swiping)
-            config.setMenu([forwardItem, infoItem, copyItem, deleteItem, selectMoreItem])
+            config.setMenu([forwardItem, openLocationItem, infoItem, copyItem, deleteItem, selectMoreItem])
         } else {
-            config.setMenu([forwardItem, infoItem, copyItem, deleteItem])
+            config.setMenu([forwardItem, openLocationItem, infoItem, copyItem, deleteItem])
         }
         return config
     }()
@@ -275,6 +276,29 @@ class ChatViewController: UITableViewController, UITableViewDropDelegate {
             }
         )
     }()
+    
+    private lazy var openLocationItem: ContextMenuProvider.ContextMenuItem = {
+        return ContextMenuProvider.ContextMenuItem(
+            title: String.localized("show_location"),
+            imageName: "map",
+            action: #selector(BaseMessageCell.openLocation),
+            onPerform: { [weak self] indexPath in
+                guard let self = self else { return }
+                let msg = self.dcContext.getMessage(id: self.messageIds[indexPath.row])
+                if let location = self.dcContext.getLocationStringForMessage(chatId: msg.chatId, msgId: msg.id) {
+                    if let url = URL(string: "om://map?v=1&ll=\(location)") {
+                        if UIApplication.shared.canOpenURL(url) {
+                            UIApplication.shared.open(url, options: [:], completionHandler: nil)
+                        } else {
+                            if let url = URL(string: "geo:\(location)") {
+                                UIApplication.shared.open(url, options: [:], completionHandler: nil)
+                            }
+                        }
+                    }
+                }
+            }
+        )
+    }()
 
     private lazy var selectMoreItem: ContextMenuProvider.ContextMenuItem = {
         return ContextMenuProvider.ContextMenuItem(
@@ -1909,14 +1933,17 @@ class ChatViewController: UITableViewController, UITableViewDropDelegate {
                 guard let self = self else {
                     return nil
                 }
-                if self.dcContext.getMessage(id: messageId).isInfo {
+                let message = self.dcContext.getMessage(id: messageId)
+                let hasLocation: Bool = message.hasLocation
+                if message.isInfo {
                     return self.contextMenu.actionProvider(indexPath: indexPath,
                                                            filters: [ { $0.action != self.replyItem.action && $0.action != self.replyPrivatelyItem.action } ])
-                } else if self.isGroupChat && !self.dcContext.getMessage(id: messageId).isFromCurrentSender {
-                    return self.contextMenu.actionProvider(indexPath: indexPath)
+                } else if self.isGroupChat && !message.isFromCurrentSender {
+                    return self.contextMenu.actionProvider(indexPath: indexPath,
+                                                           filters: [ { $0.action != self.openLocationItem.action || hasLocation } ])
                 } else {
                     return self.contextMenu.actionProvider(indexPath: indexPath,
-                                                           filters: [ { $0.action != self.replyPrivatelyItem.action } ])
+                                                           filters: [ { $0.action != self.replyPrivatelyItem.action && ($0.action != self.openLocationItem.action || hasLocation) } ])
                 }
             }
         )

+ 4 - 0
deltachat-ios/Chat/Views/Cells/BaseMessageCell.swift

@@ -549,6 +549,10 @@ public class BaseMessageCell: UITableViewCell {
     @objc func messageReplyPrivately(_ sender: Any?) {
         self.performAction(#selector(BaseMessageCell.messageReplyPrivately(_:)), with: sender)
     }
+    
+    @objc func openLocation(_ sender: Any?) {
+        self.performAction(#selector(BaseMessageCell.openLocation(_:)), with: sender)
+    }
 
     @objc func messageCopy(_ sender: Any?) {
         self.performAction(#selector(BaseMessageCell.messageCopy(_:)), with: sender)

+ 4 - 0
deltachat-ios/Info.plist

@@ -108,5 +108,9 @@
 	</array>
 	<key>ITSAppUsesNonExemptEncryption</key>
 	<false/>
+	<key>LSApplicationQueriesSchemes</key>
+	<array>
+		<string>om</string>
+	</array>
 </dict>
 </plist>