jeudi 8 mars 2012

Mise à jour pour la catégorie MPMediaItemCollection

Bien que les tests et le débogage, j'ai fait quelques modifications à cette catégorie. Je n'ai toujours pas ajouté la réorganisation, mais je pense que les méthodes qui sont ici sont maintenant assez solide. Si vous avez téléchargé la précédente, vous voudrez probablement récupérer la version mise à jour, tel qu'il est actuellement codes défensivement afin que les problèmes ne sont pas causés quand une collection devient vide par la suppression (collections élément multimédia ne peut pas être créé sans au moins un élément multimédia) .

MPMediaItemCollection-Utils.h
#import <Foundation/Foundation.h>
#import <MediaPlayer/MediaPlayer.h>

@interface MPMediaItemCollection(Utils)
/** Returns the first media item in the collection
*/

- (MPMediaItem *)firstMediaItem;

/** Returns the last media item in the collection
*/

- (MPMediaItem *)lastMediaItem;

/** This method will return the item in this media collection at a specific index
*/

- (MPMediaItem *)mediaItemAtIndex:(NSUInteger)index;

/** Given a particular media item, this method will return the next media item in the collection.
If there are multiple copies of the same media item in the list, it will return the one
after the first occurrence.
*/

- (MPMediaItem *)mediaItemAfterItem:(MPMediaItem *)compare;

/** Returns the title of the media item at a given index.
*/

- (NSString *)titleForMediaItemAtIndex:(NSUInteger)index;

/** Returns YES if the given media item occurs at least once in this collection
*/

- (BOOL)containsItem:(MPMediaItem *)compare;

/** Creates a new collection by appending otherCollection to the end of this collection
*/

- (MPMediaItemCollection *)collectionByAppendingCollection:(MPMediaItemCollection *)otherCollection;

/** Creates a new collection by appending an array of media items to the end of this collection
*/

- (MPMediaItemCollection *)collectionByAppendingMediaItems:(NSArray *)items;

/** Creates a new collection by appending a single media item to the end of this collection
*/

- (MPMediaItemCollection *)collectionByAppendingMediaItem:(MPMediaItem *)item;

/** Creates a new collection based on this collection, but excluding the specified items.
*/

- (MPMediaItemCollection *)collectionByDeletingMediaItems:(NSArray *)itemsToRemove;

/** Creates a new collection based on this collection, but which doesn't include the specified media item.
*/

- (MPMediaItemCollection *)collectionByDeletingMediaItem:(MPMediaItem *)itemToRemove;

/** Creates a new collection based on this collection, but excluding the media item at the specified index
*/

- (MPMediaItemCollection *)collectionByDeletingMediaItemAtIndex:(NSUInteger)index;

/** Creates a new collection, based on this collection, but excluding the media items starting with
(and including) the objects at index from and ending with (and including) to.
*/

- (MPMediaItemCollection *)collectionByDeletingMediaItemsFromIndex:(NSUInteger)from toIndex:(NSUInteger)to;
@end



MPMediaItemCollection-Utils.m
#import "MPMediaItemCollection-Utils.h"

@implementation MPMediaItemCollection(Utils)
- (MPMediaItem *)firstMediaItem {
return [[self items] objectAtIndex:0];
}


- (MPMediaItem *)lastMediaItem {
return [[self items] lastObject];
}


- (MPMediaItem *)mediaItemAtIndex:(NSUInteger)index {
return [[self items] objectAtIndex:index];
}


- (MPMediaItem *)mediaItemAfterItem:(MPMediaItem *)compare {
NSArray *items = [self items];

for (MPMediaItem *oneItem in items) {
if ([oneItem isEqual:compare]) {
// If last item, there is no index + 1
if (![[items lastObject] isEqual: oneItem])
return [items objectAtIndex:[items indexOfObject:oneItem] + 1];
}

}

return nil;
}


- (NSString *)titleForMediaItemAtIndex:(NSUInteger)index {
MPMediaItem *item = [[self items] objectAtIndex:index];
return [item valueForProperty:MPMediaItemPropertyTitle];
}


- (BOOL)containsItem:(MPMediaItem *)compare {
NSArray *items = [self items];

for (MPMediaItem *oneItem in items) {
if ([oneItem isEqual:compare])
return YES;
}

return NO;
}


- (MPMediaItemCollection *)collectionByAppendingCollection:(MPMediaItemCollection *)otherCollection {
return [self collectionByAppendingMediaItems:[otherCollection items]];
}


- (MPMediaItemCollection *)collectionByAppendingMediaItems:(NSArray *)items {
if (items == nil || [items count] == 0)
return nil;
NSMutableArray *appendCollection = [[[self items] mutableCopy] autorelease];
[appendCollection addObjectsFromArray:items];
return [MPMediaItemCollection collectionWithItems:appendCollection];
}


- (MPMediaItemCollection *)collectionByAppendingMediaItem:(MPMediaItem *)item {
if (item == nil)
return nil;

return [self collectionByAppendingMediaItems:[NSArray arrayWithObject:item]];
}


- (MPMediaItemCollection *)collectionByDeletingMediaItems:(NSArray *)itemsToRemove {
if (itemsToRemove == nil || [itemsToRemove count] == 0)
return [[self copy] autorelease];
NSMutableArray *items = [[[self items] mutableCopy] autorelease];
[items removeObjectsInArray:itemsToRemove];
return [MPMediaItemCollection collectionWithItems:items];
}


- (MPMediaItemCollection *)collectionByDeletingMediaItem:(MPMediaItem *)itemToRemove {
if (itemToRemove == nil)
return [[self copy] autorelease];

NSMutableArray *items = [[[self items] mutableCopy] autorelease];
[items removeObject:itemToRemove];
return [MPMediaItemCollection collectionWithItems:items];
}


- (MPMediaItemCollection *)collectionByDeletingMediaItemAtIndex:(NSUInteger)index {
NSMutableArray *items = [[[self items] mutableCopy] autorelease];
[items removeObjectAtIndex:index];
return [items count] > 0 ? [MPMediaItemCollection collectionWithItems:items] : nil;
}


- (MPMediaItemCollection *)collectionByDeletingMediaItemsFromIndex:(NSUInteger)from toIndex:(NSUInteger)to {

// Ensure from is before to
if (to < from) {
NSUInteger temp = from;
to = from;
from = temp;
}


NSMutableArray *items = [[[self items] mutableCopy] autorelease];
[items removeObjectsInRange:NSMakeRange(from, to - from)];
return [MPMediaItemCollection collectionWithItems:items];
}

@end



Aucun commentaire: