WIP - Complete App refactoring

This commit is contained in:
VincentMeilinger
2025-05-26 15:52:24 +02:00
parent 29fd3c668b
commit 5acf3b9c4f
49 changed files with 1996 additions and 543 deletions

View File

@@ -6,68 +6,177 @@
//
import SwiftUI
import SwiftData
struct MainView: View {
@StateObject var appState = AppState()
@StateObject var groceryList = GroceryList()
//@State var cookbookState: CookbookState = CookbookState()
@Environment(\.modelContext) var modelContext
@Query var recipes: [Recipe] = []
// Tab ViewModels
@StateObject var recipeViewModel = RecipeTabView.ViewModel()
@StateObject var searchViewModel = SearchTabView.ViewModel()
enum Tab {
case recipes, search, groceryList
}
var body: some View {
TabView {
RecipeTabView()
.environmentObject(recipeViewModel)
.environmentObject(appState)
.environmentObject(groceryList)
.tabItem {
Label("Recipes", systemImage: "book.closed.fill")
}
.tag(Tab.recipes)
SearchTabView()
.environmentObject(searchViewModel)
.environmentObject(appState)
.environmentObject(groceryList)
.tabItem {
Label("Search", systemImage: "magnifyingglass")
}
.tag(Tab.search)
GroceryListTabView()
.environmentObject(groceryList)
.tabItem {
if #available(iOS 17.0, *) {
Label("Grocery List", systemImage: "storefront")
} else {
Label("Grocery List", systemImage: "heart.text.square")
}
}
.tag(Tab.groceryList)
}
.task {
recipeViewModel.presentLoadingIndicator = true
await appState.getCategories()
await appState.updateAllRecipeDetails()
// Open detail view for default category
if UserSettings.shared.defaultCategory != "" {
if let cat = appState.categories.first(where: { c in
if c.name == UserSettings.shared.defaultCategory {
return true
}
return false
}) {
recipeViewModel.selectedCategory = cat
VStack {
List {
ForEach(recipes) { recipe in
Text(recipe.name)
}
}
await groceryList.load()
recipeViewModel.presentLoadingIndicator = false
Button("New") {
let recipe = Recipe(id: UUID().uuidString, name: "Neues Rezept", keywords: [], prepTime: "", cookTime: "", totalTime: "", recipeDescription: "", yield: 0, category: "", tools: [], ingredients: [], instructions: [], nutrition: [:], ingredientMultiplier: 0)
modelContext.insert(recipe)
}
}
/*NavigationSplitView {
VStack {
List(selection: $cookbookState.selectedCategory) {
ForEach(cookbookState.categories) { category in
Text(category.name)
.tag(category)
}
}
.listStyle(.plain)
.onAppear {
Task {
await cookbookState.loadCategories()
}
}
}
} content: {
if let selectedCategory = cookbookState.selectedCategory {
List(selection: $cookbookState.selectedRecipeStub) {
ForEach(cookbookState.recipeStubs[selectedCategory.name] ?? [], id: \.id) { recipeStub in
Text(recipeStub.title)
.tag(recipeStub)
}
}
.onAppear {
Task {
await cookbookState.loadRecipeStubs(category: selectedCategory.name)
}
}
} else {
Text("Please select a category.")
.foregroundColor(.secondary)
}
} detail: {
if let selectedRecipe = cookbookState.selectedRecipe {
if let recipe = cookbookState.recipes[selectedRecipe.id] {
RecipeView(recipe: recipe)
} else {
ProgressView()
.onAppear {
Task {
await cookbookState.loadRecipe(id: selectedRecipe.id)
}
}
}
} else {
Text("Please select a recipe.")
.foregroundColor(.secondary)
}
}
.toolbar {
ToolbarItem(placement: .bottomBar) {
Button(action: {
cookbookState.showGroceries = true
}) {
Label("Grocery List", systemImage: "cart")
}
}
ToolbarItem(placement: .topBarLeading) {
Button(action: {
cookbookState.showSettings = true
}) {
Label("Settings", systemImage: "gearshape")
}
}
}*/
}
}
/*struct CategoryListView: View {
@Bindable var cookbookState: CookbookState
var body: some View {
List(cookbookState.selectedAccountState.categories) { category in
NavigationLink {
RecipeListView(
cookbookState: cookbookState,
selectedCategory: category.name,
showEditView: .constant(false)
)
} label: {
HStack(alignment: .center) {
if cookbookState.selectedAccountState.selectedCategory != nil &&
category.name == cookbookState.selectedAccountState.selectedCategory?.name {
Image(systemName: "book")
} else {
Image(systemName: "book.closed.fill")
}
if category.name == "*" {
Text("Other")
.font(.system(size: 20, weight: .medium, design: .default))
} else {
Text(category.name)
.font(.system(size: 20, weight: .medium, design: .default))
}
Spacer()
Text("\(category.recipe_count)")
.font(.system(size: 15, weight: .bold, design: .default))
.foregroundStyle(Color.background)
.frame(width: 25, height: 25, alignment: .center)
.minimumScaleFactor(0.5)
.background {
Circle()
.foregroundStyle(Color.secondary)
}
}.padding(7)
}
}
}
}*/
/*struct CategoryListView: View {
@State var state: CookbookState
var body: some View {
List(selection: $state.categoryListSelection) {
ForEach(state.categories) { category in
NavigationLink(value: category) {
HStack(alignment: .center) {
if state.categoryListSelection != nil &&
category.name == state.categoryListSelection {
Image(systemName: "book")
} else {
Image(systemName: "book.closed.fill")
}
if category.name == "*" {
Text("Other")
.font(.system(size: 20, weight: .medium, design: .default))
} else {
Text(category.name)
.font(.system(size: 20, weight: .medium, design: .default))
}
Spacer()
Text("\(category.recipe_count)")
.font(.system(size: 15, weight: .bold, design: .default))
.foregroundStyle(Color.background)
.frame(width: 25, height: 25, alignment: .center)
.minimumScaleFactor(0.5)
.background {
Circle()
.foregroundStyle(Color.secondary)
}
}.padding(7)
}
}
}
}
}*/