26
26
import Foundation
27
27
28
28
protocol AudioDataManagable {
29
+ var currentStreamFinished : Bool { get }
30
+ var currentStreamFinishedWithDuration : Duration { get }
29
31
var numberOfQueued : Int { get }
30
32
var numberOfActive : Int { get }
31
33
@@ -38,6 +40,7 @@ protocol AudioDataManagable {
38
40
func setDownloadDirectory( _ dir: FileManager . SearchPathDirectory )
39
41
40
42
func clear( )
43
+ func updateDuration( d: Duration )
41
44
42
45
//Director pattern
43
46
func attach( callback: @escaping ( _ id: ID , _ progress: Double ) -> ( ) )
@@ -55,8 +58,13 @@ protocol AudioDataManagable {
55
58
}
56
59
57
60
class AudioDataManager : AudioDataManagable {
61
+ var currentStreamFinishedWithDuration : Duration = 0
62
+
58
63
var allowCellular : Bool = true
59
64
var downloadDirectory : FileManager . SearchPathDirectory = . documentDirectory
65
+
66
+ public var currentStreamFinished = false
67
+ public var totalStreamedDuration = 0
60
68
61
69
static let shared : AudioDataManagable = AudioDataManager ( )
62
70
@@ -95,6 +103,10 @@ class AudioDataManager: AudioDataManagable {
95
103
progressCallback: streamProgressListener,
96
104
doneCallback: streamDoneListener)
97
105
}
106
+
107
+ func updateDuration( d: Duration ) {
108
+ currentStreamFinishedWithDuration = d
109
+ }
98
110
99
111
func clear( ) {
100
112
streamingCallbacks = [ ]
@@ -125,6 +137,7 @@ class AudioDataManager: AudioDataManagable {
125
137
// MARK:- Streaming
126
138
extension AudioDataManager {
127
139
func startStream( withRemoteURL url: AudioURL , callback: @escaping ( StreamProgressPTO ) -> ( ) ) {
140
+ currentStreamFinished = false
128
141
if let data = FileStorage . Audio. read ( url. key) {
129
142
let dto = StreamProgressDTO . init ( progress: 1.0 , data: data, totalBytesExpected: Int64 ( data. count) )
130
143
callback ( StreamProgressPTO ( dto: dto) )
@@ -154,10 +167,12 @@ extension AudioDataManager {
154
167
streamWorker. resume ( withId: url. key)
155
168
}
156
169
func seekStream( withRemoteURL url: AudioURL , toByteOffset offset: UInt64 ) {
170
+ currentStreamFinished = false
157
171
streamWorker. seek ( withId: url. key, withByteOffset: offset)
158
172
}
159
173
160
174
func deleteStream( withRemoteURL url: AudioURL ) {
175
+ currentStreamFinished = false
161
176
streamWorker. stop ( withId: url. key)
162
177
streamingCallbacks. removeAll { ( cb: ( ID , ( StreamProgressPTO ) -> ( ) ) ) -> Bool in
163
178
return cb. 0 == url. key
@@ -230,7 +245,7 @@ extension AudioDataManager {
230
245
if error != nil {
231
246
return false
232
247
}
233
-
248
+ currentStreamFinished = true
234
249
downloadWorker. resumeAllActive ( )
235
250
return false
236
251
}
0 commit comments