Fixed a crash due to a race condition in RecipeListView

This commit is contained in:
VincentMeilinger
2024-03-10 12:50:42 +01:00
parent 2c749754f4
commit 5d82cdf0c2
4 changed files with 32 additions and 11 deletions

View File

@@ -48,7 +48,6 @@ struct RecipeCardView: View {
} }
.background(Color.backgroundHighlight) .background(Color.backgroundHighlight)
.clipShape(RoundedRectangle(cornerRadius: 17)) .clipShape(RoundedRectangle(cornerRadius: 17))
.padding(.horizontal)
.task { .task {
recipeThumb = await appState.getImage( recipeThumb = await appState.getImage(
id: recipe.recipe_id, id: recipe.recipe_id,
@@ -67,5 +66,6 @@ struct RecipeCardView: View {
fetchMode: UserSettings.shared.storeThumb ? .preferServer : .onlyServer fetchMode: UserSettings.shared.storeThumb ? .preferServer : .onlyServer
) )
} }
.frame(height: 80)
} }
} }

View File

@@ -17,10 +17,9 @@ struct RecipeListView: View {
@State var searchText: String = "" @State var searchText: String = ""
@Binding var showEditView: Bool @Binding var showEditView: Bool
@State var selectedRecipe: Recipe? = nil @State var selectedRecipe: Recipe? = nil
@State var presentRecipeView: Bool = false
var body: some View { var body: some View {
ScrollView(showsIndicators: false) { /*ScrollView(showsIndicators: false) {
LazyVStack { LazyVStack {
ForEach(recipesFiltered(), id: \.recipe_id) { recipe in ForEach(recipesFiltered(), id: \.recipe_id) { recipe in
NavigationLink(value: recipe) { NavigationLink(value: recipe) {
@@ -31,17 +30,35 @@ struct RecipeListView: View {
.buttonStyle(.plain) .buttonStyle(.plain)
.onTapGesture { .onTapGesture {
selectedRecipe = recipe selectedRecipe = recipe
presentRecipeView = true //presentRecipeView = true
} }
} }
} }
}*/
List(recipesFiltered(), id: \.recipe_id) { recipe in
RecipeCardView(recipe: recipe)
.shadow(radius: 2)
.background(
NavigationLink(value: recipe) {
EmptyView()
} }
.buttonStyle(.plain)
.opacity(0)
)
.frame(height: 85)
.listRowInsets(EdgeInsets(top: 5, leading: 10, bottom: 5, trailing: 10))
.listRowSeparatorTint(.clear)
}
.listStyle(.plain)
.searchable(text: $searchText, prompt: "Search recipes/keywords")
.navigationTitle(categoryName == "*" ? String(localized: "Other") : categoryName)
.navigationDestination(for: Recipe.self) { recipe in .navigationDestination(for: Recipe.self) { recipe in
RecipeView(viewModel: RecipeView.ViewModel(recipe: recipe)) RecipeView(viewModel: RecipeView.ViewModel(recipe: recipe))
.environmentObject(appState) .environmentObject(appState)
.environmentObject(groceryList) .environmentObject(groceryList)
} }
.navigationTitle(categoryName == "*" ? String(localized: "Other") : categoryName)
.toolbar { .toolbar {
ToolbarItem(placement: .topBarTrailing) { ToolbarItem(placement: .topBarTrailing) {
Button { Button {
@@ -52,7 +69,6 @@ struct RecipeListView: View {
} }
} }
} }
.searchable(text: $searchText, prompt: "Search recipes/keywords")
.task { .task {
await appState.getCategory( await appState.getCategory(
named: categoryName, named: categoryName,

View File

@@ -13,13 +13,12 @@ struct RecipeTabView: View {
@EnvironmentObject var appState: AppState @EnvironmentObject var appState: AppState
@EnvironmentObject var groceryList: GroceryList @EnvironmentObject var groceryList: GroceryList
@EnvironmentObject var viewModel: RecipeTabView.ViewModel @EnvironmentObject var viewModel: RecipeTabView.ViewModel
@EnvironmentObject var mainViewModel: AppState
var body: some View { var body: some View {
NavigationSplitView { NavigationSplitView {
List(selection: $viewModel.selectedCategory) { List(selection: $viewModel.selectedCategory) {
// Categories // Categories
ForEach(mainViewModel.categories) { category in ForEach(appState.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) {
@@ -72,11 +71,17 @@ struct RecipeTabView: View {
} }
.tint(.nextcloudBlue) .tint(.nextcloudBlue)
.task { .task {
viewModel.serverConnection = await mainViewModel.checkServerConnection() let connection = await appState.checkServerConnection()
DispatchQueue.main.async {
viewModel.serverConnection = connection
}
} }
.refreshable { .refreshable {
viewModel.serverConnection = await mainViewModel.checkServerConnection() let connection = await appState.checkServerConnection()
await mainViewModel.getCategories() DispatchQueue.main.async {
viewModel.serverConnection = connection
}
await appState.getCategories()
} }
} }