Permalink
Browse files

Merge pull request #3 from mark-w/master

iCloud convenience methods
  • Loading branch information...
2 parents 1a35441 + 0cf575a commit 46d740c5e0a48fde787da93b6a705aa8a7d777e7 @jaminguy committed Jan 6, 2012
@@ -14,11 +14,31 @@
- (NSString *)albumArtist;
- (NSString *)title;
- (NSString *)albumTitle;
+- (NSURL *)assetURL;
+- (NSString *)persistentID;
- (UIImage *)artworkWithSize:(CGSize)size;
- (UIImage *)artwork;
- (NSTimeInterval)trackLength;
- (NSDate *)releaseDate;
- (NSString *)releaseYearString;
- (NSNumber *)trackNumber;
- (NSString *)trackLengthString;
+
+// Returns YES if the asset for this mediaItem has not
+// been downloaded to this device's library from the cloud.
+// Otherwise NO. This method will always return YES if
+// iTunes Match is turned off.
+- (BOOL)assetNeedsToDownload;
+
+// Returns YES if the asset has been deleted via iTunes, or
+// the built-in Music app. Otherwise NO. This method will
+// always return NO if iTunes Match is turned on.
+- (BOOL)assetHasBeenDeleted;
+
+// Returns YES if this mediaItem still exists in the device's
+// library. Otherwise NO. Will return NO if this mediaItem
+// once represented an asset in the cloud, but since then iTunes
+// Match has been turned off.
+- (BOOL)existsInLibrary;
+
@end
@@ -26,6 +26,14 @@ - (NSString *)albumTitle {
return [self valueForProperty:MPMediaItemPropertyAlbumTitle];
}
+- (NSURL *)assetURL {
+ return [self valueForProperty:MPMediaItemPropertyAssetURL];
+}
+
+- (NSString *)persistentID {
+ return [self valueForProperty:MPMediaItemPropertyPersistentID];
+}
+
- (UIImage *)artworkWithSize:(CGSize)size {
UIImage *image = nil;
MPMediaItemArtwork *artwork = [self valueForProperty:MPMediaItemPropertyArtwork];
@@ -87,4 +95,28 @@ - (NSString *)trackLengthString {
return timeString;
}
+- (BOOL)assetNeedsToDownload {
+ return ([self assetURL] == nil);
+}
+
+- (BOOL)assetHasBeenDeleted {
+ if ([self assetURL] == nil) {
+ return NO;
+ } else {
+ NSString *urlString = [[self assetURL] absoluteString];
+ BOOL assetURLPointsNowhere = ([urlString rangeOfString:@"ipod-library://item/item.(null)"].location != NSNotFound);
+ return assetURLPointsNowhere;
+ }
+}
+
+- (BOOL)existsInLibrary {
+ MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:[self persistentID]
+ forProperty: MPMediaItemPropertyPersistentID];
+ MPMediaQuery *query = [[MPMediaQuery alloc] init];
+ [query addFilterPredicate:predicate];
+ BOOL exists = ([[query items] count] != 0);
+ [query release];
+ return exists;
+}
+
@end
@@ -12,4 +12,11 @@
- (NSNumber *)playbackLength;
+// Returns YES if none of the assets represented in
+// this collection have been downloaded from the cloud.
+- (BOOL)hasNoLocalItems;
+
+// Returns YES if all items in the collection have been deleted.
+- (BOOL)hasNoPlayableItems;
+
@end
@@ -19,4 +19,20 @@ - (NSNumber *)playbackLength {
return [NSNumber numberWithDouble:totalTime];
}
+- (BOOL)hasNoLocalItems {
+ for (MPMediaItem *mediaItem in self.items) {
+ if ([mediaItem existsInLibrary] && ![mediaItem assetNeedsToDownload])
+ return NO;
+ }
+ return YES;
+}
+
+- (BOOL)hasNoPlayableItems {
+ for (MPMediaItem *mediaItem in self.items) {
+ if ([mediaItem existsInLibrary] && ![mediaItem assetHasBeenDeleted])
+ return NO;
+ }
+ return YES;
+}
+
@end

0 comments on commit 46d740c

Please sign in to comment.