Skip to content

Commit

Permalink
Merge pull request #3 from thatcherclough/develop
Browse files Browse the repository at this point in the history
Fixed a bug with auto generated playlists
  • Loading branch information
thatcherclough authored Nov 10, 2020
2 parents b0311ff + 265dce1 commit 8e25a99
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
Binary file not shown.
34 changes: 17 additions & 17 deletions CoverFlow/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class ViewController: UITableViewController, UITextFieldDelegate {
var timer: Timer!
func start() {
var currentHueIndex: Int = 0
var songAndArtist = getCurrentSongAndArtist()
var albumAndArtist = getCurrentAlbumAndArtist()
let wait = self.colorDuration + self.transitionDuration

getCoverImageAndSetCurrentSongHues()
Expand Down Expand Up @@ -365,9 +365,9 @@ class ViewController: UITableViewController, UITextFieldDelegate {
self.startBackgrounding()
}

let currentSongAndArtist = self.getCurrentSongAndArtist()
if currentSongAndArtist != songAndArtist {
songAndArtist = currentSongAndArtist
let currentAlbumAndArtist = self.getCurrentAlbumAndArtist()
if currentAlbumAndArtist != albumAndArtist {
albumAndArtist = currentAlbumAndArtist
self.getCoverImageAndSetCurrentSongHues()
currentHueIndex = 0

Expand All @@ -388,34 +388,36 @@ class ViewController: UITableViewController, UITextFieldDelegate {
}
}

func getCurrentSongAndArtist() -> String {
func getCurrentAlbumAndArtist() -> String {
let player = MPMusicPlayerController.systemMusicPlayer
let nowPlaying: MPMediaItem? = player.nowPlayingItem
let songName = nowPlaying?.title
let albumName = nowPlaying?.albumTitle
let artistName = nowPlaying?.artist

if songName == nil || artistName == nil {
if albumName == nil || artistName == nil {
return "N/A"
} else {
return songName! + artistName!
return albumName! + artistName!
}
}

func getCoverImageAndSetCurrentSongHues() {
currentHues.removeAll()

let player = MPMusicPlayerController.systemMusicPlayer
let nowPlaying: MPMediaItem? = player.nowPlayingItem
let albumArt = nowPlaying?.artwork
let albumName = nowPlaying?.albumTitle
let albumArtistName = nowPlaying?.albumArtist
let artistName = (nowPlaying?.albumArtist != nil) ? nowPlaying?.albumArtist : nowPlaying?.artist

if nowPlaying != nil {
let image = albumArt?.image(at: CGSize(width: 200, height: 200)) ?? nil

if image != nil {
setCurrentSongHues(image: image!)
} else {
if albumName != nil && albumArtistName != nil {
self.getCoverFromAPI(albumName: albumName!, albumArtistName: albumArtistName!) { (url) in
if albumName != nil && artistName != nil {
self.getCoverFromAPI(albumName: albumName!, artistName: artistName!) { (url) in
if url != nil {
self.getData(from: URL(string: url!)!) { data, response, error in
if data == nil || error != nil {
Expand All @@ -433,7 +435,7 @@ class ViewController: UITableViewController, UITextFieldDelegate {
}
}
} else {
alertAndNotify(title: "Error", body: "Could not get the current song's album cover. Album name or album artist is nil.")
alertAndNotify(title: "Error", body: "Could not get the current song's album cover. Album name or artist is nil.")
}
}
} else {
Expand All @@ -445,15 +447,15 @@ class ViewController: UITableViewController, UITextFieldDelegate {
URLSession.shared.dataTask(with: url, completionHandler: completion).resume()
}

func getCoverFromAPI(albumName: String, albumArtistName: String, completion: @escaping (String?)->()) {
func getCoverFromAPI(albumName: String, artistName: String, completion: @escaping (String?)->()) {
let searchTerm = albumName.replacingOccurrences(of: " ", with: "+")
var components = URLComponents()
components.scheme = "https"
components.host = "api.music.apple.com"
components.path = "/v1/catalog/\(countryCode ?? "us")/search"
components.queryItems = [
URLQueryItem(name: "term", value: searchTerm),
URLQueryItem(name: "limit", value: "25"),
URLQueryItem(name: "limit", value: "15"),
URLQueryItem(name: "types", value: "albums"),
]
let url = components.url!
Expand Down Expand Up @@ -483,7 +485,7 @@ class ViewController: UITableViewController, UITextFieldDelegate {
continue
}

if (attributes["name"] as! String == albumName) && (attributes["artistName"] as! String == albumArtistName) {
if (attributes["name"] as! String == albumName) && (attributes["artistName"] as! String == artistName) {
guard let artwork = attributes["artwork"] as? [String: Any] else {
continue
}
Expand All @@ -507,8 +509,6 @@ class ViewController: UITableViewController, UITextFieldDelegate {
}

func setCurrentSongHues(image: UIImage) {
currentHues.removeAll()

guard let colors = ColorThief.getPalette(from: image, colorCount: 4, quality: 5, ignoreWhite: true) else {
self.alertAndNotify(title: "Notice", body: "Could not extract colors form the current song's album cover.")
return
Expand Down

0 comments on commit 8e25a99

Please sign in to comment.