Skip to content

Commit 6a50a4d

Browse files
authored
Move post revision related functions to Swift (#25009)
* Move BasePost.hasContent and isContentEmpty to Swift * Move AbstractPost.cloneFrom to Swift * Delete `AbstractPost.updatePostFrom` * Move AbstractPost.createRevision to Swift * Move AbstractPost.deleteRevision * Move AbstractPost.applyRevision to Swift * Move AbstractPost.isRevision and isOriginal to Swift * Move AbstractPost.hasRevision to Swift * Move AbstractPost.latest to Swift * Rewrite AbstractPost.hasContent and isContentEmpty
1 parent 1b9deeb commit 6a50a4d

File tree

6 files changed

+91
-122
lines changed

6 files changed

+91
-122
lines changed

Sources/WordPressData/Objective-C/AbstractPost.m

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -38,85 +38,6 @@ - (void)save
3838
#pragma mark -
3939
#pragma mark Revision management
4040

41-
- (AbstractPost *)cloneFrom:(AbstractPost *)source
42-
{
43-
for (NSString *key in [[[source entity] attributesByName] allKeys]) {
44-
if (![key isEqualToString:@"permalink"]) {
45-
[self setValue:[source valueForKey:key] forKey:key];
46-
}
47-
}
48-
for (NSString *key in [[[source entity] relationshipsByName] allKeys]) {
49-
if ([key isEqualToString:@"original"] || [key isEqualToString:@"revision"]) {
50-
continue;
51-
} else if ([key isEqualToString:@"comments"]) {
52-
[self setComments:[source comments]];
53-
} else {
54-
[self setValue: [source valueForKey:key] forKey: key];
55-
}
56-
}
57-
58-
return self;
59-
}
60-
61-
- (AbstractPost *)createRevision
62-
{
63-
NSParameterAssert(self.revision == nil);
64-
65-
AbstractPost *post = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass(self.class) inManagedObjectContext:self.managedObjectContext];
66-
[post cloneFrom:self];
67-
post.remoteStatus = AbstractPostRemoteStatusLocalRevision;
68-
[post setValue:self forKey:@"original"];
69-
[post setValue:nil forKey:@"revision"];
70-
return post;
71-
}
72-
73-
- (void)deleteRevision
74-
{
75-
if (self.revision) {
76-
[self.managedObjectContext performBlockAndWait :^{
77-
[self.managedObjectContext deleteObject:self.revision];
78-
[self willChangeValueForKey:@"revision"];
79-
[self setPrimitiveValue:nil forKey:@"revision"];
80-
[self didChangeValueForKey:@"revision"];
81-
}];
82-
}
83-
}
84-
85-
- (void)applyRevision
86-
{
87-
if ([self isOriginal]) {
88-
[self cloneFrom:self.revision];
89-
}
90-
}
91-
92-
- (AbstractPost *)updatePostFrom:(AbstractPost *)revision
93-
{
94-
for (NSString *key in [[[revision entity] attributesByName] allKeys]) {
95-
if ([key isEqualToString:@"postTitle"] ||
96-
[key isEqualToString:@"content"]) {
97-
[self setValue:[revision valueForKey:key] forKey:key];
98-
} else if ([key isEqualToString:@"dateModified"]) {
99-
[self setValue:[NSDate date] forKey:key];
100-
}
101-
}
102-
return self;
103-
}
104-
105-
- (BOOL)isRevision
106-
{
107-
return (![self isOriginal]);
108-
}
109-
110-
- (BOOL)isOriginal
111-
{
112-
return ([self original] == nil);
113-
}
114-
115-
- (AbstractPost *)latest
116-
{
117-
return [self hasRevision] ? [[self revision] latest] : self;
118-
}
119-
12041
- (AbstractPost *)revision
12142
{
12243
[self willAccessValueForKey:@"revision"];
@@ -186,11 +107,6 @@ - (BOOL)hasTags
186107
return NO;
187108
}
188109

189-
- (BOOL)hasRevision
190-
{
191-
return self.revision != nil;
192-
}
193-
194110
- (BOOL)hasRemote
195111
{
196112
return ((self.postID != nil) && ([self.postID longLongValue] > 0));

Sources/WordPressData/Objective-C/BasePost.m

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,4 @@ @implementation BasePost
2121
@dynamic suggested_slug;
2222
@dynamic pathForDisplayImage;
2323

24-
- (BOOL)hasContent
25-
{
26-
BOOL titleIsEmpty = self.postTitle ? self.postTitle.isEmpty : YES;
27-
BOOL contentIsEmpty = [self isContentEmpty];
28-
29-
return !titleIsEmpty || !contentIsEmpty;
30-
}
31-
32-
- (BOOL)isContentEmpty
33-
{
34-
BOOL isContentAnEmptyGBParagraph = [self.content isEqualToString:@"<!-- wp:paragraph -->\n<p></p>\n<!-- /wp:paragraph -->"];
35-
return self.content ? (self.content.isEmpty || isContentAnEmptyGBParagraph) : YES;
36-
}
37-
3824
@end

Sources/WordPressData/Objective-C/include/AbstractPost.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,11 @@ typedef NS_ENUM(NSUInteger, AbstractPostRemoteStatus) {
6262

6363
@property (nonatomic, strong, nullable) NSString *voiceContent;
6464

65-
// Revision management
66-
- (AbstractPost *)createRevision;
67-
- (void)deleteRevision;
68-
- (void)applyRevision;
69-
- (AbstractPost *)updatePostFrom:(AbstractPost *)revision;
70-
- (BOOL)isRevision;
71-
- (BOOL)isOriginal;
72-
73-
/// Returns the latest revision of a post.
74-
///
75-
- (AbstractPost *)latest;
76-
- (AbstractPost *)cloneFrom:(AbstractPost *)source;
7765
- (BOOL)hasPhoto;
7866
- (BOOL)hasVideo;
7967
- (BOOL)hasCategories;
8068
- (BOOL)hasTags;
8169

82-
/**
83-
* @brief Call this method to know whether this post has a revision or not.
84-
*
85-
* @returns YES if this post has a revision, NO otherwise.
86-
*/
87-
- (BOOL)hasRevision;
8870

8971
#pragma mark - Conveniece Methods
9072
- (BOOL)shouldPublishImmediately;

Sources/WordPressData/Objective-C/include/BasePost.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ NS_ASSUME_NONNULL_BEGIN
2727
*/
2828
@property (nonatomic, strong, nullable) NSString *pathForDisplayImage;
2929

30-
// Returns true if title or content is non empty
31-
- (BOOL)hasContent;
32-
33-
// True if the content field is empty, independent of the title field.
34-
- (BOOL)isContentEmpty;
35-
3630
@end
3731

3832
NS_ASSUME_NONNULL_END

Sources/WordPressData/Swift/AbstractPost.swift

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,78 @@ public extension AbstractPost {
254254
override func contentPreviewForDisplay() -> String? {
255255
mt_excerpt
256256
}
257+
258+
@objc(cloneFrom:)
259+
func clone(from source: AbstractPost) {
260+
for key in source.entity.attributesByName.keys {
261+
if key != "permalink" {
262+
setValue(source.value(forKey: key), forKey: key)
263+
}
264+
}
265+
266+
for key in source.entity.relationshipsByName.keys {
267+
if key == "original" || key == "revision" {
268+
continue
269+
} else if key == "comments" {
270+
comments = source.comments
271+
} else {
272+
setValue(source.value(forKey: key), forKey: key)
273+
}
274+
}
275+
}
276+
277+
@objc
278+
func createRevision() -> AbstractPost {
279+
precondition(managedObjectContext != nil)
280+
precondition(revision == nil, "This post must not already have a revision")
281+
282+
let post = Self(context: managedObjectContext!)
283+
post.clone(from: self)
284+
post.remoteStatus = .localRevision
285+
post.setValue(self, forKey: "original")
286+
post.setValue(nil, forKey: "revision")
287+
return post
288+
}
289+
290+
@objc
291+
func deleteRevision() {
292+
guard let revision, let context = managedObjectContext else {
293+
return
294+
}
295+
296+
context.performAndWait {
297+
context.delete(revision)
298+
willChangeValue(forKey: "revision")
299+
setPrimitiveValue(nil, forKey: "revision")
300+
didChangeValue(forKey: "revision")
301+
}
302+
}
303+
304+
@objc
305+
func applyRevision() {
306+
guard isOriginal(), let revision else {
307+
return
308+
}
309+
clone(from: revision)
310+
}
311+
312+
@objc
313+
func isRevision() -> Bool {
314+
!isOriginal()
315+
}
316+
317+
@objc
318+
func isOriginal() -> Bool {
319+
original == nil
320+
}
321+
322+
@objc
323+
func latest() -> AbstractPost {
324+
revision?.latest() ?? self
325+
}
326+
327+
@objc
328+
func hasRevision() -> Bool {
329+
revision != nil
330+
}
257331
}

Sources/WordPressData/Swift/BasePost.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,21 @@ extension BasePost {
8383
date_created_gmt = newValue
8484
}
8585
}
86+
87+
/// Returns true if title or content is non empty
88+
public func hasContent() -> Bool {
89+
if let postTitle, !postTitle.isEmpty {
90+
return true
91+
}
92+
93+
return !isContentEmpty()
94+
}
95+
96+
/// True if the content field is empty, independent of the title field.
97+
public func isContentEmpty() -> Bool {
98+
guard let content else { return true }
99+
100+
let emptyGBParagraph = "<!-- wp:paragraph -->\n<p></p>\n<!-- /wp:paragraph -->"
101+
return content.isEmpty || content == emptyGBParagraph
102+
}
86103
}

0 commit comments

Comments
 (0)