Skip to content

Commit e30fc8d

Browse files
Refactor and test coverage improvement
1 parent b48a638 commit e30fc8d

8 files changed

Lines changed: 367 additions & 181 deletions

File tree

DeviceIdentificator.xcodeproj/project.pbxproj

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
E2566523212614270061CCF7 /* DeviceIdentificatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2566522212614270061CCF7 /* DeviceIdentificatorTests.swift */; };
10+
E20BA609213924E6005BA13B /* DeviceModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = E20BA608213924E6005BA13B /* DeviceModel.swift */; };
11+
E20BA60B21393D5C005BA13B /* DeviceIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = E20BA60A21393D5C005BA13B /* DeviceIdentifier.swift */; };
12+
E20BA60D2139406F005BA13B /* DeviceModel+Name.swift in Sources */ = {isa = PBXBuildFile; fileRef = E20BA60C2139406F005BA13B /* DeviceModel+Name.swift */; };
13+
E20BA61021394202005BA13B /* DeviceIdentifierTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E20BA60E213941E1005BA13B /* DeviceIdentifierTests.swift */; };
14+
E20BA612213949D1005BA13B /* DeviceModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E20BA611213949D1005BA13B /* DeviceModelTests.swift */; };
15+
E2566523212614270061CCF7 /* UIDeviceExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2566522212614270061CCF7 /* UIDeviceExtensionsTests.swift */; };
1116
E2566525212614270061CCF7 /* DeviceIdentificator.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E2A364F6200D104900668DF9 /* DeviceIdentificator.framework */; };
1217
E2A36507200D104900668DF9 /* DeviceIdentificator.h in Headers */ = {isa = PBXBuildFile; fileRef = E2A364F9200D104900668DF9 /* DeviceIdentificator.h */; settings = {ATTRIBUTES = (Public, ); }; };
1318
E2A36511200D10DA00668DF9 /* UIDeviceExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2A36510200D10DA00668DF9 /* UIDeviceExtensions.swift */; };
@@ -24,8 +29,13 @@
2429
/* End PBXContainerItemProxy section */
2530

2631
/* Begin PBXFileReference section */
32+
E20BA608213924E6005BA13B /* DeviceModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceModel.swift; sourceTree = "<group>"; };
33+
E20BA60A21393D5C005BA13B /* DeviceIdentifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceIdentifier.swift; sourceTree = "<group>"; };
34+
E20BA60C2139406F005BA13B /* DeviceModel+Name.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DeviceModel+Name.swift"; sourceTree = "<group>"; };
35+
E20BA60E213941E1005BA13B /* DeviceIdentifierTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceIdentifierTests.swift; sourceTree = "<group>"; };
36+
E20BA611213949D1005BA13B /* DeviceModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceModelTests.swift; sourceTree = "<group>"; };
2737
E2566520212614270061CCF7 /* DeviceIdentificatorTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DeviceIdentificatorTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
28-
E2566522212614270061CCF7 /* DeviceIdentificatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceIdentificatorTests.swift; sourceTree = "<group>"; };
38+
E2566522212614270061CCF7 /* UIDeviceExtensionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIDeviceExtensionsTests.swift; sourceTree = "<group>"; };
2939
E2566524212614270061CCF7 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3040
E2A364F6200D104900668DF9 /* DeviceIdentificator.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = DeviceIdentificator.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3141
E2A364F9200D104900668DF9 /* DeviceIdentificator.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DeviceIdentificator.h; sourceTree = "<group>"; };
@@ -55,7 +65,9 @@
5565
E2566521212614270061CCF7 /* Tests */ = {
5666
isa = PBXGroup;
5767
children = (
58-
E2566522212614270061CCF7 /* DeviceIdentificatorTests.swift */,
68+
E2566522212614270061CCF7 /* UIDeviceExtensionsTests.swift */,
69+
E20BA611213949D1005BA13B /* DeviceModelTests.swift */,
70+
E20BA60E213941E1005BA13B /* DeviceIdentifierTests.swift */,
5971
E2566524212614270061CCF7 /* Info.plist */,
6072
);
6173
path = Tests;
@@ -83,6 +95,9 @@
8395
isa = PBXGroup;
8496
children = (
8597
E2A36510200D10DA00668DF9 /* UIDeviceExtensions.swift */,
98+
E20BA608213924E6005BA13B /* DeviceModel.swift */,
99+
E20BA60C2139406F005BA13B /* DeviceModel+Name.swift */,
100+
E20BA60A21393D5C005BA13B /* DeviceIdentifier.swift */,
86101
E2A364F9200D104900668DF9 /* DeviceIdentificator.h */,
87102
E2A364FA200D104900668DF9 /* Info.plist */,
88103
);
@@ -218,15 +233,20 @@
218233
isa = PBXSourcesBuildPhase;
219234
buildActionMask = 2147483647;
220235
files = (
221-
E2566523212614270061CCF7 /* DeviceIdentificatorTests.swift in Sources */,
236+
E20BA61021394202005BA13B /* DeviceIdentifierTests.swift in Sources */,
237+
E20BA612213949D1005BA13B /* DeviceModelTests.swift in Sources */,
238+
E2566523212614270061CCF7 /* UIDeviceExtensionsTests.swift in Sources */,
222239
);
223240
runOnlyForDeploymentPostprocessing = 0;
224241
};
225242
E2A364F1200D104900668DF9 /* Sources */ = {
226243
isa = PBXSourcesBuildPhase;
227244
buildActionMask = 2147483647;
228245
files = (
246+
E20BA60B21393D5C005BA13B /* DeviceIdentifier.swift in Sources */,
229247
E2A36511200D10DA00668DF9 /* UIDeviceExtensions.swift in Sources */,
248+
E20BA60D2139406F005BA13B /* DeviceModel+Name.swift in Sources */,
249+
E20BA609213924E6005BA13B /* DeviceModel.swift in Sources */,
230250
);
231251
runOnlyForDeploymentPostprocessing = 0;
232252
};

Source/DeviceIdentifier.swift

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import Foundation
2+
3+
internal struct DeviceIdentifier {
4+
private let identifier: String
5+
6+
internal init(_ identifier: String) {
7+
self.identifier = identifier
8+
}
9+
10+
internal var rawValue: String {
11+
return identifier
12+
}
13+
14+
internal var model: DeviceModel? {
15+
switch identifier {
16+
case "iPhone1,1": return .iPhone1G
17+
case "iPhone1,2": return .iPhone3G
18+
case "iPhone2,1": return .iPhone3GS
19+
case "iPhone3,1", "iPhone3,2", "iPhone3,3": return .iPhone4
20+
case "iPhone4,1": return .iPhone4S
21+
case "iPhone5,1", "iPhone5,2": return .iPhone5
22+
case "iPhone5,3", "iPhone5,4": return .iPhone5C
23+
case "iPhone6,1", "iPhone6,2": return .iPhone5S
24+
case "iPhone7,2": return .iPhone6
25+
case "iPhone7,1": return .iPhone6Plus
26+
case "iPhone8,1": return .iPhone6S
27+
case "iPhone8,2": return .iPhone6SPlus
28+
case "iPhone8,4": return .iPhoneSE
29+
case "iPhone9,1": return .iPhone7
30+
case "iPhone9,2": return .iPhone7Plus
31+
case "iPhone9,3": return .iPhone7
32+
case "iPhone9,4": return .iPhone7Plus
33+
case "iPhone10,1", "iPhone10,4": return .iPhone8
34+
case "iPhone10,2", "iPhone10,5": return .iPhone8Plus
35+
case "iPhone10,3", "iPhone10,6": return .iPhoneX
36+
37+
case "iPod1,1": return .iPodTouch1G
38+
case "iPod2,1": return .iPodTouch2G
39+
case "iPod3,1": return .iPodTouch3G
40+
case "iPod4,1": return .iPodTouch4G
41+
case "iPod5,1": return .iPodTouch5G
42+
case "iPod7,1": return .iPodTouch6G
43+
44+
case "iPad1,1", "iPad1,2": return .iPad1G
45+
case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4": return .iPad2G
46+
case "iPad3,1", "iPad3,2", "iPad3,3": return .iPad3G
47+
case "iPad3,4", "iPad3,5", "iPad3,6": return .iPad4G
48+
case "iPad2,5", "iPad2,6", "iPad2,7": return .iPadMini1
49+
case "iPad4,4", "iPad4,5", "iPad4,6": return .iPadMini2
50+
case "iPad4,7", "iPad4,8", "iPad4,9": return .iPadMini3
51+
case "iPad5,1", "iPad5,2": return .iPadMini4
52+
53+
case "iPad4,1", "iPad4,2", "iPad4,3": return .iPadAir1G
54+
case "iPad5,3", "iPad5,4": return .iPadAir2G
55+
56+
case "iPad6,3", "iPad6,4": return .iPadPro9inch1G
57+
case "iPad6,7", "iPad6,8": return .iPadPro12inch1G
58+
59+
default: return nil
60+
}
61+
}
62+
}

Source/DeviceModel+Name.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import Foundation
2+
3+
public extension DeviceModel {
4+
public var name: String {
5+
switch self {
6+
case .iPhone1G: return "iPhone 1G"
7+
case .iPhone3G: return "iPhone 3G"
8+
case .iPhone3GS: return "iPhone 3GS"
9+
case .iPhone4: return "iPhone 4"
10+
case .iPhone4S: return "iPhone 4S"
11+
case .iPhone5: return "iPhone 5"
12+
case .iPhone5C: return "iPhone 5C"
13+
case .iPhone5S: return "iPhone 5S"
14+
case .iPhone6: return "iPhone 6"
15+
case .iPhone6Plus: return "iPhone 6 Plus"
16+
case .iPhone6S: return "iPhone 6S"
17+
case .iPhone6SPlus: return "iPhone 6S Plus"
18+
case .iPhoneSE: return "iPhone SE"
19+
case .iPhone7: return "iPhone 7"
20+
case .iPhone7Plus: return "iPhone 7 Plus"
21+
case .iPhone8: return "iPhone 8"
22+
case .iPhone8Plus: return "iPhone 8 Plus"
23+
case .iPhoneX: return "iPhone X"
24+
25+
case .iPodTouch1G: return "iPod touch 1G"
26+
case .iPodTouch2G: return "iPod touch 2G"
27+
case .iPodTouch3G: return "iPod touch 3G"
28+
case .iPodTouch4G: return "iPod touch 4G"
29+
case .iPodTouch5G: return "iPod touch 5G"
30+
case .iPodTouch6G: return "iPod touch 6G"
31+
32+
case .iPad1G: return "iPad 1G"
33+
case .iPad2G: return "iPad 2G"
34+
case .iPad3G: return "iPad 3G"
35+
case .iPad4G: return "iPad 4G"
36+
case .iPadMini1: return "iPad Mini"
37+
case .iPadMini2: return "iPad Mini 2"
38+
case .iPadMini3: return "iPad Mini 3"
39+
case .iPadMini4: return "iPad Mini 4"
40+
case .iPadAir1G: return "iPad Air"
41+
case .iPadAir2G: return "iPad Air 2"
42+
43+
case .iPadPro9inch1G: return "iPad Pro 9.7-Inch 1G"
44+
case .iPadPro12inch1G: return "iPad Pro 12.9-Inch 1G"
45+
46+
case .iPadSimulator: return "iPad Simulator"
47+
case .iPhoneSimulator: return "iPhone Simulator"
48+
49+
case .iPhoneUnknown(let model): return "Unknown iPhone (\(model))"
50+
case .iPadUnknown(let model): return "Unknown iPad (\(model))"
51+
case .iPodUnknown(let model): return "Unknown iPod (\(model))"
52+
case .unknown(let model): return "Unknown iOS device (\(model))"
53+
}
54+
}
55+
}

Source/DeviceModel.swift

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import Foundation
2+
3+
public enum DeviceModel: Equatable {
4+
case iPhone1G
5+
case iPhone3G
6+
case iPhone3GS
7+
case iPhone4
8+
case iPhone4S
9+
case iPhone5
10+
case iPhone5C
11+
case iPhone5S
12+
case iPhone6
13+
case iPhone6Plus
14+
case iPhone6S
15+
case iPhone6SPlus
16+
case iPhoneSE
17+
case iPhone7
18+
case iPhone7Plus
19+
case iPhone8
20+
case iPhone8Plus
21+
case iPhoneX
22+
23+
case iPodTouch1G
24+
case iPodTouch2G
25+
case iPodTouch3G
26+
case iPodTouch4G
27+
case iPodTouch5G
28+
case iPodTouch6G
29+
30+
case iPad1G
31+
case iPad2G
32+
case iPad3G
33+
case iPad4G
34+
case iPadMini1
35+
case iPadMini2
36+
case iPadMini3
37+
case iPadMini4
38+
case iPadAir1G
39+
case iPadAir2G
40+
41+
case iPadPro9inch1G
42+
case iPadPro12inch1G
43+
44+
case iPadSimulator
45+
case iPhoneSimulator
46+
47+
case iPhoneUnknown(model: String)
48+
case iPadUnknown(model: String)
49+
case iPodUnknown(model: String)
50+
case unknown(model: String)
51+
52+
internal init(_ identifier: DeviceIdentifier) {
53+
guard let model = identifier.model else {
54+
if identifier.rawValue == "i386" || identifier.rawValue == "x86_64" {
55+
let smallerScreen = UIScreen.main.bounds.size.width < 768
56+
self = smallerScreen ? .iPhoneSimulator : .iPadSimulator
57+
} else if identifier.rawValue.hasPrefix("iPhone") {
58+
self = .iPhoneUnknown(model: identifier.rawValue)
59+
} else if identifier.rawValue.hasPrefix("iPod") {
60+
self = .iPodUnknown(model: identifier.rawValue)
61+
} else if identifier.rawValue.hasPrefix("iPad") {
62+
self = .iPadUnknown(model: identifier.rawValue)
63+
} else {
64+
self = .unknown(model: identifier.rawValue)
65+
}
66+
return
67+
}
68+
self = model
69+
}
70+
}
71+
72+
public func == (lhs: DeviceModel, rhs: DeviceModel) -> Bool {
73+
return lhs.name == rhs.name
74+
}

0 commit comments

Comments
 (0)