Files
Nextcloud-Cookbook-iOS/Nextcloud Cookbook iOS Client/Data/DataStore.swift
Hendrik Hogertz 7c824b492e Modernize networking layer and fix category navigation and recipe list bugs
Network layer:
- Replace static CookbookApi protocol with instance-based CookbookApiProtocol
  using async/throws instead of tuple returns
- Refactor ApiRequest to use URLComponents for proper URL encoding, replace
  print statements with OSLog, and return typed NetworkError cases
- Add structured NetworkError variants (httpError, connectionError, etc.)
- Remove global cookbookApi constant in favor of injected dependency on AppState
- Delete unused RecipeEditViewModel, RecipeScraper, and Scraper playground

Data & model fixes:
- Add custom Decodable for RecipeDetail with safe fallbacks for malformed JSON
- Make Category Hashable/Equatable use only `name` so NavigationSplitView
  selection survives category refreshes with updated recipe_count
- Return server-assigned ID from uploadRecipe so new recipes get their ID
  before the post-upload refresh block executes

View updates:
- Refresh both old and new category recipe lists after upload when category
  changes, mapping empty recipeCategory to "*" for uncategorized recipes
- Raise deployment target to iOS 18, adopt new SwiftUI API conventions
- Clean up alerts, onboarding views, and settings

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-15 00:47:28 +01:00

89 lines
2.7 KiB
Swift

//
// DataController.swift
// Nextcloud Cookbook iOS Client
//
// Created by Vincent Meilinger on 15.09.23.
//
import Foundation
import OSLog
import SwiftUI
class DataStore {
let fileManager = FileManager.default
static let shared = DataStore()
private static func fileURL(appending: String) throws -> URL {
try FileManager.default.url(
for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false
)
.appendingPathComponent(appending)
}
private static func fileURL() throws -> URL {
try FileManager.default.url(
for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false
)
}
func load<D: Decodable>(fromPath path: String) async throws -> D? {
let task = Task<D?, Error> {
let fileURL = try Self.fileURL(appending: path)
guard let data = try? Data(contentsOf: fileURL) else {
return nil
}
let storedRecipes = try JSONDecoder().decode(D.self, from: data)
return storedRecipes
}
return try await task.value
}
func save<D: Encodable>(data: D, toPath path: String) async {
let task = Task {
let data = try JSONEncoder().encode(data)
let outfile = try Self.fileURL(appending: path)
try data.write(to: outfile)
}
do {
_ = try await task.value
} catch {
Logger.data.error("Could not save data (path: \(path))")
}
}
func delete(path: String) {
Task {
let fileURL = try Self.fileURL(appending: path)
try fileManager.removeItem(at: fileURL)
}
}
func recipeDetailExists(recipeId: Int) -> Bool {
let filePath = "recipe\(recipeId).data"
guard let folderPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first?.path() else { return false }
return fileManager.fileExists(atPath: folderPath + filePath)
}
func clearAll() -> Bool {
Logger.data.debug("Attempting to delete all data ...")
guard let folderPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first?.path() else { return false }
do {
let filePaths = try fileManager.contentsOfDirectory(atPath: folderPath)
for filePath in filePaths {
try fileManager.removeItem(atPath: folderPath + filePath)
}
} catch {
Logger.data.error("Could not delete documents folder contents: \(error.localizedDescription)")
return false
}
Logger.data.debug("All data deleted successfully.")
return true
}
}