Skip to content

Commit 69c7213

Browse files
committed
Add some assertions and move them into functions
1 parent 0786b04 commit 69c7213

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

Sources/DynamicIslandUtilities/DynamicIslandProgressIndicatorViewController.swift

+30-11
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ open class DynamicIslandProgressIndicatorViewController: UIViewController {
2121

2222
@Clamped(between: 0...100) fileprivate var progress: Double {
2323
didSet {
24-
precondition(hasDynamicIsland,
25-
"Cannot show dynamic island progress animation on a device that does not support it!")
26-
precondition(!isProgressIndeterminate,
27-
"Cannot set progress manually when isProgressIndeterminate == true!")
24+
requiresIndeterminateProgress(equalTo: false)
2825
if isProgressIndicatorHidden {
26+
requiresState(equalTo: .ready)
2927
showProgressIndicator()
3028
state = .animating
3129
}
@@ -52,6 +50,7 @@ open class DynamicIslandProgressIndicatorViewController: UIViewController {
5250

5351
/// Provides access to a configuration type to access the progress bar and show/hide progress.
5452
public lazy var dynamicIslandProgressIndicatorConfiguration: DynamicIslandProgressIndicatorConfiguration = {
53+
requiresDynamicIsland()
5554
return .init(controller: self)
5655
}()
5756

@@ -80,17 +79,37 @@ open class DynamicIslandProgressIndicatorViewController: UIViewController {
8079
open override func viewDidLoad() {
8180
super.viewDidLoad()
8281

83-
createAndAddDynamicIslandBorderLayers()
82+
if hasDynamicIsland {
83+
createAndAddDynamicIslandBorderLayers()
84+
}
8485
}
8586

86-
87-
fileprivate func showIndeterminateProgressAnimation() {
87+
private func requiresDynamicIsland() {
8888
precondition(hasDynamicIsland,
8989
"Cannot show dynamic island progress animation on a device that does not support it!")
90-
precondition(isProgressIndeterminate,
91-
"Cannot show indeterminate progress when isProgressIndeterminate == false!")
92-
precondition(state == .ready,
93-
"Cannot show animation because progress indicator is already animating!")
90+
}
91+
92+
private func requiresIndeterminateProgress(equalTo value: Bool) {
93+
precondition(isProgressIndeterminate == value,
94+
"isProgressIndeterminate must be set to '\(value)'!")
95+
}
96+
97+
private func requiresState(equalTo value: State) {
98+
let message: String
99+
switch (value, state) {
100+
case (.ready, .animating):
101+
message = "Cannot show animation because progress indicator is already animating!"
102+
// Handle other cases here if we require them.
103+
default:
104+
message = ""
105+
}
106+
precondition(state == value, message)
107+
}
108+
109+
110+
fileprivate func showIndeterminateProgressAnimation() {
111+
requiresIndeterminateProgress(equalTo: true)
112+
requiresState(equalTo: .ready)
94113

95114
resetProgressIndicator()
96115
showProgressIndicator()

0 commit comments

Comments
 (0)