Skip to content

Commit 6bca77b

Browse files
committed
Move dynamic island check to enum
Rename hasDynamicIsland to isAvailable and move it to the DynamicIsland enum
1 parent 69c7213 commit 6bca77b

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ I will be adding more utilities to this package in the near future.
88

99
## DynamicIsland
1010

11-
A type that provides the size, origin and rect for the Dynamic Island. For example, you can do:
11+
A type that provides the size, origin, rect and some other information related to the Dynamic Island. For example, you can do:
1212

1313
```swift
1414
let size = DynamicIsland.size
@@ -50,10 +50,10 @@ doSomeWorkThatMayFinishLater { result in
5050
}
5151
```
5252

53-
In order to call this method, you need to check `hasDynamicIsland` (this is enforced at runtime), which also allows you to provide fallback logic:
53+
In order to access this property, you need to check `DynamicIsland.isAvailable` (this is enforced at runtime), which also nudges you to provide fallback logic:
5454

5555
```swift
56-
if hasDynamicIsland {
56+
if DynamicIsland.isAvailable {
5757
// Show a cool progress indicator around the Dynamic Island
5858
dynamicIslandProgressIndicatorConfiguration.showIndeterminateProgressAnimation()
5959
} else {

Sources/DynamicIslandUtilities/DynamicIsland.swift

+24-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import UIKit
99

10-
/// A type that provides the size, origin and rect for the Dynamic Island.
11-
/// - Note: This only provides the values for a static island, not one that is expanded (while a live activity is running for example)
10+
/// A type that provides the size, origin, rect and some other information related to the Dynamic Island.
11+
/// - Note: This only provides the values for a static island, not one that is expanded (while a live activity is running for example).
1212
public enum DynamicIsland {
1313

1414
/// The size of the Dynamic Island cutout.
@@ -30,4 +30,26 @@ public enum DynamicIsland {
3030
public static let cornerRadius: Double = {
3131
return size.width / 2
3232
}()
33+
34+
/// Returns whether this device supports the Dynamic Island.
35+
/// This returns `true` for iPhone 14 Pro and iPhone Pro Max, otherwise returns `false`.
36+
public static let isAvailable: Bool = {
37+
if #unavailable(iOS 16) {
38+
return false
39+
}
40+
41+
#if targetEnvironment(simulator)
42+
let identifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"]!
43+
#else
44+
var systemInfo = utsname()
45+
uname(&systemInfo)
46+
let machineMirror = Mirror(reflecting: systemInfo.machine)
47+
let identifier = machineMirror.children.reduce("") { identifier, element in
48+
guard let value = element.value as? Int8, value != 0 else { return identifier }
49+
return identifier + String(UnicodeScalar(UInt8(value)))
50+
}
51+
#endif
52+
53+
return identifier == "iPhone15,2" || identifier == "iPhone15,3"
54+
}()
3355
}

Sources/DynamicIslandUtilities/DynamicIslandProgressIndicatorViewController.swift

+3-24
Original file line numberDiff line numberDiff line change
@@ -54,38 +54,17 @@ open class DynamicIslandProgressIndicatorViewController: UIViewController {
5454
return .init(controller: self)
5555
}()
5656

57-
/// Returns whether this device supports the Dynamic Island.
58-
/// This returns `true` for iPhone 14 Pro and iPhone Pro Max, otherwise returns `false`.
59-
public var hasDynamicIsland: Bool {
60-
if #unavailable(iOS 16) {
61-
return false
62-
}
63-
64-
#if targetEnvironment(simulator)
65-
let identifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"]!
66-
#else
67-
var systemInfo = utsname()
68-
uname(&systemInfo)
69-
let machineMirror = Mirror(reflecting: systemInfo.machine)
70-
let identifier = machineMirror.children.reduce("") { identifier, element in
71-
guard let value = element.value as? Int8, value != 0 else { return identifier }
72-
return identifier + String(UnicodeScalar(UInt8(value)))
73-
}
74-
#endif
75-
76-
return identifier == "iPhone15,2" || identifier == "iPhone15,3"
77-
}
78-
7957
open override func viewDidLoad() {
8058
super.viewDidLoad()
8159

82-
if hasDynamicIsland {
60+
// TODO: Maybe do this somewhere else? Or enforce a super call.
61+
if DynamicIsland.isAvailable {
8362
createAndAddDynamicIslandBorderLayers()
8463
}
8564
}
8665

8766
private func requiresDynamicIsland() {
88-
precondition(hasDynamicIsland,
67+
precondition(DynamicIsland.isAvailable,
8968
"Cannot show dynamic island progress animation on a device that does not support it!")
9069
}
9170

0 commit comments

Comments
 (0)