diff --git a/CatFact/CatFact/ContentView.swift b/CatFact/CatFact/ContentView.swift index 3ca1f1a..c17dc1a 100644 --- a/CatFact/CatFact/ContentView.swift +++ b/CatFact/CatFact/ContentView.swift @@ -8,12 +8,29 @@ import SwiftUI struct ContentView: View { + var c = CatFact() + + var buttonTitle = "Get Fact" + + @State + var list: [CatFact] = [] + var body: some View { - VStack { - Image(systemName: "globe") - .imageScale(.large) - .foregroundStyle(.tint) - Text("Hello, world!") + ScrollView { + VStack { + ForEach(Array(list.enumerated()), id: \.offset) { fact in + Text(fact.element.fact ?? "") + +// Text(fact.element.length) + } + + Button("Get Fact") { + Task { @MainActor in + try? await c.fetch() + list.append(c) + } + } + } } .padding() } @@ -22,3 +39,27 @@ struct ContentView: View { #Preview { ContentView() } + + +class CatFact: ObservableObject, Codable { + var fact: String? + var length: Int? + var url: String? + + init(fact: String? = nil, length: Int? = nil, url: String? = nil) { + self.fact = fact + self.length = length + self.url = url + } + + func fetch() async throws { + url = "https://catfact.ninja/fact" + let request = URLRequest(url: URL(string: url!)!) + let response = try await URLSession.shared.data(for: request) + + let catFact = try JSONDecoder().decode(CatFact.self, from: response.0) + + fact = catFact.fact + length = catFact.length + } +}