Skip to content

Commit

Permalink
Adds "meta" properties to act as sorting helpers for post/pages.
Browse files Browse the repository at this point in the history
Adds a core data revision: 31
Adds a new migration mapping and entity migration policy.
  • Loading branch information
aerych committed Jun 5, 2015
1 parent 49ce023 commit dd7fa34
Show file tree
Hide file tree
Showing 9 changed files with 1,687 additions and 4 deletions.
6 changes: 6 additions & 0 deletions WordPress/Classes/Models/AbstractPost.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
@property (weak, readonly) AbstractPost *revision;
@property (nonatomic, strong) NSMutableSet *comments;
@property (nonatomic, strong) Media *featuredImage;

// By convention these should be treated as read only and not manually set.
// These are primarily used as helpers sorting fetchRequests.
@property (nonatomic, assign) BOOL metaIsLocal;
@property (nonatomic, assign) BOOL metaPublishImmediately;

/**
Used to store the post's status before its sent to the trash.
*/
Expand Down
26 changes: 25 additions & 1 deletion WordPress/Classes/Models/AbstractPost.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

@implementation AbstractPost

@dynamic blog, media;
@dynamic blog;
@dynamic media;
@dynamic metaIsLocal;
@dynamic metaPublishImmediately;
@dynamic comments;
@synthesize restorableStatus;

Expand All @@ -23,7 +26,28 @@ - (void)awakeFromFetch
// when wrong saving -- the app crashed for instance. So change our remote status to failed.
[self setPrimitiveValue:@(AbstractPostRemoteStatusFailed) forKey:@"remoteStatusNumber"];
}
}

- (void)setRemoteStatusNumber:(NSNumber *)remoteStatusNumber
{
NSLog(@"%@", NSStringFromSelector(_cmd));
self.metaIsLocal = ([remoteStatusNumber integerValue] == AbstractPostRemoteStatusLocal);

NSString *key = @"remoteStatusNumber";
[self willChangeValueForKey:key];
[self setPrimitiveValue:remoteStatusNumber forKey:key];
[self didChangeValueForKey:key];
}

- (void)setDate_created_gmt:(NSDate *)date_created_gmt
{
NSLog(@"%@", NSStringFromSelector(_cmd));
self.metaPublishImmediately = (date_created_gmt == nil);

NSString *key = @"date_created_gmt";
[self willChangeValueForKey:key];
[self setPrimitiveValue:date_created_gmt forKey:key];
[self didChangeValueForKey:key];
}

+ (NSString *const)remoteUniqueIdentifier
Expand Down
5 changes: 5 additions & 0 deletions WordPress/Classes/Utility/Migrations/PostToPost30To31.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#import <CoreData/CoreData.h>

@interface PostToPost30To31 : NSEntityMigrationPolicy

@end
54 changes: 54 additions & 0 deletions WordPress/Classes/Utility/Migrations/PostToPost30To31.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#import "PostToPost30To31.h"
#import "AbstractPost.h"

@implementation PostToPost30To31

- (BOOL)beginEntityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
{
DDLogInfo(@"%@ %@ (%@ -> %@)", self, NSStringFromSelector(_cmd), [mapping sourceEntityName], [mapping destinationEntityName]);
return YES;
}


- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sourceInstance
entityMapping:(NSEntityMapping *)mapping
manager:(NSMigrationManager *)manager
error:(NSError *__autoreleasing *)error
{
NSMutableArray *sourceKeys = [sourceInstance.entity.attributesByName.allKeys mutableCopy];
NSDictionary *sourceValues = [sourceInstance dictionaryWithValuesForKeys:sourceKeys];
NSManagedObject *destinationInstance = [NSEntityDescription insertNewObjectForEntityForName:mapping.destinationEntityName
inManagedObjectContext:manager.destinationContext];
NSArray *destinationKeys = destinationInstance.entity.attributesByName.allKeys;
for (NSString *key in destinationKeys) {
id value = [sourceValues valueForKey:key];
// Avoid NULL values
if (value && ![value isEqual:[NSNull null]]) {
[destinationInstance setValue:value forKey:key];
}
}

// Sets the new meta publish immediately property value.
id value = [sourceInstance valueForKey:@"date_created_gmt"];
id metaValue = (!value || [value isEqual:[NSNull null]]) ? @YES : @NO;
[destinationInstance setValue:metaValue forKey:@"metaPublishImmediately"];

// Sets the new meta is local property value.
value = [sourceInstance valueForKey:@"remoteStatusNumber"];
metaValue = [value isEqualToNumber:@2]? @YES : @NO;
[destinationInstance setValue:@YES forKey:@"metaIsLocal"];

This comment has been minimized.

Copy link
@koke

koke Jun 9, 2015

Member

I think that @YES was meant to be metaValue

This comment has been minimized.

Copy link
@aerych

aerych Jun 9, 2015

Author Member

Good catch


[manager associateSourceInstance:sourceInstance
withDestinationInstance:destinationInstance
forEntityMapping:mapping];
return YES;
}

- (BOOL)endEntityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError *__autoreleasing *)error
{
DDLogInfo(@"%@ %@ (%@ -> %@)", self, NSStringFromSelector(_cmd), [mapping sourceEntityName], [mapping destinationEntityName]);
return YES;
}


@end

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,10 @@ - (NSArray *)sortDescriptorsForFetchRequest
{
// Ascending only for scheduled posts/pages.
BOOL ascending = self.currentPostListFilter.filterType == PostListStatusFilterScheduled;
NSSortDescriptor *sortDescriptorLocal = [NSSortDescriptor sortDescriptorWithKey:@"metaIsLocal" ascending:NO];
NSSortDescriptor *sortDescriptorImmediately = [NSSortDescriptor sortDescriptorWithKey:@"metaPublishImmediately" ascending:NO];
NSSortDescriptor *sortDescriptorDate = [NSSortDescriptor sortDescriptorWithKey:@"date_created_gmt" ascending:ascending];
return @[sortDescriptorDate];
return @[sortDescriptorLocal, sortDescriptorImmediately, sortDescriptorDate];
}

- (void)updateAndPerformFetchRequest
Expand Down
2 changes: 1 addition & 1 deletion WordPress/Classes/WordPress.xcdatamodeld/.xccurrentversion
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<plist version="1.0">
<dict>
<key>_XCCurrentVersionName</key>
<string>WordPress 30.xcdatamodel</string>
<string>WordPress 31.xcdatamodel</string>
</dict>
</plist>
Loading

0 comments on commit dd7fa34

Please sign in to comment.