-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathOpenInOptions.swift
328 lines (291 loc) · 16.3 KB
/
OpenInOptions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import Foundation
import UIKit
import TelegramCore
import CoreLocation
import MapKit
import AccountContext
import UrlEscaping
public enum OpenInItem {
case url(url: String)
case location(location: TelegramMediaMap, directions: OpenInLocationDirections?)
}
public enum OpenInLocationDirections: Equatable {
case walking
case driving
case transit
var transportType: MKDirectionsTransportType {
switch self {
case .walking:
return .walking
case .transit:
return .transit
case .driving:
return .automobile
}
}
public var launchOptions: String {
switch self {
case .walking:
return MKLaunchOptionsDirectionsModeWalking
case .transit:
return MKLaunchOptionsDirectionsModeTransit
case .driving:
return MKLaunchOptionsDirectionsModeDriving
}
}
}
public enum OpenInApplication: Equatable {
case safari
case maps
case other(title: String, identifier: Int64, scheme: String, store: String?)
}
public enum OpenInAction {
case none
case openUrl(url: String)
case openLocation(latitude: Double, longitude: Double, directions: OpenInLocationDirections?)
}
public final class OpenInOption {
public let identifier: String
public let application: OpenInApplication
public let action: () -> OpenInAction
public init(identifier: String, application: OpenInApplication, action: @escaping () -> OpenInAction) {
self.identifier = identifier
self.application = application
self.action = action
}
public var title: String {
get {
switch self.application {
case .safari:
return "Safari"
case .maps:
return "Maps"
case let .other(title, _, _, _):
return title
}
}
}
}
public func availableOpenInOptions(context: AccountContext, item: OpenInItem) -> [OpenInOption] {
return allOpenInOptions(context: context, item: item).filter { option in
if case let .other(_, _, scheme, _) = option.application {
return context.sharedContext.applicationBindings.canOpenUrl("\(scheme)://")
} else {
return true
}
}
}
private func allOpenInOptions(context: AccountContext, item: OpenInItem) -> [OpenInOption] {
var options: [OpenInOption] = []
switch item {
case let .url(url):
var skipSafari = false
if url.contains("youtube.com/") || url.contains("youtu.be/") {
let updatedUrl = url.replacingOccurrences(of: "https://", with: "youtube://").replacingOccurrences(of: "http://", with: "youtube://")
options.append(OpenInOption(identifier: "youtube", application: .other(title: "YouTube", identifier: 544007664, scheme: "youtube", store: nil), action: {
return .openUrl(url: updatedUrl)
}))
skipSafari = true
}
if !skipSafari {
options.append(OpenInOption(identifier: "safari", application: .safari, action: {
return .openUrl(url: url)
}))
}
options.append(OpenInOption(identifier: "chrome", application: .other(title: "Chrome", identifier: 535886823, scheme: "googlechrome", store: nil), action: {
if let url = URL(string: url), var components = URLComponents(url: url, resolvingAgainstBaseURL: true) {
components.scheme = components.scheme == "https" ? "googlechromes" : "googlechrome"
if let url = components.string {
return .openUrl(url: url)
}
}
return .none
}))
options.append(OpenInOption(identifier: "firefox", application: .other(title: "Firefox", identifier: 989804926, scheme: "firefox", store: nil), action: {
if let escapedUrl = url.addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed) {
return .openUrl(url: "firefox://open-url?url=\(escapedUrl)")
}
return .none
}))
options.append(OpenInOption(identifier: "firefoxFocus", application: .other(title: "Firefox Focus", identifier: 1055677337, scheme: "firefox-focus", store: nil), action: {
if let escapedUrl = url.addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed) {
return .openUrl(url: "firefox-focus://open-url?url=\(escapedUrl)")
}
return .none
}))
options.append(OpenInOption(identifier: "operaMini", application: .other(title: "Opera Mini", identifier: 363729560, scheme: "opera-http", store: "es"), action: {
if let url = URL(string: url), var components = URLComponents(url: url, resolvingAgainstBaseURL: true) {
components.scheme = components.scheme == "https" ? "opera-https" : "opera-http"
if let url = components.string {
return .openUrl(url: url)
}
}
return .none
}))
options.append(OpenInOption(identifier: "operaTouch", application: .other(title: "Opera Touch", identifier: 1411869974, scheme: "touch-http", store: nil), action: {
if let url = URL(string: url), var components = URLComponents(url: url, resolvingAgainstBaseURL: true) {
components.scheme = components.scheme == "https" ? "touch-https" : "touch-http"
if let url = components.string {
return .openUrl(url: url)
}
}
return .none
}))
options.append(OpenInOption(identifier: "duckDuckGo", application: .other(title: "DuckDuckGo", identifier: 663592361, scheme: "ddgQuickLink", store: nil), action: {
return .openUrl(url: "ddgQuickLink://\(url)")
}))
options.append(OpenInOption(identifier: "edge", application: .other(title: "Microsoft Edge", identifier: 1288723196, scheme: "microsoft-edge-http", store: nil), action: {
if let url = URL(string: url), var components = URLComponents(url: url, resolvingAgainstBaseURL: true) {
components.scheme = components.scheme == "https" ? "microsoft-edge-https" : "microsoft-edge-http"
if let url = components.string {
return .openUrl(url: url)
}
}
return .none
}))
options.append(OpenInOption(identifier: "yandex", application: .other(title: "Yandex Browser", identifier: 483693909, scheme: "yandexbrowser-open-url", store: nil), action: {
if let escapedUrl = url.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) {
return .openUrl(url: "yandexbrowser-open-url://\(escapedUrl)")
}
return .none
}))
options.append(OpenInOption(identifier: "brave", application: .other(title: "Brave", identifier: 1052879175, scheme: "brave", store: nil), action: {
if let escapedUrl = url.addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed) {
return .openUrl(url: "brave://open-url?url=\(escapedUrl)")
}
return .none
}))
options.append(OpenInOption(identifier: "dolphin", application: .other(title: "Dolphin", identifier: 1440710469, scheme: "dolphin", store: "us"), action: {
return .openUrl(url: "dolphin://\(url)")
}))
options.append(OpenInOption(identifier: "onion", application: .other(title: "Onion Browser", identifier: 519296448, scheme: "onionhttp", store: nil), action: {
if let url = URL(string: url), var components = URLComponents(url: url, resolvingAgainstBaseURL: true) {
components.scheme = components.scheme == "https" ? "onionhttps" : "onionhttp"
if let url = components.string {
return .openUrl(url: url)
}
}
return .none
}))
options.append(OpenInOption(identifier: "ucbrowser", application: .other(title: "UC Browser", identifier: 1048518592, scheme: "ucbrowser", store: nil), action: {
return .openUrl(url: "ucbrowser://\(url)")
}))
options.append(OpenInOption(identifier: "alook", application: .other(title: "Alook Browser", identifier: 1261944766, scheme: "alook", store: nil), action: {
return .openUrl(url: "alook://\(url)")
}))
case let .location(location, directions):
let lat = location.latitude
let lon = location.longitude
if directions == nil {
if let venue = location.venue, let venueId = venue.id, let provider = venue.provider, provider == "foursquare" {
options.append(OpenInOption(identifier: "foursquare", application: .other(title: "Foursquare", identifier: 306934924, scheme: "foursquare", store: nil), action: {
return .openUrl(url: "foursquare://venues/\(venueId)")
}))
}
}
options.append(OpenInOption(identifier: "appleMaps", application: .maps, action: {
return .openLocation(latitude: lat, longitude: lon, directions: directions)
}))
options.append(OpenInOption(identifier: "googleMaps", application: .other(title: "Google Maps", identifier: 585027354, scheme: "comgooglemaps-x-callback", store: nil), action: {
let coordinates = "\(lat),\(lon)"
if let directions = directions {
let directionsMode: String
switch directions {
case .walking:
directionsMode = "walking"
case .driving:
directionsMode = "driving"
case .transit:
directionsMode = "transit"
}
return .openUrl(url: "comgooglemaps-x-callback://?daddr=\(coordinates)&directionsmode=\(directionsMode)&x-success=telegram://?resume=true&x-source=Telegram")
} else {
if let venue = location.venue, let venueId = venue.id, let provider = venue.provider, provider == "gplaces" {
return .openUrl(url: "https://www.google.com/maps/search/?api=1&query=\(venue.address ?? "")&query_place_id=\(venueId)")
} else {
return .openUrl(url: "comgooglemaps-x-callback://?center=\(coordinates)&q=\(coordinates)&x-success=telegram://?resume=true&x-source=Telegram")
}
}
}))
options.append(OpenInOption(identifier: "yandexMaps", application: .other(title: "Yandex.Maps", identifier: 313877526, scheme: "yandexmaps", store: nil), action: {
if let _ = directions {
return .openUrl(url: "yandexmaps://build_route_on_map?lat_to=\(lat)&lon_to=\(lon)")
} else {
return .openUrl(url: "yandexmaps://maps.yandex.ru/?pt=\(lon),\(lat)&z=16")
}
}))
options.append(OpenInOption(identifier: "uber", application: .other(title: "Uber", identifier: 368677368, scheme: "uber", store: nil), action: {
let dropoffName: String
let dropoffAddress: String
if let title = location.venue?.title.addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed), title.count > 0 {
dropoffName = title
} else {
dropoffName = ""
}
if let address = location.venue?.address?.addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed), address.count > 0 {
dropoffAddress = address
} else {
dropoffAddress = ""
}
return .openUrl(url: "uber://?client_id=&action=setPickup&pickup=my_location&dropoff[latitude]=\(lat)&dropoff[longitude]=\(lon)&dropoff[nickname]=\(dropoffName)&dropoff[formatted_address]=\(dropoffAddress)")
}))
options.append(OpenInOption(identifier: "lyft", application: .other(title: "Lyft", identifier: 529379082, scheme: "lyft", store: nil), action: {
return .openUrl(url: "lyft://ridetype?id=lyft&destination[latitude]=\(lat)&destination[longitude]=\(lon)")
}))
if let _ = directions {
options.append(OpenInOption(identifier: "citymapper", application: .other(title: "Citymapper", identifier: 469463298, scheme: "citymapper", store: nil), action: {
let endName: String
let endAddress: String
if let title = location.venue?.title.addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed), title.count > 0 {
endName = title
} else {
endName = ""
}
if let address = location.venue?.address?.addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed), address.count > 0 {
endAddress = address
} else {
endAddress = ""
}
return .openUrl(url: "citymapper://directions?endcoord=\(lat),\(lon)&endname=\(endName)&endaddress=\(endAddress)")
}))
options.append(OpenInOption(identifier: "yandexNavi", application: .other(title: "Yandex.Navi", identifier: 474500851, scheme: "yandexnavi", store: nil), action: {
return .openUrl(url: "yandexnavi://build_route_on_map?lat_to=\(lat)&lon_to=\(lon)")
}))
}
options.append(OpenInOption(identifier: "2gis", application: .other(title: "2GIS", identifier: 481627348, scheme: "dgis", store: "ru"), action: {
let coordinates = "\(lon),\(lat)"
if let _ = directions {
return .openUrl(url: "dgis://2gis.ru/routeSearch/to/\(coordinates)/go")
} else {
return .openUrl(url: "dgis://2gis.ru/geo/\(coordinates)")
}
}))
options.append(OpenInOption(identifier: "moovit", application: .other(title: "Moovit", identifier: 498477945, scheme: "moovit", store: nil), action: {
if let _ = directions {
let destName: String
if let title = location.venue?.title.addingPercentEncoding(withAllowedCharacters: .urlQueryValueAllowed), title.count > 0 {
destName = title
} else {
destName = ""
}
return .openUrl(url: "moovit://directions?dest_lat=\(lat)&dest_lon=\(lon)&dest_name=\(destName)&partner_id=Telegram")
} else {
return .openUrl(url: "moovit://nearby?lat=\(lat)&lon=\(lon)&partner_id=Telegram")
}
}))
if directions == nil {
options.append(OpenInOption(identifier: "hereMaps", application: .other(title: "HERE Maps", identifier: 955837609, scheme: "here-location", store: nil), action: {
return .openUrl(url: "here-location://\(lat),\(lon)")
}))
}
options.append(OpenInOption(identifier: "waze", application: .other(title: "Waze", identifier: 323229106, scheme: "waze", store: nil), action: {
let url = "waze://?ll=\(lat),\(lon)"
if let _ = directions {
return .openUrl(url: url.appending("&navigate=yes"))
} else {
return .openUrl(url: url)
}
}))
}
return options
}