Skip to content

Commit 6c01329

Browse files
Zandor300honghaoz
andauthored
Fix building SwiftUI previews on macOS. (#389)
* fixes macOS build * Use description instead of safeDescription inside description. * Version bump (v5.2.2) --------- Co-authored-by: Honghao Zhang <m@honghao.dev>
1 parent e1c258c commit 6c01329

File tree

7 files changed

+108
-14
lines changed

7 files changed

+108
-14
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# Changelog
22

3+
## Version 5.2.2
4+
5+
Releasedate: 2024-02-06
6+
7+
```ruby
8+
pod 'DeviceKit', '~> 5.2'
9+
```
10+
11+
### Bugfixes
12+
13+
- Fix building SwiftUI previews on macOS properly. ([#389](https://github.com/devicekit/DeviceKit/pull/389))
14+
15+
### Contributors
16+
17+
Thanks to all the contributers of this release!
18+
- [honghaoz](https://github.com/honghaoz)
19+
320
## Version 5.2.1
421

522
Releasedate: 2024-01-17

DeviceKit.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'DeviceKit'
3-
s.version = '5.2.1'
3+
s.version = '5.2.2'
44
s.summary = 'DeviceKit is a µ-framework that provides a value-type replacement of UIDevice.'
55

66
s.description = <<-DESC

DeviceKit.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@
384384
"@executable_path/Frameworks",
385385
"@loader_path/Frameworks",
386386
);
387-
MARKETING_VERSION = 5.2.1;
387+
MARKETING_VERSION = 5.2.2;
388388
MTL_ENABLE_DEBUG_INFO = YES;
389389
PRODUCT_BUNDLE_IDENTIFIER = me.dennisweissmann.DeviceKit;
390390
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -441,7 +441,7 @@
441441
"@executable_path/Frameworks",
442442
"@loader_path/Frameworks",
443443
);
444-
MARKETING_VERSION = 5.2.1;
444+
MARKETING_VERSION = 5.2.2;
445445
MTL_ENABLE_DEBUG_INFO = NO;
446446
PRODUCT_BUNDLE_IDENTIFIER = me.dennisweissmann.DeviceKit;
447447
PRODUCT_NAME = "$(TARGET_NAME)";

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
`DeviceKit` is a value-type replacement of [`UIDevice`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/).
2020

21-
## Current version 5.2.1
21+
## Current version 5.2.2
2222
See our detailed [changelog](CHANGELOG.md) for the latest features, improvements and bug fixes.
2323

2424
## Features

Source/Device.generated.swift

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,8 @@ public enum Device {
628628
case "i386", "x86_64", "arm64": return simulator(mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "watchOS"))
629629
default: return unknown(identifier)
630630
}
631+
#else
632+
return unknown(identifier)
631633
#endif
632634
}
633635

@@ -879,6 +881,8 @@ public enum Device {
879881
}
880882
#elseif os(tvOS)
881883
return (width: -1, height: -1)
884+
#else
885+
return (width: -1, height: -1)
882886
#endif
883887
}
884888

@@ -1158,6 +1162,8 @@ public enum Device {
11581162
return allTVs
11591163
#elseif os(watchOS)
11601164
return allWatches
1165+
#else
1166+
return []
11611167
#endif
11621168
}
11631169

@@ -1219,8 +1225,10 @@ public enum Device {
12191225
guard isCurrent else { return nil }
12201226
#if os(watchOS)
12211227
return WKInterfaceDevice.current().name
1222-
#else
1228+
#elseif canImport(UIKit)
12231229
return UIDevice.current.name
1230+
#else
1231+
return nil
12241232
#endif
12251233
}
12261234

@@ -1235,8 +1243,10 @@ public enum Device {
12351243
} else {
12361244
return UIDevice.current.systemName
12371245
}
1238-
#else
1246+
#elseif canImport(UIKit)
12391247
return UIDevice.current.systemName
1248+
#else
1249+
return nil
12401250
#endif
12411251
}
12421252

@@ -1245,8 +1255,10 @@ public enum Device {
12451255
guard isCurrent else { return nil }
12461256
#if os(watchOS)
12471257
return WKInterfaceDevice.current().systemVersion
1248-
#else
1258+
#elseif canImport(UIKit)
12491259
return UIDevice.current.systemVersion
1260+
#else
1261+
return nil
12501262
#endif
12511263
}
12521264

@@ -1255,8 +1267,10 @@ public enum Device {
12551267
guard isCurrent else { return nil }
12561268
#if os(watchOS)
12571269
return WKInterfaceDevice.current().model
1258-
#else
1270+
#elseif canImport(UIKit)
12591271
return UIDevice.current.model
1272+
#else
1273+
return nil
12601274
#endif
12611275
}
12621276

@@ -1265,8 +1279,10 @@ public enum Device {
12651279
guard isCurrent else { return nil }
12661280
#if os(watchOS)
12671281
return WKInterfaceDevice.current().localizedModel
1268-
#else
1282+
#elseif canImport(UIKit)
12691283
return UIDevice.current.localizedModel
1284+
#else
1285+
return nil
12701286
#endif
12711287
}
12721288

@@ -1385,6 +1401,8 @@ public enum Device {
13851401
}
13861402
#elseif os(tvOS)
13871403
return nil
1404+
#else
1405+
return nil
13881406
#endif
13891407
}
13901408

@@ -1536,6 +1554,11 @@ extension Device: CustomStringConvertible {
15361554
case .simulator(let model): return "Simulator (\(model.description))"
15371555
case .unknown(let identifier): return identifier
15381556
}
1557+
#else
1558+
switch self {
1559+
case .simulator(let model): return "Simulator (\(model.description))"
1560+
case .unknown(let identifier): return identifier
1561+
}
15391562
#endif
15401563
}
15411564

@@ -1664,6 +1687,11 @@ extension Device: CustomStringConvertible {
16641687
case .simulator(let model): return "Simulator (\(model.safeDescription))"
16651688
case .unknown(let identifier): return identifier
16661689
}
1690+
#else
1691+
switch self {
1692+
case .simulator(let model): return "Simulator (\(model.safeDescription))"
1693+
case .unknown(let identifier): return identifier
1694+
}
16671695
#endif
16681696
}
16691697

@@ -2298,6 +2326,8 @@ extension Device {
22982326
case .simulator(let model): return model.cpu
22992327
case .unknown: return .unknown
23002328
}
2329+
#else
2330+
return .unknown
23012331
#endif
23022332
}
23032333
}
@@ -2347,6 +2377,8 @@ extension Device.CPU: CustomStringConvertible {
23472377
case .s9: return "S9"
23482378
case .unknown: return "unknown"
23492379
}
2380+
#else
2381+
return "unknown"
23502382
#endif
23512383
}
23522384
}

Source/Device.swift.gyb

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ public enum Device {
421421
case "i386", "x86_64", "arm64": return simulator(mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "watchOS"))
422422
default: return unknown(identifier)
423423
}
424+
#else
425+
return unknown(identifier)
424426
#endif
425427
}
426428

@@ -482,6 +484,8 @@ public enum Device {
482484
}
483485
#elseif os(tvOS)
484486
return (width: -1, height: -1)
487+
#else
488+
return (width: -1, height: -1)
485489
#endif
486490
}
487491

@@ -761,6 +765,8 @@ public enum Device {
761765
return allTVs
762766
#elseif os(watchOS)
763767
return allWatches
768+
#else
769+
return []
764770
#endif
765771
}
766772

@@ -822,8 +828,10 @@ public enum Device {
822828
guard isCurrent else { return nil }
823829
#if os(watchOS)
824830
return WKInterfaceDevice.current().name
825-
#else
831+
#elseif canImport(UIKit)
826832
return UIDevice.current.name
833+
#else
834+
return nil
827835
#endif
828836
}
829837

@@ -838,8 +846,10 @@ public enum Device {
838846
} else {
839847
return UIDevice.current.systemName
840848
}
841-
#else
849+
#elseif canImport(UIKit)
842850
return UIDevice.current.systemName
851+
#else
852+
return nil
843853
#endif
844854
}
845855

@@ -848,8 +858,10 @@ public enum Device {
848858
guard isCurrent else { return nil }
849859
#if os(watchOS)
850860
return WKInterfaceDevice.current().systemVersion
851-
#else
861+
#elseif canImport(UIKit)
852862
return UIDevice.current.systemVersion
863+
#else
864+
return nil
853865
#endif
854866
}
855867

@@ -858,8 +870,10 @@ public enum Device {
858870
guard isCurrent else { return nil }
859871
#if os(watchOS)
860872
return WKInterfaceDevice.current().model
861-
#else
873+
#elseif canImport(UIKit)
862874
return UIDevice.current.model
875+
#else
876+
return nil
863877
#endif
864878
}
865879

@@ -868,8 +882,10 @@ public enum Device {
868882
guard isCurrent else { return nil }
869883
#if os(watchOS)
870884
return WKInterfaceDevice.current().localizedModel
871-
#else
885+
#elseif canImport(UIKit)
872886
return UIDevice.current.localizedModel
887+
#else
888+
return nil
873889
#endif
874890
}
875891

@@ -893,6 +909,8 @@ public enum Device {
893909
}
894910
#elseif os(tvOS)
895911
return nil
912+
#else
913+
return nil
896914
#endif
897915
}
898916

@@ -948,6 +966,11 @@ extension Device: CustomStringConvertible {
948966
case .simulator(let model): return "Simulator (\(model.description))"
949967
case .unknown(let identifier): return identifier
950968
}
969+
#else
970+
switch self {
971+
case .simulator(let model): return "Simulator (\(model.description))"
972+
case .unknown(let identifier): return identifier
973+
}
951974
#endif
952975
}
953976

@@ -980,6 +1003,11 @@ extension Device: CustomStringConvertible {
9801003
case .simulator(let model): return "Simulator (\(model.safeDescription))"
9811004
case .unknown(let identifier): return identifier
9821005
}
1006+
#else
1007+
switch self {
1008+
case .simulator(let model): return "Simulator (\(model.safeDescription))"
1009+
case .unknown(let identifier): return identifier
1010+
}
9831011
#endif
9841012
}
9851013

@@ -1468,6 +1496,8 @@ extension Device {
14681496
case .simulator(let model): return model.cpu
14691497
case .unknown: return .unknown
14701498
}
1499+
#else
1500+
return .unknown
14711501
#endif
14721502
}
14731503
}
@@ -1490,6 +1520,8 @@ extension Device.CPU: CustomStringConvertible {
14901520
% end
14911521
case .unknown: return "unknown"
14921522
}
1523+
#else
1524+
return "unknown"
14931525
#endif
14941526
}
14951527
}

Tests/Tests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,31 @@ class DeviceKitTests: XCTestCase {
1717
let device = Device.current
1818

1919
func testDeviceSimulator() {
20+
#if os(macOS)
21+
XCTAssertFalse(device.isOneOf(Device.allSimulators))
22+
#else
2023
XCTAssertTrue(device.isOneOf(Device.allSimulators))
24+
#endif
2125
}
2226

2327
func testIsSimulator() {
28+
#if os(macOS)
29+
XCTAssertFalse(device.isSimulator)
30+
#else
2431
XCTAssertTrue(device.isSimulator)
32+
#endif
2533
}
2634

2735
func testDeviceDescription() {
36+
#if os(macOS)
37+
#else
2838
XCTAssertTrue(device.description.hasPrefix("Simulator"))
2939
XCTAssertTrue(device.description.contains("iPhone")
3040
|| device.description.contains("iPad")
3141
|| device.description.contains("iPod")
3242
|| device.description.contains("TV")
3343
|| device.description.contains("Apple Watch"))
44+
#endif
3445
}
3546

3647
func testIsCanvas() {
@@ -40,6 +51,8 @@ class DeviceKitTests: XCTestCase {
4051
let otherDevice: Device = device == .appleTVHD ? .appleTV4K : .appleTVHD
4152
#elseif os(watchOS)
4253
let otherDevice: Device = device == .appleWatchUltra ? .appleWatchSeries8_41mm : .appleWatchUltra
54+
#else
55+
let otherDevice: Device = .unknown("mac")
4356
#endif
4457
XCTAssertEqual(otherDevice.isCanvas, nil)
4558
XCTAssertEqual(device.isCanvas, false)

0 commit comments

Comments
 (0)