orvyn-lang 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/GUIDE.md +149 -0
- package/LANGUAGE.md +877 -24
- package/README.md +227 -131
- package/brand/orvyn-sunset-animated.svg +15 -0
- package/package.json +3 -1
- package/src/cli/orvyn.js +216 -489
- package/src/core/signal.js +104 -6
- package/src/devtools/runtime.js +25 -1
- package/src/lang/a11y.js +102 -0
- package/src/lang/auth.js +239 -0
- package/src/lang/bundle-native.js +10 -6
- package/src/lang/codegen.js +498 -64
- package/src/lang/device.js +41 -0
- package/src/lang/doctor.js +60 -0
- package/src/lang/env.js +27 -0
- package/src/lang/errors.js +513 -0
- package/src/lang/form.js +160 -0
- package/src/lang/index.js +2 -1
- package/src/lang/lexer.js +2 -0
- package/src/lang/list.js +44 -0
- package/src/lang/native/android/MainActivity.kt +43 -4
- package/src/lang/native/android/OrvynDevice.kt +163 -0
- package/src/lang/native/android/OrvynEngine.kt +154 -7
- package/src/lang/native/android/OrvynRenderer.kt +519 -40
- package/src/lang/native/android.js +74 -11
- package/src/lang/native/build.js +130 -60
- package/src/lang/native/config.js +108 -0
- package/src/lang/native/ios/OrvynApp.swift +6 -0
- package/src/lang/native/ios/OrvynEngine.swift +268 -4
- package/src/lang/native/ios/OrvynViews.swift +429 -31
- package/src/lang/native/ios.js +75 -2
- package/src/lang/native-runtime.js +423 -57
- package/src/lang/page.js +52 -0
- package/src/lang/parser.js +243 -15
- package/src/lang/prerender.js +300 -0
- package/src/lang/read-import.js +11 -0
- package/src/lang/router.js +266 -0
- package/src/lang/runtime-shared.js +820 -10
- package/src/lang/runtime.js +375 -52
- package/src/lang/select.js +13 -0
- package/src/lang/server-code.js +233 -0
- package/src/lang/server-export.js +78 -0
- package/src/lang/server-libs.js +53 -0
- package/src/lang/server.js +409 -0
- package/src/lang/test-runner.js +134 -0
- package/src/lang/types.js +312 -0
- package/src/lang/validation.js +55 -0
- package/src/lang/vite-plugin.js +82 -3
- package/src/lang/web-overlay.js +578 -0
- package/src/lang/widgets.js +94 -0
- package/src/lang/words.js +38 -0
- package/src/runtime/mount.js +116 -27
- package/src/runtime/patch.js +90 -11
- package/src/runtime/style.js +11 -1
- package/src/styling/fluid.js +91 -4
- package/src/styling/spec.js +306 -0
- package/src/core/context.js +0 -19
- package/src/core/lifecycle.js +0 -8
- package/src/core/resource.js +0 -27
- package/src/core/router.js +0 -20
- package/src/core/theme.js +0 -10
- package/src/devtools/devicePreview/PreviewFrame.js +0 -214
- package/src/devtools/devicePreview/index.js +0 -6
- package/src/devtools/overlay.js +0 -105
- package/src/runtime/createRenderer.js +0 -22
- package/src/runtime/h.js +0 -93
- package/src/runtime/hydrate.js +0 -59
- package/src/runtime/jsx-dev-runtime.js +0 -7
- package/src/runtime/jsx-runtime.js +0 -13
- package/src/runtime/partialHydration.js +0 -154
- package/src/runtime/platform.js +0 -135
- package/src/runtime/renderToStream.js +0 -127
- package/src/runtime/renderToString.js +0 -121
- package/src/runtime/resume.js +0 -62
|
@@ -3,9 +3,14 @@
|
|
|
3
3
|
// Runs the compiled Orvyn app (app.js) in JavaScriptCore and publishes the
|
|
4
4
|
// UI tree it renders. Taps and typing are sent back to JS as events.
|
|
5
5
|
|
|
6
|
+
import CoreLocation
|
|
6
7
|
import Foundation
|
|
7
8
|
import JavaScriptCore
|
|
9
|
+
import PhotosUI
|
|
10
|
+
import UniformTypeIdentifiers
|
|
11
|
+
import Security
|
|
8
12
|
import SwiftUI
|
|
13
|
+
import UIKit
|
|
9
14
|
|
|
10
15
|
struct ONode: Identifiable {
|
|
11
16
|
let id: String
|
|
@@ -13,12 +18,18 @@ struct ONode: Identifiable {
|
|
|
13
18
|
let s: [String: Any]
|
|
14
19
|
let x: String?
|
|
15
20
|
let ph: String?
|
|
21
|
+
let inputType: String?
|
|
16
22
|
let v: String?
|
|
17
23
|
let src: String?
|
|
18
24
|
let href: String?
|
|
25
|
+
let a: String?
|
|
26
|
+
let rh: Double?
|
|
27
|
+
let o: [String]
|
|
19
28
|
let tap: Int?
|
|
20
29
|
let change: Int?
|
|
21
30
|
let c: [ONode]
|
|
31
|
+
let theme: [String: Any]?
|
|
32
|
+
let canGoBack: Bool
|
|
22
33
|
|
|
23
34
|
init(_ d: [String: Any]) {
|
|
24
35
|
id = d["id"] as? String ?? UUID().uuidString
|
|
@@ -26,13 +37,19 @@ struct ONode: Identifiable {
|
|
|
26
37
|
s = d["s"] as? [String: Any] ?? [:]
|
|
27
38
|
x = d["x"] as? String
|
|
28
39
|
ph = d["ph"] as? String
|
|
40
|
+
inputType = d["inputType"] as? String
|
|
29
41
|
v = d["v"] as? String
|
|
30
42
|
src = d["src"] as? String
|
|
31
43
|
href = d["href"] as? String
|
|
44
|
+
a = d["a"] as? String
|
|
45
|
+
rh = (d["rh"] as? NSNumber)?.doubleValue
|
|
46
|
+
o = d["o"] as? [String] ?? []
|
|
32
47
|
let e = d["e"] as? [String: Any] ?? [:]
|
|
33
48
|
tap = (e["tap"] as? NSNumber)?.intValue
|
|
34
49
|
change = (e["change"] as? NSNumber)?.intValue
|
|
35
50
|
c = (d["c"] as? [[String: Any]] ?? []).map(ONode.init)
|
|
51
|
+
theme = d["theme"] as? [String: Any]
|
|
52
|
+
canGoBack = d["canGoBack"] as? Bool ?? false
|
|
36
53
|
}
|
|
37
54
|
}
|
|
38
55
|
|
|
@@ -45,10 +62,13 @@ final class OrvynEngine: ObservableObject {
|
|
|
45
62
|
let devServer = ProcessInfo.processInfo.environment["ORVYN_DEV_SERVER"]
|
|
46
63
|
|
|
47
64
|
private var context: JSContext!
|
|
65
|
+
private let device = ODevice()
|
|
48
66
|
// bumps on every reload so callbacks from the old app are ignored
|
|
49
67
|
private var generation = 0
|
|
50
68
|
private var devVersion: String?
|
|
51
69
|
private var testScriptStarted = false
|
|
70
|
+
private var initialURL: String?
|
|
71
|
+
private var fetchTasks: [Int: URLSessionDataTask] = [:]
|
|
52
72
|
|
|
53
73
|
init() {
|
|
54
74
|
if devServer != nil {
|
|
@@ -127,6 +147,9 @@ final class OrvynEngine: ObservableObject {
|
|
|
127
147
|
self.context = context
|
|
128
148
|
|
|
129
149
|
installHost(context, generation)
|
|
150
|
+
if let initialURL {
|
|
151
|
+
context.setObject(initialURL, forKeyedSubscript: "__orvynInitialUrl" as NSString)
|
|
152
|
+
}
|
|
130
153
|
context.evaluateScript(code, withSourceURL: url)
|
|
131
154
|
|
|
132
155
|
if !testScriptStarted {
|
|
@@ -137,6 +160,15 @@ final class OrvynEngine: ObservableObject {
|
|
|
137
160
|
|
|
138
161
|
// MARK: - Native → JS
|
|
139
162
|
|
|
163
|
+
func openURL(_ url: String) {
|
|
164
|
+
initialURL = url
|
|
165
|
+
context?.objectForKeyedSubscript("__orvynDeepLink")?.call(withArguments: [url])
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
func goBack() {
|
|
169
|
+
_ = context?.objectForKeyedSubscript("__orvynBack")?.call(withArguments: [])
|
|
170
|
+
}
|
|
171
|
+
|
|
140
172
|
func fire(_ handler: Int?, _ value: String? = nil) {
|
|
141
173
|
guard let handler else { return }
|
|
142
174
|
let event = context.objectForKeyedSubscript("__orvynEvent")
|
|
@@ -166,16 +198,38 @@ final class OrvynEngine: ObservableObject {
|
|
|
166
198
|
}
|
|
167
199
|
context.setObject(render, forKeyedSubscript: "__orvynRender" as NSString)
|
|
168
200
|
|
|
201
|
+
let device: @convention(block) (Int, String) -> Void = { [weak self] id, kind in
|
|
202
|
+
DispatchQueue.main.async {
|
|
203
|
+
self?.device.ask(kind) { json, error in
|
|
204
|
+
guard self?.generation == generation else { return }
|
|
205
|
+
self?.context.objectForKeyedSubscript("__orvynDeviceDone")?
|
|
206
|
+
.call(withArguments: [id, json, error])
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
context.setObject(device, forKeyedSubscript: "__orvynDevice" as NSString)
|
|
211
|
+
|
|
212
|
+
// JavaScriptCore has no timers of its own
|
|
213
|
+
var nextTimer = 0
|
|
214
|
+
var timers = Set<Int>()
|
|
169
215
|
let setTimeout: @convention(block) (JSValue, JSValue) -> Int = { [weak self] callback, ms in
|
|
170
216
|
let delay = ms.isNumber ? ms.toDouble() : 0
|
|
217
|
+
nextTimer += 1
|
|
218
|
+
let id = nextTimer
|
|
219
|
+
timers.insert(id)
|
|
171
220
|
DispatchQueue.main.asyncAfter(deadline: .now() + delay / 1000) { [weak self] in
|
|
172
|
-
guard self?.generation == generation else { return }
|
|
221
|
+
guard self?.generation == generation, timers.remove(id) != nil else { return }
|
|
173
222
|
callback.call(withArguments: [])
|
|
174
223
|
}
|
|
175
|
-
return
|
|
224
|
+
return id
|
|
176
225
|
}
|
|
177
226
|
context.setObject(setTimeout, forKeyedSubscript: "setTimeout" as NSString)
|
|
178
227
|
|
|
228
|
+
let clearTimeout: @convention(block) (JSValue) -> Void = { id in
|
|
229
|
+
if id.isNumber { timers.remove(Int(id.toInt32())) }
|
|
230
|
+
}
|
|
231
|
+
context.setObject(clearTimeout, forKeyedSubscript: "clearTimeout" as NSString)
|
|
232
|
+
|
|
179
233
|
let fetch: @convention(block) (Int, String, String, JSValue, String) -> Void = { [weak self] id, url, method, body, headersJson in
|
|
180
234
|
guard let target = URL(string: url) else {
|
|
181
235
|
self?.fetchDone(id, 0, "Invalid URL", "", generation)
|
|
@@ -194,7 +248,7 @@ final class OrvynEngine: ObservableObject {
|
|
|
194
248
|
request.setValue(value, forHTTPHeaderField: key)
|
|
195
249
|
}
|
|
196
250
|
}
|
|
197
|
-
URLSession.shared.dataTask(with: request) { data, response, error in
|
|
251
|
+
let task = URLSession.shared.dataTask(with: request) { [weak self] data, response, error in
|
|
198
252
|
let http = response as? HTTPURLResponse
|
|
199
253
|
let status = error == nil ? (http?.statusCode ?? 0) : 0
|
|
200
254
|
let contentType = http?.value(forHTTPHeaderField: "Content-Type") ?? ""
|
|
@@ -202,12 +256,20 @@ final class OrvynEngine: ObservableObject {
|
|
|
202
256
|
?? String(data: data ?? Data(), encoding: .utf8)
|
|
203
257
|
?? ""
|
|
204
258
|
DispatchQueue.main.async {
|
|
259
|
+
self?.fetchTasks.removeValue(forKey: id)
|
|
205
260
|
self?.fetchDone(id, status, text, contentType, generation)
|
|
206
261
|
}
|
|
207
|
-
}
|
|
262
|
+
}
|
|
263
|
+
self?.fetchTasks[id] = task
|
|
264
|
+
task.resume()
|
|
208
265
|
}
|
|
209
266
|
context.setObject(fetch, forKeyedSubscript: "__orvynFetch" as NSString)
|
|
210
267
|
|
|
268
|
+
let abortFetch: @convention(block) (Int) -> Void = { [weak self] id in
|
|
269
|
+
self?.fetchTasks.removeValue(forKey: id)?.cancel()
|
|
270
|
+
}
|
|
271
|
+
context.setObject(abortFetch, forKeyedSubscript: "__orvynAbortFetch" as NSString)
|
|
272
|
+
|
|
211
273
|
// saved state → UserDefaults (kept after the app closes)
|
|
212
274
|
let storageGet: @convention(block) (String) -> Any = { key in
|
|
213
275
|
UserDefaults.standard.string(forKey: key) ?? NSNull()
|
|
@@ -219,6 +281,75 @@ final class OrvynEngine: ObservableObject {
|
|
|
219
281
|
}
|
|
220
282
|
context.setObject(storageSet, forKeyedSubscript: "__orvynStorageSet" as NSString)
|
|
221
283
|
|
|
284
|
+
let storageRemove: @convention(block) (String) -> Void = { key in
|
|
285
|
+
UserDefaults.standard.removeObject(forKey: key)
|
|
286
|
+
}
|
|
287
|
+
context.setObject(storageRemove, forKeyedSubscript: "__orvynStorageRemove" as NSString)
|
|
288
|
+
|
|
289
|
+
// secure credentials → iOS Keychain (strictly separate from saved state)
|
|
290
|
+
let keychainService = (Bundle.main.bundleIdentifier ?? "dev.orvyn.app") + ".secure"
|
|
291
|
+
|
|
292
|
+
let secureGet: @convention(block) (String) -> Any = { key in
|
|
293
|
+
let query: [String: Any] = [
|
|
294
|
+
kSecClass as String: kSecClassGenericPassword,
|
|
295
|
+
kSecAttrService as String: keychainService,
|
|
296
|
+
kSecAttrAccount as String: key,
|
|
297
|
+
kSecReturnData as String: true,
|
|
298
|
+
kSecMatchLimit as String: kSecMatchLimitOne
|
|
299
|
+
]
|
|
300
|
+
var item: CFTypeRef?
|
|
301
|
+
let status = SecItemCopyMatching(query as CFDictionary, &item)
|
|
302
|
+
if status == errSecSuccess, let data = item as? Data, let text = String(data: data, encoding: .utf8) {
|
|
303
|
+
return text
|
|
304
|
+
}
|
|
305
|
+
return NSNull()
|
|
306
|
+
}
|
|
307
|
+
context.setObject(secureGet, forKeyedSubscript: "__orvynSecureGet" as NSString)
|
|
308
|
+
|
|
309
|
+
let secureSet: @convention(block) (String, String) -> Any = { key, value in
|
|
310
|
+
let data = value.data(using: .utf8) ?? Data()
|
|
311
|
+
let query: [String: Any] = [
|
|
312
|
+
kSecClass as String: kSecClassGenericPassword,
|
|
313
|
+
kSecAttrService as String: keychainService,
|
|
314
|
+
kSecAttrAccount as String: key
|
|
315
|
+
]
|
|
316
|
+
let updateStatus = SecItemUpdate(query as CFDictionary, [kSecValueData as String: data] as CFDictionary)
|
|
317
|
+
if updateStatus == errSecSuccess {
|
|
318
|
+
return NSNull()
|
|
319
|
+
}
|
|
320
|
+
var addQuery = query
|
|
321
|
+
addQuery[kSecValueData as String] = data
|
|
322
|
+
addQuery[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
|
|
323
|
+
let addStatus = SecItemAdd(addQuery as CFDictionary, nil)
|
|
324
|
+
return addStatus == errSecSuccess ? NSNull() : "Keychain write failed (\(addStatus))"
|
|
325
|
+
}
|
|
326
|
+
context.setObject(secureSet, forKeyedSubscript: "__orvynSecureSet" as NSString)
|
|
327
|
+
|
|
328
|
+
let secureRemove: @convention(block) (String) -> Any = { key in
|
|
329
|
+
let query: [String: Any] = [
|
|
330
|
+
kSecClass as String: kSecClassGenericPassword,
|
|
331
|
+
kSecAttrService as String: keychainService,
|
|
332
|
+
kSecAttrAccount as String: key
|
|
333
|
+
]
|
|
334
|
+
let status = SecItemDelete(query as CFDictionary)
|
|
335
|
+
return (status == errSecSuccess || status == errSecItemNotFound)
|
|
336
|
+
? NSNull()
|
|
337
|
+
: "Keychain delete failed (\(status))"
|
|
338
|
+
}
|
|
339
|
+
context.setObject(secureRemove, forKeyedSubscript: "__orvynSecureRemove" as NSString)
|
|
340
|
+
|
|
341
|
+
let secureClear: @convention(block) () -> Any = {
|
|
342
|
+
let query: [String: Any] = [
|
|
343
|
+
kSecClass as String: kSecClassGenericPassword,
|
|
344
|
+
kSecAttrService as String: keychainService
|
|
345
|
+
]
|
|
346
|
+
let status = SecItemDelete(query as CFDictionary)
|
|
347
|
+
return (status == errSecSuccess || status == errSecItemNotFound)
|
|
348
|
+
? NSNull()
|
|
349
|
+
: "Keychain clear failed (\(status))"
|
|
350
|
+
}
|
|
351
|
+
context.setObject(secureClear, forKeyedSubscript: "__orvynSecureClear" as NSString)
|
|
352
|
+
|
|
222
353
|
let console = JSValue(newObjectIn: context)!
|
|
223
354
|
for level in ["log", "warn", "error", "info"] {
|
|
224
355
|
let log: @convention(block) () -> Void = {
|
|
@@ -265,3 +396,136 @@ final class OrvynEngine: ObservableObject {
|
|
|
265
396
|
return nil
|
|
266
397
|
}
|
|
267
398
|
}
|
|
399
|
+
|
|
400
|
+
// Camera, photo library, files and location. One request at a time.
|
|
401
|
+
final class ODevice: NSObject, PHPickerViewControllerDelegate, UIImagePickerControllerDelegate,
|
|
402
|
+
UINavigationControllerDelegate, UIDocumentPickerDelegate, CLLocationManagerDelegate {
|
|
403
|
+
typealias Done = (_ json: String, _ error: String) -> Void
|
|
404
|
+
|
|
405
|
+
private var done: Done?
|
|
406
|
+
private var locations: CLLocationManager?
|
|
407
|
+
|
|
408
|
+
func ask(_ kind: String, _ done: @escaping Done) {
|
|
409
|
+
if self.done != nil { return done("", "Already waiting for another choice") }
|
|
410
|
+
self.done = done
|
|
411
|
+
switch kind {
|
|
412
|
+
case "photo":
|
|
413
|
+
var setup = PHPickerConfiguration()
|
|
414
|
+
setup.filter = .images
|
|
415
|
+
let picker = PHPickerViewController(configuration: setup)
|
|
416
|
+
picker.delegate = self
|
|
417
|
+
present(picker)
|
|
418
|
+
case "camera":
|
|
419
|
+
guard UIImagePickerController.isSourceTypeAvailable(.camera) else {
|
|
420
|
+
return finish(error: "This device has no camera")
|
|
421
|
+
}
|
|
422
|
+
let picker = UIImagePickerController()
|
|
423
|
+
picker.sourceType = .camera
|
|
424
|
+
picker.delegate = self
|
|
425
|
+
present(picker)
|
|
426
|
+
case "file":
|
|
427
|
+
let picker = UIDocumentPickerViewController(forOpeningContentTypes: [.item], asCopy: true)
|
|
428
|
+
picker.delegate = self
|
|
429
|
+
present(picker)
|
|
430
|
+
case "location":
|
|
431
|
+
let manager = CLLocationManager()
|
|
432
|
+
locations = manager
|
|
433
|
+
manager.delegate = self
|
|
434
|
+
if manager.authorizationStatus == .notDetermined {
|
|
435
|
+
manager.requestWhenInUseAuthorization()
|
|
436
|
+
} else {
|
|
437
|
+
locationManagerDidChangeAuthorization(manager)
|
|
438
|
+
}
|
|
439
|
+
default:
|
|
440
|
+
finish(error: "Unknown request: \(kind)")
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
private func present(_ controller: UIViewController) {
|
|
445
|
+
// a swipe down would otherwise leave the app waiting forever
|
|
446
|
+
controller.isModalInPresentation = true
|
|
447
|
+
let window = UIApplication.shared.connectedScenes
|
|
448
|
+
.compactMap { $0 as? UIWindowScene }
|
|
449
|
+
.flatMap(\.windows)
|
|
450
|
+
.first { $0.isKeyWindow }
|
|
451
|
+
var top = window?.rootViewController
|
|
452
|
+
while let next = top?.presentedViewController { top = next }
|
|
453
|
+
guard let top else { return finish(error: "Could not open the picker") }
|
|
454
|
+
top.present(controller, animated: true)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
private func finish(_ value: [String: Any]? = nil, error: String = "") {
|
|
458
|
+
let done = self.done
|
|
459
|
+
self.done = nil
|
|
460
|
+
let json = value.flatMap { try? JSONSerialization.data(withJSONObject: $0) }
|
|
461
|
+
.flatMap { String(data: $0, encoding: .utf8) } ?? ""
|
|
462
|
+
DispatchQueue.main.async { done?(json, error) }
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
private func file(_ name: String, _ type: String, _ data: Data) -> [String: Any] {
|
|
466
|
+
["name": name, "type": type, "size": data.count, "dataUrl": "data:\(type);base64,\(data.base64EncodedString())"]
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// large photos are scaled down so the app stays quick
|
|
470
|
+
private func photo(_ image: UIImage) -> [String: Any]? {
|
|
471
|
+
let scale = min(1, 1600 / max(image.size.width, image.size.height))
|
|
472
|
+
let size = CGSize(width: image.size.width * scale, height: image.size.height * scale)
|
|
473
|
+
let format = UIGraphicsImageRendererFormat()
|
|
474
|
+
format.scale = 1
|
|
475
|
+
let small = UIGraphicsImageRenderer(size: size, format: format).image { _ in
|
|
476
|
+
image.draw(in: CGRect(origin: .zero, size: size))
|
|
477
|
+
}
|
|
478
|
+
return small.jpegData(compressionQuality: 0.85).map { file("photo.jpg", "image/jpeg", $0) }
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
|
|
482
|
+
picker.dismiss(animated: true)
|
|
483
|
+
guard let provider = results.first?.itemProvider, provider.canLoadObject(ofClass: UIImage.self) else {
|
|
484
|
+
return finish()
|
|
485
|
+
}
|
|
486
|
+
provider.loadObject(ofClass: UIImage.self) { [weak self] object, _ in
|
|
487
|
+
DispatchQueue.main.async {
|
|
488
|
+
self?.finish((object as? UIImage).flatMap { self?.photo($0) })
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
|
|
494
|
+
picker.dismiss(animated: true)
|
|
495
|
+
finish((info[.originalImage] as? UIImage).flatMap { photo($0) })
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
|
|
499
|
+
picker.dismiss(animated: true)
|
|
500
|
+
finish()
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
|
|
504
|
+
guard let url = urls.first, let data = try? Data(contentsOf: url) else {
|
|
505
|
+
return finish(error: "Could not read the file")
|
|
506
|
+
}
|
|
507
|
+
let type = UTType(filenameExtension: url.pathExtension)?.preferredMIMEType ?? "application/octet-stream"
|
|
508
|
+
finish(file(url.lastPathComponent, type, data))
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
|
|
512
|
+
finish()
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
|
|
516
|
+
switch manager.authorizationStatus {
|
|
517
|
+
case .authorizedWhenInUse, .authorizedAlways: manager.requestLocation()
|
|
518
|
+
case .denied, .restricted: finish(error: "Location was not allowed")
|
|
519
|
+
default: break
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
func locationManager(_ manager: CLLocationManager, didUpdateLocations found: [CLLocation]) {
|
|
524
|
+
guard let place = found.last, done != nil else { return }
|
|
525
|
+
finish(["latitude": place.coordinate.latitude, "longitude": place.coordinate.longitude, "accuracy": place.horizontalAccuracy])
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
|
|
529
|
+
if done != nil { finish(error: "Could not get the location") }
|
|
530
|
+
}
|
|
531
|
+
}
|