Skip to content

Commit

Permalink
NowPlayingに曲名・アートワークを表示できるようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
amylaseF85 committed Nov 1, 2019
1 parent 7aa7e71 commit 99164c6
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions DJYusaku/RequestsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class RequestsViewController: UIViewController {
}

NotificationCenter.default.addObserver(self, selector: #selector(handleRequestsUpdated), name: .requestQueueToRequestsVCName, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(handlePlayingItemChanged), name: NSNotification.Name.MPMusicPlayerControllerNowPlayingItemDidChange, object: nil)
}

override func viewDidAppear(_ animated: Bool) {
Expand All @@ -64,7 +65,23 @@ class RequestsViewController: UIViewController {
}
}

@objc func handlePlayingItemChanged(notification: NSNotification){
guard RequestQueue.shared.countRequests() > 0 else { return }
let nowPlayingMusicDataModel = RequestQueue.shared.getRequest(index: 0)
RequestQueue.shared.removeRequest(index: 0)
DispatchQueue.global().async {
let fetchedImage = Artwork.fetch(url: nowPlayingMusicDataModel.artworkUrl)
DispatchQueue.main.async {
self.tableView.reloadData()
self.playingTitle.text = nowPlayingMusicDataModel.title
self.playingArtwork.image = fetchedImage

}
}
}

@objc func handleRequestsUpdated(notification: NSNotification){
print("handleRequestsUpdated")
// リクエスト画面を更新
DispatchQueue.main.async{
self.tableView.reloadData()
Expand Down Expand Up @@ -97,10 +114,7 @@ class RequestsViewController: UIViewController {
let descripter = MPMusicPlayerStoreQueueDescriptor(storeIDs: [songID])
musicPlayerApplicationController.setQueue(with: descripter)
musicPlayerApplicationController.play()
RequestQueue.shared.removeRequest(index: 0) // TableViewには表示しない
DispatchQueue.main.async{
self.tableView.reloadData()
}

}
}

Expand All @@ -114,11 +128,7 @@ class RequestsViewController: UIViewController {
mutableQueue.remove(mutableQueue.items[0])
}, completionHandler: { [unowned self] queue, error in
guard self.musicPlayerApplicationController.nowPlayingItem != nil && error == nil else { return }
// RequestQueueと辻褄を合わせる
RequestQueue.shared.removeRequest(index: 0)
DispatchQueue.main.async{
self.tableView.reloadData()
}
// 何もしない
})
}

Expand Down

0 comments on commit 99164c6

Please sign in to comment.