223 lines
6.8 KiB
Swift
223 lines
6.8 KiB
Swift
//
|
|
// CookbookState.swift
|
|
// Nextcloud Cookbook iOS Client
|
|
//
|
|
// Created by Vincent Meilinger on 29.05.24.
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftUI
|
|
/*
|
|
|
|
@Observable
|
|
class CookbookState {
|
|
/// Caches recipe categories.
|
|
var categories: [Category] = []
|
|
|
|
/// Caches RecipeStubs.
|
|
var recipeStubs: [String: [RecipeStub]] = [:]
|
|
|
|
/// Caches Recipes by recipe id.
|
|
var recipes: [String: Recipe] = [:]
|
|
|
|
/// Caches recipe thumbnails by recipe id.
|
|
var thumbnails: [String: UIImage] = [:]
|
|
|
|
/// Caches recipe images by recipe id.
|
|
var images: [String: UIImage] = [:]
|
|
|
|
/// Caches recipe keywords.
|
|
var keywords: [RecipeKeyword] = []
|
|
|
|
/// Read and write interfaces.
|
|
var localReadInterface: ReadInterface
|
|
var localWriteInterface: WriteInterface
|
|
var remoteReadInterface: ReadInterface?
|
|
var remoteWriteInterface: WriteInterface?
|
|
|
|
/// UI state variables
|
|
var selectedCategory: Category? = nil
|
|
var selectedRecipeStub: RecipeStub? = nil
|
|
var showSettings: Bool = false
|
|
var showGroceries: Bool = false
|
|
|
|
/// Grocery List
|
|
var groceryList = GroceryList()
|
|
|
|
init(
|
|
localReadInterface: ReadInterface,
|
|
localWriteInterface: WriteInterface,
|
|
remoteReadInterface: ReadInterface? = nil,
|
|
remoteWriteInterface: WriteInterface? = nil
|
|
|
|
) {
|
|
self.localReadInterface = localReadInterface
|
|
self.localWriteInterface = localWriteInterface
|
|
self.remoteReadInterface = remoteReadInterface
|
|
self.remoteWriteInterface = remoteWriteInterface
|
|
}
|
|
|
|
init() {
|
|
let accountLoader = AccountLoader()
|
|
rI, wI = accountLoader.load
|
|
|
|
}
|
|
}
|
|
|
|
|
|
extension CookbookState {
|
|
func loadCategories(remoteFirst: Bool = false) async {
|
|
if remoteFirst {
|
|
if let remoteReadInterface, let categories = await remoteReadInterface.getCategories() {
|
|
self.categories = categories
|
|
return
|
|
}
|
|
|
|
if let categories = await localReadInterface.getCategories() {
|
|
self.categories = categories
|
|
return
|
|
}
|
|
} else {
|
|
if let categories = await localReadInterface.getCategories() {
|
|
self.categories = categories
|
|
return
|
|
}
|
|
|
|
guard let remoteReadInterface else { return }
|
|
if let categories = await remoteReadInterface.getCategories() {
|
|
self.categories = categories
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func loadRecipeStubs(category: String, remoteFirst: Bool = false) async {
|
|
if remoteFirst {
|
|
if let remoteReadInterface, let stubs = await remoteReadInterface.getRecipeStubs() {
|
|
self.recipeStubs[category] = stubs
|
|
return
|
|
}
|
|
|
|
if let stubs = await localReadInterface.getRecipeStubs() {
|
|
self.recipeStubs[category] = stubs
|
|
return
|
|
}
|
|
} else {
|
|
if let stubs = await localReadInterface.getRecipeStubs() {
|
|
self.recipeStubs[category] = stubs
|
|
return
|
|
}
|
|
|
|
guard let remoteReadInterface else { return }
|
|
if let stubs = await remoteReadInterface.getRecipeStubs() {
|
|
self.recipeStubs[category] = stubs
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func loadKeywords(remoteFirst: Bool = false) async {
|
|
if remoteFirst {
|
|
if let remoteReadInterface, let keywords = await remoteReadInterface.getTags() {
|
|
self.keywords = keywords
|
|
return
|
|
}
|
|
|
|
if let keywords = await localReadInterface.getTags() {
|
|
self.keywords = keywords
|
|
return
|
|
}
|
|
} else {
|
|
if let keywords = await localReadInterface.getTags() {
|
|
self.keywords = keywords
|
|
return
|
|
}
|
|
|
|
guard let remoteReadInterface else { return }
|
|
if let keywords = await remoteReadInterface.getTags() {
|
|
self.keywords = keywords
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func loadRecipe(id: String, remoteFirst: Bool = false) async {
|
|
if remoteFirst {
|
|
if let remoteReadInterface, let recipe = await remoteReadInterface.getRecipe(id: id) {
|
|
self.recipes[id] = recipe
|
|
return
|
|
}
|
|
|
|
if let recipe = await localReadInterface.getRecipe(id: id) {
|
|
self.recipes[id] = recipe
|
|
return
|
|
}
|
|
} else {
|
|
if let recipe = await localReadInterface.getRecipe(id: id) {
|
|
self.recipes[id] = recipe
|
|
return
|
|
}
|
|
|
|
guard let remoteReadInterface else { return }
|
|
if let recipe = await remoteReadInterface.getRecipe(id: id) {
|
|
self.recipes[id] = recipe
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
class AccountLoader {
|
|
func initInterfaces() async -> [ReadInterface & WriteInterface] {
|
|
let accounts = await self.loadAccounts("accounts.data")
|
|
|
|
if accounts.isEmpty && UserSettings.shared.serverAddress != "" {
|
|
print("Creating new Account from legacy Cookbook client account.")
|
|
let auth = Authentication(
|
|
baseUrl: UserSettings.shared.serverAddress,
|
|
user: UserSettings.shared.username,
|
|
token: UserSettings.shared.authString
|
|
)
|
|
let authKey = "legacyNextcloud"
|
|
let legacyAccount = Account(
|
|
id: UUID(),
|
|
name: "Nextcloud",
|
|
type: .nextcloud,
|
|
apiVersion: "1.0",
|
|
authKey: authKey
|
|
)
|
|
await saveAccounts([legacyAccount], "accounts.data")
|
|
legacyAccount.storeAuth(auth)
|
|
|
|
let interface = NextcloudDataInterface(auth: auth, version: legacyAccount.apiVersion)
|
|
return [interface]
|
|
} else {
|
|
print("Recovering existing accounts.")
|
|
var interfaces: [ReadInterface & WriteInterface] = []
|
|
for account in accounts {
|
|
if let interface: CookbookInterface = account.getInterface() {
|
|
interfaces.append(interface)
|
|
}
|
|
}
|
|
return interfaces
|
|
}
|
|
}
|
|
|
|
func loadAccounts(_ path: String) async -> [Account] {
|
|
do {
|
|
return try await DataStore.shared.load(fromPath: path) ?? []
|
|
} catch (let error) {
|
|
print(error)
|
|
return []
|
|
}
|
|
}
|
|
|
|
func saveAccounts(_ accounts: [Account], _ path: String) async {
|
|
await DataStore.shared.save(data: accounts, toPath: path)
|
|
}
|
|
}
|
|
|
|
*/
|