2022-01-04 12:12:24 +00:00
|
|
|
//
|
|
|
|
// Copyright 2021 New Vector Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2022-08-03 09:46:54 +00:00
|
|
|
import UIKit
|
|
|
|
import CoreLocation
|
2022-01-04 12:12:24 +00:00
|
|
|
|
|
|
|
extension UIActivity.ActivityType {
|
|
|
|
static let shareToMapsApp = UIActivity.ActivityType("Element.ShareToMapsApp")
|
|
|
|
}
|
|
|
|
|
2022-02-03 12:37:14 +00:00
|
|
|
class ShareToMapsAppActivity: UIActivity {
|
|
|
|
enum MapsAppType {
|
2022-01-04 12:12:24 +00:00
|
|
|
case apple
|
|
|
|
case google
|
2022-02-17 13:41:09 +00:00
|
|
|
case osm
|
2022-01-04 12:12:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-25 11:38:21 +00:00
|
|
|
private let type: MapsAppType
|
|
|
|
private let location: CLLocationCoordinate2D
|
2022-01-04 12:12:24 +00:00
|
|
|
|
|
|
|
private override init() {
|
|
|
|
fatalError()
|
|
|
|
}
|
|
|
|
|
2022-02-03 12:37:14 +00:00
|
|
|
init(type: MapsAppType, location: CLLocationCoordinate2D) {
|
2022-01-04 12:12:24 +00:00
|
|
|
self.type = type
|
|
|
|
self.location = location
|
|
|
|
}
|
|
|
|
|
2022-02-03 12:37:14 +00:00
|
|
|
static func urlForMapsAppType(_ type: MapsAppType, location: CLLocationCoordinate2D) -> URL {
|
2022-01-04 12:12:24 +00:00
|
|
|
switch type {
|
|
|
|
case .apple:
|
|
|
|
return URL(string: "https://maps.apple.com?ll=\(location.latitude),\(location.longitude)&q=Pin")!
|
|
|
|
case .google:
|
|
|
|
return URL(string: "https://www.google.com/maps/search/?api=1&query=\(location.latitude),\(location.longitude)")!
|
2022-02-17 13:41:09 +00:00
|
|
|
case .osm:
|
|
|
|
return URL(string: "https://www.openstreetmap.org/?mlat=\(location.latitude)&mlon=\(location.longitude)")!
|
2022-01-04 12:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-03 12:37:14 +00:00
|
|
|
override var activityTitle: String? {
|
2022-01-04 12:12:24 +00:00
|
|
|
switch type {
|
|
|
|
case .apple:
|
|
|
|
return VectorL10n.locationSharingOpenAppleMaps
|
|
|
|
case .google:
|
|
|
|
return VectorL10n.locationSharingOpenGoogleMaps
|
2022-02-17 13:41:09 +00:00
|
|
|
case .osm:
|
|
|
|
return VectorL10n.locationSharingOpenOpenStreetMaps
|
2022-01-04 12:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var activityCategory: UIActivity.Category {
|
|
|
|
return .action
|
|
|
|
}
|
|
|
|
|
2022-02-03 12:37:14 +00:00
|
|
|
override var activityType: UIActivity.ActivityType {
|
2022-01-04 12:12:24 +00:00
|
|
|
return .shareToMapsApp
|
|
|
|
}
|
|
|
|
|
2022-02-03 12:37:14 +00:00
|
|
|
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
|
2022-01-04 12:12:24 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-02-03 12:37:14 +00:00
|
|
|
override func prepare(withActivityItems activityItems: [Any]) {
|
2022-01-04 12:12:24 +00:00
|
|
|
let url = Self.urlForMapsAppType(type, location: location)
|
|
|
|
|
|
|
|
UIApplication.shared.open(url, options: [:]) { [weak self] result in
|
|
|
|
self?.activityDidFinish(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|