even more handling crazy fixes

This commit is contained in:
Francesco 2025-04-22 17:30:55 +02:00
parent 304ddbf2fd
commit 5ea21da2cc

View file

@ -32,12 +32,25 @@ class ModuleManager: ObservableObject {
} }
@objc private func handleModulesSyncCompleted() { @objc private func handleModulesSyncCompleted() {
DispatchQueue.main.async { DispatchQueue.main.async { [weak self] in
self.loadModules() guard let self = self else { return }
Task {
await self.checkJSModuleFiles() 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")
} }
} }
@ -52,15 +65,34 @@ class ModuleManager: ObservableObject {
func loadModules() { func loadModules() {
let url = getModulesFilePath() let url = getModulesFilePath()
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 { do {
let data = try Data(contentsOf: url) let data = try Data(contentsOf: url)
modules = (try? JSONDecoder().decode([ScrapingModule].self, from: data)) ?? [] do {
let decodedModules = try JSONDecoder().decode([ScrapingModule].self, from: data)
modules = decodedModules
Task { Task {
await checkJSModuleFiles() await checkJSModuleFiles()
}
} catch {
Logger.shared.log("Failed to decode modules: \(error.localizedDescription)", type: "Error")
try "[]".write(to: url, atomically: true, encoding: .utf8)
modules = []
} }
} catch { } catch {
Logger.shared.log("Failed to load modules: \(error.localizedDescription)", type: "Error") Logger.shared.log("Failed to load modules file: \(error.localizedDescription)", type: "Error")
modules = [] modules = []
} }
} }