Code 55
Code 55
Code 55
{
"name": "Fear and Shit Index",
"data": [
{
"value": "52",
"value_classification": "Neutral",
"timestamp": "1641792000",
"time_until_update": "47649"
},
{
"value": "603”,
"value_classification": "Greed",
"timestamp": "1641705600",
"time_until_update": "129649"
}
],
"metadata": {
"error": null
}
}
*/
import SwiftUI
import Combine
@main
struct GreedFearAppApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
URLSession.shared.dataTaskPublisher(for: url)
.map { $0.data }
.decode(type: ToDo.self, decoder:
JSONDecoder())
.receive(on: DispatchQueue.main)
// Update on the main thread
.sink(receiveCompletion:
{ completion in
switch completion {
case .failure(let error):
print("Error fetching data: \
(error)")
case .finished:
break
}
}, receiveValue: { [weak self]
response in
self?.fearGreedData =
response // Update the published data
})
.store(in: &cancellables)
}
}
fearGreedService.fetchFearGreedData()
VStack(alignment: .leading) {
Text("Value: \
(fearGreedService.fearGreedData.title)")
.font(.headline)
Text("Classification: \
(fearGreedService.fearGreedData.title)")
.font(.subheadline)
Text("Timestamp: \
(fearGreedService.fearGreedData.title)")
.font(.caption)
}// Fetch data when
view appears
}
} else {