浏览代码

implement simple UI to start location tracking from chat's attach menu

cyberta 5 年之前
父节点
当前提交
053454a7c1
共有 2 个文件被更改,包括 42 次插入0 次删除
  1. 2 0
      deltachat-ios/AppDelegate.swift
  2. 40 0
      deltachat-ios/Controller/ChatViewController.swift

+ 2 - 0
deltachat-ios/AppDelegate.swift

@@ -20,6 +20,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
     private let dcContext = DcContext()
     var appCoordinator: AppCoordinator!
     var relayHelper: RelayHelper!
+    var locationManager: LocationManager!
     // static let appCoordinatorDeprecated = AppCoordinatorDeprecated()
     static var progress: Float = 0 // TODO: delete
     static var lastErrorString: String?
@@ -79,6 +80,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         appCoordinator = AppCoordinator(window: window, dcContext: dcContext)
         appCoordinator.start()
         RelayHelper.setup(dcContext)
+        locationManager = LocationManager(context: dcContext)
         UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum)
         start()
         setStockTranslations()

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

@@ -1087,11 +1087,16 @@ extension ChatViewController: MessagesLayoutDelegate {
         let cameraAction = PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: cameraButtonPressed(_:))
         let documentAction = UIAlertAction(title: String.localized("documents"), style: .default, handler: documentActionPressed(_:))
         let voiceMessageAction = UIAlertAction(title: String.localized("voice_message"), style: .default, handler: voiceMessageButtonPressed(_:))
+        let isLocationStreaming = dcContext.isSendingLocationsToChat(chatId: chatId)
+        let locationStreamingAction = UIAlertAction(title: isLocationStreaming ? String.localized("stop_sharing_location") : String.localized("location"),
+                                                    style: isLocationStreaming ? .destructive : .default,
+                                                    handler: locationStreamingButtonPressed(_:))
 
         alert.addAction(cameraAction)
         alert.addAction(galleryAction)
         alert.addAction(documentAction)
         alert.addAction(voiceMessageAction)
+        alert.addAction(locationStreamingAction)
         alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
         self.present(alert, animated: true, completion: nil)
     }
@@ -1112,6 +1117,41 @@ extension ChatViewController: MessagesLayoutDelegate {
         coordinator?.showPhotoVideoLibrary(delegate: self)
     }
 
+    private func locationStreamingButtonPressed(_ action: UIAlertAction) {
+        let isLocationStreaming = dcContext.isSendingLocationsToChat(chatId: chatId)
+        if isLocationStreaming {
+            locationStreamingFor(seconds: 0)
+        } else {
+            let alert = UIAlertController(title: nil, message: nil, preferredStyle: .safeActionSheet)
+            let select5min = UIAlertAction(title: String.localized("share_location_for_5_minutes"), style: .default, handler: locationStreamingAction(duration: Time.fiveMinutes))
+            let select30min = UIAlertAction(title: String.localized("share_location_for_30_minutes"), style: .default, handler: locationStreamingAction(duration: Time.thirtyMinutes))
+            let select1hour = UIAlertAction(title: String.localized("share_location_for_one_hour"), style: .default, handler: locationStreamingAction(duration: Time.oneHour))
+            let select2hours = UIAlertAction(title: String.localized("share_location_for_two_hours"), style: .default, handler: locationStreamingAction(duration: Time.twoHours))
+            let select6hours = UIAlertAction(title: String.localized("share_location_for_six_hours"), style: .default, handler: locationStreamingAction(duration: Time.sixHours))
+            alert.addAction(select5min)
+            alert.addAction(select30min)
+            alert.addAction(select1hour)
+            alert.addAction(select2hours)
+            alert.addAction(select6hours)
+            alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
+            self.present(alert, animated: true, completion: nil)
+        }
+    }
+
+    private func locationStreamingAction(duration: Int) -> (_ alertAction: UIAlertAction) -> () {
+        return { _ in
+            self.locationStreamingFor(seconds: duration)
+        }
+    }
+
+    private func locationStreamingFor(seconds: Int) {
+            guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
+                return
+            }
+            appDelegate.locationManager.shareLocation(chatId: self.chatId, duration: seconds)
+            self.dcContext.sendLocationsToChat(chatId: self.chatId, seconds: seconds)
+    }
+
 }
 
 // MARK: - MessageCellDelegate