yeah tf is this idk (#89)

This commit is contained in:
cranci 2025-04-19 09:42:44 +02:00 committed by GitHub
commit 079fd395d7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 57 additions and 37 deletions

View file

@ -34,8 +34,9 @@ struct SoraApp: App {
.accentColor(settings.accentColor) .accentColor(settings.accentColor)
.onAppear { .onAppear {
settings.updateAppearance() settings.updateAppearance()
if UserDefaults.standard.bool(forKey: "refreshModulesOnLaunch") { iCloudSyncManager.shared.syncModulesFromiCloud()
Task { Task {
if UserDefaults.standard.bool(forKey: "refreshModulesOnLaunch") {
await moduleManager.refreshModules() await moduleManager.refreshModules()
} }
} }
@ -97,4 +98,4 @@ struct SoraApp: App {
Logger.shared.log("Unknown authentication service", type: "Error") Logger.shared.log("Unknown authentication service", type: "Error")
} }
} }
} }

View file

@ -26,7 +26,7 @@ class ModuleManager: ObservableObject {
DispatchQueue.main.async { DispatchQueue.main.async {
self.loadModules() self.loadModules()
Task { Task {
await self.cehckJSModuleFIle() await self.checkJSModuleFiles()
} }
Logger.shared.log("Reloaded modules after iCloud sync") Logger.shared.log("Reloaded modules after iCloud sync")
} }
@ -46,20 +46,26 @@ class ModuleManager: ObservableObject {
modules = (try? JSONDecoder().decode([ScrapingModule].self, from: data)) ?? [] modules = (try? JSONDecoder().decode([ScrapingModule].self, from: data)) ?? []
Task { Task {
await cehckJSModuleFIle() await checkJSModuleFiles()
} }
} }
func cehckJSModuleFIle() async { func checkJSModuleFiles() async {
Logger.shared.log("Checking JS module files...", type: "Info")
var missingCount = 0
for module in modules { for module in modules {
let localUrl = getDocumentsDirectory().appendingPathComponent(module.localPath) let localUrl = getDocumentsDirectory().appendingPathComponent(module.localPath)
if (!fileManager.fileExists(atPath: localUrl.path)) { if !fileManager.fileExists(atPath: localUrl.path) {
missingCount += 1
do { do {
guard let scriptUrl = URL(string: module.metadata.scriptUrl) else { guard let scriptUrl = URL(string: module.metadata.scriptUrl) else {
Logger.shared.log("Invalid script URL for module: \(module.metadata.sourceName)", type: "Error") Logger.shared.log("Invalid script URL for module: \(module.metadata.sourceName)", type: "Error")
continue continue
} }
Logger.shared.log("Downloading missing JS file for: \(module.metadata.sourceName)", type: "Info")
let (scriptData, _) = try await URLSession.custom.data(from: scriptUrl) let (scriptData, _) = try await URLSession.custom.data(from: scriptUrl)
guard let jsContent = String(data: scriptData, encoding: .utf8) else { guard let jsContent = String(data: scriptData, encoding: .utf8) else {
Logger.shared.log("Invalid script encoding for module: \(module.metadata.sourceName)", type: "Error") Logger.shared.log("Invalid script encoding for module: \(module.metadata.sourceName)", type: "Error")
@ -67,12 +73,18 @@ class ModuleManager: ObservableObject {
} }
try jsContent.write(to: localUrl, atomically: true, encoding: .utf8) try jsContent.write(to: localUrl, atomically: true, encoding: .utf8)
Logger.shared.log("Recovered missing JS file for module: \(module.metadata.sourceName)") Logger.shared.log("Successfully downloaded JS file for module: \(module.metadata.sourceName)")
} catch { } catch {
Logger.shared.log("Failed to recover JS file for module: \(module.metadata.sourceName) - \(error.localizedDescription)", type: "Error") Logger.shared.log("Failed to download JS file for module: \(module.metadata.sourceName) - \(error.localizedDescription)", type: "Error")
} }
} }
} }
if missingCount > 0 {
Logger.shared.log("Downloaded \(missingCount) missing module JS files", type: "Info")
} else {
Logger.shared.log("All module JS files are present", type: "Info")
}
} }
private func saveModules() { private func saveModules() {

View file

@ -153,35 +153,42 @@ class iCloudSyncManager {
} }
func syncModulesFromiCloud() { func syncModulesFromiCloud() {
DispatchQueue.global(qos: .background).async { guard let iCloudURL = self.ubiquityContainerURL else {
guard let iCloudURL = self.ubiquityContainerURL else { return } Logger.shared.log("iCloud container not available", type: "Warning")
let localModulesURL = self.getLocalModulesFileURL() return
let iCloudModulesURL = iCloudURL.appendingPathComponent(self.modulesFileName) }
do {
guard FileManager.default.fileExists(atPath: iCloudModulesURL.path) else { return } let localModulesURL = self.getLocalModulesFileURL()
let iCloudModulesURL = iCloudURL.appendingPathComponent(self.modulesFileName)
let shouldCopy: Bool
if FileManager.default.fileExists(atPath: localModulesURL.path) { do {
let localData = try Data(contentsOf: localModulesURL) if !FileManager.default.fileExists(atPath: iCloudModulesURL.path) {
let iCloudData = try Data(contentsOf: iCloudModulesURL) Logger.shared.log("No modules file found in iCloud", type: "Info")
shouldCopy = localData != iCloudData return
} else {
shouldCopy = true
}
if shouldCopy {
if FileManager.default.fileExists(atPath: localModulesURL.path) {
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")
} }
let shouldCopy: Bool
if FileManager.default.fileExists(atPath: localModulesURL.path) {
let localData = try Data(contentsOf: localModulesURL)
let iCloudData = try Data(contentsOf: iCloudModulesURL)
shouldCopy = localData != iCloudData
} else {
shouldCopy = true
}
if shouldCopy {
Logger.shared.log("Syncing modules from iCloud", type: "Info")
if FileManager.default.fileExists(atPath: localModulesURL.path) {
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")
} }
} }