Store a _groceryState JSON field on each recipe to track which ingredients have been added, completed, or removed. Uses per-item last-writer-wins conflict resolution with ISO 8601 timestamps. Debounced push (2s) avoids excessive API calls; pull reconciles on recipe open and app launch. Includes a settings toggle to enable/disable sync. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
92 lines
3.1 KiB
Swift
92 lines
3.1 KiB
Swift
//
|
|
// ContentView.swift
|
|
// Nextcloud Cookbook iOS Client
|
|
//
|
|
// Created by Vincent Meilinger on 06.09.23.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
struct MainView: View {
|
|
@StateObject var appState = AppState()
|
|
@StateObject var groceryList = GroceryListManager()
|
|
|
|
// Tab ViewModels
|
|
@StateObject var recipeViewModel = RecipeTabView.ViewModel()
|
|
@StateObject var searchViewModel = SearchTabView.ViewModel()
|
|
|
|
@ObservedObject private var userSettings = UserSettings.shared
|
|
|
|
@State private var selectedTab: Tab = .recipes
|
|
|
|
enum Tab {
|
|
case recipes, search, groceryList
|
|
}
|
|
|
|
var body: some View {
|
|
TabView(selection: $selectedTab) {
|
|
SwiftUI.Tab("Recipes", systemImage: "book.closed.fill", value: .recipes) {
|
|
RecipeTabView()
|
|
.environmentObject(recipeViewModel)
|
|
.environmentObject(appState)
|
|
.environmentObject(groceryList)
|
|
}
|
|
|
|
SwiftUI.Tab("Search", systemImage: "magnifyingglass", value: .search, role: .search) {
|
|
SearchTabView()
|
|
.environmentObject(searchViewModel)
|
|
.environmentObject(appState)
|
|
.environmentObject(groceryList)
|
|
}
|
|
|
|
if userSettings.groceryListMode != GroceryListMode.appleReminders.rawValue {
|
|
SwiftUI.Tab("Grocery List", systemImage: "storefront", value: .groceryList) {
|
|
GroceryListTabView()
|
|
.environmentObject(groceryList)
|
|
}
|
|
}
|
|
}
|
|
.tabViewStyle(.sidebarAdaptable)
|
|
.modifier(TabBarMinimizeModifier())
|
|
.onChange(of: userSettings.groceryListMode) { _, newValue in
|
|
if newValue == GroceryListMode.appleReminders.rawValue && selectedTab == .groceryList {
|
|
selectedTab = .recipes
|
|
}
|
|
Task {
|
|
await groceryList.load()
|
|
}
|
|
}
|
|
.task {
|
|
recipeViewModel.presentLoadingIndicator = true
|
|
await appState.getCategories()
|
|
await appState.updateAllRecipeDetails()
|
|
|
|
// Preload category images
|
|
for category in appState.categories {
|
|
await appState.getCategoryImage(for: category.name)
|
|
}
|
|
|
|
// Load recently viewed recipes
|
|
await appState.loadRecentRecipes()
|
|
|
|
// 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
|
|
}
|
|
}
|
|
await groceryList.load()
|
|
groceryList.configureSyncManager(appState: appState)
|
|
if UserSettings.shared.grocerySyncEnabled {
|
|
await groceryList.syncManager?.performInitialSync()
|
|
}
|
|
recipeViewModel.presentLoadingIndicator = false
|
|
}
|
|
}
|
|
}
|