added duplicate module prevention

This commit is contained in:
cranci1 2025-01-27 15:47:09 +01:00
parent 24793e1310
commit e74b9d8c78
2 changed files with 11 additions and 4 deletions

View file

@ -42,6 +42,10 @@ class ModuleManager: ObservableObject {
throw NSError(domain: "Invalid metadata URL", code: -1)
}
if modules.contains(where: { $0.metadataUrl == metadataUrl }) {
throw NSError(domain: "Module already exists", code: -1)
}
let (metadataData, _) = try await URLSession.custom.data(from: url)
let metadata = try JSONDecoder().decode(ModuleMetadata.self, from: metadataData)
@ -72,7 +76,7 @@ class ModuleManager: ObservableObject {
return module
}
func deleteModule(_ module: ScrapingModule) {
let localUrl = getDocumentsDirectory().appendingPathComponent(module.localPath)
try? fileManager.removeItem(at: localUrl)
@ -81,10 +85,9 @@ class ModuleManager: ObservableObject {
saveModules()
Logger.shared.log("Deleted module: \(module.metadata.sourceName)")
}
func getModuleContent(_ module: ScrapingModule) throws -> String {
let localUrl = getDocumentsDirectory().appendingPathComponent(module.localPath)
return try String(contentsOf: localUrl, encoding: .utf8)
}
}

View file

@ -153,7 +153,11 @@ struct SettingsViewModule: View {
} catch {
DispatchQueue.main.async {
isLoading = false
errorMessage = "Failed to add module: \(error.localizedDescription)"
if (error as NSError).domain == "Module already exists" {
errorMessage = "Module already exists"
} else {
errorMessage = "Failed to add module: \(error.localizedDescription)"
}
Logger.shared.log("Failed to add module: \(error.localizedDescription)")
}
}