summaryrefslogtreecommitdiffstats
path: root/MusicConverter/Model/Napster.swift
diff options
context:
space:
mode:
Diffstat (limited to 'MusicConverter/Model/Napster.swift')
-rw-r--r--MusicConverter/Model/Napster.swift53
1 files changed, 47 insertions, 6 deletions
diff --git a/MusicConverter/Model/Napster.swift b/MusicConverter/Model/Napster.swift
index 21d8919..b45e7db 100644
--- a/MusicConverter/Model/Napster.swift
+++ b/MusicConverter/Model/Napster.swift
@@ -26,6 +26,11 @@ class Napster: ObservableObject {
cachedNapsterSource?.name ?? id
}
+ /*var synced: Bool {
+ self.appleChoices.count == self.cachedNapsterSource?.trackCount ?? -1
+ }*/
+ var synced = false
+
init(id: String, api: AppleMusicApi) {
self.id = id
self.api = api
@@ -43,6 +48,7 @@ class Napster: ObservableObject {
public func update(completion: @escaping (Error?) -> Void) {
self.cachedNapsterSource = NANapsterPlaylist(id: id)
+ self.appleChoices = []; // TODO: keep choices, but update
self.cachedNapsterSource?.update { result in
switch result {
case .failure(let e):
@@ -91,12 +97,14 @@ class Napster: ObservableObject {
}
completion(.success(d))
}
+ // error also changes self
DispatchQueue.main.async {
self.objectWillChange.send()
}
}
}
+ // TODO: do not add songs without playbackParams
public func resolveTracks(completion: @escaping (Error?) -> Void) {
guard self.tracks != nil else {
completion(NapsterError.TracksEmpty)
@@ -109,14 +117,35 @@ class Napster: ObservableObject {
return
}
do {
- try self.appleChoices.append(AppleChoice(id: self.appleChoices.count, songs: songs.map({s in AppleMusicApi.SongInfo(song: s)})))
+ let choiceSongs = songs.filter { song in
+ //return true //song.attributes.playParams != nil
+ return song.attributes.playParams != nil
+ }.map { song in
+ AppleMusicApi.SongInfo(song: song)
+ }
+ if choiceSongs.count > 0 {
+ try self.appleChoices.append(AppleChoice(id: self.appleChoices.count, songs: choiceSongs))
+ } else {
+ print("did not found any playable songs for \(track.name)")
+ }
} catch {
print("error adding to choices: \(error)")
}
DispatchQueue.main.async {
self.objectWillChange.send()
}
- }, completion: completion) // TODO: do I need the completion?
+ }, completion: { error in
+ if let error = error {
+ print("error resolving: \(error)")
+ } else {
+ self.synced = true
+ print("finisched syncing")
+ }
+ DispatchQueue.main.async {
+ self.objectWillChange.send()
+ }
+ completion(error)
+ })
}
public func updateId(_ id: String) {
@@ -124,6 +153,8 @@ class Napster: ObservableObject {
// Invalidate caches
}
+ //public func createApplePlaylistAPI(completion: @escaping (Result<))
+
public func createApplePlaylist(completion: @escaping (Result<MPMediaPlaylist, Error>) -> Void) {
/*self.musicPlayer.setQueue(with: self.appleChoices.map({s in
s.song.attributes.playParams.id
@@ -158,7 +189,11 @@ class Napster: ObservableObject {
return
}
- guard self.appleChoices.count > 0 else {
+ /*guard self.appleChoices.count > 0 else {
+ completion(.failure(NapsterError.NotEnoughSongs))
+ return
+ }*/
+ guard self.synced else {
completion(.failure(NapsterError.NotEnoughSongs))
return
}
@@ -169,20 +204,26 @@ class Napster: ObservableObject {
var callback: (Error?) -> Void = {_ in}
callback = { error in
+ print("callback: adding song \(self.appleChoices[pos].song.name) to playlist")
guard error == nil else {
completion(.failure(error!))
return
}
- if pos < self.appleChoices.count {
+ if pos < self.appleChoices.count - 1 {
pos += 1;
- playlist.addItem(withProductID: self.appleChoices[pos].song.attributes.playParams.id, completionHandler: callback)
+ while self.appleChoices[pos].song.attributes.playParams?.id == nil {
+ print("did not found a id for song \(self.appleChoices[pos].song.name)")
+ pos += 1;
+ }
+ playlist.addItem(withProductID: self.appleChoices[pos].song.attributes.playParams!.id, completionHandler: callback)
} else {
completion(.success(()))
}
}
- playlist.addItem(withProductID: self.appleChoices[pos].song.attributes.playParams.id, completionHandler: callback)
+ print("add first item")
+ playlist.addItem(withProductID: self.appleChoices[pos].song.attributes.playParams!.id, completionHandler: callback)
}
public struct AppleChoice: Identifiable, CustomStringConvertible {