Last time we accidently introduced a bug where the selected pod details can be overwritten by our default pod.
Objectives
- Fix Pod.default bug
- Fix Preview
- Fix Task reloading bug
Steps
Replace default
In PodDetailView.swift replace = Pod.default
with : Pod
.
Fix Preview
Load and decode get-pod.json, and pass it to our view.
struct PodDetailView_Previews: PreviewProvider {
static var pod = File.data(from: "get-pod", withExtension: .json)?.toPod
static var previews: some View {
PodDetailView(pod: pod!)
}
}
Delete Task
Delete task as we are passing the data from PodListView.swift.
.task {
if let response = await Network().getPod() {
pod = response
}
}
PodDetailView.swift
import SwiftUI
struct PodListView: View {
@State var pods: [Pod] = []
var body: some View {
NavigationView {
List {
ForEach(pods, id: \.self) { pod in
NavigationLink(destination: PodDetailView(pod: pod)) {
Text(pod.title)
}
}
}
.navigationTitle(pods.isEmpty ? "Fetching Pods..." : "SpacePod")
.task {
if pods.isEmpty { await getPods() }
}
.refreshable {
await getPods()
}
}
}
private func getPods() async {
if let response = await Network().getPods() {
pods = response
}
}
}
struct PodListView_Previews: PreviewProvider {
static var pods = File.data(from: "get-pods", withExtension: .json)?.toPods
static var previews: some View {
PodListView(pods: pods!)
}
}