Skip to content

Commit 32477e1

Browse files
committed
Adds "meta" properties to act as sorting helpers for post/pages.
Adds a core data revision: 31 Adds a new migration mapping and entity migration policy.
1 parent cbddc0a commit 32477e1

File tree

9 files changed

+1687
-4
lines changed

9 files changed

+1687
-4
lines changed

WordPress/Classes/Models/AbstractPost.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
@property (weak, readonly) AbstractPost *revision;
1515
@property (nonatomic, strong) NSMutableSet *comments;
1616
@property (nonatomic, strong) Media *featuredImage;
17+
18+
// By convention these should be treated as read only and not manually set.
19+
// These are primarily used as helpers sorting fetchRequests.
20+
@property (nonatomic, assign) BOOL metaIsLocal;
21+
@property (nonatomic, assign) BOOL metaPublishImmediately;
22+
1723
/**
1824
Used to store the post's status before its sent to the trash.
1925
*/

WordPress/Classes/Models/AbstractPost.m

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
@implementation AbstractPost
77

8-
@dynamic blog, media;
8+
@dynamic blog;
9+
@dynamic media;
10+
@dynamic metaIsLocal;
11+
@dynamic metaPublishImmediately;
912
@dynamic comments;
1013
@synthesize restorableStatus;
1114

@@ -23,7 +26,28 @@ - (void)awakeFromFetch
2326
// when wrong saving -- the app crashed for instance. So change our remote status to failed.
2427
[self setPrimitiveValue:@(AbstractPostRemoteStatusFailed) forKey:@"remoteStatusNumber"];
2528
}
29+
}
30+
31+
- (void)setRemoteStatusNumber:(NSNumber *)remoteStatusNumber
32+
{
33+
NSLog(@"%@", NSStringFromSelector(_cmd));
34+
self.metaIsLocal = ([remoteStatusNumber integerValue] == AbstractPostRemoteStatusLocal);
35+
36+
NSString *key = @"remoteStatusNumber";
37+
[self willChangeValueForKey:key];
38+
[self setPrimitiveValue:remoteStatusNumber forKey:key];
39+
[self didChangeValueForKey:key];
40+
}
41+
42+
- (void)setDate_created_gmt:(NSDate *)date_created_gmt
43+
{
44+
NSLog(@"%@", NSStringFromSelector(_cmd));
45+
self.metaPublishImmediately = (date_created_gmt == nil);
2646

47+
NSString *key = @"date_created_gmt";
48+
[self willChangeValueForKey:key];
49+
[self setPrimitiveValue:date_created_gmt forKey:key];
50+
[self didChangeValueForKey:key];
2751
}
2852

2953
+ (NSString *const)remoteUniqueIdentifier
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#import <CoreData/CoreData.h>
2+
3+
@interface PostToPost30To31 : NSEntityMigrationPolicy
4+
5+
@end
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#import "PostToPost30To31.h"
2+
#import "AbstractPost.h"
3+
4+
@implementation PostToPost30To31
5+
6+
- (BOOL)beginEntityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
7+
{
8+
DDLogInfo(@"%@ %@ (%@ -> %@)", self, NSStringFromSelector(_cmd), [mapping sourceEntityName], [mapping destinationEntityName]);
9+
return YES;
10+
}
11+
12+
13+
- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sourceInstance
14+
entityMapping:(NSEntityMapping *)mapping
15+
manager:(NSMigrationManager *)manager
16+
error:(NSError *__autoreleasing *)error
17+
{
18+
NSMutableArray *sourceKeys = [sourceInstance.entity.attributesByName.allKeys mutableCopy];
19+
NSDictionary *sourceValues = [sourceInstance dictionaryWithValuesForKeys:sourceKeys];
20+
NSManagedObject *destinationInstance = [NSEntityDescription insertNewObjectForEntityForName:mapping.destinationEntityName
21+
inManagedObjectContext:manager.destinationContext];
22+
NSArray *destinationKeys = destinationInstance.entity.attributesByName.allKeys;
23+
for (NSString *key in destinationKeys) {
24+
id value = [sourceValues valueForKey:key];
25+
// Avoid NULL values
26+
if (value && ![value isEqual:[NSNull null]]) {
27+
[destinationInstance setValue:value forKey:key];
28+
}
29+
}
30+
31+
// Sets the new meta publish immediately property value.
32+
id value = [sourceInstance valueForKey:@"date_created_gmt"];
33+
id metaValue = (!value || [value isEqual:[NSNull null]]) ? @YES : @NO;
34+
[destinationInstance setValue:metaValue forKey:@"metaPublishImmediately"];
35+
36+
// Sets the new meta is local property value.
37+
value = [sourceInstance valueForKey:@"remoteStatusNumber"];
38+
metaValue = [value isEqualToNumber:@2]? @YES : @NO;
39+
[destinationInstance setValue:@YES forKey:@"metaIsLocal"];
40+
41+
[manager associateSourceInstance:sourceInstance
42+
withDestinationInstance:destinationInstance
43+
forEntityMapping:mapping];
44+
return YES;
45+
}
46+
47+
- (BOOL)endEntityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError *__autoreleasing *)error
48+
{
49+
DDLogInfo(@"%@ %@ (%@ -> %@)", self, NSStringFromSelector(_cmd), [mapping sourceEntityName], [mapping destinationEntityName]);
50+
return YES;
51+
}
52+
53+
54+
@end

WordPress/Classes/Utility/Migrations/WordPress-30-31.xcmappingmodel/xcmapping.xml

Lines changed: 1165 additions & 0 deletions
Large diffs are not rendered by default.

WordPress/Classes/ViewRelated/Post/AbstractPostListViewController.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,10 @@ - (NSArray *)sortDescriptorsForFetchRequest
502502
{
503503
// Ascending only for scheduled posts/pages.
504504
BOOL ascending = self.currentPostListFilter.filterType == PostListStatusFilterScheduled;
505+
NSSortDescriptor *sortDescriptorLocal = [NSSortDescriptor sortDescriptorWithKey:@"metaIsLocal" ascending:NO];
506+
NSSortDescriptor *sortDescriptorImmediately = [NSSortDescriptor sortDescriptorWithKey:@"metaPublishImmediately" ascending:NO];
505507
NSSortDescriptor *sortDescriptorDate = [NSSortDescriptor sortDescriptorWithKey:@"date_created_gmt" ascending:ascending];
506-
return @[sortDescriptorDate];
508+
return @[sortDescriptorLocal, sortDescriptorImmediately, sortDescriptorDate];
507509
}
508510

509511
- (void)updateAndPerformFetchRequest

WordPress/Classes/WordPress.xcdatamodeld/.xccurrentversion

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
<plist version="1.0">
44
<dict>
55
<key>_XCCurrentVersionName</key>
6-
<string>WordPress 30.xcdatamodel</string>
6+
<string>WordPress 31.xcdatamodel</string>
77
</dict>
88
</plist>

0 commit comments

Comments
 (0)