Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

T3NativePresentation.swift

75 lines2.2 KB

1import ExpoModulesCore
2import UIKit
3​
4final class T3PresentationSources {
5 private class Entry {
6 weak var view: UIView?
7 init(_ view: UIView) { self.view = view }
8 }
9​
10 private var entries: [String: Entry] = [:]
11​
12 func register(_ view: UIView, identifier: String) {
13 entries[identifier] = Entry(view)
14 }
15​
16 func remove(_ view: UIView, identifier: String) {
17 if entries[identifier]?.view == nil || entries[identifier]?.view === view {
18 entries.removeValue(forKey: identifier)
19 }
20 }
21​
22 func view(for identifier: String) -> UIView? {
23 // Use the child bounds, not the wrapper's potentially stretched layout bounds.
24 entries[identifier]?.view?.subviews.first
25 }
27​
28final class T3PresentationSourceView: ExpoView {
29 weak var sources: T3PresentationSources?
30 var identifier = "" {
31 didSet {
32 sources?.remove(self, identifier: oldValue)
33 if !identifier.isEmpty { sources?.register(self, identifier: identifier) }
34 }
35 }
36​
37 deinit {
38 sources?.remove(self, identifier: identifier)
39 }
41​
42func presentFileShare(
43 url: URL,
44 title: String,
45 source: UIView?,
46 presenter: UIViewController,
47 promise: Promise
48) throws {
49 guard url.isFileURL, FileManager.default.isReadableFile(atPath: url.path) else {
50 throw NSError(
51 domain: "T3NativePresentation",
52 code: 1,
53 userInfo: [NSLocalizedDescriptionKey: "The file is no longer available."]
54 )
55 }
56​
57 guard let origin = source ?? presenter.view else {
58 throw NSError(
59 domain: "T3NativePresentation",
60 code: 2,
61 userInfo: [NSLocalizedDescriptionKey: "The presenting screen is no longer open."]
62 )
63 }
64​
65 let activity = UIActivityViewController(activityItems: [url], applicationActivities: nil)
66 activity.title = title
67 activity.overrideUserInterfaceStyle = source?.traitCollection.userInterfaceStyle
68 ?? presenter.traitCollection.userInterfaceStyle
69 activity.completionWithItemsHandler = { _, _, _, _ in promise.resolve(nil) }
70 activity.modalPresentationStyle = .popover
71 activity.popoverPresentationController?.sourceView = origin
72 activity.popoverPresentationController?.sourceRect = source?.bounds
73 ?? CGRect(x: origin.bounds.midX, y: origin.bounds.maxY, width: 0, height: 0)
74 presenter.present(activity, animated: true)