diff --git a/Sora/Utils/Modules/ModuleManager.swift b/Sora/Utils/Modules/ModuleManager.swift index 9c1d48d..0503025 100644 --- a/Sora/Utils/Modules/ModuleManager.swift +++ b/Sora/Utils/Modules/ModuleManager.swift @@ -14,6 +14,15 @@ class ModuleManager: ObservableObject { private let modulesFileName = "modules.json" init() { + let url = getModulesFilePath() + if (!FileManager.default.fileExists(atPath: url.path)) { + do { + try "[]".write(to: url, atomically: true, encoding: .utf8) + Logger.shared.log("Created empty modules file", type: "Info") + } catch { + Logger.shared.log("Failed to create modules file: \(error.localizedDescription)", type: "Error") + } + } loadModules() NotificationCenter.default.addObserver(self, selector: #selector(handleModulesSyncCompleted), name: .modulesSyncDidComplete, object: nil) } @@ -23,12 +32,25 @@ class ModuleManager: ObservableObject { } @objc private func handleModulesSyncCompleted() { - DispatchQueue.main.async { - self.loadModules() - Task { - await self.checkJSModuleFiles() + DispatchQueue.main.async { [weak self] in + guard let self = self else { return } + + do { + let url = self.getModulesFilePath() + if FileManager.default.fileExists(atPath: url.path) { + self.loadModules() + Task { + await self.checkJSModuleFiles() + } + Logger.shared.log("Reloaded modules after iCloud sync") + } else { + Logger.shared.log("No modules file found after sync", type: "Error") + self.modules = [] + } + } catch { + Logger.shared.log("Error handling modules sync: \(error.localizedDescription)", type: "Error") + self.modules = [] } - Logger.shared.log("Reloaded modules after iCloud sync") } } @@ -42,11 +64,36 @@ class ModuleManager: ObservableObject { func loadModules() { let url = getModulesFilePath() - guard let data = try? Data(contentsOf: url) else { return } - modules = (try? JSONDecoder().decode([ScrapingModule].self, from: data)) ?? [] - Task { - await checkJSModuleFiles() + guard FileManager.default.fileExists(atPath: url.path) else { + Logger.shared.log("Modules file does not exist, creating empty one", type: "Info") + do { + try "[]".write(to: url, atomically: true, encoding: .utf8) + modules = [] + } catch { + Logger.shared.log("Failed to create modules file: \(error.localizedDescription)", type: "Error") + modules = [] + } + return + } + + do { + let data = try Data(contentsOf: url) + do { + let decodedModules = try JSONDecoder().decode([ScrapingModule].self, from: data) + modules = decodedModules + + Task { + await checkJSModuleFiles() + } + } catch { + Logger.shared.log("Failed to decode modules: \(error.localizedDescription)", type: "Error") + try "[]".write(to: url, atomically: true, encoding: .utf8) + modules = [] + } + } catch { + Logger.shared.log("Failed to load modules file: \(error.localizedDescription)", type: "Error") + modules = [] } } diff --git a/public-build/Sulfur.ipa b/public-build/Sulfur.ipa index f7b083e..0d71231 100644 Binary files a/public-build/Sulfur.ipa and b/public-build/Sulfur.ipa differ