Skip to content

Commit 0058212

Browse files
committed
Merge branch 'release/3.0.1'
2 parents 4c55b75 + 595290f commit 0058212

11 files changed

+716
-600
lines changed

Sources/Device.swift

Lines changed: 2 additions & 583 deletions
Large diffs are not rendered by default.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// Device
3+
// Thingy
4+
//
5+
// Created by Bojan Dimovski on 19/01/2020.
6+
// Copyright © 2020 Bojan Dimovski. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public extension Device {
12+
13+
/// Checks if the current device is the same as the compared model.
14+
///
15+
/// - Parameter to: A model to compare the current device against.
16+
/// - Returns: True if the device is the same, and false otherwise.
17+
/// - Throws: An error when comparing incompatible families, product lines or unknown products.
18+
func isEqual(to compared: Device) throws -> Bool {
19+
return try Device.compare(lhs: self, rhs: compared, sign: ==)
20+
}
21+
22+
/// Checks if the current device is newer (or same) than the compared model.
23+
///
24+
/// - Parameter than: A model to compare the current device against.
25+
/// - Returns: True if the device is newer or the same, and false if it's older.
26+
/// - Throws: An error when comparing incompatible families, product lines or unknown products.
27+
func isNewerOrEqual(than compared: Device) throws -> Bool {
28+
return try Device.compare(lhs: self, rhs: compared, sign: >=)
29+
}
30+
31+
/// Checks if the current device is older than the compared model.
32+
///
33+
/// - Parameter than: A model to compare the current device against.
34+
/// - Returns: True if the device is older, and false if it's newer or the same.
35+
/// - Throws: An error when comparing incompatible families, product lines or unknown products.
36+
func isOlder(than compared: Device) throws -> Bool {
37+
return try Device.compare(lhs: self, rhs: compared, sign: <)
38+
}
39+
40+
internal static func compare(lhs: Device, rhs: Device, sign: ((RawDevice, RawDevice) -> Bool)) throws -> Bool {
41+
if case .unknown = lhs {
42+
throw ThingyError.incomparableUnknownProduct
43+
}
44+
45+
if case .unknown = rhs {
46+
throw ThingyError.incomparableUnknownProduct
47+
}
48+
49+
guard lhs.family == rhs.family
50+
else {
51+
throw ThingyError.incomparableFamilies
52+
}
53+
54+
if let lhsProductLine = lhs.productLine,
55+
let rhsProductLine = rhs.productLine,
56+
lhsProductLine != rhsProductLine {
57+
throw ThingyError.incomparableProductLines
58+
}
59+
60+
let lhsRaw = RawDevice(family: lhs.family, modelNumber: lhs.lowestNumber)
61+
let rhsRaw = RawDevice(family: rhs.family, modelNumber: rhs.lowestNumber)
62+
63+
return sign(lhsRaw, rhsRaw)
64+
}
65+
66+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
//
2+
// Device
3+
// Thingy
4+
//
5+
// Created by Bojan Dimovski on 19/01/2020.
6+
// Copyright © 2020 Bojan Dimovski. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public extension Device {
12+
13+
/// Returns the display size in inches.
14+
var displaySize: Display.Size {
15+
return display.size
16+
}
17+
18+
/// Returns an extended info on the display, including all resolutions, scale and density.
19+
var display: Display {
20+
switch self {
21+
case .iPhone4S:
22+
return Display(size: .screen3_5Inch, resolution: CGSize(width: 320, height: 480), physicalResolution: CGSize(width: 640, height: 960), renderedResolution: CGSize(width: 640, height: 960), scale: 2.0, density: 326, hasTrueTone: false, colorSpace: .sRGB)
23+
24+
case .iPhone5,
25+
.iPhone5c,
26+
.iPhone5s,
27+
.iPhoneSE,
28+
.iPodTouch5G,
29+
.iPodTouch6G,
30+
.iPodTouch7G:
31+
return Display(size: .screen4Inch, resolution: CGSize(width: 320, height: 568), physicalResolution: CGSize(width: 640, height: 1136), renderedResolution: CGSize(width: 640, height: 1136), scale: 2.0, density: 326, hasTrueTone: false, colorSpace: .sRGB)
32+
33+
case .iPhone6,
34+
.iPhone6s:
35+
return Display(size: .screen4_7Inch, resolution: CGSize(width: 375, height: 667), physicalResolution: CGSize(width: 750, height: 1334), renderedResolution: CGSize(width: 750, height: 1334), scale: 2.0, density: 326, hasTrueTone: false, colorSpace: .sRGB)
36+
37+
case .iPhone7:
38+
return Display(size: .screen4_7Inch, resolution: CGSize(width: 375, height: 667), physicalResolution: CGSize(width: 750, height: 1334), renderedResolution: CGSize(width: 750, height: 1334), scale: 2.0, density: 326, hasTrueTone: false, colorSpace: .p3)
39+
40+
case .iPhone8:
41+
return Display(size: .screen4_7Inch, resolution: CGSize(width: 375, height: 667), physicalResolution: CGSize(width: 750, height: 1334), renderedResolution: CGSize(width: 750, height: 1334), scale: 2.0, density: 326, hasTrueTone: true, colorSpace: .p3)
42+
43+
case .iPhone6Plus,
44+
.iPhone6sPlus:
45+
return Display(size: .screen5_5Inch, resolution: CGSize(width: 414, height: 736), physicalResolution: CGSize(width: 1080, height: 1920), renderedResolution: CGSize(width: 1242, height: 2208), scale: 3.0, density: 401, hasTrueTone: false, colorSpace: .sRGB)
46+
47+
case .iPhone7Plus:
48+
return Display(size: .screen5_5Inch, resolution: CGSize(width: 414, height: 736), physicalResolution: CGSize(width: 1080, height: 1920), renderedResolution: CGSize(width: 1242, height: 2208), scale: 3.0, density: 401, hasTrueTone: false, colorSpace: .p3)
49+
50+
case .iPhone8Plus:
51+
return Display(size: .screen5_5Inch, resolution: CGSize(width: 414, height: 736), physicalResolution: CGSize(width: 1080, height: 1920), renderedResolution: CGSize(width: 1242, height: 2208), scale: 3.0, density: 401, hasTrueTone: true, colorSpace: .p3)
52+
53+
case .iPhoneX, .iPhoneXS, .iPhone11Pro:
54+
return Display(size: .screen5_8Inch, resolution: CGSize(width: 375, height: 812), physicalResolution: CGSize(width: 1125, height: 2436), renderedResolution: CGSize(width: 1125, height: 2436), scale: 3.0, density: 458, hasTrueTone: true, colorSpace: .p3)
55+
56+
case .iPhoneXR, .iPhone11:
57+
return Display(size: .screen6_1Inch, resolution: CGSize(width: 414, height: 896), physicalResolution: CGSize(width: 828, height: 1792), renderedResolution: CGSize(width: 828, height: 1792), scale: 2.0, density: 326, hasTrueTone: true, colorSpace: .p3)
58+
59+
case .iPhoneXSMax, .iPhone11ProMax:
60+
return Display(size: .screen6_5Inch, resolution: CGSize(width: 414, height: 896), physicalResolution: CGSize(width: 1242, height: 2688), renderedResolution: CGSize(width: 1242, height: 2688), scale: 3.0, density: 458, hasTrueTone: true, colorSpace: .p3)
61+
62+
case .appleTV4:
63+
return Display(size: .notApplicable, resolution: CGSize(width: 1920, height: 1080), physicalResolution: CGSize(width: 1920, height: 1080), renderedResolution: CGSize(width: 1920, height: 1080), scale: 1.0, density: 0, hasTrueTone: false, colorSpace: .sRGB)
64+
65+
case .appleTV4K:
66+
return Display(size: .notApplicable, resolution: CGSize(width: 3840, height: 2160), physicalResolution: CGSize(width: 3840, height: 2160), renderedResolution: CGSize(width: 3840, height: 2160), scale: 1.0, density: 0, hasTrueTone: false, colorSpace: .sRGB)
67+
68+
case .iPad2:
69+
return Display(size: .screen9_7Inch, resolution: CGSize(width: 1024, height: 768), physicalResolution: CGSize(width: 1024, height: 768), renderedResolution: CGSize(width: 1024, height: 768), scale: 1.0, density: 132, hasTrueTone: false, colorSpace: .sRGB)
70+
71+
case .iPad3,
72+
.iPad4,
73+
.iPad5,
74+
.iPad6,
75+
.iPadAir,
76+
.iPadAir2:
77+
return Display(size: .screen9_7Inch, resolution: CGSize(width: 1024, height: 768), physicalResolution: CGSize(width: 2048, height: 1536), renderedResolution: CGSize(width: 2048, height: 1536), scale: 2.0, density: 264, hasTrueTone: false, colorSpace: .sRGB)
78+
79+
case .iPad7:
80+
return Display(size: .screen10_2Inch, resolution: CGSize(width: 1080, height: 810), physicalResolution: CGSize(width: 2160, height: 1620), renderedResolution: CGSize(width: 2160, height: 1620), scale: 2.0, density: 264, hasTrueTone: false, colorSpace: .sRGB)
81+
82+
case .iPadPro12Inch:
83+
return Display(size: .screen12_9Inch, resolution: CGSize(width: 1366, height: 1024), physicalResolution: CGSize(width: 2732, height: 2048), renderedResolution: CGSize(width: 2732, height: 2048), scale: 2.0, density: 264, hasTrueTone: false, colorSpace: .sRGB)
84+
85+
case .iPadPro9Inch:
86+
return Display(size: .screen9_7Inch, resolution: CGSize(width: 1024, height: 768), physicalResolution: CGSize(width: 2048, height: 1536), renderedResolution: CGSize(width: 2048, height: 1536), scale: 2.0, density: 264, hasTrueTone: true, colorSpace: .p3)
87+
88+
case .iPadPro12Inch2G,
89+
.iPadPro12Inch3G:
90+
return Display(size: .screen12_9Inch, resolution: CGSize(width: 1366, height: 1024), physicalResolution: CGSize(width: 2732, height: 2048), renderedResolution: CGSize(width: 2732, height: 2048), scale: 2.0, density: 264, hasTrueTone: true, colorSpace: .p3)
91+
92+
case .iPadPro10Inch,
93+
.iPadAir3:
94+
return Display(size: .screen10_5Inch, resolution: CGSize(width: 1112, height: 834), physicalResolution: CGSize(width: 2224, height: 1668), renderedResolution: CGSize(width: 2224, height: 1668), scale: 2.0, density: 264, hasTrueTone: true, colorSpace: .p3)
95+
96+
case .iPadPro11Inch:
97+
return Display(size: .screen11Inch, resolution: CGSize(width: 1194, height: 834), physicalResolution: CGSize(width: 2388, height: 1668), renderedResolution: CGSize(width: 2388, height: 2668), scale: 2.0, density: 264, hasTrueTone: true, colorSpace: .p3)
98+
99+
case .iPadMini:
100+
return Display(size: .screen7_9Inch, resolution: CGSize(width: 1024, height: 768), physicalResolution: CGSize(width: 1024, height: 768), renderedResolution: CGSize(width: 1024, height: 768), scale: 1.0, density: 163, hasTrueTone: false, colorSpace: .sRGB)
101+
102+
case .iPadMini2,
103+
.iPadMini3,
104+
.iPadMini4:
105+
return Display(size: .screen7_9Inch, resolution: CGSize(width: 1024, height: 768), physicalResolution: CGSize(width: 2048, height: 1536), renderedResolution: CGSize(width: 2048, height: 1536), scale: 2.0, density: 326, hasTrueTone: false, colorSpace: .sRGB)
106+
107+
case .iPadMini5:
108+
return Display(size: .screen7_9Inch, resolution: CGSize(width: 1024, height: 768), physicalResolution: CGSize(width: 2048, height: 1536), renderedResolution: CGSize(width: 2048, height: 1536), scale: 2.0, density: 326, hasTrueTone: false, colorSpace: .p3)
109+
110+
case let .simulator(model):
111+
return model.display
112+
113+
case .watch(.screen38mm),
114+
.watchSeries1(.screen38mm),
115+
.watchSeries2(.screen38mm),
116+
.watchSeries3(.screen38mm):
117+
return Display(size: .screen38mm, resolution: CGSize(width: 170, height: 136), physicalResolution: CGSize(width: 340, height: 272), renderedResolution: CGSize(width: 340, height: 272), scale: 2.0, density: 290, hasTrueTone: false, colorSpace: .sRGB)
118+
case .watch(.screen42mm),
119+
.watchSeries1(.screen42mm),
120+
.watchSeries2(.screen42mm),
121+
.watchSeries3(.screen42mm):
122+
return Display(size: .screen42mm, resolution: CGSize(width: 195, height: 156), physicalResolution: CGSize(width: 390, height: 312), renderedResolution: CGSize(width: 390, height: 312), scale: 2.0, density: 303, hasTrueTone: false, colorSpace: .sRGB)
123+
case .watchSeries4(.screen40mm),
124+
.watchSeries5(.screen40mm):
125+
return Display(size: .screen40mm, resolution: CGSize(width: 197, height: 162), physicalResolution: CGSize(width: 394, height: 324), renderedResolution: CGSize(width: 394, height: 324), scale: 2.0, density: 326, hasTrueTone: false, colorSpace: .sRGB)
126+
case .watchSeries4(.screen44mm),
127+
.watchSeries5(.screen44mm):
128+
return Display(size: .screen44mm, resolution: CGSize(width: 224, height: 184), physicalResolution: CGSize(width: 448, height: 368), renderedResolution: CGSize(width: 448, height: 368), scale: 2.0, density: 326, hasTrueTone: false, colorSpace: .sRGB)
129+
130+
default:
131+
return Display(size: .notApplicable, resolution: CGSize.zero, physicalResolution: CGSize.zero, renderedResolution: CGSize.zero, scale: 0, density: 0, hasTrueTone: false, colorSpace: .sRGB)
132+
133+
}
134+
}
135+
136+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//
2+
// Device
3+
// Thingy
4+
//
5+
// Created by Bojan Dimovski on 19/01/2020.
6+
// Copyright © 2020 Bojan Dimovski. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
public extension Device {
12+
13+
/// Associated family for each device.
14+
var family: Family {
15+
switch self {
16+
case .iPhone4S,
17+
.iPhone5,
18+
.iPhone5c,
19+
.iPhone5s,
20+
.iPhone6,
21+
.iPhone6Plus,
22+
.iPhone6s,
23+
.iPhone6sPlus,
24+
.iPhoneSE,
25+
.iPhone7,
26+
.iPhone7Plus,
27+
.iPhone8,
28+
.iPhone8Plus,
29+
.iPhoneX,
30+
.iPhoneXR,
31+
.iPhoneXS,
32+
.iPhoneXSMax,
33+
.iPhone11,
34+
.iPhone11Pro,
35+
.iPhone11ProMax:
36+
return .phone
37+
38+
case .iPodTouch5G,
39+
.iPodTouch6G,
40+
.iPodTouch7G:
41+
return .pod
42+
43+
case .appleTV4,
44+
.appleTV4K:
45+
return .tv
46+
47+
case .watch,
48+
.watchSeries1,
49+
.watchSeries2,
50+
.watchSeries3,
51+
.watchSeries4,
52+
.watchSeries5:
53+
return .watch
54+
55+
case .iPad2,
56+
.iPad3,
57+
.iPad4,
58+
.iPad5,
59+
.iPad6,
60+
.iPad7,
61+
.iPadAir,
62+
.iPadAir2,
63+
.iPadAir3,
64+
.iPadPro12Inch,
65+
.iPadPro12Inch2G,
66+
.iPadPro12Inch3G,
67+
.iPadPro9Inch,
68+
.iPadPro10Inch,
69+
.iPadPro11Inch,
70+
.iPadMini,
71+
.iPadMini2,
72+
.iPadMini3,
73+
.iPadMini4,
74+
.iPadMini5:
75+
return .pad
76+
77+
case let .simulator(model):
78+
return model.family
79+
80+
case let .unknown(family):
81+
return family
82+
}
83+
}
84+
85+
}

0 commit comments

Comments
 (0)