Advertisement
Xaniasty

Untitled

May 24th, 2024
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. import SwiftUI
  2.  
  3. struct PhotoGridView: View {
  4. @State private var recipes: [Recipe] = []
  5.  
  6. let columns = [
  7. GridItem(.flexible()),
  8. GridItem(.flexible())
  9. ]
  10.  
  11. @State private var selectedRecipe: Recipe?
  12.  
  13. var body: some View {
  14. NavigationView {
  15. ScrollView {
  16. LazyVGrid(columns: columns, spacing: 20) {
  17. ForEach(recipes) { recipe in
  18. RecipeGridItem(recipe: recipe)
  19. .onTapGesture {
  20. selectedRecipe = recipe
  21. }
  22. }
  23. }
  24. .padding()
  25. }
  26. .navigationTitle("Przepisy")
  27. .sheet(item: $selectedRecipe) { recipe in
  28. RecipeDetailView(recipe: recipe)
  29. }
  30. .onAppear(perform: loadRecipes)
  31. }
  32. }
  33.  
  34. private func loadRecipes() {
  35. // Załaduj przepisy z pliku JSON
  36. if let url = Bundle.main.url(forResource: "recipes", withExtension: "json"),
  37. let data = try? Data(contentsOf: url),
  38. let loadedRecipes = try? JSONDecoder().decode([Recipe].self, from: data) {
  39. self.recipes = loadedRecipes
  40. }
  41. }
  42. }
  43.  
  44. struct PhotoGridView_Previews: PreviewProvider {
  45. static var previews: some View {
  46. PhotoGridView()
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement