WIP - Complete App refactoring
This commit is contained in:
@@ -37,12 +37,21 @@ class DataStore {
|
||||
guard let data = try? Data(contentsOf: fileURL) else {
|
||||
return nil
|
||||
}
|
||||
let storedRecipes = try JSONDecoder().decode(D.self, from: data)
|
||||
return storedRecipes
|
||||
let decodedData = try JSONDecoder().decode(D.self, from: data)
|
||||
return decodedData
|
||||
}
|
||||
return try await task.value
|
||||
}
|
||||
|
||||
func loadDynamic(fromPath path: String, type: Decodable.Type) async throws -> Any? {
|
||||
let fileURL = try Self.fileURL(appending: path)
|
||||
guard let data = try? Data(contentsOf: fileURL) else {
|
||||
return nil
|
||||
}
|
||||
let decoded = try JSONDecoder().decode(type, from: data)
|
||||
return decoded
|
||||
}
|
||||
|
||||
func save<D: Encodable>(data: D, toPath path: String) async {
|
||||
let task = Task {
|
||||
let data = try JSONEncoder().encode(data)
|
||||
@@ -69,6 +78,27 @@ class DataStore {
|
||||
return fileManager.fileExists(atPath: folderPath + filePath)
|
||||
}
|
||||
|
||||
func listAllFolders(dir: String) -> [String] {
|
||||
guard let baseURL = try? Self.fileURL() else {
|
||||
print("Failed to retrieve documents directory.")
|
||||
return []
|
||||
}
|
||||
|
||||
let targetURL = baseURL.appendingPathComponent(dir)
|
||||
|
||||
do {
|
||||
let contents = try fileManager.contentsOfDirectory(at: targetURL, includingPropertiesForKeys: [.isDirectoryKey], options: [.skipsHiddenFiles])
|
||||
|
||||
let folders = contents.filter { url in
|
||||
(try? url.resourceValues(forKeys: [.isDirectoryKey]).isDirectory) ?? false
|
||||
}
|
||||
return folders.map { $0.lastPathComponent }
|
||||
} catch {
|
||||
print("Error listing folders in \(dir): \(error)")
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
func clearAll() -> Bool {
|
||||
print("Attempting to delete all data ...")
|
||||
guard let folderPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first?.path() else { return false }
|
||||
|
||||
Reference in New Issue
Block a user