fixed module icloud sync maybe

This commit is contained in:
Francesco 2025-04-19 09:20:17 +02:00
parent e9c9340986
commit 7ed5048b0b
3 changed files with 43 additions and 12 deletions

View file

@ -11,4 +11,5 @@ extension Notification.Name {
static let iCloudSyncDidComplete = Notification.Name("iCloudSyncDidComplete")
static let ContinueWatchingDidUpdate = Notification.Name("ContinueWatchingDidUpdate")
static let DownloadManagerStatusUpdate = Notification.Name("DownloadManagerStatusUpdate")
static let modulesSyncDidComplete = Notification.Name("modulesSyncDidComplete")
}

View file

@ -15,6 +15,21 @@ class ModuleManager: ObservableObject {
init() {
loadModules()
NotificationCenter.default.addObserver(self, selector: #selector(handleModulesSyncCompleted), name: .modulesSyncDidComplete, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
@objc private func handleModulesSyncCompleted() {
DispatchQueue.main.async {
self.loadModules()
Task {
await self.cehckJSModuleFIle()
}
Logger.shared.log("Reloaded modules after iCloud sync")
}
}
private func getDocumentsDirectory() -> URL {
@ -31,19 +46,30 @@ class ModuleManager: ObservableObject {
modules = (try? JSONDecoder().decode([ScrapingModule].self, from: data)) ?? []
Task {
for module in modules {
let localUrl = getDocumentsDirectory().appendingPathComponent(module.localPath)
if (!fileManager.fileExists(atPath: localUrl.path)) {
do {
let scriptUrl = URL(string: module.metadata.scriptUrl)
guard let scriptUrl = scriptUrl else { continue }
let (scriptData, _) = try await URLSession.custom.data(from: scriptUrl)
guard let jsContent = String(data: scriptData, encoding: .utf8) else { continue }
try jsContent.write(to: localUrl, atomically: true, encoding: .utf8)
Logger.shared.log("Recovered missing JS file for module: \(module.metadata.sourceName)")
} catch {
Logger.shared.log("Failed to recover JS file for module: \(module.metadata.sourceName) - \(error.localizedDescription)")
await cehckJSModuleFIle()
}
}
func cehckJSModuleFIle() async {
for module in modules {
let localUrl = getDocumentsDirectory().appendingPathComponent(module.localPath)
if (!fileManager.fileExists(atPath: localUrl.path)) {
do {
guard let scriptUrl = URL(string: module.metadata.scriptUrl) else {
Logger.shared.log("Invalid script URL for module: \(module.metadata.sourceName)", type: "Error")
continue
}
let (scriptData, _) = try await URLSession.custom.data(from: scriptUrl)
guard let jsContent = String(data: scriptData, encoding: .utf8) else {
Logger.shared.log("Invalid script encoding for module: \(module.metadata.sourceName)", type: "Error")
continue
}
try jsContent.write(to: localUrl, atomically: true, encoding: .utf8)
Logger.shared.log("Recovered missing JS file for module: \(module.metadata.sourceName)")
} catch {
Logger.shared.log("Failed to recover JS file for module: \(module.metadata.sourceName) - \(error.localizedDescription)", type: "Error")
}
}
}

View file

@ -174,6 +174,10 @@ class iCloudSyncManager {
try FileManager.default.removeItem(at: localModulesURL)
}
try FileManager.default.copyItem(at: iCloudModulesURL, to: localModulesURL)
DispatchQueue.main.async {
NotificationCenter.default.post(name: .modulesSyncDidComplete, object: nil)
}
}
} catch {
Logger.shared.log("iCloud modules fetch error: \(error)", type: "Error")