|
| 1 | +import SwiftUI |
| 2 | +import Foundation |
| 3 | + |
| 4 | +// MARK: - Data Models |
| 5 | +struct MealCategoryResponse: Decodable { |
| 6 | + let categories: [Food] |
| 7 | +} |
| 8 | + |
| 9 | +struct Food: Identifiable, Decodable, Hashable { |
| 10 | + let id: String |
| 11 | + let name: String |
| 12 | + let thumbnailURL: URL |
| 13 | + let description: String |
| 14 | + |
| 15 | + enum CodingKeys: String, CodingKey { |
| 16 | + case id = "idCategory" |
| 17 | + case name = "strCategory" |
| 18 | + case thumbnailURL = "strCategoryThumb" |
| 19 | + case description = "strCategoryDescription" |
| 20 | + } |
| 21 | +} |
| 22 | +// MARK: - Actor |
| 23 | + |
| 24 | +/// Actor that manages fetching and storing food items in a thread-safe way. |
| 25 | +actor FoodStore { |
| 26 | + private(set) var foods: [Food] = [] |
| 27 | + |
| 28 | + func fetchFoodData() async throws { |
| 29 | + guard let url = URL(string: "https://github.com/janeshsutharios/REST_GET_API/raw/refs/heads/main/food.json") else { |
| 30 | + throw URLError(.badURL) |
| 31 | + } |
| 32 | + |
| 33 | + let (data, _) = try await URLSession.shared.data(from: url) |
| 34 | + let decodedFoods = try JSONDecoder().decode(MealCategoryResponse.self, from: data) |
| 35 | + self.foods = decodedFoods.categories |
| 36 | + } |
| 37 | + |
| 38 | + func getFoods() -> [Food] { |
| 39 | + return foods |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// MARK: - ViewModel |
| 44 | +@MainActor |
| 45 | +class FoodViewModel: ObservableObject { |
| 46 | + @Published var items: [Food] = [] |
| 47 | + @Published var error: String? = nil |
| 48 | + private let store = FoodStore() |
| 49 | + |
| 50 | + func load() { |
| 51 | + Task { |
| 52 | + let result = await getFoods() |
| 53 | + switch result { |
| 54 | + case .success(let foods): |
| 55 | + await MainActor.run { |
| 56 | + self.items = foods |
| 57 | + self.error = nil |
| 58 | + } |
| 59 | + case .failure(let err): |
| 60 | + await MainActor.run { |
| 61 | + self.error = err.localizedDescription |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + nonisolated func getFoods() async -> Result<[Food], Error> { |
| 68 | + do { |
| 69 | + try await store.fetchFoodData() |
| 70 | + let foods = await store.getFoods() |
| 71 | + return .success(foods) |
| 72 | + } catch { |
| 73 | + return .failure(error) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | +} |
| 78 | + |
| 79 | +// MARK: - View |
| 80 | + |
| 81 | +struct FoodListView: View { |
| 82 | + @StateObject private var viewModel = FoodViewModel() |
| 83 | + |
| 84 | + var body: some View { |
| 85 | + NavigationView { |
| 86 | + List(viewModel.items) { item in |
| 87 | + HStack(alignment: .top, spacing: 12) { |
| 88 | + AsyncImage(url: item.thumbnailURL) { image in |
| 89 | + image |
| 90 | + .resizable() |
| 91 | + .aspectRatio(contentMode: .fill) |
| 92 | + .frame(width: 90, height: 90) |
| 93 | + .clipShape(Circle()) |
| 94 | + } placeholder: { |
| 95 | + ProgressView() |
| 96 | + } |
| 97 | + VStack(alignment: .leading, spacing: 4) { |
| 98 | + Text(item.name) |
| 99 | + .font(.headline) |
| 100 | + Text(item.description) |
| 101 | + .font(.subheadline) |
| 102 | + .foregroundColor(.gray) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + .navigationTitle("🍱 Food Menu") |
| 107 | + .onAppear { |
| 108 | + viewModel.load() |
| 109 | + } |
| 110 | + .overlay { |
| 111 | + if let error = viewModel.error { |
| 112 | + Text("Error: \(error)") |
| 113 | + .foregroundColor(.red) |
| 114 | + .padding() |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// MARK: - Preview |
| 122 | + |
| 123 | +struct ContentView_Previews: PreviewProvider { |
| 124 | + static var previews: some View { |
| 125 | + FoodListView() |
| 126 | + } |
| 127 | +} |
0 commit comments