mirror of
https://github.com/cranci1/Sora.git
synced 2026-04-20 16:12:50 +00:00
fixed main thread issue
This commit is contained in:
parent
7c3b8e74d9
commit
2f4e51ffc2
1 changed files with 35 additions and 34 deletions
|
|
@ -90,43 +90,44 @@ class ModuleManager: ObservableObject {
|
||||||
let localUrl = getDocumentsDirectory().appendingPathComponent(module.localPath)
|
let localUrl = getDocumentsDirectory().appendingPathComponent(module.localPath)
|
||||||
return try String(contentsOf: localUrl, encoding: .utf8)
|
return try String(contentsOf: localUrl, encoding: .utf8)
|
||||||
}
|
}
|
||||||
|
|
||||||
func refreshModules() async {
|
func refreshModules() async {
|
||||||
for (index, module) in modules.enumerated() {
|
for (index, module) in modules.enumerated() {
|
||||||
do {
|
do {
|
||||||
let (metadataData, _) = try await URLSession.custom.data(from: URL(string: module.metadataUrl)!)
|
let (metadataData, _) = try await URLSession.custom.data(from: URL(string: module.metadataUrl)!)
|
||||||
let newMetadata = try JSONDecoder().decode(ModuleMetadata.self, from: metadataData)
|
let newMetadata = try JSONDecoder().decode(ModuleMetadata.self, from: metadataData)
|
||||||
|
|
||||||
if newMetadata.version != module.metadata.version {
|
if newMetadata.version != module.metadata.version {
|
||||||
guard let scriptUrl = URL(string: newMetadata.scriptUrl) else {
|
guard let scriptUrl = URL(string: newMetadata.scriptUrl) else {
|
||||||
throw NSError(domain: "Invalid script URL", code: -1)
|
throw NSError(domain: "Invalid script URL", code: -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
let (scriptData, _) = try await URLSession.custom.data(from: scriptUrl)
|
||||||
|
guard let jsContent = String(data: scriptData, encoding: .utf8) else {
|
||||||
|
throw NSError(domain: "Invalid script encoding", code: -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
let localUrl = getDocumentsDirectory().appendingPathComponent(module.localPath)
|
||||||
|
try jsContent.write(to: localUrl, atomically: true, encoding: .utf8)
|
||||||
|
|
||||||
|
let updatedModule = ScrapingModule(
|
||||||
|
id: module.id,
|
||||||
|
metadata: newMetadata,
|
||||||
|
localPath: module.localPath,
|
||||||
|
metadataUrl: module.metadataUrl,
|
||||||
|
isActive: module.isActive
|
||||||
|
)
|
||||||
|
|
||||||
|
await MainActor.run {
|
||||||
|
self.modules[index] = updatedModule
|
||||||
|
self.saveModules()
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger.shared.log("Updated module: \(module.metadata.sourceName) to version \(newMetadata.version)")
|
||||||
}
|
}
|
||||||
|
} catch {
|
||||||
let (scriptData, _) = try await URLSession.custom.data(from: scriptUrl)
|
Logger.shared.log("Failed to refresh module: \(module.metadata.sourceName) - \(error.localizedDescription)")
|
||||||
guard let jsContent = String(data: scriptData, encoding: .utf8) else {
|
|
||||||
throw NSError(domain: "Invalid script encoding", code: -1)
|
|
||||||
}
|
|
||||||
|
|
||||||
let localUrl = getDocumentsDirectory().appendingPathComponent(module.localPath)
|
|
||||||
try jsContent.write(to: localUrl, atomically: true, encoding: .utf8)
|
|
||||||
|
|
||||||
modules[index] = ScrapingModule(
|
|
||||||
id: module.id,
|
|
||||||
metadata: newMetadata,
|
|
||||||
localPath: module.localPath,
|
|
||||||
metadataUrl: module.metadataUrl,
|
|
||||||
isActive: module.isActive
|
|
||||||
)
|
|
||||||
|
|
||||||
Logger.shared.log("Updated module: \(module.metadata.sourceName) to version \(newMetadata.version)")
|
|
||||||
}
|
}
|
||||||
} catch {
|
|
||||||
Logger.shared.log("Failed to refresh module: \(module.metadata.sourceName) - \(error.localizedDescription)")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
self.saveModules()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue