Skip to content

Commit 82bedb1

Browse files
committed
fix preloading, center progress view on preview
1 parent 2bf6216 commit 82bedb1

File tree

1 file changed

+100
-29
lines changed

1 file changed

+100
-29
lines changed

Django Files/Views/Preview/Preview.swift

Lines changed: 100 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ struct ContentPreview: View {
2323
Group {
2424
if isLoading {
2525
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+
// }
3131
} else if let error = error {
3232
VStack {
3333
Image(systemName: "exclamationmark.triangle")
@@ -36,24 +36,24 @@ struct ContentPreview: View {
3636
.multilineTextAlignment(.center)
3737
.padding()
3838
}
39-
.onAppear {
40-
print("❌ ContentPreview: Error view appeared - \(error.localizedDescription)")
41-
}
39+
// .onAppear {
40+
// print("❌ ContentPreview: Error view appeared - \(error.localizedDescription)")
41+
// }
4242
} else {
4343
contentView
44-
.onAppear {
45-
print("✅ ContentPreview: Content view appeared")
46-
}
44+
// .onAppear {
45+
// print("✅ ContentPreview: Content view appeared")
46+
// }
4747
}
4848
}
4949
.onAppear {
50-
print("📱 ContentPreview: View appeared - URL: \(fileURL)")
50+
// print("📱 ContentPreview: View appeared - URL: \(fileURL)")
5151
loadContent()
5252
loadFileDetails()
5353
isPreviewing = true
5454
}
5555
.onDisappear {
56-
print("👋 ContentPreview: View disappeared")
56+
// print("👋 ContentPreview: View disappeared")
5757
isPreviewing = false
5858
}
5959
}
@@ -145,17 +145,26 @@ struct ContentPreview: View {
145145
}
146146

147147
private func loadContent() {
148-
print("📥 ContentPreview: Starting content load")
149-
isLoading = true
148+
// print("📥 ContentPreview: Starting content load")
150149

151150
// For video, audio, and PDF, we don't need to download the content as we'll use the URL directly
152151
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")
154153
isLoading = false
155154
return
156155
}
157156

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+
159168
Task {
160169
do {
161170
let data = try await CachedContentLoader.loadContent(from: fileURL)
@@ -164,7 +173,7 @@ struct ContentPreview: View {
164173
self.isLoading = false
165174
}
166175
} catch {
167-
print("❌ ContentPreview: Download error - \(error.localizedDescription)")
176+
// print("❌ ContentPreview: Download error - \(error.localizedDescription)")
168177
await MainActor.run {
169178
self.error = error
170179
self.isLoading = false
@@ -174,24 +183,22 @@ struct ContentPreview: View {
174183
}
175184

176185
private func loadFileDetails() {
177-
print("📋 ContentPreview: Loading file details")
186+
// print("📋 ContentPreview: Loading file details")
178187
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")
180189
return
181190
}
182191
let baseURL = URL(string: "https://\(serverURL)")!
183192
let api = DFAPI(url: baseURL, token: "")
184193

185194
Task {
186-
print("🌐 ContentPreview: Fetching file details from API")
195+
// print("🌐 ContentPreview: Fetching file details from API")
187196
if let details = await api.getFileDetails(fileID: file.id) {
188-
print("✅ ContentPreview: Successfully fetched file details")
197+
// print("✅ ContentPreview: Successfully fetched file details")
189198
await MainActor.run {
190199
self.fileDetails = details
191200
self.selectedFileDetails = details
192201
}
193-
} else {
194-
print("❌ ContentPreview: Failed to fetch file details")
195202
}
196203
}
197204
}
@@ -298,9 +305,38 @@ struct PageViewController: UIViewControllerRepresentable {
298305
vc.view.backgroundColor = .clear
299306
vc.view.isOpaque = false
300307
preloadedViewControllers[index] = vc
308+
309+
// Trigger content loading for this view controller
310+
Task {
311+
await preloadContentForViewController(vc, file: file)
312+
}
313+
301314
return vc
302315
}
303316

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+
304340
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
305341
guard let currentVC = viewController as? UIHostingController<ContentPreview>,
306342
let currentIndex = parent.files.firstIndex(where: { $0.id == currentVC.rootView.file.id }),
@@ -433,21 +469,56 @@ struct FilePreviewView: View {
433469
for fileToLoad in filesToLoad {
434470
group.addTask {
435471
await loadSingleFileRedirect(fileToLoad)
472+
// Also preload content for files that need it
473+
await preloadFileContent(fileToLoad)
436474
}
437475
}
438476
}
439477
}
440478

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+
441504
var body: some View {
442505
GeometryReader { geometry in
443506
ZStack {
444507
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()
450519
}
520+
Spacer()
521+
}
451522
} else {
452523
PageViewController(
453524
files: allFiles,

0 commit comments

Comments
 (0)