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

T3NativeControlsModule.swift

213 lines8.3 KB

1import ExpoModulesCore
2import Security
3import UIKit
4​
5public final class T3NativeControlsModule: Module {
6 private let presentationSources = T3PresentationSources()
7 private var videoPresentation: T3NativeVideoPresentation?
8 private var filePresentation: T3NativeFilePresentation?
9​
10 public func definition() -> ModuleDefinition {
11 Name("T3NativeControls")
12​
13 AsyncFunction("presentVideo") { (url: URL, title: String, sourceIdentifier: String, identifier: String, promise: Promise) in
14 try self.presentVideo(
15 url: url,
16 title: title,
17 sourceIdentifier: sourceIdentifier,
18 identifier: identifier,
19 promise: promise
20 )
21 }.runOnQueue(.main)
22​
23 AsyncFunction("dismissVideo") { (identifier: String) in
24 self.dismissVideo(identifier: identifier)
25 }.runOnQueue(.main)
26​
27 AsyncFunction("presentFile") { (url: URL, title: String, sourceIdentifier: String, identifier: String, promise: Promise) in
28 try self.presentFile(url: url, title: title, sourceIdentifier: sourceIdentifier,
29 identifier: identifier, promise: promise)
30 }.runOnQueue(.main)
31​
32 AsyncFunction("dismissFile") { (identifier: String) in
33 self.dismissFile(identifier: identifier)
34 }.runOnQueue(.main)
35​
36 OnDestroy {
37 let presentation = self.videoPresentation
38 let file = self.filePresentation
39 DispatchQueue.main.async {
40 presentation?.dismiss()
41 file?.dismiss()
42 }
43 }
44​
45 View(T3PresentationSourceView.self) {
46 ViewName("PresentationSource")
47 Prop("identifier") { (view: T3PresentationSourceView, identifier: String) in
48 view.sources = self.presentationSources
49 view.identifier = identifier
50 }
51 }
52​
53 AsyncFunction("shareFileFromSource") { (url: URL, title: String, identifier: String, promise: Promise) in
54 try self.shareFile(url: url, title: title, sourceIdentifier: identifier, promise: promise)
55 }.runOnQueue(.main)
56​
57 Function("getShowcasePairingUrl") {
58 let arguments = ProcessInfo.processInfo.arguments
59 guard
60 let flagIndex = arguments.firstIndex(of: "--showcasePairingUrl"),
61 arguments.indices.contains(flagIndex + 1)
62 else {
63 return nil as String?
64 }
65 return arguments[flagIndex + 1]
66 }
67​
68 Function("getShowcaseScene") { () -> String? in
69 let scenePath = NSHomeDirectory() + "/Library/Caches/T3ShowcaseScene"
70 if let storedScene = try? String(contentsOfFile: scenePath, encoding: .utf8)
71 .trimmingCharacters(in: .whitespacesAndNewlines), !storedScene.isEmpty {
72 return storedScene
73 }
74 let arguments = ProcessInfo.processInfo.arguments
75 guard
76 let flagIndex = arguments.firstIndex(of: "--showcaseScene"),
77 arguments.indices.contains(flagIndex + 1)
78 else {
79 return nil as String?
80 }
81 return arguments[flagIndex + 1]
82 }
83​
84 // The palette is fixed for the whole capture, so it only ever arrives as a
85 // launch argument — unlike the scene, which the runner rewrites in place.
86 Function("getShowcaseTheme") { () -> String? in
87 let arguments = ProcessInfo.processInfo.arguments
88 guard
89 let flagIndex = arguments.firstIndex(of: "--showcaseTheme"),
90 arguments.indices.contains(flagIndex + 1)
91 else {
92 return nil as String?
93 }
94 return arguments[flagIndex + 1]
95 }
96​
97 Function("getShowcaseOrientation") { () -> String? in
98 let arguments = ProcessInfo.processInfo.arguments
99 guard
100 let flagIndex = arguments.firstIndex(of: "--showcaseOrientation"),
101 arguments.indices.contains(flagIndex + 1)
102 else {
103 return nil as String?
104 }
105 return arguments[flagIndex + 1]
106 }
107​
108 // Rotates the interface without Simulator menu UI scripting, which CI
109 // runners cannot perform (osascript is denied Accessibility access there).
110 AsyncFunction("applyShowcaseOrientation") { (orientation: String) in
111 guard #available(iOS 16.0, *) else { return }
112 let mask: UIInterfaceOrientationMask = orientation == "landscape" ? .landscapeRight : .portrait
113 for case let windowScene as UIWindowScene in UIApplication.shared.connectedScenes {
114 windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: mask)) { error in
115 NSLog("T3NativeControls applyShowcaseOrientation(\(orientation)) failed: \(error)")
116 }
117 for window in windowScene.windows {
118 window.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations()
119 }
120 }
121 }.runOnQueue(.main)
122​
123 // The geometry request above can fail transiently (for example before the
124 // scene is foreground-active), so callers poll this until it settles.
125 // Screen bounds — not the scene's interface orientation — decide the
126 // answer because they match the captured framebuffer: with iPadOS
127 // windowing active, a floating landscape window still reports a portrait
128 // screen, and screenshots would come out portrait.
129 AsyncFunction("getInterfaceOrientation") { () -> String in
130 guard
131 let windowScene = UIApplication.shared.connectedScenes
132 .compactMap({ $0 as? UIWindowScene })
133 .first
134 else {
135 return "unknown"
136 }
137 let bounds = windowScene.screen.coordinateSpace.bounds
138 return bounds.width > bounds.height ? "landscape" : "portrait"
139 }.runOnQueue(.main)
140​
141 Function("prepareShowcaseCapture") {
142 for itemClass in [kSecClassGenericPassword, kSecClassInternetPassword] {
143 SecItemDelete([kSecClass as String: itemClass] as CFDictionary)
144 }
145 }
146​
147 Function("markShowcaseReady") { (scene: String) in
148 let readyPath = NSHomeDirectory() + "/Library/Caches/T3ShowcaseReadyScene"
149 try? scene.write(toFile: readyPath, atomically: true, encoding: .utf8)
150 }
151 }
152​
153 private func presentVideo(url: URL, title: String, sourceIdentifier: String, identifier: String, promise: Promise) throws {
154 let isPlayableURL = url.isFileURL
155 ? FileManager.default.isReadableFile(atPath: url.path)
156 : (["https", "http"].contains(url.scheme?.lowercased() ?? "") && url.host != nil)
157 guard videoPresentation == nil, filePresentation == nil,
158 let presenter = appContext?.utilities?.currentViewController(),
159 isPlayableURL
160 else {
161 throw NSError(
162 domain: "T3NativeVideo",
163 code: 2,
164 userInfo: [NSLocalizedDescriptionKey: "The video preview is no longer available."]
165 )
166 }
167 let presentation = T3NativeVideoPresentation(identifier: identifier, url: url, title: title) { [weak self] error in
168 self?.videoPresentation = nil
169 if let error { promise.reject(error) } else { promise.resolve(nil) }
170 }
171 videoPresentation = presentation
172 presentation.present(from: presenter, sources: presentationSources, sourceIdentifier: sourceIdentifier)
173 }
174​
175 private func dismissVideo(identifier: String) {
176 if videoPresentation?.identifier == identifier { videoPresentation?.dismiss() }
177 }
178​
179 private func presentFile(url: URL, title: String, sourceIdentifier: String,
180 identifier: String, promise: Promise) throws {
181 guard filePresentation == nil, videoPresentation == nil,
182 let presenter = appContext?.utilities?.currentViewController()
183 else { throw URLError(.cannotLoadFromNetwork) }
184 let file = T3NativeFilePresentation(identifier: identifier, sources: presentationSources,
185 sourceIdentifier: sourceIdentifier) { [weak self] error in
186 self?.filePresentation = nil
187 if let error { promise.reject(error) } else { promise.resolve(nil) }
188 }
189 filePresentation = file
190 file.present(url: url, title: title, from: presenter)
191 }
192​
193 private func dismissFile(identifier: String) {
194 if filePresentation?.identifier == identifier { filePresentation?.dismiss() }
195 }
196​
197 private func shareFile(url: URL, title: String, sourceIdentifier: String, promise: Promise) throws {
198 guard let presenter = appContext?.utilities?.currentViewController() else {
199 throw NSError(
200 domain: "T3NativePresentation",
201 code: 2,
202 userInfo: [NSLocalizedDescriptionKey: "The presenting screen is no longer open."]
203 )
204 }
205 try presentFileShare(
206 url: url,
207 title: title,
208 source: presentationSources.view(for: sourceIdentifier),
209 presenter: presenter,
210 promise: promise
211 )
212 }