Created
April 25, 2020 18:19
-
-
Save unnamedd/7983e9b324a5ebc2f723b4a606e7a680 to your computer and use it in GitHub Desktop.
A small extension for PreviewDevice to avoid enter the values in the SwiftUI previews manually
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* PreviewDevice+ListOfPads.swift | |
* Copyright (c) Thiago Holanda 2020 | |
* https://twitter.com/tholanda | |
* | |
* MIT license | |
*/ | |
import SwiftUI | |
extension PreviewDevice { | |
private enum Devices: String { | |
case iPadMini4 = "iPad mini 4" | |
case iPadAir2 = "iPad Air 2" | |
case iPadPro_9_7 = "iPad Pro (9.7-inch)" | |
case iPadPro_12_9 = "iPad Pro (12.9-inch)" | |
case iPad5thGen = "iPad (5th generation)" | |
case iPadPro_12_9_2ndGen = "iPad Pro (12.9-inch) (2nd generation)" | |
case iPadPro_10_5 = "iPad Pro (10.5-inch)" | |
case iPad6thGen = "iPad (6th generation)" | |
case iPadPro_11 = "iPad Pro (11-inch)" | |
case iPadPro_12_9_3rdGen = "iPad Pro (12.9-inch) (3rd generation)" | |
case iPadMini5thGen = "iPad mini (5th generation)" | |
case iPadAir3rdGen = "iPad Air (3rd generation)" | |
} | |
private enum Orientation { | |
case portrait | |
case landscape | |
} | |
/// iPad mini 4 | |
static var iPadMini4 = PreviewDevice(rawValue: Devices.iPadMini4.rawValue) | |
/// iPad Air 2 | |
static var iPadAir2 = PreviewDevice(rawValue: Devices.iPadAir2.rawValue) | |
/// iPad Pro (9.7-inch) | |
static var iPadPro_9_7 = PreviewDevice(rawValue: Devices.iPadPro_9_7.rawValue) | |
/// iPad Pro (12.9-inch) | |
static var iPadPro_12_9 = PreviewDevice(rawValue: Devices.iPadPro_12_9.rawValue) | |
/// iPad (5th generation) | |
static var iPad5thGen = PreviewDevice(rawValue: Devices.iPad5thGen.rawValue) | |
/// iPad Pro (12.9-inch) (2nd generation) | |
static var iPadPro_12_9_2ndGen = PreviewDevice(rawValue: Devices.iPadPro_12_9_2ndGen.rawValue) | |
/// iPad Pro (10.5-inch) | |
static var iPadPro_10_5 = PreviewDevice(rawValue: Devices.iPadPro_10_5.rawValue) | |
/// iPad (6th generation) | |
static var iPad6thGen = PreviewDevice(rawValue: Devices.iPad6thGen.rawValue) | |
/// iPad Pro (11-inch) | |
static var iPadPro_11 = PreviewDevice(rawValue: Devices.iPadPro_11.rawValue) | |
/// iPad Pro (12.9-inch) (3rd generation) | |
static var iPadPro_12_9_3rdGen = PreviewDevice(rawValue: Devices.iPadPro_12_9_3rdGen.rawValue) | |
/// iPad mini (5th generation) | |
static var iPadMini5thGen = PreviewDevice(rawValue: Devices.iPadMini5thGen.rawValue) | |
/// iPad Air (3rd generation) | |
static var iPadAir3rdGen = PreviewDevice(rawValue: Devices.iPadAir3rdGen.rawValue) | |
/// Syntax sugar for the property `rawvalue` | |
var displayName: String { | |
rawValue | |
} | |
var landscapeLayout: PreviewLayout { | |
screenSize(orientation: .landscape) | |
} | |
var portraitLayout: PreviewLayout { | |
screenSize(orientation: .landscape) | |
} | |
private func screenSize(orientation: Orientation) -> PreviewLayout { | |
guard let device = Devices(rawValue: rawValue) else { | |
return .sizeThatFits | |
} | |
switch device { | |
case .iPadMini4, | |
.iPadMini5thGen: | |
return orientation == .portrait ? | |
.fixed(width: 1536, height: 2048) | |
: .fixed(width: 2048, height: 1536) | |
case .iPadAir2, | |
.iPadAir3rdGen: | |
return orientation == .portrait ? | |
.fixed(width: 1536, height: 2048) | |
: .fixed(width: 2048, height: 1536) | |
case .iPadPro_9_7, | |
.iPad5thGen, | |
.iPad6thGen: | |
return orientation == .portrait ? | |
.fixed(width: 1536, height: 2048) | |
: .fixed(width: 2048, height: 1536) | |
case .iPadPro_10_5: | |
return orientation == .portrait ? | |
.fixed(width: 1668, height: 2224) | |
: .fixed(width: 2224, height: 1668) | |
case .iPadPro_11: | |
return orientation == .portrait ? | |
.fixed(width: 1536, height: 2048) | |
: .fixed(width: 2048, height: 1536) | |
case .iPadPro_12_9, | |
.iPadPro_12_9_2ndGen, | |
.iPadPro_12_9_3rdGen: | |
return orientation == .portrait ? | |
.fixed(width: 2048, height: 2732) | |
: .fixed(width: 2732, height: 2048) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
let device = PreviewDevice.iPadMini4 | |
return ContentView() | |
.previewLayout(device.landscapeLayout) | |
.previewDisplayName(device.displayName) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment