Advertisement
Xaniasty

Untitled

May 24th, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. import SwiftUI
  2.  
  3. // Model `Recipe` z polem `imagePath`
  4. struct Recipe: Identifiable, Decodable {
  5. let id = UUID()
  6. var filename: String
  7. var name: String
  8. var description: String
  9. var estimatedTime: String
  10. var imagePath: String // Dodane pole `imagePath`
  11. }
  12.  
  13. struct PhotoGridView: View {
  14. @State private var recipes: [Recipe] = []
  15. @State private var selectedRecipe: Recipe? = nil
  16.  
  17. private let columns: [GridItem] = Array(repeating: .init(.flexible()), count: 2)
  18.  
  19. var body: some View {
  20. ScrollView {
  21. LazyVGrid(columns: columns, spacing: 20) {
  22. ForEach(recipes) { recipe in
  23. RecipeGridItem(recipe: recipe)
  24. .onTapGesture {
  25. selectedRecipe = recipe
  26. }
  27. }
  28. }
  29. .padding()
  30. .sheet(item: $selectedRecipe) { recipe in
  31. RecipeDetailView(recipe: recipe)
  32. }
  33. }
  34. .onAppear {
  35. loadRecipes()
  36. }
  37. }
  38.  
  39. private func loadRecipes() {
  40. if let url = Bundle.main.url(forResource: "recipes", withExtension: "json"),
  41. let data = try? Data(contentsOf: url),
  42. let loadedRecipes = try? JSONDecoder().decode([Recipe].self, from: data) {
  43. // Aktualizacja ścieżki obrazu na pełną ścieżkę
  44. for index in 0..<loadedRecipes.count {
  45. let imageName = loadedRecipes[index].filename
  46. if let imagePath = Bundle.main.path(forResource: "Photos/\(imageName)", ofType: "jpg") {
  47. loadedRecipes[index].imagePath = imagePath
  48. }
  49. }
  50. self.recipes = loadedRecipes
  51. }
  52. }
  53. }
  54.  
  55. struct RecipeGridItem: View {
  56. let recipe: Recipe
  57.  
  58. var body: some View {
  59. VStack {
  60. if let uiImage = UIImage(named: recipe.imagePath) {
  61. Image(uiImage: uiImage)
  62. .resizable()
  63. .aspectRatio(contentMode: .fill)
  64. .frame(width: 150, height: 150)
  65. .clipped()
  66. }
  67. Text(recipe.name)
  68. .font(.headline)
  69. .padding(.top, 5)
  70. Text(recipe.description)
  71. .font(.subheadline)
  72. .foregroundColor(.gray)
  73. Text("Time: \(recipe.estimatedTime)")
  74. .font(.caption)
  75. .foregroundColor(.secondary)
  76. }
  77. }
  78. }
  79.  
  80. struct RecipeDetailView: View {
  81. let recipe: Recipe
  82.  
  83. var body: some View {
  84. VStack {
  85. if let uiImage = UIImage(named: recipe.imagePath) {
  86. Image(uiImage: uiImage)
  87. .resizable()
  88. .aspectRatio(contentMode: .fit)
  89. .padding()
  90. }
  91. Text(recipe.name)
  92. .font(.title)
  93. .padding(.bottom, 10)
  94. Text(recipe.description)
  95. .font(.body)
  96. .padding(.bottom, 10)
  97. Text("Estimated Time: \(recipe.estimatedTime)")
  98. .font(.subheadline)
  99. .foregroundColor(.gray)
  100. }
  101. .padding()
  102. }
  103. }
  104.  
  105. struct ContentView: View {
  106. var body: some View {
  107. PhotoGridView()
  108. }
  109. }
  110.  
  111. struct ContentView_Previews: PreviewProvider {
  112. static var previews: some View {
  113. ContentView()
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement