-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMpMediumItemExtension.swift
More file actions
86 lines (62 loc) · 2.46 KB
/
MpMediumItemExtension.swift
File metadata and controls
86 lines (62 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// MpMediumItemExtension.swift
// Peak
//
// Created by Cameron Monks on 5/2/17.
// Copyright © 2017 Connor Monks. All rights reserved.
//
import Foundation
import MediaPlayer
extension MPMediaItem: BasicSong {
var type: PeakMusicController.MusicType {
return .AppleMusic
}
/*PRIVATE STRUCT TO STORE PROPERTIES*/
private struct customProperties{
static var cover: UIImage?
}
/*PROPERTIES*/
var albumCover: UIImage?{
get{
return objc_getAssociatedObject(self, &customProperties.cover) as? UIImage ?? nil
}
set{
if let unwrappedValue = newValue{
objc_setAssociatedObject(self, &customProperties.cover, unwrappedValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
func getId() -> String { return "\(self.playbackStoreID)" }
func getTrackName() -> String { return self.title! }
func getCollectionName() -> String { return albumTitle! }
func getArtistName() -> String { return artist ?? "No Artist" }
func getTrackTimeMillis() -> Int {return Int(self.playbackDuration); }
func getImage() -> UIImage? {
if albumCover != nil{ //Check if we already have the album cover
return albumCover
}else{
//We don't have the album cover so let's see if we can get it
if let image = artwork?.image(at: CGSize()){
albumCover = image
return image
}else{
//Let's try fetching it from the internet
ConnectingToInternet.getSong(id: getId(), completion: {
self.albumCover = $0.getImage()
//Now let's try going through this again to return the new albumCover
DispatchQueue.main.async {
NotificationCenter.default.post(Notification(name: .systemMusicPlayerNowPlayingChanged))
}
})
}
}
return artwork?.image(at: CGSize());
}
func getDateAdded() -> Date? { return dateAdded }
func isEqual(to song: BasicSong) -> Bool {
if playbackStoreID == song.getId(){
return true
}
return false
}
}