Added tabs and a grocery list
This commit is contained in:
@@ -11,8 +11,6 @@ import UIKit
|
||||
|
||||
|
||||
@MainActor class MainViewModel: ObservableObject {
|
||||
@ObservedObject var userSettings = UserSettings.shared
|
||||
|
||||
@Published var categories: [Category] = []
|
||||
@Published var recipes: [String: [Recipe]] = [:]
|
||||
@Published var recipeDetails: [Int: RecipeDetail] = [:]
|
||||
@@ -30,10 +28,10 @@ import UIKit
|
||||
self.api = api
|
||||
self.dataStore = DataStore()
|
||||
|
||||
if userSettings.authString == "" {
|
||||
let loginString = "\(userSettings.username):\(userSettings.token)"
|
||||
if UserSettings.shared.authString == "" {
|
||||
let loginString = "\(UserSettings.shared.username):\(UserSettings.shared.token)"
|
||||
let loginData = loginString.data(using: String.Encoding.utf8)!
|
||||
userSettings.authString = loginData.base64EncodedString()
|
||||
UserSettings.shared.authString = loginData.base64EncodedString()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +49,7 @@ import UIKit
|
||||
*/
|
||||
func getCategories() async {
|
||||
let (categories, _) = await api.getCategories(
|
||||
auth: userSettings.authString
|
||||
auth: UserSettings.shared.authString
|
||||
)
|
||||
if let categories = categories {
|
||||
print("Successfully loaded categories")
|
||||
@@ -99,7 +97,7 @@ import UIKit
|
||||
|
||||
func getServer(store: Bool = false) async -> Bool {
|
||||
let (recipes, _) = await api.getCategory(
|
||||
auth: userSettings.authString,
|
||||
auth: UserSettings.shared.authString,
|
||||
named: categoryString
|
||||
)
|
||||
if let recipes = recipes {
|
||||
@@ -132,16 +130,16 @@ import UIKit
|
||||
for category in self.categories {
|
||||
await updateRecipeDetails(in: category.name)
|
||||
}
|
||||
userSettings.lastUpdate = Date()
|
||||
UserSettings.shared.lastUpdate = Date()
|
||||
}
|
||||
|
||||
func updateRecipeDetails(in category: String) async {
|
||||
guard userSettings.storeRecipes else { return }
|
||||
guard UserSettings.shared.storeRecipes else { return }
|
||||
guard let recipes = self.recipes[category] else { return }
|
||||
for recipe in recipes {
|
||||
if needsUpdate(category: category, lastModified: recipe.dateModified) {
|
||||
print("\(recipe.name) needs an update. (last modified: \(recipe.dateModified)")
|
||||
await updateRecipeDetail(id: recipe.recipe_id, withThumb: userSettings.storeThumb, withImage: userSettings.storeImages)
|
||||
await updateRecipeDetail(id: recipe.recipe_id, withThumb: UserSettings.shared.storeThumb, withImage: UserSettings.shared.storeImages)
|
||||
} else {
|
||||
print("\(recipe.name) is up to date.")
|
||||
}
|
||||
@@ -161,7 +159,7 @@ import UIKit
|
||||
*/
|
||||
func getRecipes() async -> [Recipe] {
|
||||
let (recipes, error) = await api.getRecipes(
|
||||
auth: userSettings.authString
|
||||
auth: UserSettings.shared.authString
|
||||
)
|
||||
if let recipes = recipes {
|
||||
return recipes
|
||||
@@ -201,7 +199,7 @@ import UIKit
|
||||
|
||||
func getServer() async -> RecipeDetail? {
|
||||
let (recipe, error) = await api.getRecipe(
|
||||
auth: userSettings.authString,
|
||||
auth: UserSettings.shared.authString,
|
||||
id: id
|
||||
)
|
||||
if let recipe = recipe {
|
||||
@@ -294,7 +292,7 @@ import UIKit
|
||||
|
||||
func getServer() async -> UIImage? {
|
||||
let (image, _) = await api.getImage(
|
||||
auth: userSettings.authString,
|
||||
auth: UserSettings.shared.authString,
|
||||
id: id,
|
||||
size: size
|
||||
)
|
||||
@@ -370,7 +368,7 @@ import UIKit
|
||||
|
||||
func getServer() async -> [RecipeKeyword]? {
|
||||
let (tags, _) = await api.getTags(
|
||||
auth: userSettings.authString
|
||||
auth: UserSettings.shared.authString
|
||||
)
|
||||
return tags
|
||||
}
|
||||
@@ -423,7 +421,7 @@ import UIKit
|
||||
*/
|
||||
func deleteRecipe(withId id: Int, categoryName: String) async -> RequestAlert? {
|
||||
let (error) = await api.deleteRecipe(
|
||||
auth: userSettings.authString,
|
||||
auth: UserSettings.shared.authString,
|
||||
id: id
|
||||
)
|
||||
|
||||
@@ -454,7 +452,7 @@ import UIKit
|
||||
*/
|
||||
func checkServerConnection() async -> Bool {
|
||||
let (categories, _) = await api.getCategories(
|
||||
auth: userSettings.authString
|
||||
auth: UserSettings.shared.authString
|
||||
)
|
||||
if let categories = categories {
|
||||
self.categories = categories
|
||||
@@ -483,12 +481,12 @@ import UIKit
|
||||
var error: NetworkError? = nil
|
||||
if createNew {
|
||||
error = await api.createRecipe(
|
||||
auth: userSettings.authString,
|
||||
auth: UserSettings.shared.authString,
|
||||
recipe: recipeDetail
|
||||
)
|
||||
} else {
|
||||
error = await api.updateRecipe(
|
||||
auth: userSettings.authString,
|
||||
auth: UserSettings.shared.authString,
|
||||
recipe: recipeDetail
|
||||
)
|
||||
}
|
||||
@@ -501,7 +499,7 @@ import UIKit
|
||||
func importRecipe(url: String) async -> (RecipeDetail?, RequestAlert?) {
|
||||
guard let data = JSONEncoder.safeEncode(RecipeImportRequest(url: url)) else { return (nil, .REQUEST_DROPPED) }
|
||||
let (recipeDetail, error) = await api.importRecipe(
|
||||
auth: userSettings.authString,
|
||||
auth: UserSettings.shared.authString,
|
||||
data: data
|
||||
)
|
||||
if error != nil {
|
||||
@@ -509,6 +507,7 @@ import UIKit
|
||||
}
|
||||
return (recipeDetail, nil)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user