Skip to content

Commit

Permalink
First shot on fixing the sorting issue reported by many users.
Browse files Browse the repository at this point in the history
The position of the album in the parent album is now preserved.
Updated RestKit to latest master & did some formatting
  • Loading branch information
David Steinberger committed Oct 21, 2011
1 parent 67433af commit 581223f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 38 deletions.
79 changes: 50 additions & 29 deletions Classes/RKMTree.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@

#import "RKMTree.h"

@interface RKMTree()
@interface RKMTree ()

- (NSArray*) sortEntitiesByRelativePosition;
- (NSString*)getItemIDFromURL;
- (NSArray *)sortEntitiesByRelativePosition;
- (NSString *)getItemIDFromURL;

@end

Expand All @@ -37,39 +37,60 @@ @implementation RKMTree
@dynamic url;
@dynamic rEntity;

#pragma mark -
#pragma mark private
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -

- (NSArray*) sortEntitiesByRelativePosition {
NSSortDescriptor* descriptor1 = [NSSortDescriptor sortDescriptorWithKey:@"positionInAlbum" ascending:YES];
NSArray* entities = [[self.rEntity allObjects] sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor1, nil]];
return entities;
- (RKMEntity *)root {
if ([self.rEntity count] > 0) {
NSArray *filtered =
[[self sortEntitiesByRelativePosition]
filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:
@"(itemID == %@)", [self getItemIDFromURL]]];
RKMEntity *entity = [filtered objectAtIndex:0];
return entity;
}
else {
return nil;
}
}

- (NSString*)getItemIDFromURL {
NSArray *urlComponents = [self.url componentsSeparatedByString:@ "/"];
NSString *tmp = [urlComponents lastObject];
NSString * itemID = [tmp stringByReplacingOccurrencesOfString:@"?depth=1" withString:@""];
return itemID;

- (NSArray *)children {
if ([self.rEntity count] > 1) {
NSArray *filtered =
[[self sortEntitiesByRelativePosition]
filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:
@"(itemID != %@)", [self getItemIDFromURL]]];
return filtered;
}
else {
return nil;
}
}

- (RKMEntity*) root {
if ([self.rEntity count] > 0) {
NSArray *filtered = [[self sortEntitiesByRelativePosition] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(itemID == %@)", [self getItemIDFromURL]]];
RKMEntity* entity = [filtered objectAtIndex:0];
return entity;
} else {
return nil;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark private

- (NSArray *)sortEntitiesByRelativePosition {
NSSortDescriptor *descriptor =
[NSSortDescriptor sortDescriptorWithKey:@"positionInAlbum" ascending:YES];
NSArray *entities =
[[self.rEntity allObjects] sortedArrayUsingDescriptors:
[NSArray arrayWithObjects:descriptor, nil]];
return entities;
}

- (NSArray*) children {
if ([self.rEntity count] > 1) {
NSArray *filtered = [[self sortEntitiesByRelativePosition] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(itemID != %@)", [self getItemIDFromURL]]];
return filtered;
} else {
return nil;
}

- (NSString *)getItemIDFromURL {
NSArray *urlComponents = [self.url componentsSeparatedByString:@ "/"];
NSString *tmp = [urlComponents lastObject];
NSString *itemID = [tmp stringByReplacingOccurrencesOfString:@"?depth=1" withString:@""];
return itemID;
}


@end
53 changes: 45 additions & 8 deletions Classes/RKObjectLoaderTTModel+fix.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@

// RestKit
#import "RKMTree.h"
#import "RKMItem.h"
#import "RKMEntity.h"

@implementation RKObjectLoaderTTModel (fix)

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark RKModelLoaderDelegate

- (void)objectLoader:(RKObjectLoader *)loader willMapData:(inout id *)mappableData {
Expand All @@ -40,20 +43,54 @@ - (void)objectLoader:(RKObjectLoader *)loader willMapData:(inout id *)mappableDa
NSMutableArray *newEntity =
[[NSMutableArray alloc] initWithCapacity:[origEntities count]];

NSDictionary *entity =
[( (NSDictionary *)[origEntities objectAtIndex:0] ) objectForKey:
@"entity"];
int i = [[entity objectForKey:@"id"] intValue];
int i = 0;
for (NSDictionary *origEntity in origEntities) {
NSMutableDictionary *oneEntity = [origEntity mutableCopy];

// inject the position in the array
NSMutableDictionary *entity =
([(NSDictionary *)[oneEntity objectForKey:@"entity"]
mutableCopy]);
[entity setObject:[NSString stringWithFormat:@"%i",
i] forKey:@"positionInAlbum"];

/*
* Each album is at a certain position within it's parent album.
* If we just iterate over the album and enumerate the items the
*position in
* the parent album is lost.
* For that reason the position index of the album (root) must be
*preserved.
*/
if (i == 0) {
RKManagedObjectStore *store =
[RKObjectManager sharedManager].objectStore;
NSString *predicateString = [entity objectForKey:@"id"];

NSFetchRequest *request = [RKMEntity fetchRequest];
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@
"itemID = %@", predicateString, nil];
[request setPredicate:predicate];

NSError *error;
NSArray *objects =
[[store managedObjectContext]
executeFetchRequest:
request error:&error];
if ([objects count] > 0) {
RKMEntity *object =
[objects objectAtIndex:0];

NSNumber *positionInAlbum =
[NSNumber numberWithInt:
[object.positionInAlbum intValue]];

[entity setObject:positionInAlbum forKey:
@"positionInAlbum"];
}
}
else {
[entity setObject:[NSString stringWithFormat:@"%i",
i] forKey:@"positionInAlbum"];
}
[oneEntity removeObjectForKey:@"entity"];
[oneEntity setObject:entity forKey:@"entity"];

Expand Down

0 comments on commit 581223f

Please sign in to comment.