Code cleanup

This commit is contained in:
VincentMeilinger
2024-01-25 22:51:08 +01:00
parent f6f9ca553a
commit 10cb851a5a
18 changed files with 131 additions and 107 deletions

View File

@@ -14,7 +14,7 @@
A70171942AA8E72000064C43 /* Nextcloud_Cookbook_iOS_ClientTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70171932AA8E72000064C43 /* Nextcloud_Cookbook_iOS_ClientTests.swift */; }; A70171942AA8E72000064C43 /* Nextcloud_Cookbook_iOS_ClientTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70171932AA8E72000064C43 /* Nextcloud_Cookbook_iOS_ClientTests.swift */; };
A701719E2AA8E72000064C43 /* Nextcloud_Cookbook_iOS_ClientUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A701719D2AA8E72000064C43 /* Nextcloud_Cookbook_iOS_ClientUITests.swift */; }; A701719E2AA8E72000064C43 /* Nextcloud_Cookbook_iOS_ClientUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A701719D2AA8E72000064C43 /* Nextcloud_Cookbook_iOS_ClientUITests.swift */; };
A70171A02AA8E72000064C43 /* Nextcloud_Cookbook_iOS_ClientUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A701719F2AA8E72000064C43 /* Nextcloud_Cookbook_iOS_ClientUITestsLaunchTests.swift */; }; A70171A02AA8E72000064C43 /* Nextcloud_Cookbook_iOS_ClientUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A701719F2AA8E72000064C43 /* Nextcloud_Cookbook_iOS_ClientUITestsLaunchTests.swift */; };
A70171AD2AA8EF4700064C43 /* MainViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70171AC2AA8EF4700064C43 /* MainViewModel.swift */; }; A70171AD2AA8EF4700064C43 /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70171AC2AA8EF4700064C43 /* AppState.swift */; };
A70171AF2AB2116B00064C43 /* NetworkHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70171AE2AB2116B00064C43 /* NetworkHandler.swift */; }; A70171AF2AB2116B00064C43 /* NetworkHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70171AE2AB2116B00064C43 /* NetworkHandler.swift */; };
A70171B12AB211DF00064C43 /* CustomError.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70171B02AB211DF00064C43 /* CustomError.swift */; }; A70171B12AB211DF00064C43 /* CustomError.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70171B02AB211DF00064C43 /* CustomError.swift */; };
A70171B42AB2122900064C43 /* NetworkRequests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70171B32AB2122900064C43 /* NetworkRequests.swift */; }; A70171B42AB2122900064C43 /* NetworkRequests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A70171B32AB2122900064C43 /* NetworkRequests.swift */; };
@@ -85,7 +85,7 @@
A70171992AA8E72000064C43 /* Nextcloud Cookbook iOS ClientUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Nextcloud Cookbook iOS ClientUITests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; A70171992AA8E72000064C43 /* Nextcloud Cookbook iOS ClientUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Nextcloud Cookbook iOS ClientUITests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
A701719D2AA8E72000064C43 /* Nextcloud_Cookbook_iOS_ClientUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Nextcloud_Cookbook_iOS_ClientUITests.swift; sourceTree = "<group>"; }; A701719D2AA8E72000064C43 /* Nextcloud_Cookbook_iOS_ClientUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Nextcloud_Cookbook_iOS_ClientUITests.swift; sourceTree = "<group>"; };
A701719F2AA8E72000064C43 /* Nextcloud_Cookbook_iOS_ClientUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Nextcloud_Cookbook_iOS_ClientUITestsLaunchTests.swift; sourceTree = "<group>"; }; A701719F2AA8E72000064C43 /* Nextcloud_Cookbook_iOS_ClientUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Nextcloud_Cookbook_iOS_ClientUITestsLaunchTests.swift; sourceTree = "<group>"; };
A70171AC2AA8EF4700064C43 /* MainViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainViewModel.swift; sourceTree = "<group>"; }; A70171AC2AA8EF4700064C43 /* AppState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppState.swift; sourceTree = "<group>"; };
A70171AE2AB2116B00064C43 /* NetworkHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkHandler.swift; sourceTree = "<group>"; }; A70171AE2AB2116B00064C43 /* NetworkHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkHandler.swift; sourceTree = "<group>"; };
A70171B02AB211DF00064C43 /* CustomError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomError.swift; sourceTree = "<group>"; }; A70171B02AB211DF00064C43 /* CustomError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomError.swift; sourceTree = "<group>"; };
A70171B32AB2122900064C43 /* NetworkRequests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkRequests.swift; sourceTree = "<group>"; }; A70171B32AB2122900064C43 /* NetworkRequests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkRequests.swift; sourceTree = "<group>"; };
@@ -185,6 +185,7 @@
A781E75F2AF8228100452F6F /* RecipeImport */, A781E75F2AF8228100452F6F /* RecipeImport */,
A9CA6CED2B4C084100F78AB5 /* RecipeExport */, A9CA6CED2B4C084100F78AB5 /* RecipeExport */,
A703226B2ABAF60D00D7C4ED /* Extensions */, A703226B2ABAF60D00D7C4ED /* Extensions */,
A76B8A702AE002AE00096CEC /* Alerts.swift */,
A76B8A6E2ADFFA8800096CEC /* SupportedLanguage.swift */, A76B8A6E2ADFFA8800096CEC /* SupportedLanguage.swift */,
A7AEAE632AD5521400135378 /* Localizable.xcstrings */, A7AEAE632AD5521400135378 /* Localizable.xcstrings */,
A70171852AA8E71F00064C43 /* Assets.xcassets */, A70171852AA8E71F00064C43 /* Assets.xcassets */,
@@ -234,7 +235,7 @@
A70171B72AB2445700064C43 /* ViewModels */ = { A70171B72AB2445700064C43 /* ViewModels */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
A70171AC2AA8EF4700064C43 /* MainViewModel.swift */, A70171AC2AA8EF4700064C43 /* AppState.swift */,
A79AA8E12AFF8C14007D25F2 /* RecipeEditViewModel.swift */, A79AA8E12AFF8C14007D25F2 /* RecipeEditViewModel.swift */,
); );
path = ViewModels; path = ViewModels;
@@ -244,18 +245,12 @@
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
A70171832AA8E71900064C43 /* MainView.swift */, A70171832AA8E71900064C43 /* MainView.swift */,
A70171CC2AB501B100064C43 /* SettingsView.swift */,
A977D0DC2B6002DA009783A9 /* Tabs */, A977D0DC2B6002DA009783A9 /* Tabs */,
A7FB0D782B25C65200A3469E /* Onboarding */, A7FB0D782B25C65200A3469E /* Onboarding */,
A70171BD2AB4987900064C43 /* CategoryDetailView.swift */, A9C3BE502B630E3900562C79 /* Recipes */,
A70171C12AB498C600064C43 /* RecipeCardView.swift */, A9C3BE512B630E8300562C79 /* RecipeEditing */,
A70171BF2AB498A900064C43 /* RecipeDetailView.swift */, A9C3BE522B630F1300562C79 /* ReusableViews */,
A70D7CA02AC73CA700D53DBF /* RecipeEditView.swift */,
A70171CC2AB501B100064C43 /* SettingsView.swift */,
A7F3F8E72ACBFC760076C227 /* KeywordPickerView.swift */,
A7F3F8E92ACC221C0076C227 /* CategoryPickerView.swift */,
A76B8A702AE002AE00096CEC /* Alerts.swift */,
A7CD3FD12B2C546A00D764AD /* CollapsibleView.swift */,
A9D89AAF2B4FE97800F49D92 /* TimerView.swift */,
); );
path = Views; path = Views;
sourceTree = "<group>"; sourceTree = "<group>";
@@ -335,6 +330,35 @@
path = Tabs; path = Tabs;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
A9C3BE502B630E3900562C79 /* Recipes */ = {
isa = PBXGroup;
children = (
A70171BD2AB4987900064C43 /* CategoryDetailView.swift */,
A70171C12AB498C600064C43 /* RecipeCardView.swift */,
A70171BF2AB498A900064C43 /* RecipeDetailView.swift */,
A9D89AAF2B4FE97800F49D92 /* TimerView.swift */,
);
path = Recipes;
sourceTree = "<group>";
};
A9C3BE512B630E8300562C79 /* RecipeEditing */ = {
isa = PBXGroup;
children = (
A70D7CA02AC73CA700D53DBF /* RecipeEditView.swift */,
A7F3F8E72ACBFC760076C227 /* KeywordPickerView.swift */,
A7F3F8E92ACC221C0076C227 /* CategoryPickerView.swift */,
);
path = RecipeEditing;
sourceTree = "<group>";
};
A9C3BE522B630F1300562C79 /* ReusableViews */ = {
isa = PBXGroup;
children = (
A7CD3FD12B2C546A00D764AD /* CollapsibleView.swift */,
);
path = ReusableViews;
sourceTree = "<group>";
};
A9CA6CED2B4C084100F78AB5 /* RecipeExport */ = { A9CA6CED2B4C084100F78AB5 /* RecipeExport */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
@@ -528,7 +552,7 @@
A79AA8ED2B063AD5007D25F2 /* NextcloudApi.swift in Sources */, A79AA8ED2B063AD5007D25F2 /* NextcloudApi.swift in Sources */,
A70171822AA8E71900064C43 /* Nextcloud_Cookbook_iOS_ClientApp.swift in Sources */, A70171822AA8E71900064C43 /* Nextcloud_Cookbook_iOS_ClientApp.swift in Sources */,
A74D33C32AFCD1C300D06555 /* RecipeScraper.swift in Sources */, A74D33C32AFCD1C300D06555 /* RecipeScraper.swift in Sources */,
A70171AD2AA8EF4700064C43 /* MainViewModel.swift in Sources */, A70171AD2AA8EF4700064C43 /* AppState.swift in Sources */,
A76B8A6F2ADFFA8800096CEC /* SupportedLanguage.swift in Sources */, A76B8A6F2ADFFA8800096CEC /* SupportedLanguage.swift in Sources */,
A977D0E02B600318009783A9 /* RecipeTabView.swift in Sources */, A977D0E02B600318009783A9 /* RecipeTabView.swift in Sources */,
A703226F2ABB1DD700D7C4ED /* ColorExtension.swift in Sources */, A703226F2ABB1DD700D7C4ED /* ColorExtension.swift in Sources */,

View File

@@ -11,7 +11,7 @@ import SwiftUI
@main @main
struct Nextcloud_Cookbook_iOS_ClientApp: App { struct Nextcloud_Cookbook_iOS_ClientApp: App {
@StateObject var mainViewModel = MainViewModel() @StateObject var mainViewModel = AppState()
@AppStorage("onboarding") var onboarding = true @AppStorage("onboarding") var onboarding = true
@AppStorage("language") var language = Locale.current.language.languageCode?.identifier ?? "en" @AppStorage("language") var language = Locale.current.language.languageCode?.identifier ?? "en"

View File

@@ -10,7 +10,7 @@ import SwiftUI
import UIKit import UIKit
@MainActor class MainViewModel: ObservableObject { @MainActor class AppState: ObservableObject {
@Published var categories: [Category] = [] @Published var categories: [Category] = []
@Published var recipes: [String: [Recipe]] = [:] @Published var recipes: [String: [Recipe]] = [:]
@Published var recipeDetails: [Int: RecipeDetail] = [:] @Published var recipeDetails: [Int: RecipeDetail] = [:]
@@ -513,7 +513,7 @@ import UIKit
extension MainViewModel { extension AppState {
func loadLocal<T: Codable>(path: String) async -> T? { func loadLocal<T: Codable>(path: String) async -> T? {
do { do {
return try await dataStore.load(fromPath: path) return try await dataStore.load(fromPath: path)
@@ -612,7 +612,7 @@ extension DateFormatter {
// Timer logic // Timer logic
extension MainViewModel { extension AppState {
func createTimer(forRecipe recipeId: String, duration: DurationComponents) -> RecipeTimer { func createTimer(forRecipe recipeId: String, duration: DurationComponents) -> RecipeTimer {
let timer = RecipeTimer(duration: duration) let timer = RecipeTimer(duration: duration)
timers[recipeId] = timer timers[recipeId] = timer

View File

@@ -9,7 +9,7 @@ import Foundation
import SwiftUI import SwiftUI
@MainActor class RecipeEditViewModel: ObservableObject { @MainActor class RecipeEditViewModel: ObservableObject {
@ObservedObject var mainViewModel: MainViewModel @ObservedObject var mainViewModel: AppState
@Published var recipe: RecipeDetail = RecipeDetail() @Published var recipe: RecipeDetail = RecipeDetail()
@Published var prepDuration: DurationComponents = DurationComponents() @Published var prepDuration: DurationComponents = DurationComponents()
@@ -29,12 +29,12 @@ import SwiftUI
var waitingForUpload: Bool = false var waitingForUpload: Bool = false
init(mainViewModel: MainViewModel, uploadNew: Bool) { init(mainViewModel: AppState, uploadNew: Bool) {
self.mainViewModel = mainViewModel self.mainViewModel = mainViewModel
self.uploadNew = uploadNew self.uploadNew = uploadNew
} }
init(mainViewModel: MainViewModel, recipeDetail: RecipeDetail, uploadNew: Bool) { init(mainViewModel: AppState, recipeDetail: RecipeDetail, uploadNew: Bool) {
self.mainViewModel = mainViewModel self.mainViewModel = mainViewModel
self.recipe = recipeDetail self.recipe = recipeDetail
self.uploadNew = uploadNew self.uploadNew = uploadNew

View File

@@ -8,19 +8,19 @@
import SwiftUI import SwiftUI
struct MainView: View { struct MainView: View {
@StateObject var viewModel = MainViewModel() @StateObject var viewModel = AppState()
@StateObject var groceryList = GroceryList() @StateObject var groceryList = GroceryList()
@StateObject var recipeViewModel = RecipeTabView.ViewModel()
@State var selectedCategory: Category? = nil @StateObject var searchViewModel = SearchTabView.ViewModel()
@State var showLoadingIndicator: Bool = false
enum Tab { enum Tab {
case recipes, search, groceryList, settings case recipes, search, groceryList
} }
var body: some View { var body: some View {
TabView { TabView {
RecipeTabView(selectedCategory: $selectedCategory, showLoadingIndicator: $showLoadingIndicator) RecipeTabView()
.environmentObject(recipeViewModel)
.environmentObject(viewModel) .environmentObject(viewModel)
.environmentObject(groceryList) .environmentObject(groceryList)
.tabItem { .tabItem {
@@ -29,6 +29,7 @@ struct MainView: View {
.tag(Tab.recipes) .tag(Tab.recipes)
SearchTabView() SearchTabView()
.environmentObject(searchViewModel)
.environmentObject(viewModel) .environmentObject(viewModel)
.environmentObject(groceryList) .environmentObject(groceryList)
.tabItem { .tabItem {
@@ -42,16 +43,9 @@ struct MainView: View {
Label("Grocery List", systemImage: "storefront") Label("Grocery List", systemImage: "storefront")
} }
.tag(Tab.groceryList) .tag(Tab.groceryList)
SettingsView()
.environmentObject(viewModel)
.tabItem {
Label("Settings", systemImage: "gearshape")
}
.tag(Tab.settings)
} }
.task { .task {
showLoadingIndicator = true recipeViewModel.presentLoadingIndicator = true
await viewModel.getCategories() await viewModel.getCategories()
await viewModel.updateAllRecipeDetails() await viewModel.updateAllRecipeDetails()
@@ -63,11 +57,11 @@ struct MainView: View {
} }
return false return false
}) { }) {
self.selectedCategory = cat recipeViewModel.selectedCategory = cat
} }
} }
showLoadingIndicator = false
await groceryList.load() await groceryList.load()
recipeViewModel.presentLoadingIndicator = false
} }
} }
} }

View File

@@ -13,7 +13,7 @@ import SwiftUI
struct CategoryDetailView: View { struct CategoryDetailView: View {
@State var categoryName: String @State var categoryName: String
@State var searchText: String = "" @State var searchText: String = ""
@ObservedObject var viewModel: MainViewModel @ObservedObject var viewModel: AppState
@Binding var showEditView: Bool @Binding var showEditView: Bool
var body: some View { var body: some View {

View File

@@ -9,7 +9,7 @@ import Foundation
import SwiftUI import SwiftUI
struct RecipeCardView: View { struct RecipeCardView: View {
@State var viewModel: MainViewModel @State var viewModel: AppState
@State var recipe: Recipe @State var recipe: Recipe
@State var recipeThumb: UIImage? @State var recipeThumb: UIImage?
@State var isDownloaded: Bool? = nil @State var isDownloaded: Bool? = nil

View File

@@ -10,7 +10,7 @@ import SwiftUI
struct RecipeDetailView: View { struct RecipeDetailView: View {
@ObservedObject var viewModel: MainViewModel @ObservedObject var viewModel: AppState
@State var recipe: Recipe @State var recipe: Recipe
@State var recipeDetail: RecipeDetail? @State var recipeDetail: RecipeDetail?
@State var recipeImage: UIImage? @State var recipeImage: UIImage?
@@ -214,7 +214,7 @@ fileprivate struct ShareView: View {
fileprivate struct RecipeDurationSection: View { fileprivate struct RecipeDurationSection: View {
@ObservedObject var viewModel: MainViewModel @ObservedObject var viewModel: AppState
@State var recipeDetail: RecipeDetail @State var recipeDetail: RecipeDetail
var body: some View { var body: some View {

View File

@@ -11,7 +11,7 @@ import SwiftUI
struct SettingsView: View { struct SettingsView: View {
@EnvironmentObject var viewModel: MainViewModel @EnvironmentObject var viewModel: AppState
@ObservedObject var userSettings = UserSettings.shared @ObservedObject var userSettings = UserSettings.shared
@State fileprivate var alertType: SettingsAlert = .NONE @State fileprivate var alertType: SettingsAlert = .NONE

View File

@@ -10,25 +10,18 @@ import SwiftUI
struct RecipeTabView: View { struct RecipeTabView: View {
@Binding var selectedCategory: Category? @EnvironmentObject var viewModel: RecipeTabView.ViewModel
@Binding var showLoadingIndicator: Bool @EnvironmentObject var mainViewModel: AppState
@EnvironmentObject var viewModel: MainViewModel
@StateObject var userSettings: UserSettings = UserSettings.shared
@State private var showEditView: Bool = false
@State private var serverConnection: Bool = false
var body: some View { var body: some View {
NavigationSplitView { NavigationSplitView {
List(selection: $selectedCategory) { List(selection: $viewModel.selectedCategory) {
// Categories // Categories
ForEach(viewModel.categories) { category in ForEach(mainViewModel.categories) { category in
if category.recipe_count != 0 { if category.recipe_count != 0 {
NavigationLink(value: category) { NavigationLink(value: category) {
HStack(alignment: .center) { HStack(alignment: .center) {
if selectedCategory != nil && category.name == selectedCategory!.name { if viewModel.selectedCategory != nil && category.name == viewModel.selectedCategory!.name {
Image(systemName: "book") Image(systemName: "book")
} else { } else {
Image(systemName: "book.closed.fill") Image(systemName: "book.closed.fill")
@@ -52,53 +45,59 @@ struct RecipeTabView: View {
} }
.navigationTitle("Cookbooks") .navigationTitle("Cookbooks")
.toolbar { .toolbar {
RecipeTabViewToolBar( RecipeTabViewToolBar()
viewModel: viewModel, }
showEditView: $showEditView, .navigationDestination(isPresented: $viewModel.presentSettingsView) {
serverConnection: $serverConnection, SettingsView()
showLoadingIndicator: $showLoadingIndicator
)
} }
} detail: { } detail: {
NavigationStack { NavigationStack {
if let category = selectedCategory { if let category = viewModel.selectedCategory {
CategoryDetailView( CategoryDetailView(
categoryName: category.name, categoryName: category.name,
viewModel: viewModel, viewModel: mainViewModel,
showEditView: $showEditView showEditView: $viewModel.presentEditView
) )
.id(category.id) // Workaround: This is needed to update the detail view when the selection changes .id(category.id) // Workaround: This is needed to update the detail view when the selection changes
} }
} }
} }
.tint(.nextcloudBlue) .tint(.nextcloudBlue)
.sheet(isPresented: $showEditView) { .sheet(isPresented: $viewModel.presentEditView) {
RecipeEditView( RecipeEditView(
viewModel: viewModel:
RecipeEditViewModel( RecipeEditViewModel(
mainViewModel: viewModel, mainViewModel: mainViewModel,
uploadNew: true uploadNew: true
), ),
isPresented: $showEditView isPresented: $viewModel.presentEditView
) )
} }
.task { .task {
self.serverConnection = await viewModel.checkServerConnection() viewModel.serverConnection = await mainViewModel.checkServerConnection()
} }
.refreshable { .refreshable {
self.serverConnection = await viewModel.checkServerConnection() viewModel.serverConnection = await mainViewModel.checkServerConnection()
await viewModel.getCategories() await mainViewModel.getCategories()
} }
} }
class ViewModel: ObservableObject {
@Published var presentEditView: Bool = false
@Published var presentSettingsView: Bool = false
@Published var presentLoadingIndicator: Bool = false
@Published var presentConnectionPopover: Bool = false
@Published var serverConnection: Bool = false
@Published var selectedCategory: Category? = nil
}
} }
fileprivate struct RecipeTabViewToolBar: ToolbarContent { fileprivate struct RecipeTabViewToolBar: ToolbarContent {
@ObservedObject var viewModel: MainViewModel @EnvironmentObject var mainViewModel: AppState
@Binding var showEditView: Bool @EnvironmentObject var viewModel: RecipeTabView.ViewModel
@Binding var serverConnection: Bool
@Binding var showLoadingIndicator: Bool
@State private var presentPopover: Bool = false
var body: some ToolbarContent { var body: some ToolbarContent {
// Top left menu toolbar item // Top left menu toolbar item
@@ -106,20 +105,26 @@ fileprivate struct RecipeTabViewToolBar: ToolbarContent {
Menu { Menu {
Button { Button {
Task { Task {
showLoadingIndicator = true viewModel.presentLoadingIndicator = true
UserSettings.shared.lastUpdate = Date.distantPast UserSettings.shared.lastUpdate = Date.distantPast
await viewModel.getCategories() await mainViewModel.getCategories()
for category in viewModel.categories { for category in mainViewModel.categories {
await viewModel.getCategory(named: category.name, fetchMode: .preferServer) await mainViewModel.getCategory(named: category.name, fetchMode: .preferServer)
} }
await viewModel.updateAllRecipeDetails() await mainViewModel.updateAllRecipeDetails()
showLoadingIndicator = false viewModel.presentLoadingIndicator = false
} }
} label: { } label: {
Text("Refresh all") Text("Refresh all")
Image(systemName: "icloud.and.arrow.down") Image(systemName: "icloud.and.arrow.down")
} }
Button {
viewModel.presentSettingsView = true
} label: {
Text("Settings")
Image(systemName: "gearshape")
}
} label: { } label: {
Image(systemName: "ellipsis.circle") Image(systemName: "ellipsis.circle")
} }
@@ -129,18 +134,18 @@ fileprivate struct RecipeTabViewToolBar: ToolbarContent {
ToolbarItem(placement: .topBarTrailing) { ToolbarItem(placement: .topBarTrailing) {
Button { Button {
print("Check server connection") print("Check server connection")
presentPopover = true viewModel.presentConnectionPopover = true
} label: { } label: {
if showLoadingIndicator { if viewModel.presentLoadingIndicator {
ProgressView() ProgressView()
} else if serverConnection { } else if viewModel.serverConnection {
Image(systemName: "checkmark.icloud") Image(systemName: "checkmark.icloud")
} else { } else {
Image(systemName: "xmark.icloud") Image(systemName: "xmark.icloud")
} }
}.popover(isPresented: $presentPopover) { }.popover(isPresented: $viewModel.presentConnectionPopover) {
VStack(alignment: .leading) { VStack(alignment: .leading) {
Text(serverConnection ? LocalizedStringKey("Connected to server.") : LocalizedStringKey("Unable to connect to server.")) Text(viewModel.serverConnection ? LocalizedStringKey("Connected to server.") : LocalizedStringKey("Unable to connect to server."))
.bold() .bold()
Text("Last updated: \(DateFormatter.utcToString(date: UserSettings.shared.lastUpdate))") Text("Last updated: \(DateFormatter.utcToString(date: UserSettings.shared.lastUpdate))")
@@ -156,7 +161,7 @@ fileprivate struct RecipeTabViewToolBar: ToolbarContent {
ToolbarItem(placement: .topBarTrailing) { ToolbarItem(placement: .topBarTrailing) {
Button { Button {
print("Add new recipe") print("Add new recipe")
showEditView = true viewModel.presentEditView = true
} label: { } label: {
Image(systemName: "plus.circle.fill") Image(systemName: "plus.circle.fill")
} }

View File

@@ -10,26 +10,17 @@ import SwiftUI
struct SearchTabView: View { struct SearchTabView: View {
@EnvironmentObject var viewModel: MainViewModel @EnvironmentObject var viewModel: SearchTabView.ViewModel
@EnvironmentObject var mainViewModel: AppState
var body: some View {
RecipeSearchView(viewModel: viewModel)
}
}
struct RecipeSearchView: View {
@ObservedObject var viewModel: MainViewModel
@State var searchText: String = ""
@State var allRecipes: [Recipe] = []
var body: some View { var body: some View {
NavigationStack { NavigationStack {
VStack { VStack {
ScrollView(showsIndicators: false) { ScrollView(showsIndicators: false) {
LazyVStack { LazyVStack {
ForEach(recipesFiltered(), id: \.recipe_id) { recipe in ForEach(viewModel.recipesFiltered(), id: \.recipe_id) { recipe in
NavigationLink(value: recipe) { NavigationLink(value: recipe) {
RecipeCardView(viewModel: viewModel, recipe: recipe) RecipeCardView(viewModel: mainViewModel, recipe: recipe)
.shadow(radius: 2) .shadow(radius: 2)
} }
.buttonStyle(.plain) .buttonStyle(.plain)
@@ -37,16 +28,25 @@ struct RecipeSearchView: View {
} }
} }
.navigationDestination(for: Recipe.self) { recipe in .navigationDestination(for: Recipe.self) { recipe in
RecipeDetailView(viewModel: viewModel, recipe: recipe) RecipeDetailView(viewModel: mainViewModel, recipe: recipe)
} }
.searchable(text: $searchText, prompt: "Search recipes/keywords") .searchable(text: $viewModel.searchText, prompt: "Search recipes/keywords")
} }
.navigationTitle("Search recipe") .navigationTitle("Search recipe")
} }
.task { .task {
allRecipes = await viewModel.getRecipes() if viewModel.allRecipes.isEmpty {
viewModel.allRecipes = await mainViewModel.getRecipes()
} }
} }
.refreshable {
viewModel.allRecipes = await mainViewModel.getRecipes()
}
}
class ViewModel: ObservableObject {
@Published var allRecipes: [Recipe] = []
@Published var searchText: String = ""
func recipesFiltered() -> [Recipe] { func recipesFiltered() -> [Recipe] {
guard searchText != "" else { return allRecipes } guard searchText != "" else { return allRecipes }
@@ -56,3 +56,4 @@ struct RecipeSearchView: View {
} }
} }
} }
}