Skip to content

Commit cf3f2c1

Browse files
committed
showcase improved SwiftUI ProgressWaveformView
Now that we have access to the shape, we can create an easier, more flexible and 2x faster view that also supports animation.
1 parent 0d7ea44 commit cf3f2c1

File tree

1 file changed

+7
-34
lines changed

1 file changed

+7
-34
lines changed

Example/DSWaveformImageExample-iOS/SwiftUIExample/ProgressWaveformView.swift

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,19 @@
11
import DSWaveformImage
2+
import DSWaveformImageViews
23
import SwiftUI
34

45
struct ProgressWaveformView: View {
56
let audioURL: URL
67
let progress: Double
78

8-
private let configuration = Waveform.Configuration(
9-
style: .striped(.init(color: .red, width: 3, spacing: 4)),
10-
damping: .init(percentage: 0.125, sides: .both)
11-
)
12-
13-
@StateObject private var waveformDrawer = WaveformImageDrawer()
14-
@State private var waveformImage: UIImage = UIImage()
15-
169
var body: some View {
1710
GeometryReader { geometry in
18-
ZStack {
19-
Image(uiImage: waveformImage)
20-
.resizable()
21-
.renderingMode(.template)
22-
.foregroundColor(.white)
23-
24-
Image(uiImage: waveformImage)
25-
.resizable()
26-
.mask(alignment: .leading) {
27-
Rectangle().frame(width: geometry.size.width * progress)
28-
}
29-
}
30-
.onAppear {
31-
guard waveformImage.size == .zero else { return }
32-
update(size: geometry.size, url: audioURL, configuration: configuration)
11+
WaveformView(audioURL: audioURL) { shape in
12+
shape.fill(.white)
13+
shape.fill(.red).mask(alignment: .leading) {
14+
Rectangle().frame(width: geometry.size.width * progress)
3315
}
34-
.onChange(of: geometry.size) { update(size: $0, url: audioURL, configuration: configuration) }
35-
.onChange(of: audioURL) { update(size: geometry.size, url: $0, configuration: configuration) }
36-
.onChange(of: configuration) { update(size: geometry.size, url: audioURL, configuration: $0) }
37-
}
38-
}
39-
40-
private func update(size: CGSize, url: URL, configuration: Waveform.Configuration) {
41-
Task(priority: .userInitiated) {
42-
let image = try! await waveformDrawer.waveformImage(fromAudioAt: url, with: configuration.with(size: size))
43-
await MainActor.run { waveformImage = image }
16+
}
4417
}
4518
}
4619
}
@@ -53,7 +26,7 @@ struct ProgressExampleView: View {
5326
VStack {
5427
ProgressWaveformView(audioURL: audioURL, progress: progress)
5528

56-
Button(action: { progress = .random(in: 0...1) }) {
29+
Button(action: { withAnimation { progress = .random(in: 0...1) }}) {
5730
Label("Progress", systemImage: "dice.fill")
5831
}.buttonStyle(.borderedProminent)
5932
}

0 commit comments

Comments
 (0)