From aba5e44bca959bb6a58d5eadf8dd8898e4acabbc Mon Sep 17 00:00:00 2001 From: Arnab Chakraborty <11457760+Rocky43007@users.noreply.github.com> Date: Thu, 28 Nov 2024 03:44:21 -0500 Subject: [PATCH] Only print when in debug mode --- .../native-functions/NativeFunctions.swift | 82 ++++++++++++++----- 1 file changed, 62 insertions(+), 20 deletions(-) diff --git a/apps/mobile/modules/native-functions/NativeFunctions.swift b/apps/mobile/modules/native-functions/NativeFunctions.swift index d28ff6fa1..6a3ea4d76 100644 --- a/apps/mobile/modules/native-functions/NativeFunctions.swift +++ b/apps/mobile/modules/native-functions/NativeFunctions.swift @@ -12,17 +12,17 @@ import QuickLook @objc(NativeFunctions) class NativeFunctions: NSObject, QLPreviewControllerDataSource { private var fileURL: URL? - + @objc static func requiresMainQueueSetup() -> Bool { return true } - + private func getBookmarkStoragePath(for id: Int) -> URL { let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] return documentsDirectory.appendingPathComponent("\(id).sd_bookmark") } - + @objc func saveLocation(_ path: String, locationId: NSNumber, @@ -35,41 +35,51 @@ class NativeFunctions: NSObject, QLPreviewControllerDataSource { return } defer { url.stopAccessingSecurityScopedResource() } - + let bookmarkData = try url.bookmarkData( options: .minimalBookmark, includingResourceValuesForKeys: nil, relativeTo: nil ) - + let bookmarkPath = getBookmarkStoragePath(for: locationId.intValue) try bookmarkData.write(to: bookmarkPath, options: .atomicWrite) - + resolve(["success": true]) } catch { reject("ERROR", "Failed to create bookmark: \(error.localizedDescription)", nil) } } - + @objc func previewFile(_ path: String, locationId: NSNumber, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) { + #if DEBUG print("🔍 PreviewFile called with path: \(path), locationId: \(locationId)") - + #endif + do { let bookmarkPath = getBookmarkStoragePath(for: locationId.intValue) + #if DEBUG print("📁 Bookmark path: \(bookmarkPath)") - + #endif + let fileURL = URL(fileURLWithPath: path) + #if DEBUG print("📄 File URL: \(fileURL)") - + #endif + if FileManager.default.fileExists(atPath: bookmarkPath.path) { + #if DEBUG print("✅ Bookmark exists at path") + #endif let bookmarkData = try Data(contentsOf: bookmarkPath) + #if DEBUG print("📊 Bookmark data size: \(bookmarkData.count) bytes") - + #endif + var isStale = false let directoryURL = try URL( resolvingBookmarkData: bookmarkData, @@ -77,74 +87,106 @@ class NativeFunctions: NSObject, QLPreviewControllerDataSource { relativeTo: nil, bookmarkDataIsStale: &isStale ) + #if DEBUG print("📂 Resolved directory URL: \(directoryURL)") print("🔄 Is bookmark stale? \(isStale)") - + #endif + guard directoryURL.startAccessingSecurityScopedResource() else { + #if DEBUG print("❌ Failed to access security-scoped resource for directory") + #endif reject("ERROR", "Cannot access directory", nil) return } defer { directoryURL.stopAccessingSecurityScopedResource() + #if DEBUG print("🔒 Stopped accessing security-scoped resource") + #endif } - + let fileName = fileURL.lastPathComponent + #if DEBUG print("📝 File name: \(fileName)") - + #endif + let resolvedFileURL = directoryURL.appendingPathComponent(fileName) + #if DEBUG print("🎯 Resolved file URL: \(resolvedFileURL)") - + #endif + // Check if file exists at resolved path if FileManager.default.fileExists(atPath: resolvedFileURL.path) { + #if DEBUG print("✅ File exists at resolved path") + #endif } else { + #if DEBUG print("⚠️ File does not exist at resolved path") + #endif } - + self.fileURL = resolvedFileURL + #if DEBUG print("💾 Set fileURL for QuickLook: \(resolvedFileURL)") + #endif } else { + #if DEBUG print("❌ Bookmark not found at path: \(bookmarkPath)") + #endif reject("ERROR", "Bookmark not found for this location", nil) return } - + + #if DEBUG print("🚀 Preparing to present QuickLook controller") + #endif DispatchQueue.main.async { let previewController = QLPreviewController() previewController.dataSource = self - + guard let presentedVC = RCTPresentedViewController() else { + #if DEBUG print("❌ Failed to get presented view controller") + #endif reject("ERROR", "Cannot present preview", nil) return } - + + #if DEBUG print("📱 Presenting QuickLook controller") + #endif presentedVC.present(previewController, animated: true) { + #if DEBUG print("✨ QuickLook controller presented successfully") + #endif resolve(["success": true]) } } } catch { + #if DEBUG print("💥 Error occurred: \(error.localizedDescription)") print("🔍 Detailed error: \(error)") + #endif reject("ERROR", "Failed to preview file: \(error.localizedDescription)", nil) } } - + // MARK: - QLPreviewControllerDataSource func numberOfPreviewItems(in controller: QLPreviewController) -> Int { + #if DEBUG print("📊 numberOfPreviewItems called, returning: \(fileURL != nil ? 1 : 0)") + #endif return fileURL != nil ? 1 : 0 } func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem { + #if DEBUG print("🎯 previewItemAt called for index: \(index)") print("📄 Returning fileURL: \(String(describing: fileURL))") + #endif return fileURL! as QLPreviewItem } }