Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #3 from mark-w/master

iCloud convenience methods
  • Loading branch information...
commit 46d740c5e0a48fde787da93b6a705aa8a7d777e7 2 parents 1a35441 + 0cf575a
@jaminguy authored
View
20 Classes/MPMediaItem+JGExtensions.h
@@ -14,6 +14,8 @@
- (NSString *)albumArtist;
- (NSString *)title;
- (NSString *)albumTitle;
+- (NSURL *)assetURL;
+- (NSString *)persistentID;
- (UIImage *)artworkWithSize:(CGSize)size;
- (UIImage *)artwork;
- (NSTimeInterval)trackLength;
@@ -21,4 +23,22 @@
- (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
View
32 Classes/MPMediaItem+JGExtensions.m
@@ -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
View
7 Classes/MPMediaItemCollection+JGExtensions.h
@@ -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
View
16 Classes/MPMediaItemCollection+JGExtensions.m
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.