SpacePod 22 - SwiftUI Previews - Device, DisplayName, & InterfaceOrientation
Previously we added a “Fetching Pods…” view whenever we don’t have any pods to display. This works well enough on iPads, but results in a blank screen on iPhones. Let’s fix the problem and add some additional previews along the way.
Problem
Fetching pods doesn’t show on iPhones in portrait mode
Goals
- Show “Fetching Pods” on iPhone and iPads in any orientation
- Use
.previewDevice()
- Use
.previewDisplayName()
- Use
.previewInterfaceOrientation()
Steps
- Create a preview with empty pods
- Add a second
Text("Fetching Pods...")
- Attach task to NavigationView
- Move list inside
if let pod = pods.first {
- Add previewDevice
- Add previewDisplayName
- Add previewInterfaceOrientation
- Create two iPad Layouts
View
NavigationView Task
NavigationView {
}
.task {
if pods.isEmpty { await getPods() }
}
List
if let pod = pods.first {
List {
...
“Fetching Pods…”
} else {
Text("Fetching Pods...")
Text("Fetching Pods...")
}
Previews
PodListView(pods: [])
.previewDevice(PreviewDevice(rawValue: "iPhone 13 mini"))
.previewDisplayName("iPhone 13 mini - Fetching Pods...")
PodListView(pods: pods!)
.previewDevice(PreviewDevice(rawValue: "iPhone 13 mini"))
.previewDisplayName("iPhone 13 mini - Fetching Pods...")
PodListView(pods: [])
.previewDevice(PreviewDevice(rawValue: "iPad mini (6th generation)"))
.previewDisplayName("iPad mini - Fetching Pods...")
.previewInterfaceOrientation(.landscapeRight)
PodListView(pods: pods!)
.previewDevice(PreviewDevice(rawValue: "iPad mini (6th generation)"))
.previewDisplayName("iPad mini")
.previewInterfaceOrientation(.landscapeRight)
PodListView.swift Complete
import SwiftUI
struct PodListView: View {
@State var pods: [Pod] = []
var body: some View {
NavigationView {
if let pod = pods.first {
List {
ForEach(pods, id: \.self) { pod in
NavigationLink(destination: PodDetailView(pod: pod)) {
Text(pod.title)
}
}
}
.navigationTitle("SpacePod")
.navigationBarTitleDisplayMode(.inline)
.refreshable {
await getPods()
}
PodDetailView(pod: pod)
} else {
Text("Fetching Pods...")
Text("Fetching Pods...")
}
}
.task {
if pods.isEmpty { 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 {
// Disable some of these if your previews are timing out.
PodListView(pods: [])
.previewDevice(PreviewDevice(rawValue: "iPhone 13 mini"))
.previewDisplayName("iPhone 13 mini - Fetching Pods...")
PodListView(pods: pods!)
.previewDevice(PreviewDevice(rawValue: "iPhone 13 mini"))
.previewDisplayName("iPhone 13 mini")
PodListView(pods: [])
.previewDevice(PreviewDevice(rawValue: "iPad mini (6th generation)"))
.previewDisplayName("iPad mini - Fetching Pods...")
.previewInterfaceOrientation(.landscapeRight)
PodListView(pods: pods!)
.previewDevice(PreviewDevice(rawValue: "iPad mini (6th generation)"))
.previewDisplayName("iPhone 13 mini")
.previewInterfaceOrientation(.landscapeRight)
}
}