Skip to content

Commit 4888126

Browse files
committed
fix onChange of deprecation warnings for visionOS 1.0
1 parent c745d01 commit 4888126

3 files changed

Lines changed: 51 additions & 5 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import SwiftUI
2+
3+
// workaround for crashes in iOS 15 when using #available in ViewBuilders
4+
// see https://developer.apple.com/forums/thread/650818
5+
// not sure if this is still relevant, but keeping it due to its obscurity when it occurs
6+
// and because I cannot verify that it does not happen anymore due to lack of devices
7+
public struct LazyContent<Content: View>: View {
8+
let content: () -> Content
9+
10+
public init(@ViewBuilder content: @escaping () -> Content) {
11+
self.content = content
12+
}
13+
14+
public var body: some View {
15+
content()
16+
}
17+
}
18+
19+
// This is here to support visionOS / iOS 17 and remove the deprecation warning relating about the usage of
20+
// @available(visionOS, deprecated: 1.0, message: "Use `onChange` with a two or zero parameter action closure instead.")
21+
// @inlinable public func onChange<V>(of value: V, perform action: @escaping (_ newValue: V) -> Void) -> some View where V : Equatable
22+
public struct OnChange<V: Equatable>: ViewModifier {
23+
private var value: V
24+
private var action: (_ newValue: V) -> Void
25+
26+
public init(of value: V, action: @escaping (_ newValue: V) -> Void) {
27+
self.value = value
28+
self.action = action
29+
}
30+
31+
public func body(content: Content) -> some View {
32+
if #available(iOS 17, visionOS 1.0, *) {
33+
LazyContent {
34+
content
35+
.onChange(of: value) { _, newValue in
36+
action(newValue)
37+
}
38+
}
39+
} else {
40+
content
41+
.onChange(of: value) { newValue in
42+
action(newValue)
43+
}
44+
}
45+
}
46+
}

Sources/DSWaveformImageViews/SwiftUI/WaveformLiveCanvas.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public struct WaveformLiveCanvas: View {
3737
.onAppear {
3838
waveformDrawer.shouldDrawSilencePadding = shouldDrawSilencePadding
3939
}
40-
.onChange(of: shouldDrawSilencePadding) { newValue in
40+
.modifier(OnChange(of: shouldDrawSilencePadding, action: { newValue in
4141
waveformDrawer.shouldDrawSilencePadding = newValue
42-
}
42+
}))
4343
}
4444
}
4545

Sources/DSWaveformImageViews/SwiftUI/WaveformView.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public struct WaveformView<Content: View>: View {
4343
guard samples.isEmpty else { return }
4444
update(size: geometry.size, url: audioURL, configuration: configuration)
4545
}
46-
.onChange(of: geometry.size) { update(size: $0, url: audioURL, configuration: configuration) }
47-
.onChange(of: audioURL) { update(size: geometry.size, url: $0, configuration: configuration) }
48-
.onChange(of: configuration) { update(size: geometry.size, url: audioURL, configuration: $0) }
46+
.modifier(OnChange(of: geometry.size, action: { newValue in update(size: newValue, url: audioURL, configuration: configuration) }))
47+
.modifier(OnChange(of: audioURL, action: { newValue in update(size: geometry.size, url: audioURL, configuration: configuration) }))
48+
.modifier(OnChange(of: configuration, action: { newValue in update(size: geometry.size, url: audioURL, configuration: newValue) }))
4949
}
5050
}
5151

0 commit comments

Comments
 (0)