Skip to content

Commit f8e4acc

Browse files
juliusmarmingeJulius Marminge
andauthored
feat(mobile): add native image and PDF previews (pingdotgg#8959)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 6d15c5b commit f8e4acc

24 files changed

Lines changed: 933 additions & 211 deletions

‎apps/mobile/app.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ const config: ExpoConfig = {
206206
},
207207
NSLocalNetworkUsageDescription:
208208
"Allow T3 Code to connect to T3 Code servers on your local network or tailnet.",
209+
NSPhotoLibraryAddUsageDescription: "Allow T3 Code to save images to your photo library.",
209210
ITSAppUsesNonExemptEncryption: false,
210211
// The App Store screenshot harness rotates the iPad interface from
211212
// inside the app (CI denies osascript the Accessibility access that

‎apps/mobile/modules/t3-native-controls/ios/T3NativeControlsModule.swift‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import UIKit
55
public final class T3NativeControlsModule: Module {
66
private let presentationSources = T3PresentationSources()
77
private var videoPresentation: T3NativeVideoPresentation?
8+
private var filePresentation: T3NativeFilePresentation?
89

910
public func definition() -> ModuleDefinition {
1011
Name("T3NativeControls")
@@ -23,9 +24,22 @@ public final class T3NativeControlsModule: Module {
2324
self.dismissVideo(identifier: identifier)
2425
}.runOnQueue(.main)
2526

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+
2636
OnDestroy {
2737
let presentation = self.videoPresentation
28-
DispatchQueue.main.async { presentation?.dismiss() }
38+
let file = self.filePresentation
39+
DispatchQueue.main.async {
40+
presentation?.dismiss()
41+
file?.dismiss()
42+
}
2943
}
3044

3145
View(T3PresentationSourceView.self) {
@@ -140,7 +154,7 @@ public final class T3NativeControlsModule: Module {
140154
let isPlayableURL = url.isFileURL
141155
? FileManager.default.isReadableFile(atPath: url.path)
142156
: (["https", "http"].contains(url.scheme?.lowercased() ?? "") && url.host != nil)
143-
guard videoPresentation == nil,
157+
guard videoPresentation == nil, filePresentation == nil,
144158
let presenter = appContext?.utilities?.currentViewController(),
145159
isPlayableURL
146160
else {
@@ -162,6 +176,24 @@ public final class T3NativeControlsModule: Module {
162176
if videoPresentation?.identifier == identifier { videoPresentation?.dismiss() }
163177
}
164178

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+
165197
private func shareFile(url: URL, title: String, sourceIdentifier: String, promise: Promise) throws {
166198
guard let presenter = appContext?.utilities?.currentViewController() else {
167199
throw NSError(
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import ImageIO
2+
import QuickLook
3+
import UIKit
4+
import UniformTypeIdentifiers
5+
6+
private final class FilePreviewItem: NSObject, QLPreviewItem {
7+
var previewItemURL: URL?
8+
var previewItemTitle: String?
9+
}
10+
11+
private final class FilePreviewController: QLPreviewController {
12+
var onAppear: (() -> Void)?
13+
14+
override func viewDidAppear(_ animated: Bool) {
15+
super.viewDidAppear(animated)
16+
onAppear?()
17+
}
18+
}
19+
20+
/// Quick Look owns image and document controls, zooming, and source-view transitions.
21+
final class T3NativeFilePresentation: NSObject, QLPreviewControllerDataSource,
22+
QLPreviewControllerDelegate, UIAdaptivePresentationControllerDelegate {
23+
let identifier: String
24+
private var controller: UIViewController?
25+
private let completion: (Error?) -> Void
26+
private weak var sources: T3PresentationSources?
27+
private let sourceIdentifier: String
28+
private let item = FilePreviewItem()
29+
private var loading: Task<Void, Never>?
30+
private var dismissRequested = false
31+
private var finished = false
32+
33+
init(identifier: String, sources: T3PresentationSources, sourceIdentifier: String, completion: @escaping (Error?) -> Void) {
34+
self.identifier = identifier
35+
self.sources = sources
36+
self.sourceIdentifier = sourceIdentifier
37+
self.completion = completion
38+
super.init()
39+
}
40+
41+
func present(url: URL, title: String, from presenter: UIViewController) {
42+
loading = Task { @MainActor [self] in
43+
do {
44+
let file = try await Self.prepareFile(url: url, title: title)
45+
guard !finished, !Task.isCancelled else {
46+
try? FileManager.default.removeItem(at: file.deletingLastPathComponent())
47+
return
48+
}
49+
item.previewItemURL = file
50+
item.previewItemTitle = title
51+
let preview = FilePreviewController()
52+
preview.delegate = self
53+
preview.dataSource = self
54+
preview.onAppear = { [weak self] in self?.resumePendingDismissal() }
55+
controller = preview
56+
presenter.present(preview, animated: !UIAccessibility.isReduceMotionEnabled) { [self] in
57+
resumePendingDismissal()
58+
}
59+
preview.presentationController?.delegate = self
60+
} catch {
61+
finish(error: error)
62+
}
63+
}
64+
}
65+
66+
func dismiss() {
67+
dismissRequested = true
68+
loading?.cancel()
69+
guard !finished else { return }
70+
guard let controller else { finish(); return }
71+
// Drain Close from viewDidAppear after opening or cancelling an interactive dismissal.
72+
// Starting a second modal transition while UIKit is settling the first can strand it.
73+
guard !controller.isBeingPresented, !controller.isBeingDismissed else { return }
74+
controller.dismiss(animated: !UIAccessibility.isReduceMotionEnabled) { [self] in finish() }
75+
}
76+
77+
private func resumePendingDismissal() {
78+
// Appearance callbacks run before UIKit has cleared the current transition.
79+
DispatchQueue.main.async { [weak self] in
80+
if self?.dismissRequested == true { self?.dismiss() }
81+
}
82+
}
83+
84+
func numberOfPreviewItems(in controller: QLPreviewController) -> Int { item.previewItemURL == nil ? 0 : 1 }
85+
86+
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
87+
item
88+
}
89+
90+
func previewController(_ controller: QLPreviewController, transitionViewFor item: QLPreviewItem) -> UIView? {
91+
guard !UIAccessibility.isReduceMotionEnabled else { return nil }
92+
return sources?.view(for: sourceIdentifier)
93+
}
94+
95+
func previewController(_ controller: QLPreviewController, frameFor item: QLPreviewItem,
96+
inSourceView view: AutoreleasingUnsafeMutablePointer<UIView?>) -> CGRect {
97+
guard !UIAccessibility.isReduceMotionEnabled, let source = sources?.view(for: sourceIdentifier) else { return .zero }
98+
view.pointee = source
99+
return source.bounds
100+
}
101+
102+
func previewControllerDidDismiss(_ controller: QLPreviewController) { finish() }
103+
104+
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) { finish() }
105+
106+
private func finish(error: Error? = nil) {
107+
guard !finished else { return }
108+
finished = true
109+
loading?.cancel()
110+
loading = nil
111+
if let file = item.previewItemURL {
112+
try? FileManager.default.removeItem(at: file.deletingLastPathComponent())
113+
}
114+
item.previewItemURL = nil
115+
DispatchQueue.main.async { [completion] in completion(error) }
116+
}
117+
118+
/// Copy original bytes so preview and sharing do not mutate a draft or workspace file.
119+
nonisolated private static func prepareFile(url: URL, title: String) async throws -> URL {
120+
try Task.checkCancellation()
121+
let directory = FileManager.default.temporaryDirectory.appendingPathComponent("t3-preview-\(UUID().uuidString)")
122+
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
123+
do {
124+
let download = directory.appendingPathComponent("original")
125+
if url.isFileURL {
126+
try FileManager.default.copyItem(at: url, to: download)
127+
} else if url.scheme == "data" {
128+
try Data(contentsOf: url).write(to: download, options: .atomic)
129+
} else {
130+
guard ["https", "http"].contains(url.scheme?.lowercased() ?? "") else {
131+
throw URLError(.unsupportedURL)
132+
}
133+
let (temporaryFile, response) = try await URLSession.shared.download(from: url)
134+
guard let response = response as? HTTPURLResponse, (200..<300).contains(response.statusCode) else {
135+
throw URLError(.badServerResponse)
136+
}
137+
try FileManager.default.moveItem(at: temporaryFile, to: download)
138+
}
139+
try Task.checkCancellation()
140+
let type: UTType
141+
if let image = CGImageSourceCreateWithURL(download as CFURL, nil),
142+
CGImageSourceGetCount(image) > 0, let imageType = CGImageSourceGetType(image),
143+
let detectedType = UTType(imageType as String) {
144+
type = detectedType
145+
} else if CGPDFDocument(download as CFURL) != nil {
146+
type = .pdf
147+
} else {
148+
throw URLError(.cannotDecodeContentData)
149+
}
150+
let filename = URL(fileURLWithPath: title).lastPathComponent as NSString
151+
let originalExtension = filename.pathExtension
152+
let fileExtension = UTType(filenameExtension: originalExtension) == type
153+
? originalExtension : type.preferredFilenameExtension ?? "png"
154+
let stem = filename.deletingPathExtension
155+
var name = String(stem.prefix(60)).components(separatedBy: .controlCharacters).joined(separator: "_")
156+
while name.utf8.count > 200 { name.removeLast() }
157+
let file = directory.appendingPathComponent("\(name.isEmpty ? "Preview" : name).\(fileExtension)")
158+
try FileManager.default.moveItem(at: download, to: file)
159+
return file
160+
} catch {
161+
try? FileManager.default.removeItem(at: directory)
162+
throw error
163+
}
164+
}
165+
}

‎apps/mobile/src/components/ComposerAttachmentStrip.tsx‎

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ import { Alert, Image, Pressable, ScrollView, View } from "react-native";
66
import { AppText as Text } from "./AppText";
77
import type { DraftComposerAttachment, DraftComposerFileAttachment } from "../lib/composerImages";
88
import { VideoAttachmentTile } from "./VideoAttachmentTile";
9-
import { loadLocalVideoPreview } from "../lib/localVideoPreview";
9+
import { loadLocalAttachmentPreview } from "../lib/localAttachmentPreview";
10+
import { PresentationSource } from "./NativePresentation";
11+
import type { FilePreviewSource } from "./FilePreviewModal";
12+
import { isPdfFile } from "../lib/filePreview";
1013

1114
export interface ComposerAttachmentStripProps {
1215
/** Attachments to display. */
1316
readonly attachments: ReadonlyArray<DraftComposerAttachment>;
1417
/** Called when the user removes an attachment. */
1518
readonly onRemove: (imageId: string) => void;
16-
/** Called when the user taps on an image thumbnail to preview it. */
17-
readonly onPressImage?: (previewUri: string) => void;
19+
/** Called when the user taps an image or PDF to preview it. */
20+
readonly onPressPreview?: (source: FilePreviewSource) => void;
1821
readonly onPressVideo?: (
1922
attachment: DraftComposerFileAttachment,
2023
sourceIdentifier: string,
@@ -32,7 +35,7 @@ export function ComposerAttachmentThumbnail(props: {
3235
readonly size: number;
3336
readonly borderRadius: number;
3437
readonly compact?: boolean;
35-
readonly onPressImage?: (previewUri: string) => void;
38+
readonly onPressPreview?: (source: FilePreviewSource) => void;
3639
readonly onPressVideo?: (
3740
attachment: DraftComposerFileAttachment,
3841
sourceIdentifier: string,
@@ -41,17 +44,30 @@ export function ComposerAttachmentThumbnail(props: {
4144
const { attachment } = props;
4245
const style = { width: props.size, height: props.size, borderRadius: props.borderRadius };
4346
if (attachment.type === "image") {
47+
const sourceIdentifier = `draft-image:${attachment.id}`;
4448
return (
45-
<Pressable
46-
onPress={props.onPressImage ? () => props.onPressImage?.(attachment.previewUri) : undefined}
47-
>
48-
<Image
49-
source={{ uri: attachment.previewUri }}
50-
style={style}
51-
className="bg-subtle"
52-
resizeMode="cover"
53-
/>
54-
</Pressable>
49+
<PresentationSource identifier={sourceIdentifier}>
50+
<Pressable
51+
accessibilityRole="imagebutton"
52+
accessibilityLabel={`Open ${attachment.name}`}
53+
disabled={!props.onPressPreview}
54+
onPress={() =>
55+
props.onPressPreview?.({
56+
kind: "image",
57+
uri: attachment.dataUrl,
58+
name: attachment.name,
59+
sourceIdentifier,
60+
})
61+
}
62+
>
63+
<Image
64+
source={{ uri: attachment.previewUri }}
65+
style={style}
66+
className="bg-subtle"
67+
resizeMode="cover"
68+
/>
69+
</Pressable>
70+
</PresentationSource>
5571
);
5672
}
5773
const onPressVideo = props.onPressVideo;
@@ -60,27 +76,42 @@ export function ComposerAttachmentThumbnail(props: {
6076
<ComposerVideoAttachment {...props} attachment={attachment} onPressVideo={onPressVideo} />
6177
);
6278
}
79+
const canPreview = isPdfFile(attachment) && props.onPressPreview !== undefined;
80+
const sourceIdentifier = `draft-file:${attachment.id}`;
6381
return (
64-
<View
65-
className={
66-
props.compact
67-
? "items-center justify-center bg-subtle"
68-
: "items-center justify-center gap-1 bg-subtle px-2"
69-
}
70-
style={style}
71-
>
72-
<SymbolView
73-
name="doc.text"
74-
size={props.compact ? 15 : 22}
75-
tintColor="#a3a3a3"
76-
type="monochrome"
77-
/>
78-
{!props.compact ? (
79-
<Text className="w-full text-center text-2xs text-foreground" numberOfLines={1}>
80-
{attachment.name}
81-
</Text>
82-
) : null}
83-
</View>
82+
<PresentationSource identifier={sourceIdentifier}>
83+
<Pressable
84+
accessibilityRole="button"
85+
accessibilityLabel={`Open ${attachment.name}`}
86+
disabled={!canPreview}
87+
onPress={() =>
88+
props.onPressPreview?.({
89+
kind: "pdf",
90+
name: attachment.name,
91+
attachment,
92+
sourceIdentifier,
93+
})
94+
}
95+
className={
96+
props.compact
97+
? "items-center justify-center bg-subtle"
98+
: "items-center justify-center gap-1 bg-subtle px-2"
99+
}
100+
style={style}
101+
>
102+
<SymbolView
103+
name="doc.text"
104+
size={props.compact ? 15 : 22}
105+
tintColor="#a3a3a3"
106+
type="monochrome"
107+
/>
108+
{!props.compact ? (
109+
<Text className="w-full text-center text-2xs text-foreground" numberOfLines={1}>
110+
{attachment.name}
111+
</Text>
112+
) : null}
113+
</Pressable>
114+
</PresentationSource>
84115
);
85116
}
86117

@@ -113,7 +144,7 @@ function ComposerVideoAttachment(props: {
113144
shareRef.current = controller;
114145
setSharing(true);
115146
void (async () => {
116-
const preview = await loadLocalVideoPreview(attachment, controller.signal);
147+
const preview = await loadLocalAttachmentPreview(attachment, controller.signal);
117148
if (!preview) return;
118149
try {
119150
await preview.share(controller.signal, sourceIdentifier);
@@ -185,7 +216,7 @@ export function ComposerAttachmentStrip(props: ComposerAttachmentStripProps) {
185216
attachment={attachment}
186217
size={size}
187218
borderRadius={radius}
188-
onPressImage={props.onPressImage}
219+
onPressPreview={props.onPressPreview}
189220
onPressVideo={props.onPressVideo}
190221
/>
191222
<Pressable

0 commit comments

Comments
 (0)