SpacePod 21 SwiftUI ‘Split View’
YouTube
Issues
- Large empty area when app launches
- Navigation title layout issues
Steps
- Detail View (do not put in in an HStack)
- Loading View
- NavigationView tweaks
PodListView.swift Changes
Loading…
Display First Pod
if let pod = pods.first {
PodDetailView(pod: pod)
} else {
Text("Fetching Pods...")
}
Nav View Tweaks
List {
...
.navigationTitle("SpacePod")
.navigationBarTitleDisplayMode(.inline)
PodListView.swift Complete
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("SpacePod")
.navigationBarTitleDisplayMode(.inline)
.task {
if pods.isEmpty { await getPods() }
}
.refreshable {
await getPods()
}
if let pod = pods.first {
PodDetailView(pod: pod)
} else {
Text("Fetching Pods...")
}
}
}
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!)
}
}