initial commit
This commit is contained in:
68
Nextcloud Cookbook iOS Client/Data/DataModels.swift
Normal file
68
Nextcloud Cookbook iOS Client/Data/DataModels.swift
Normal file
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// DataModels.swift
|
||||
// Nextcloud Cookbook iOS Client
|
||||
//
|
||||
// Created by Vincent Meilinger on 15.09.23.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
struct Category: Codable {
|
||||
let name: String
|
||||
let recipe_count: Int
|
||||
}
|
||||
|
||||
struct Recipe: Codable {
|
||||
let name: String
|
||||
let keywords: String
|
||||
let dateCreated: String
|
||||
let dateModified: String
|
||||
let imageUrl: String
|
||||
let imagePlaceholderUrl: String
|
||||
let recipe_id: Int
|
||||
}
|
||||
|
||||
struct RecipeDetail: Codable {
|
||||
let name: String
|
||||
let keywords: String
|
||||
let dateCreated: String
|
||||
let dateModified: String
|
||||
let imageUrl: String
|
||||
let id: String
|
||||
let prepTime: String?
|
||||
let cookTime: String?
|
||||
let totalTime: String?
|
||||
let description: String
|
||||
let url: String
|
||||
let recipeYield: Int
|
||||
let recipeCategory: String
|
||||
let tool: [String]
|
||||
let recipeIngredient: [String]
|
||||
let recipeInstructions: [String]
|
||||
|
||||
static func error() -> RecipeDetail {
|
||||
return RecipeDetail(
|
||||
name: "Error: Unable to load recipe.",
|
||||
keywords: "",
|
||||
dateCreated: "",
|
||||
dateModified: "",
|
||||
imageUrl: "", id: "",
|
||||
prepTime: "",
|
||||
cookTime: "",
|
||||
totalTime: "",
|
||||
description: "",
|
||||
url: "",
|
||||
recipeYield: 0,
|
||||
recipeCategory: "",
|
||||
tool: [],
|
||||
recipeIngredient: [],
|
||||
recipeInstructions: []
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
struct RecipeImage {
|
||||
let thumb: UIImage
|
||||
let full: UIImage?
|
||||
}
|
||||
54
Nextcloud Cookbook iOS Client/Data/DataStore.swift
Normal file
54
Nextcloud Cookbook iOS Client/Data/DataStore.swift
Normal file
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// DataController.swift
|
||||
// Nextcloud Cookbook iOS Client
|
||||
//
|
||||
// Created by Vincent Meilinger on 15.09.23.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
|
||||
class DataStore {
|
||||
private static func fileURL(appending: String) throws -> URL {
|
||||
try FileManager.default.url(
|
||||
for: .documentDirectory,
|
||||
in: .userDomainMask,
|
||||
appropriateFor: nil,
|
||||
create: false
|
||||
)
|
||||
|
||||
.appendingPathComponent(appending)
|
||||
}
|
||||
|
||||
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 throws {
|
||||
let task = Task {
|
||||
let data = try JSONEncoder().encode(data)
|
||||
let outfile = try Self.fileURL(appending: path)
|
||||
try data.write(to: outfile)
|
||||
}
|
||||
_ = try await task.value
|
||||
}
|
||||
|
||||
func clearAll() {
|
||||
do {
|
||||
try FileManager.default.removeItem(at: Self.fileURL(appending: ""))
|
||||
} catch {
|
||||
print("Could not delete file, probably read-only filesystem")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
43
Nextcloud Cookbook iOS Client/Data/UserDefaults.swift
Normal file
43
Nextcloud Cookbook iOS Client/Data/UserDefaults.swift
Normal file
@@ -0,0 +1,43 @@
|
||||
//
|
||||
// UserDefaults.swift
|
||||
// Nextcloud Cookbook iOS Client
|
||||
//
|
||||
// Created by Vincent Meilinger on 15.09.23.
|
||||
//
|
||||
|
||||
|
||||
import Foundation
|
||||
import Combine
|
||||
|
||||
class UserSettings: ObservableObject {
|
||||
@Published var username: String {
|
||||
didSet {
|
||||
UserDefaults.standard.set(username, forKey: "username")
|
||||
}
|
||||
}
|
||||
|
||||
@Published var token: String {
|
||||
didSet {
|
||||
UserDefaults.standard.set(token, forKey: "token")
|
||||
}
|
||||
}
|
||||
|
||||
@Published var serverAddress: String {
|
||||
didSet {
|
||||
UserDefaults.standard.set(serverAddress, forKey: "serverAddress")
|
||||
}
|
||||
}
|
||||
|
||||
@Published var onboarding: Bool {
|
||||
didSet {
|
||||
UserDefaults.standard.set(onboarding, forKey: "onboarding")
|
||||
}
|
||||
}
|
||||
|
||||
init() {
|
||||
self.username = UserDefaults.standard.object(forKey: "username") as? String ?? ""
|
||||
self.token = UserDefaults.standard.object(forKey: "token") as? String ?? ""
|
||||
self.serverAddress = UserDefaults.standard.object(forKey: "serverAddress") as? String ?? ""
|
||||
self.onboarding = UserDefaults.standard.object(forKey: "onboarding") as? Bool ?? true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user