Skip to content

Commit d6aa125

Browse files
committed
feat(device): add current type helper and platform labels
1 parent dffa326 commit d6aa125

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ case .unknown:
8484
}
8585
```
8686

87+
### Current Platform Label
88+
```swift
89+
let currentType = Device.currentType()
90+
let platformLabel = currentType.platformDisplayName
91+
// Example: "iOS", "tvOS", "macOS"
92+
```
93+
8794
## Available Properties
8895
Property | Description
8996
-------- | -----------
@@ -97,6 +104,8 @@ API | Description
97104
`enableLogging()` | Enables logging output via [AppLogger](https://github.com/thatfactory/applogger).
98105
`os()` | Returns the operating system version based on `-[NSProcessInfo operatingSystemVersionString]` and `-[NSProcessInfo operatingSystemVersion]`.
99106
`type()` | Returns the type of the device based on the result of the `os()` and `targetEnvironment()` functions.
107+
`currentType()` | Returns the current `DeviceType` without manually creating a `Device` instance.
108+
`platformDisplayName` | Returns a stable user-facing platform label from a `DeviceType`.
100109

101110
## Integration
102111
### Xcode

Sources/Device/Device+Type.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,36 @@ public enum DeviceType: Equatable {
1111
case unknown
1212
}
1313

14+
public extension DeviceType {
15+
16+
/// Returns a stable user-facing label for the platform.
17+
var platformDisplayName: String {
18+
switch self {
19+
case .iPhone, .iPad:
20+
return "iOS"
21+
case .mac(isCatalyst: false):
22+
return "macOS"
23+
case .mac(isCatalyst: true):
24+
return "Mac Catalyst"
25+
case .tv:
26+
return "tvOS"
27+
case .watch:
28+
return "watchOS"
29+
case .vision:
30+
return "visionOS"
31+
case .unknown:
32+
return "Unknown"
33+
}
34+
}
35+
}
36+
1437
public extension Device {
1538

39+
/// Returns the current host device type.
40+
static func currentType() -> DeviceType {
41+
Device().type()
42+
}
43+
1644
/// Returns the `DeviceType`, based on the result of the `os()` and `targetEnvironment()` functions.
1745
///
1846
/// Originally, the documentation for the `os()` function and `Preprocessor Directives` (AKA *Conditional

Tests/DeviceTests/DeviceTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,23 @@ import XCTest
2424
XCTAssertTrue(osVersion.minor >= 0, "The minor version isn't equal or greater than zero.")
2525
XCTAssertTrue(osVersion.patch >= 0, "The patchVersion version isn't equal or greater than zero.")
2626
}
27+
28+
func testCurrentTypeReturnsKnownCase() {
29+
let currentType = Device.currentType()
30+
switch currentType {
31+
case .iPhone, .iPad, .mac, .tv, .watch, .vision, .unknown:
32+
XCTAssertTrue(true)
33+
}
34+
}
35+
36+
func testPlatformDisplayNameMapping() {
37+
XCTAssertEqual(DeviceType.iPhone.platformDisplayName, "iOS")
38+
XCTAssertEqual(DeviceType.iPad.platformDisplayName, "iOS")
39+
XCTAssertEqual(DeviceType.mac(isCatalyst: false).platformDisplayName, "macOS")
40+
XCTAssertEqual(DeviceType.mac(isCatalyst: true).platformDisplayName, "Mac Catalyst")
41+
XCTAssertEqual(DeviceType.tv.platformDisplayName, "tvOS")
42+
XCTAssertEqual(DeviceType.watch.platformDisplayName, "watchOS")
43+
XCTAssertEqual(DeviceType.vision.platformDisplayName, "visionOS")
44+
XCTAssertEqual(DeviceType.unknown.platformDisplayName, "Unknown")
45+
}
2746
}

0 commit comments

Comments
 (0)