Permalink
Browse files

Implemented MPMedia data source stuff

  • Loading branch information...
indragiek committed Aug 27, 2012
1 parent 1fa0163 commit 27e11dc91642005344c48875977f42e2526765cf
Showing with 960 additions and 288 deletions.
  1. +0 −14 Content Sources/MPMediaLibrary/Categories/MPMediaItem+SMKAdditions.h
  2. +0 −21 Content Sources/MPMediaLibrary/Categories/MPMediaItem+SMKAdditions.m
  3. +22 −0 Content Sources/MPMediaLibrary/Data Model/SMKMPMediaAlbum.h
  4. +160 −0 Content Sources/MPMediaLibrary/Data Model/SMKMPMediaAlbum.m
  5. +16 −0 Content Sources/MPMediaLibrary/Data Model/SMKMPMediaArtist.h
  6. +85 −0 Content Sources/MPMediaLibrary/Data Model/SMKMPMediaArtist.m
  7. +16 −0 Content Sources/MPMediaLibrary/Data Model/SMKMPMediaPlaylist.h
  8. +68 −0 Content Sources/MPMediaLibrary/Data Model/SMKMPMediaPlaylist.m
  9. +24 −0 Content Sources/MPMediaLibrary/Data Model/SMKMPMediaPredicate.h
  10. +25 −0 Content Sources/MPMediaLibrary/Data Model/SMKMPMediaPredicate.m
  11. +26 −0 Content Sources/MPMediaLibrary/Data Model/SMKMPMediaTrack.h
  12. +130 −0 Content Sources/MPMediaLibrary/Data Model/SMKMPMediaTrack.m
  13. +19 −0 Content Sources/MPMediaLibrary/SMKMPMediaContentSource.h
  14. +57 −0 Content Sources/MPMediaLibrary/SMKMPMediaContentSource.m
  15. +20 −0 Content Sources/MPMediaLibrary/SMKMPMediaHelpers.h
  16. +23 −0 Content Sources/MPMediaLibrary/SMKMPMediaHelpers.m
  17. +0 −14 Content Sources/MPMediaLibrary/SMKMPMediaLibraryContentSource.h
  18. +0 −13 Content Sources/MPMediaLibrary/SMKMPMediaLibraryContentSource.m
  19. +1 −2 Content Sources/Spotify/Data Model/SPAlbum+SMKAlbum.m
  20. +1 −1 Content Sources/Spotify/Data Model/SPArtist+SMKArtist.m
  21. +1 −1 Content Sources/Spotify/Data Model/SPPlaylist+SMKPlaylist.m
  22. +3 −0 Content Sources/Spotify/SMKSpotifyContentSource.m
  23. +4 −3 Content Sources/iTunes/Data Model/mogenerator/SMKITunesAlbum.h
  24. +5 −0 Content Sources/iTunes/Data Model/mogenerator/SMKITunesAlbum.m
  25. +8 −4 Content Sources/iTunes/Data Model/mogenerator/SMKiTunesTrack.h
  26. +5 −0 Content Sources/iTunes/Data Model/mogenerator/SMKiTunesTrack.m
  27. +2 −0 Content Sources/iTunes/SMKiTunesContentSource.m
  28. +1 −1 MPMediaLibraryExample/DetailViewController.h
  29. +36 −12 MPMediaLibraryExample/DetailViewController.m
  30. +19 −63 MPMediaLibraryExample/MasterViewController.m
  31. +57 −33 MPMediaLibraryExample/en.lproj/MainStoryboard_iPad.storyboard
  32. +8 −2 Protocols/SMKAlbum.h
  33. +1 −1 Protocols/SMKArtist.h
  34. +12 −3 Protocols/SMKArtworkObject.h
  35. +8 −2 Protocols/SMKContentSource.h
  36. +22 −2 Protocols/SMKTrack.h
  37. +73 −79 SNRMusicKit.xcodeproj/project.pbxproj
  38. BIN ...cKit.xcodeproj/project.xcworkspace/xcuserdata/indragie.xcuserdatad/UserInterfaceState.xcuserstate
  39. +2 −4 SNRMusicKitiOS/SNRMusicKitiOS.h
  40. +0 −13 SNRMusicKitiOS/SNRMusicKitiOS.m
@@ -1,14 +0,0 @@
-//
-// MPMediaItem+SMKAdditions.h
-// SNRMusicKit
-//
-// Created by Indragie Karunaratne on 2012-08-24.
-// Copyright (c) 2012 Indragie Karunaratne. All rights reserved.
-//
-
-#import <MediaPlayer/MediaPlayer.h>
-#import "SMKTrack.h"
-
-@interface MPMediaItem (SMKAdditions) <SMKTrack>
-
-@end
@@ -1,21 +0,0 @@
-//
-// MPMediaItem+SMKAdditions.m
-// SNRMusicKit
-//
-// Created by Indragie Karunaratne on 2012-08-24.
-// Copyright (c) 2012 Indragie Karunaratne. All rights reserved.
-//
-
-#import "MPMediaItem+SMKAdditions.h"
-
-@implementation MPMediaItem (SMKAdditions)
-- (id<SMKArtist>)artist
-{
- NSString *persistentID = [[self class] persistentIDPropertyForGroupingType:MPMediaGroupingAlbumArtist];
- MPMediaQuery *query = [MPMediaQuery artistsQuery];
- [query addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:persistentID forProperty:MPMediaItemPropertyPersistentID]];
- if ([[query items] count]) {
- return [[query items] objectAtIndex:0];
- }
-}
-@end
@@ -0,0 +1,22 @@
+//
+// SMKMPMediaAlbum.h
+// SNRMusicKit
+//
+// Created by Indragie Karunaratne on 2012-08-27.
+// Copyright (c) 2012 Indragie Karunaratne. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import <MediaPlayer/MediaPlayer.h>
+#import "SMKAlbum.h"
+#import "SMKArtworkObject.h"
+
+@interface SMKMPMediaAlbum : NSObject <SMKAlbum, SMKArtworkObject>
+@property (nonatomic, strong) MPMediaItemCollection *representedObject;
+@property (nonatomic, assign, readonly) id<SMKContentSource> contentSource;
+
+// SMKAlbum @optional
+@property (nonatomic, assign, readonly) NSUInteger releaseYear;
+@property (nonatomic, assign, readonly) NSUInteger rating;
+@property (nonatomic, assign, readonly) NSTimeInterval duration;
+@end
@@ -0,0 +1,160 @@
+//
+// SMKMPMediaAlbum.m
+// SNRMusicKit
+//
+// Created by Indragie Karunaratne on 2012-08-27.
+// Copyright (c) 2012 Indragie Karunaratne. All rights reserved.
+//
+
+#import "SMKMPMediaAlbum.h"
+#import "SMKMPMediaContentSource.h"
+#import "SMKMPMediaHelpers.h"
+
+@interface SMKMPMediaArtist (SMKInternal)
+- (id)initWithRepresentedObject:(MPMediaItemCollection *)object contentSource:(id<SMKContentSource>)contentSource;
+@end
+
+@interface SMKMPMediaTrack (SMKInternal)
+- (id)initWithRepresentedObject:(MPMediaItem*)object contentSource:(id<SMKContentSource>)contentSource;
+@end
+
+@implementation SMKMPMediaAlbum
+
+- (id)initWithRepresentedObject:(MPMediaItemCollection *)object contentSource:(id<SMKContentSource>)contentSource
+{
+ if ((self = [super init])) {
+ _representedObject = object;
+ _contentSource = contentSource;
+ }
+ return self;
+}
+
+#pragma mark - SMKContentObject
+
+- (NSString *)uniqueIdentifier
+{
+ return [self.representedObject valueForKey:MPMediaItemPropertyPersistentID];
+}
+
+- (NSString *)name
+{
+ return [self.representedObject.representativeItem valueForKey:MPMediaItemPropertyAlbumTitle];
+}
+
++ (NSSet *)supportedSortKeys
+{
+ return [NSSet setWithObjects:@"name", @"releaseYear", @"artist", @"rating", @"duration", @"isCompilation", nil];
+}
+
+#pragma mark - SMKArtworkObject
+
+- (void)fetchArtworkWithSize:(SMKArtworkSize)size
+ completionHandler:(void(^)(SMKPlatformNativeImage *image, NSError *error))handler
+{
+ CGSize targetSize = CGSizeZero;
+ switch (size) {
+ case SMKArtworkSizeSmallest:
+ targetSize = CGSizeMake(72.0, 72.0);
+ break;
+ case SMKArtworkSizeSmall:
+ targetSize = CGSizeMake(150.0, 150.0);
+ break;
+ case SMKArtworkSizeLarge:
+ targetSize = CGSizeMake(300.0, 300.0);
+ break;
+ case SMKArtworkSizeLargest:
+ targetSize = CGSizeMake(600.0, 600.0);
+ default:
+ break;
+ }
+ [self fetchArtworkWithTargetSize:targetSize completionHandler:handler];
+}
+
+- (void)fetchArtworkWithTargetSize:(CGSize)size completionHandler:(void(^)(SMKPlatformNativeImage *image, NSError *error))handler
+{
+ __weak SMKMPMediaAlbum *weakSelf = self;
+ dispatch_async([(SMKMPMediaContentSource*)self.contentSource queryQueue], ^{
+ SMKMPMediaAlbum *strongSelf = weakSelf;
+ MPMediaItemArtwork *artwork = [strongSelf.representedObject.representativeItem valueForKey:MPMediaItemPropertyArtwork];
+ UIImage *image = [artwork imageWithSize:size];
+ dispatch_async(dispatch_get_main_queue(), ^{
+ if (handler) handler(image, nil);
+ });
+ });
+}
+
+#pragma mark - SMKAlbum
+
+- (id<SMKArtist>)artist
+{
+ MPMediaItem *representativeItem = self.representedObject.representativeItem;
+ MPMediaQuery *artistQuery = [MPMediaQuery artistsQuery];
+ MPMediaPropertyPredicate *artistPredicate = [SMKMPMediaHelpers predicateForArtistNameOfItem:representativeItem];
+ if (artistPredicate) {
+ artistQuery.filterPredicates = [NSSet setWithObjects:artistPredicate, nil];
+ NSArray *collections = artistQuery.collections;
+ if ([collections count]) {
+ return [[SMKMPMediaArtist alloc] initWithRepresentedObject:[collections objectAtIndex:0] contentSource:self.contentSource];
+ }
+ }
+ return nil;
+}
+
+- (NSUInteger)releaseYear
+{
+ NSDate *releaseDate = [self.representedObject.representativeItem valueForKey:MPMediaItemPropertyReleaseDate];
+ if (releaseDate) {
+ NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:releaseDate];
+ return [components year];
+ }
+ return 0;
+}
+
+- (NSUInteger)rating
+{
+ return [[self.representedObject.representativeItem valueForKey:MPMediaItemPropertyRating] unsignedIntegerValue];
+}
+
+- (NSTimeInterval)duration
+{
+ NSTimeInterval totalDuration = 0.0;
+ for (MPMediaItem *item in self.representedObject.items) {
+ totalDuration += [[item valueForKey:MPMediaItemPropertyPlaybackDuration] doubleValue];
+ }
+ return totalDuration;
+}
+
+- (void)fetchTracksWithSortDescriptors:(NSArray *)sortDescriptors
+ predicate:(id)predicate
+ completionHandler:(void(^)(NSArray *tracks, NSError *error))handler
+{
+ __weak SMKMPMediaAlbum *weakSelf = self;
+ dispatch_async([(SMKMPMediaContentSource*)self.contentSource queryQueue], ^{
+ SMKMPMediaAlbum *strongSelf = weakSelf;
+ NSMutableArray *tracks = [NSMutableArray array];
+ NSArray *items = nil;
+ if (!predicate) {
+ items = strongSelf.representedObject.items;
+ } else if ([strongSelf name]) {
+ MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
+ MPMediaPropertyPredicate *albumPredicate = [MPMediaPropertyPredicate predicateWithValue:[strongSelf name] forProperty:MPMediaItemPropertyAlbumTitle];
+ NSMutableSet *predicates = [NSMutableSet setWithObject:albumPredicate];
+ MPMediaPropertyPredicate *artistPredicate = [SMKMPMediaHelpers predicateForArtistNameOfItem:strongSelf.representedObject.representativeItem];
+ if (artistPredicate)
+ [predicates addObject:artistPredicate];
+ if (predicate)
+ [predicates addObjectsFromArray:[[(SMKMPMediaPredicate *)predicate predicates] allObjects]];
+ items = songsQuery.items;
+ }
+ [items enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
+ SMKMPMediaTrack *track = [[SMKMPMediaTrack alloc] initWithRepresentedObject:obj contentSource:strongSelf.contentSource];
+ [tracks addObject:track];
+ }];
+ if ([sortDescriptors count])
+ [tracks sortUsingDescriptors:sortDescriptors];
+ dispatch_async(dispatch_get_main_queue(), ^{
+ if (handler) handler(tracks, nil);
+ });
+ });
+}
+@end
@@ -0,0 +1,16 @@
+//
+// SMKMPMediaArtist.h
+// SNRMusicKit
+//
+// Created by Indragie Karunaratne on 2012-08-27.
+// Copyright (c) 2012 Indragie Karunaratne. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import <MediaPlayer/MediaPlayer.h>
+#import "SMKArtist.h"
+
+@interface SMKMPMediaArtist : NSObject <SMKArtist>
+@property (nonatomic, strong, readonly) MPMediaItemCollection *representedObject;
+@property (nonatomic, assign, readonly) id<SMKContentSource> contentSource;
+@end
@@ -0,0 +1,85 @@
+//
+// SMKMPMediaArtist.m
+// SNRMusicKit
+//
+// Created by Indragie Karunaratne on 2012-08-27.
+// Copyright (c) 2012 Indragie Karunaratne. All rights reserved.
+//
+
+#import "SMKMPMediaArtist.h"
+#import "SMKMPMediaContentSource.h"
+
+@interface SMKMPMediaAlbum (SMKInternal)
+- (id)initWithRepresentedObject:(MPMediaItemCollection *)object contentSource:(id<SMKContentSource>)contentSource;
+@end
+
+@implementation SMKMPMediaArtist
+
+- (id)initWithRepresentedObject:(MPMediaItemCollection *)object contentSource:(id<SMKContentSource>)contentSource
+{
+ if ((self = [super init])) {
+ _representedObject = object;
+ _contentSource = contentSource;
+ }
+ return self;
+}
+
+#pragma mark - SMKContentObject
+
+- (NSString *)uniqueIdentifier
+{
+ return [self.representedObject valueForKey:MPMediaItemPropertyPersistentID];
+}
+
+- (NSString *)name
+{
+ return [self.representedObject.representativeItem valueForKey:MPMediaItemPropertyAlbumArtist];
+}
+
++ (NSSet *)supportedSortKeys
+{
+ return [NSSet setWithObjects:@"name", nil];
+}
+
+#pragma mark - SMKArtist
+
+- (void)fetchAlbumsWithSortDescriptors:(NSArray *)sortDescriptors
+ predicate:(id)predicate
+ completionHandler:(void(^)(NSArray *albums, NSError *error))handler
+{
+ __weak SMKMPMediaArtist *weakSelf = self;
+ dispatch_async([(SMKMPMediaContentSource*)self.contentSource queryQueue], ^{
+ SMKMPMediaArtist *strongSelf = weakSelf;
+ MPMediaQuery *albumsQuery = [MPMediaQuery albumsQuery];
+ MPMediaPropertyPredicate *artistPredicate = nil;
+ NSString *albumArtistName = [strongSelf name];
+ NSString *artistName = [strongSelf.representedObject.representativeItem valueForKey:MPMediaItemPropertyArtist];
+ if ([albumArtistName length]) {
+ artistPredicate = [MPMediaPropertyPredicate predicateWithValue:albumArtistName forProperty:MPMediaItemPropertyAlbumArtist];
+ } else {
+ artistPredicate = [MPMediaPropertyPredicate predicateWithValue:artistName forProperty:MPMediaItemPropertyArtist];
+ }
+ if (artistPredicate) {
+ NSMutableSet *predicates = [NSMutableSet setWithObject:artistPredicate];
+ if (predicate)
+ [predicates addObjectsFromArray:[[(SMKMPMediaPredicate *)predicate predicates] allObjects]];
+ albumsQuery.filterPredicates = predicates;
+ NSArray *collections = albumsQuery.collections;
+ NSMutableArray *albums = [NSMutableArray arrayWithCapacity:[collections count]];
+ [collections enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
+ SMKMPMediaAlbum *album = [[SMKMPMediaAlbum alloc] initWithRepresentedObject:obj contentSource:strongSelf.contentSource];
+ [albums addObject:album];
+ }];
+ if ([sortDescriptors count])
+ [albums sortUsingDescriptors:sortDescriptors];
+ dispatch_async(dispatch_get_main_queue(), ^{
+ if (handler) handler(albums, nil);
+ });
+ } else {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ if (handler) handler(nil, nil);
+ });
+ }
+ });
+}
+@end
@@ -0,0 +1,16 @@
+//
+// SMKMPMediaPlaylist.h
+// SNRMusicKit
+//
+// Created by Indragie Karunaratne on 2012-08-27.
+// Copyright (c) 2012 Indragie Karunaratne. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import <MediaPlayer/MediaPlayer.h>
+#import "SMKPlaylist.h"
+
+@interface SMKMPMediaPlaylist : NSObject <SMKPlaylist>
+@property (nonatomic, strong) MPMediaItemCollection *representedObject;
+@property (nonatomic, assign, readonly) id<SMKContentSource> contentSource;
+@end
Oops, something went wrong.

0 comments on commit 27e11dc

Please sign in to comment.