Skip to content

Commit

Permalink
Fix image loading for non-touchbar users
Browse files Browse the repository at this point in the history
  • Loading branch information
xzzz9097 committed Aug 1, 2017
1 parent b44e6e2 commit d83a95a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
27 changes: 27 additions & 0 deletions Muse/Imageable+Download.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,33 @@ protocol Imageable: class {

}

extension NSImage {

static func download(from url: URL,
fallback: NSImage,
callback: @escaping (NSImage) -> ()) {
let session = URLSession.shared

let downloadTask = session.downloadTask(with: url, completionHandler: {
url, response, error in

if error == nil && url != nil {
if let data = NSData(contentsOf: url!) {
if let image = NSImage(data: data as Data) {
DispatchQueue.main.async { callback(image) }
}
}
} else {
// Fallback to the provided default image
DispatchQueue.main.async { callback(fallback) }
}
})

downloadTask.resume()
}

}

extension Imageable {

// Loading function implementation
Expand Down
30 changes: 16 additions & 14 deletions Muse/WindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1087,22 +1087,15 @@ class WindowController: NSWindowController, NSWindowDelegate, SliderDelegate {
}
}

func updateTouchBarUI() {
songArtworkTitleButton?.title = song.name.truncate(at: songTitleMaximumLength)
songArtworkTitleButton?.sizeToFit()

controlsSegmentedView?.setImage(helper.isPlaying ? .pause : .play,
forSegment: 1)

func fetchArtwork() {
if let stringURL = helper.artwork() as? String,
let artworkURL = URL(string: stringURL) {
songArtworkTitleButton?.loadImage(from: artworkURL, fallback: .defaultBg, callback: { image in
self.image = image
})
NSImage.download(from: artworkURL,
fallback: .defaultBg) { self.image = $0 }
} else if let image = helper.artwork() as? NSImage {
self.image = image
} else if let descriptor = helper.artwork() as? NSAppleEventDescriptor,
let image = NSImage(data: descriptor.data) {
let image = NSImage(data: descriptor.data) {
// Handles PNG artwork images
self.image = image
} else if song.isValid {
Expand All @@ -1113,13 +1106,22 @@ class WindowController: NSWindowController, NSWindowDelegate, SliderDelegate {
SpotifyHelper.shared.fetchTrackInfo(title: self.song.name,
artist: self.song.artist)
{ track in
self.songArtworkTitleButton?.loadImage(from: URL(string:track.album.artUri)!, fallback: .defaultBg, callback: { image in
self.image = image
})
NSImage.download(from: URL(string:track.album.artUri)!,
fallback: .defaultBg) { self.image = $0 }
}
} else {
self.image = NSImage.defaultBg
}
}

func updateTouchBarUI() {
songArtworkTitleButton?.title = song.name.truncate(at: songTitleMaximumLength)
songArtworkTitleButton?.sizeToFit()

controlsSegmentedView?.setImage(helper.isPlaying ? .pause : .play,
forSegment: 1)

fetchArtwork()

updateLikeButton()
}
Expand Down

0 comments on commit d83a95a

Please sign in to comment.