Skip to content

Commit 6a66f68

Browse files
authored
Translate AbstractPost to Swift (#25040)
* Move AbstractPost.isScheduled to Swift * Move AbstractPost.isDraft to Swift * Move AbstractPost.isPublished to Swift * Move AbstractPost.originalIsDraft to Swift * Move AbstractPost.shouldPublishImmediately to Swift * Move AbstractPost.hasPhoto and hasVideo to Swift * Move AbstractPost.hasRemote to Swift * Remove unused function AbstractPost.blogURL * Remove AbstractPost.isPrivateAtWPCom * Move AbstractPost.parsedOtherTerms to Swift * Move AbstractPost.updatePathForDisplayImageBasedOnContent to Swift * Remove unused function `AbstractPost.save` * Move a couple of methods into a private extension * Remove unneeded `@objc` declarations * Remove a few helpers
1 parent 537a9d2 commit 6a66f68

File tree

9 files changed

+97
-197
lines changed

9 files changed

+97
-197
lines changed

Sources/WordPressData/Objective-C/AbstractPost.m

Lines changed: 0 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ @implementation AbstractPost
2828
@dynamic permalinkTemplateURL;
2929
@synthesize voiceContent;
3030

31-
#pragma mark - Life Cycle Methods
32-
33-
- (void)save
34-
{
35-
[[ContextManager sharedInstance] saveContext:self.managedObjectContext];
36-
}
37-
3831
#pragma mark -
3932
#pragma mark Revision management
4033

@@ -58,45 +51,6 @@ - (AbstractPost *)original
5851

5952
#pragma mark - Helpers
6053

61-
- (BOOL)dateCreatedIsNilOrEqualToDateModified
62-
{
63-
return self.date_created_gmt == nil || [self.date_created_gmt isEqualToDate:self.dateModified];
64-
}
65-
66-
- (BOOL)hasPhoto
67-
{
68-
if ([self.media count] == 0) {
69-
return NO;
70-
}
71-
72-
if (self.featuredImage != nil) {
73-
return YES;
74-
}
75-
76-
for (Media *media in self.media) {
77-
if (media.mediaType == MediaTypeImage) {
78-
return YES;
79-
}
80-
}
81-
82-
return NO;
83-
}
84-
85-
- (BOOL)hasVideo
86-
{
87-
if ([self.media count] == 0) {
88-
return NO;
89-
}
90-
91-
for (Media *media in self.media) {
92-
if (media.mediaType == MediaTypeVideo) {
93-
return YES;
94-
}
95-
}
96-
97-
return NO;
98-
}
99-
10054
- (BOOL)hasCategories
10155
{
10256
return NO;
@@ -107,93 +61,4 @@ - (BOOL)hasTags
10761
return NO;
10862
}
10963

110-
- (BOOL)hasRemote
111-
{
112-
return ((self.postID != nil) && ([self.postID longLongValue] > 0));
113-
}
114-
115-
#pragma mark - Convenience methods
116-
117-
// If the post has a scheduled status.
118-
- (BOOL)isScheduled
119-
{
120-
return ([self.status isEqualToString:PostStatusScheduled]);
121-
}
122-
123-
- (BOOL)isDraft
124-
{
125-
return [self.status isEqualToString:PostStatusDraft];
126-
}
127-
128-
- (BOOL)isPublished
129-
{
130-
return [self.status isEqualToString:PostStatusPublish];
131-
}
132-
133-
- (BOOL)originalIsDraft
134-
{
135-
if ([self.status isEqualToString:PostStatusDraft]) {
136-
return YES;
137-
} else if (self.isRevision && [self.original.status isEqualToString:PostStatusDraft]) {
138-
return YES;
139-
}
140-
return NO;
141-
}
142-
143-
- (BOOL)shouldPublishImmediately
144-
{
145-
/// - warning: Yes, this is WordPress logic and it matches the behavior on
146-
/// the web. If `dateCreated` is the same as `dateModified`, the system
147-
/// uses it to represent a "no publish date selected" scenario.
148-
return [self originalIsDraft] && [self dateCreatedIsNilOrEqualToDateModified];
149-
}
150-
151-
- (NSURL *)blogURL
152-
{
153-
return [NSURL URLWithString:self.blog.url];
154-
}
155-
156-
- (BOOL)isPrivateAtWPCom
157-
{
158-
return self.blog.isPrivateAtWPCom;
159-
}
160-
161-
#pragma mark - Post
162-
163-
- (void)updatePathForDisplayImageBasedOnContent
164-
{
165-
// First lets check the post content for a suitable image
166-
NSString *result = [DisplayableImageHelper searchPostContentForImageToDisplay:self.content];
167-
if (result.length > 0) {
168-
self.pathForDisplayImage = result;
169-
}
170-
// If none found let's see if some galleries are available
171-
NSSet *mediaIDs = [DisplayableImageHelper searchPostContentForAttachmentIdsInGalleries:self.content];
172-
for (Media *media in self.blog.media) {
173-
NSNumber *mediaID = media.mediaID;
174-
if (mediaID && [mediaIDs containsObject:mediaID]) {
175-
result = media.remoteURL;
176-
}
177-
}
178-
self.pathForDisplayImage = result;
179-
}
180-
181-
- (void)setParsedOtherTerms:(NSDictionary<NSString *, NSArray<NSString *> *> *)data
182-
{
183-
if (data == nil) {
184-
self.rawOtherTerms = nil;
185-
} else {
186-
self.rawOtherTerms = [NSJSONSerialization dataWithJSONObject:data options:0 error:nil];
187-
}
188-
}
189-
190-
- (NSDictionary<NSString *, NSArray<NSString *> *> *)parseOtherTerms
191-
{
192-
if (self.rawOtherTerms == nil) {
193-
return [NSDictionary dictionary];
194-
}
195-
196-
return [NSJSONSerialization JSONObjectWithData:self.rawOtherTerms options:0 error:nil] ?: [NSDictionary dictionary];
197-
}
198-
19964
@end

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

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

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

65-
- (BOOL)hasPhoto;
66-
- (BOOL)hasVideo;
6765
- (BOOL)hasCategories;
6866
- (BOOL)hasTags;
6967

70-
71-
#pragma mark - Conveniece Methods
72-
- (BOOL)shouldPublishImmediately;
73-
- (BOOL)isPrivateAtWPCom;
74-
75-
76-
#pragma mark - Unsaved Changes
77-
78-
/**
79-
Returns YES if the post is has a `future` post status
80-
*/
81-
- (BOOL)isScheduled;
82-
83-
/**
84-
Returns YES if the post is a draft
85-
*/
86-
- (BOOL)isDraft;
87-
88-
/**
89-
Returns YES if the post is a published.
90-
*/
91-
- (BOOL)isPublished;
92-
93-
/**
94-
Returns YES if the original post is a draft
95-
*/
96-
- (BOOL)originalIsDraft;
97-
98-
// Does the post exist on the blog?
99-
- (BOOL)hasRemote;
100-
101-
// Save changes to disk
102-
- (void)save;
103-
104-
/**
105-
* Updates the path for the display image by looking at the post content and trying to find an good image to use.
106-
* If no appropiated image is found the path is set to nil.
107-
*/
108-
- (void)updatePathForDisplayImageBasedOnContent;
109-
110-
- (void)setParsedOtherTerms:(NSDictionary<NSString *, NSArray<NSString *> *> *)data;
111-
- (NSDictionary<NSString *, NSArray<NSString *> *> *)parseOtherTerms;
112-
11368
@end
11469

11570
@interface AbstractPost (CoreDataGeneratedAccessors)

Sources/WordPressData/Swift/AbstractPost.swift

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public extension AbstractPost {
4444
statuses.contains(status ?? .draft)
4545
}
4646

47-
@objc
4847
var remoteStatus: AbstractPostRemoteStatus {
4948
get {
5049
guard let remoteStatusNumber = remoteStatusNumber?.uintValue,
@@ -72,7 +71,6 @@ public extension AbstractPost {
7271
///
7372
/// - returns: The localized title for the specified status, or the status if a title was not found.
7473
///
75-
@objc
7674
static func title(forStatus status: String) -> String {
7775
switch status {
7876
case PostStatusDraft:
@@ -243,7 +241,7 @@ public extension AbstractPost {
243241
func dateStringForDisplay() -> String? {
244242
if self.originalIsDraft() || self.status == .pending {
245243
return dateModified?.toMediumString()
246-
} else if self.isScheduled() {
244+
} else if self.status == .scheduled {
247245
return self.dateCreated?.mediumStringWithTime()
248246
} else if self.shouldPublishImmediately() {
249247
return NSLocalizedString("Publish Immediately", comment: "A short phrase indicating a post is due to be immedately published.")
@@ -274,7 +272,6 @@ public extension AbstractPost {
274272
}
275273
}
276274

277-
@objc
278275
func createRevision() -> AbstractPost {
279276
precondition(managedObjectContext != nil)
280277
precondition(revision == nil, "This post must not already have a revision")
@@ -287,7 +284,6 @@ public extension AbstractPost {
287284
return post
288285
}
289286

290-
@objc
291287
func deleteRevision() {
292288
guard let revision, let context = managedObjectContext else {
293289
return
@@ -301,31 +297,93 @@ public extension AbstractPost {
301297
}
302298
}
303299

304-
@objc
305300
func applyRevision() {
306301
guard isOriginal(), let revision else {
307302
return
308303
}
309304
clone(from: revision)
310305
}
311306

312-
@objc
313307
func isRevision() -> Bool {
314308
!isOriginal()
315309
}
316310

317-
@objc
318311
func isOriginal() -> Bool {
319312
original == nil
320313
}
321314

322-
@objc
323315
func latest() -> AbstractPost {
324316
revision?.latest() ?? self
325317
}
326318

327-
@objc
328319
func hasRevision() -> Bool {
329320
revision != nil
330321
}
322+
323+
/// Returns YES if the original post is a draft
324+
func originalIsDraft() -> Bool {
325+
if status == .draft {
326+
return true
327+
} else if isRevision(), original?.status == .draft {
328+
return true
329+
}
330+
return false
331+
}
332+
333+
func shouldPublishImmediately() -> Bool {
334+
// - warning: Yes, this is WordPress logic and it matches the behavior on
335+
// the web. If `dateCreated` is the same as `dateModified`, the system
336+
// uses it to represent a "no publish date selected" scenario.
337+
originalIsDraft() && (date_created_gmt == nil || date_created_gmt == dateModified)
338+
}
339+
340+
/// Does the post exist on the blog?
341+
func hasRemote() -> Bool {
342+
(postID?.int64Value ?? 0) > 0
343+
}
344+
345+
@objc
346+
var parsedOtherTerms: [String: [String]] {
347+
get {
348+
guard let rawOtherTerms else {
349+
return [:]
350+
}
351+
352+
return (try? JSONSerialization.jsonObject(with: rawOtherTerms) as? [String: [String]]) ?? [:]
353+
}
354+
set {
355+
rawOtherTerms = try? JSONSerialization.data(withJSONObject: newValue)
356+
}
357+
}
358+
359+
/// Updates the path for the display image by looking at the post content and trying to find an good image to use.
360+
/// If no appropiated image is found the path is set to nil.
361+
@objc
362+
func updatePathForDisplayImageBasedOnContent() {
363+
guard let content else {
364+
return
365+
}
366+
367+
if let result = DisplayableImageHelper.searchPostContentForImage(toDisplay: content), !result.isEmpty {
368+
pathForDisplayImage = result
369+
return
370+
}
371+
372+
guard let allMedia = blog.media, !allMedia.isEmpty else { return }
373+
374+
let mediaIDs = DisplayableImageHelper.searchPostContentForAttachmentIds(inGalleries: content) as? Set<NSNumber> ?? []
375+
for media in allMedia {
376+
guard let media = media as? Media else { continue }
377+
378+
guard let mediaID = media.mediaID,
379+
mediaIDs.contains(mediaID) else {
380+
continue
381+
}
382+
383+
if let remoteURL = media.remoteURL {
384+
pathForDisplayImage = remoteURL
385+
break
386+
}
387+
}
388+
}
331389
}

Sources/WordPressData/Swift/RemotePostCreateParameters+Helpers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extension RemotePostCreateParameters {
2020
excerpt = post.mt_excerpt
2121
slug = post.wp_slug
2222
featuredImageID = post.featuredImage?.mediaID?.intValue
23-
otherTerms = post.parseOtherTerms()
23+
otherTerms = post.parsedOtherTerms
2424
switch post {
2525
case let page as Page:
2626
parentPageID = page.parentID?.intValue

WordPress/Classes/ViewRelated/Gutenberg/EditorMediaUtility.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class EditorMediaUtility {
139139
let requestURL: URL
140140
if url.isFileURL {
141141
requestURL = url
142-
} else if post.isPrivateAtWPCom() && url.isHostedAtWPCom {
142+
} else if post.blog.isPrivateAtWPCom() && url.isHostedAtWPCom {
143143
// private wpcom image needs special handling.
144144
// the size that WPImageHelper expects is pixel size
145145
size.width = size.width * scale

WordPress/Classes/ViewRelated/Post/EditPostViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class EditPostViewController: UIViewController {
241241
}
242242
self.afterDismiss?()
243243
guard let post = self.post?.getOriginal(),
244-
post.isPublished(),
244+
post.status == .publish,
245245
!self.editingExistingPost,
246246
let controller = presentingController else {
247247
return

0 commit comments

Comments
 (0)