@@ -23,11 +23,11 @@ struct ContentPreview: View {
23
23
Group {
24
24
if isLoading {
25
25
ProgressView ( )
26
- . onAppear {
27
- print ( " 🔄 ContentPreview: Loading view appeared " )
28
- print ( " 📄 ContentPreview: MIME type: \( mimeType) " )
29
- print ( " 🔗 ContentPreview: File URL: \( fileURL) " )
30
- }
26
+ // .onAppear {
27
+ // print("🔄 ContentPreview: Loading view appeared")
28
+ // print("📄 ContentPreview: MIME type: \(mimeType)")
29
+ // print("🔗 ContentPreview: File URL: \(fileURL)")
30
+ // }
31
31
} else if let error = error {
32
32
VStack {
33
33
Image ( systemName: " exclamationmark.triangle " )
@@ -36,24 +36,24 @@ struct ContentPreview: View {
36
36
. multilineTextAlignment ( . center)
37
37
. padding ( )
38
38
}
39
- . onAppear {
40
- print ( " ❌ ContentPreview: Error view appeared - \( error. localizedDescription) " )
41
- }
39
+ // .onAppear {
40
+ // print("❌ ContentPreview: Error view appeared - \(error.localizedDescription)")
41
+ // }
42
42
} else {
43
43
contentView
44
- . onAppear {
45
- print ( " ✅ ContentPreview: Content view appeared " )
46
- }
44
+ // .onAppear {
45
+ // print("✅ ContentPreview: Content view appeared")
46
+ // }
47
47
}
48
48
}
49
49
. onAppear {
50
- print ( " 📱 ContentPreview: View appeared - URL: \( fileURL) " )
50
+ // print("📱 ContentPreview: View appeared - URL: \(fileURL)")
51
51
loadContent ( )
52
52
loadFileDetails ( )
53
53
isPreviewing = true
54
54
}
55
55
. onDisappear {
56
- print ( " 👋 ContentPreview: View disappeared " )
56
+ // print("👋 ContentPreview: View disappeared")
57
57
isPreviewing = false
58
58
}
59
59
}
@@ -145,17 +145,26 @@ struct ContentPreview: View {
145
145
}
146
146
147
147
private func loadContent( ) {
148
- print ( " 📥 ContentPreview: Starting content load " )
149
- isLoading = true
148
+ // print("📥 ContentPreview: Starting content load")
150
149
151
150
// For video, audio, and PDF, we don't need to download the content as we'll use the URL directly
152
151
if mimeType. starts ( with: " video/ " ) || mimeType. starts ( with: " audio/ " ) || mimeType == " application/pdf " {
153
- print ( " 🎥 ContentPreview: Using direct URL for media/PDF " )
152
+ // print("🎥 ContentPreview: Using direct URL for media/PDF")
154
153
isLoading = false
155
154
return
156
155
}
157
156
158
- print ( " 📥 ContentPreview: Downloading content from URL " )
157
+ // Check if content is already cached
158
+ if let cachedData = ImageCache . shared. getContent ( for: fileURL. absoluteString) {
159
+ // print("✅ ContentPreview: Found cached content")
160
+ self . content = cachedData
161
+ self . isLoading = false
162
+ return
163
+ }
164
+
165
+ // print("📥 ContentPreview: Downloading content from URL")
166
+ isLoading = true
167
+
159
168
Task {
160
169
do {
161
170
let data = try await CachedContentLoader . loadContent ( from: fileURL)
@@ -164,7 +173,7 @@ struct ContentPreview: View {
164
173
self . isLoading = false
165
174
}
166
175
} catch {
167
- print ( " ❌ ContentPreview: Download error - \( error. localizedDescription) " )
176
+ // print("❌ ContentPreview: Download error - \(error.localizedDescription)")
168
177
await MainActor . run {
169
178
self . error = error
170
179
self . isLoading = false
@@ -174,24 +183,22 @@ struct ContentPreview: View {
174
183
}
175
184
176
185
private func loadFileDetails( ) {
177
- print ( " 📋 ContentPreview: Loading file details " )
186
+ // print("📋 ContentPreview: Loading file details")
178
187
guard let serverURL = URL ( string: file. url) ? . host else {
179
- print ( " ❌ ContentPreview: Could not extract server URL from file URL " )
188
+ // print("❌ ContentPreview: Could not extract server URL from file URL")
180
189
return
181
190
}
182
191
let baseURL = URL ( string: " https:// \( serverURL) " ) !
183
192
let api = DFAPI ( url: baseURL, token: " " )
184
193
185
194
Task {
186
- print ( " 🌐 ContentPreview: Fetching file details from API " )
195
+ // print("🌐 ContentPreview: Fetching file details from API")
187
196
if let details = await api. getFileDetails ( fileID: file. id) {
188
- print ( " ✅ ContentPreview: Successfully fetched file details " )
197
+ // print("✅ ContentPreview: Successfully fetched file details")
189
198
await MainActor . run {
190
199
self . fileDetails = details
191
200
self . selectedFileDetails = details
192
201
}
193
- } else {
194
- print ( " ❌ ContentPreview: Failed to fetch file details " )
195
202
}
196
203
}
197
204
}
@@ -298,9 +305,38 @@ struct PageViewController: UIViewControllerRepresentable {
298
305
vc. view. backgroundColor = . clear
299
306
vc. view. isOpaque = false
300
307
preloadedViewControllers [ index] = vc
308
+
309
+ // Trigger content loading for this view controller
310
+ Task {
311
+ await preloadContentForViewController ( vc, file: file)
312
+ }
313
+
301
314
return vc
302
315
}
303
316
317
+ private func preloadContentForViewController( _ vc: UIHostingController < ContentPreview > , file: DFFile ) async {
318
+ // Only preload content for files that need it (not video, audio, or PDF)
319
+ if file. mime. starts ( with: " video/ " ) || file. mime. starts ( with: " audio/ " ) || file. mime == " application/pdf " {
320
+ return
321
+ }
322
+
323
+ let urlString = parent. redirectURLs [ file. raw] ?? file. raw
324
+ guard let url = URL ( string: urlString) else { return }
325
+
326
+ // Check if content is already cached
327
+ if ImageCache . shared. getContent ( for: url. absoluteString) != nil {
328
+ return
329
+ }
330
+
331
+ // Preload the content
332
+ do {
333
+ let _ = try await CachedContentLoader . loadContent ( from: url)
334
+ // print("✅ PageViewController preloaded content for: \(file.name)")
335
+ } catch {
336
+ // print("❌ PageViewController failed to preload content for: \(file.name) - \(error.localizedDescription)")
337
+ }
338
+ }
339
+
304
340
func pageViewController( _ pageViewController: UIPageViewController , viewControllerBefore viewController: UIViewController ) -> UIViewController ? {
305
341
guard let currentVC = viewController as? UIHostingController < ContentPreview > ,
306
342
let currentIndex = parent. files. firstIndex ( where: { $0. id == currentVC. rootView. file. id } ) ,
@@ -433,21 +469,56 @@ struct FilePreviewView: View {
433
469
for fileToLoad in filesToLoad {
434
470
group. addTask {
435
471
await loadSingleFileRedirect ( fileToLoad)
472
+ // Also preload content for files that need it
473
+ await preloadFileContent ( fileToLoad)
436
474
}
437
475
}
438
476
}
439
477
}
440
478
479
+ @MainActor
480
+ private func preloadFileContent( _ file: DFFile ) async {
481
+ // Only preload content for files that need it (not video, audio, or PDF)
482
+ if file. mime. starts ( with: " video/ " ) || file. mime. starts ( with: " audio/ " ) || file. mime == " application/pdf " {
483
+ return
484
+ }
485
+
486
+ // Get the redirect URL if available, otherwise use the raw URL
487
+ let urlString = redirectURLs [ file. raw] ?? file. raw
488
+ guard let url = URL ( string: urlString) else { return }
489
+
490
+ // Check if content is already cached
491
+ if ImageCache . shared. getContent ( for: url. absoluteString) != nil {
492
+ return
493
+ }
494
+
495
+ // Preload the content
496
+ do {
497
+ let _ = try await CachedContentLoader . loadContent ( from: url)
498
+ // print("✅ Preloaded content for: \(file.name)")
499
+ } catch {
500
+ // print("❌ Failed to preload content for: \(file.name) - \(error.localizedDescription)")
501
+ }
502
+ }
503
+
441
504
var body : some View {
442
505
GeometryReader { geometry in
443
506
ZStack {
444
507
if redirectURLs [ file. raw] == nil {
445
- ProgressView ( )
446
- . onAppear {
447
- Task {
448
- await preloadFiles ( )
449
- }
508
+ VStack {
509
+ Spacer ( )
510
+ HStack {
511
+ Spacer ( )
512
+ ProgressView ( )
513
+ . onAppear {
514
+ Task {
515
+ await preloadFiles ( )
516
+ }
517
+ }
518
+ Spacer ( )
450
519
}
520
+ Spacer ( )
521
+ }
451
522
} else {
452
523
PageViewController (
453
524
files: allFiles,
0 commit comments