Skip to content

Commit fe79080

Browse files
Merge pull request #42 from bastienFalcou/refactor-indent
Refactor AudioVisualizationView Update Code Indent
2 parents 9e266f1 + 0d55640 commit fe79080

File tree

1 file changed

+50
-44
lines changed

1 file changed

+50
-44
lines changed

SoundWave/Classes/AudioVisualizationView.swift

+50-44
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public class AudioVisualizationView: BaseNibView {
1313
case read
1414
case write
1515
}
16-
17-
private enum LevelBarType {
18-
case upper
19-
case lower
20-
case single
21-
}
16+
17+
private enum LevelBarType {
18+
case upper
19+
case lower
20+
case single
21+
}
2222

2323
@IBInspectable public var meteringLevelBarWidth: CGFloat = 3.0 {
2424
didSet {
@@ -35,22 +35,22 @@ public class AudioVisualizationView: BaseNibView {
3535
self.setNeedsDisplay()
3636
}
3737
}
38-
@IBInspectable public var meteringLevelBarSingleStick: Bool = false {
39-
didSet {
40-
self.setNeedsDisplay()
41-
}
42-
}
38+
@IBInspectable public var meteringLevelBarSingleStick: Bool = false {
39+
didSet {
40+
self.setNeedsDisplay()
41+
}
42+
}
4343

4444
public var audioVisualizationMode: AudioVisualizationMode = .read
45-
45+
4646
public var audioVisualizationTimeInterval: TimeInterval = 0.05 // Time interval between each metering bar representation
4747

4848
// Specify a `gradientPercentage` to have the width of gradient be that percentage of the view width (starting from left)
4949
// The rest of the screen will be filled by `self.gradientStartColor` to display nicely.
5050
// Do not specify any `gradientPercentage` for gradient calculating fitting size automatically.
5151
public var currentGradientPercentage: Float?
5252

53-
private var meteringLevelsArray: [Float] = [] // Mutating recording array (values are percentage: 0.0 to 1.0)
53+
private var meteringLevelsArray: [Float] = [] // Mutating recording array (values are percentage: 0.0 to 1.0)
5454
private var meteringLevelsClusteredArray: [Float] = [] // Generated read mode array (values are percentage: 0.0 to 1.0)
5555

5656
private var currentMeteringLevelsArray: [Float] {
@@ -78,7 +78,7 @@ public class AudioVisualizationView: BaseNibView {
7878
static var audioVisualizationDefaultGradientEndColor: UIColor {
7979
return UIColor(red: 166.0 / 255.0, green: 150.0 / 255.0, blue: 225.0 / 255.0, alpha: 1.0)
8080
}
81-
81+
8282
@IBInspectable public var gradientStartColor: UIColor = AudioVisualizationView.audioVisualizationDefaultGradientStartColor {
8383
didSet {
8484
self.setNeedsDisplay()
@@ -90,13 +90,13 @@ public class AudioVisualizationView: BaseNibView {
9090
}
9191
}
9292

93-
override public init(frame: CGRect) {
94-
super.init(frame: frame)
95-
}
93+
override public init(frame: CGRect) {
94+
super.init(frame: frame)
95+
}
9696

97-
required public init?(coder aDecoder: NSCoder) {
98-
super.init(coder: aDecoder)
99-
}
97+
required public init?(coder aDecoder: NSCoder) {
98+
super.init(coder: aDecoder)
99+
}
100100

101101
override public func draw(_ rect: CGRect) {
102102
super.draw(rect)
@@ -176,7 +176,7 @@ public class AudioVisualizationView: BaseNibView {
176176
self.play(for: 2)
177177
}
178178
}
179-
179+
180180
public func play(for duration: TimeInterval) {
181181
guard self.audioVisualizationMode == .read else {
182182
fatalError("trying to read audio visualization in write mode")
@@ -198,12 +198,12 @@ public class AudioVisualizationView: BaseNibView {
198198
guard let this = self else {
199199
return
200200
}
201-
201+
202202
if timerDuration >= duration {
203203
this.stop()
204204
return
205205
}
206-
206+
207207
this.currentGradientPercentage = Float(timerDuration) / Float(duration)
208208
this.setNeedsDisplay()
209209
}
@@ -305,37 +305,43 @@ public class AudioVisualizationView: BaseNibView {
305305
let offset = max(self.currentMeteringLevelsArray.count - self.maximumNumberBars, 0)
306306

307307
for index in offset..<self.currentMeteringLevelsArray.count {
308-
if self.meteringLevelBarSingleStick {
309-
self.drawBar(index - offset, meteringLevelIndex: index, levelBarType: .single, context: context)
310-
} else {
311-
self.drawBar(index - offset, meteringLevelIndex: index, levelBarType: .upper, context: context)
312-
self.drawBar(index - offset, meteringLevelIndex: index, levelBarType: .lower, context: context)
313-
}
308+
if self.meteringLevelBarSingleStick {
309+
self.drawBar(index - offset, meteringLevelIndex: index, levelBarType: .single, context: context)
310+
} else {
311+
self.drawBar(index - offset, meteringLevelIndex: index, levelBarType: .upper, context: context)
312+
self.drawBar(index - offset, meteringLevelIndex: index, levelBarType: .lower, context: context)
313+
}
314314
}
315315
}
316316

317317
private func drawBar(_ barIndex: Int, meteringLevelIndex: Int, levelBarType: LevelBarType, context: CGContext) {
318318
context.saveGState()
319319

320-
var barRect: CGRect
320+
var barRect: CGRect
321321

322322
let xPointForMeteringLevel = self.xPointForMeteringLevel(barIndex)
323323
let heightForMeteringLevel = self.heightForMeteringLevel(self.currentMeteringLevelsArray[meteringLevelIndex])
324324

325-
switch levelBarType {
326-
case .upper:
327-
barRect = CGRect(x: xPointForMeteringLevel, y: self.centerY - heightForMeteringLevel,
328-
width: self.meteringLevelBarWidth, height: heightForMeteringLevel)
329-
case .lower:
330-
barRect = CGRect(x: xPointForMeteringLevel, y: self.centerY, width: self.meteringLevelBarWidth,
331-
height: heightForMeteringLevel)
332-
case .single:
333-
barRect = CGRect(x: xPointForMeteringLevel, y: self.centerY - heightForMeteringLevel,
334-
width: self.meteringLevelBarWidth, height: heightForMeteringLevel * 2)
335-
}
336-
337-
let barPath: UIBezierPath = UIBezierPath(roundedRect: barRect, cornerRadius: self.meteringLevelBarCornerRadius)
338-
325+
switch levelBarType {
326+
case .upper:
327+
barRect = CGRect(x: xPointForMeteringLevel,
328+
y: self.centerY - heightForMeteringLevel,
329+
width: self.meteringLevelBarWidth,
330+
height: heightForMeteringLevel)
331+
case .lower:
332+
barRect = CGRect(x: xPointForMeteringLevel,
333+
y: self.centerY,
334+
width: self.meteringLevelBarWidth,
335+
height: heightForMeteringLevel)
336+
case .single:
337+
barRect = CGRect(x: xPointForMeteringLevel,
338+
y: self.centerY - heightForMeteringLevel,
339+
width: self.meteringLevelBarWidth,
340+
height: heightForMeteringLevel * 2)
341+
}
342+
343+
let barPath: UIBezierPath = UIBezierPath(roundedRect: barRect, cornerRadius: self.meteringLevelBarCornerRadius)
344+
339345
UIColor.black.set()
340346
barPath.fill()
341347

0 commit comments

Comments
 (0)