Skip to content

Commit 194bfc3

Browse files
committed
Implemented DataSource
1 parent 5ad1267 commit 194bfc3

File tree

5 files changed

+61
-94
lines changed

5 files changed

+61
-94
lines changed

IHWaveformView.xcodeproj/xcuserdata/mdibrahimhassan.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,4 @@
22
<Bucket
33
type = "1"
44
version = "2.0">
5-
<Breakpoints>
6-
<BreakpointProxy
7-
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
8-
<BreakpointContent
9-
shouldBeEnabled = "Yes"
10-
ignoreCount = "0"
11-
continueAfterRunningActions = "No"
12-
filePath = "IHWaveformView/IHWaveFormView.swift"
13-
timestampString = "532931083.779678"
14-
startingColumnNumber = "9223372036854775807"
15-
endingColumnNumber = "9223372036854775807"
16-
startingLineNumber = "134"
17-
endingLineNumber = "134"
18-
landmarkName = "getPath(url:)"
19-
landmarkType = "7">
20-
</BreakpointContent>
21-
</BreakpointProxy>
22-
<BreakpointProxy
23-
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
24-
<BreakpointContent
25-
shouldBeEnabled = "Yes"
26-
ignoreCount = "0"
27-
continueAfterRunningActions = "No"
28-
filePath = "IHWaveformView/IHWaveFormView.swift"
29-
timestampString = "532931083.780408"
30-
startingColumnNumber = "9223372036854775807"
31-
endingColumnNumber = "9223372036854775807"
32-
startingLineNumber = "153"
33-
endingLineNumber = "153"
34-
landmarkName = "getPath(url:)"
35-
landmarkType = "7">
36-
</BreakpointContent>
37-
</BreakpointProxy>
38-
<BreakpointProxy
39-
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
40-
<BreakpointContent
41-
shouldBeEnabled = "Yes"
42-
ignoreCount = "0"
43-
continueAfterRunningActions = "No"
44-
filePath = "IHWaveformView/IHWaveFormView.swift"
45-
timestampString = "532931083.780963"
46-
startingColumnNumber = "9223372036854775807"
47-
endingColumnNumber = "9223372036854775807"
48-
startingLineNumber = "147"
49-
endingLineNumber = "147"
50-
landmarkName = "getPath(url:)"
51-
landmarkType = "7">
52-
</BreakpointContent>
53-
</BreakpointProxy>
54-
</Breakpoints>
555
</Bucket>

IHWaveformView/IHWaveFormView.swift

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,28 @@ protocol IHWaveFormViewDelegate : class {
3636

3737
class IHWaveFormView: UIView, AVAudioPlayerDelegate {
3838
private var player: AVAudioPlayer!
39+
fileprivate let centerLineView = UIView()
3940
private var dataArray : [Float] = []
4041
private var totalCount : Int = 0
4142
private var xPoint : CGFloat = 0.0
4243
private var gameTimer: Timer!
43-
private var internalLineWidth : CGFloat!
44-
private var internalLineSeperation : CGFloat!
45-
weak var delegate : IHWaveFormViewDelegate?
46-
weak var dataSource : IHWaveFormViewDataSource?
44+
private var internalLineWidth : CGFloat = 2.0
45+
private var internalLineSeperation : CGFloat = 1.0
46+
fileprivate var width : CGFloat?
4747

48+
private var urlToPlay : URL {
49+
return (dataSource?.urlToPlay())!
50+
}
51+
52+
weak var delegate : IHWaveFormViewDelegate?
53+
weak var dataSource : IHWaveFormViewDataSource? {
54+
didSet {
55+
if (dataSource?.urlToPlay() != nil) {
56+
self.getPath(url: (dataSource?.urlToPlay())!)
57+
}
58+
}
59+
}
60+
4861
required init?(coder aDecoder: NSCoder) {
4962
super.init(coder: aDecoder)
5063
xPoint = 0.0
@@ -55,12 +68,9 @@ class IHWaveFormView: UIView, AVAudioPlayerDelegate {
5568
xPoint = 0.0
5669
}
5770

58-
fileprivate var width : CGFloat?
59-
6071
override func draw(_ rect: CGRect) {
6172
super.draw(rect)
62-
self.commonInit()
63-
self.getPath(url: urlToPlay!)
73+
commonInit()
6474
}
6575

6676
var orientationChangesCount : Int = 0
@@ -72,61 +82,53 @@ class IHWaveFormView: UIView, AVAudioPlayerDelegate {
7282
redrawView()
7383
}
7484

75-
func eraseView() {
85+
private func eraseView() {
7686
for views in subviews {
7787
views.removeFromSuperview()
7888
}
7989
self.layer.sublayers?.forEach { $0.removeFromSuperlayer() }
8090
}
8191

82-
internal func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
92+
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
8393
self.gameTimer.invalidate()
8494
delegate?.didFinishPlayBack()
8595
}
8696

87-
var centerLineView : UIView?
8897

89-
func addCentreLine(){
90-
centerLineView = UIView.init(frame: CGRect.init(x: 0, y: self.frame.size.height / 2.0, width: self.frame.size.width, height: 1))
91-
centerLineView?.backgroundColor = .black
92-
self.addSubview(centerLineView!)
98+
private func addCentreLine(){
99+
centerLineView.frame = CGRect.init(x: 0, y: self.frame.size.height / 2.0, width: self.frame.size.width, height: 1)
100+
centerLineView.backgroundColor = .black
101+
self.addSubview(centerLineView)
93102
}
94103

95-
func invertColor(_ color : UIColor) -> UIColor {
104+
private func invertColor(_ color : UIColor) -> UIColor {
96105
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
97106
self.backgroundColor?.getRed(&r, green: &g, blue: &b, alpha: &a)
98107
let lineColor = UIColor.init(red: self.invertB(r), green: self.invertB(g), blue: self.invert(b), alpha: a)
99108
return lineColor
100109
}
101110

102-
func invertB(_ val : CGFloat) -> CGFloat{
111+
private func invertB(_ val : CGFloat) -> CGFloat{
103112
return (1.0 - val) * 0.9
104113
}
105114

106-
func invert(_ val : CGFloat) -> CGFloat{
115+
private func invert(_ val : CGFloat) -> CGFloat{
107116
if (val > 0.6 && val < 0.90){
108117
return 0.6 + 0.1
109118
}
110119
return (1.0 - val)
111120
}
112121

113-
private var urlToPlay : URL?
114-
115-
func setUpView(urlToPlay : URL, lineWith : CGFloat?, lineSeperation : CGFloat?) {
116-
if (lineWith != nil){
117-
internalLineWidth = lineWith
118-
}
119-
if (lineSeperation != nil){
120-
internalLineSeperation = lineSeperation
121-
}
122-
self.urlToPlay = urlToPlay
123-
}
124-
125122
func commonInit(){
126-
internalLineWidth = 2.0
127-
internalLineSeperation = 1.0
128-
// self.addOverlayLabels()
129123
self.addCentreLine()
124+
guard let lineWidth = dataSource?.lineWidth?() else {
125+
return
126+
}
127+
guard let lineSeperation = dataSource?.lineSeperation?() else {
128+
return
129+
}
130+
internalLineWidth = lineWidth
131+
internalLineSeperation = lineSeperation
130132
}
131133

132134
private func getPath(url : URL){
@@ -207,10 +209,9 @@ class IHWaveFormView: UIView, AVAudioPlayerDelegate {
207209
private func redrawView() {
208210
eraseView()
209211
self.commonInit()
210-
self.getPath(url: urlToPlay!)
212+
self.getPath(url: urlToPlay)
211213
}
212214

213-
214215
private func generatePoints1(dBVal : String){
215216
let aPath = UIBezierPath()
216217
let floatVal : Float = Float(dBVal)!

IHWaveformView/Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
<array>
3333
<string>UIInterfaceOrientationPortrait</string>
3434
<string>UIInterfaceOrientationLandscapeLeft</string>
35+
<string>UIInterfaceOrientationPortraitUpsideDown</string>
36+
<string>UIInterfaceOrientationLandscapeRight</string>
3537
</array>
3638
<key>UISupportedInterfaceOrientations~ipad</key>
3739
<array>

IHWaveformView/ViewController.swift

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,39 @@
2121
//SOFTWARE.
2222

2323
import UIKit
24-
class ViewController: UIViewController, IHWaveFormViewDelegate {
24+
class ViewController: UIViewController {
25+
26+
@IBOutlet var musicView: IHWaveFormView!
27+
override func viewDidLoad() {
28+
super.viewDidLoad()
29+
30+
// self.musicView.setUpView(urlToPlay: url!, lineWith: 2.0, lineSeperation: 1.0)
31+
self.musicView.backgroundColor = #colorLiteral(red: 0.9607843137, green: 0.9607843137, blue: 0.8509803922, alpha: 1)
32+
self.musicView.delegate = self
33+
self.musicView.dataSource = self
34+
35+
}
36+
37+
}
38+
39+
extension ViewController: IHWaveFormViewDelegate {
2540
func didFinishPlayBack() {
2641
print ("playBack Finished")
2742
}
2843

2944
func didStartPlayingWithSuccess() {
30-
"Playback started successfully"
45+
print ("Playback started successfully")
3146
}
47+
}
48+
49+
extension ViewController: IHWaveFormViewDataSource {
3250

33-
@IBOutlet var musicView: IHWaveFormView!
34-
override func viewDidLoad() {
35-
super.viewDidLoad()
51+
func urlToPlay() -> URL {
3652
var url : URL?
3753
let path = Bundle.main.path(forResource: "bensound-sunny.mp3", ofType:nil)!
3854
url = URL(fileURLWithPath: path)
39-
self.musicView.setUpView(urlToPlay: url!, lineWith: 2.0, lineSeperation: 1.0)
40-
self.musicView.backgroundColor = #colorLiteral(red: 0.9607843137, green: 0.9607843137, blue: 0.8509803922, alpha: 1)
41-
self.musicView.delegate = self
42-
55+
return url!
4356
}
4457

58+
4559
}

0 commit comments

Comments
 (0)