Skip to content

Commit 1d5260c

Browse files
authored
Move some functions in post models to Swift (#25003)
* Move BasePost.dateCreated from Objective-C to Swift * Translate AbstractPost.dateStringForDisplay to Swift * Remove AbstractPost.authorNameForDisplay * Move AbstractPost.contentPreviewForDisplay to Swift * Move ReaderPost.isCrossPost to Swift * Move ReaderPost.isP2Type to Swift * Remove ReaderPost.authorString * Move ReaderPost.sourceAttributionStyle * Move a few simple functions in ReaderPost to Swift
1 parent 79b9d30 commit 1d5260c

File tree

15 files changed

+72
-101
lines changed

15 files changed

+72
-101
lines changed

Sources/WordPressData/Objective-C/AbstractPost.m

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -256,33 +256,11 @@ - (BOOL)shouldPublishImmediately
256256
return [self originalIsDraft] && [self dateCreatedIsNilOrEqualToDateModified];
257257
}
258258

259-
- (NSString *)authorNameForDisplay
260-
{
261-
return [self.author makePlainText];
262-
}
263-
264259
- (NSURL *)blogURL
265260
{
266261
return [NSURL URLWithString:self.blog.url];
267262
}
268263

269-
- (NSString *)contentPreviewForDisplay
270-
{
271-
return self.mt_excerpt;
272-
}
273-
274-
- (NSString *)dateStringForDisplay
275-
{
276-
if ([self originalIsDraft] || [self.status isEqualToString:PostStatusPending]) {
277-
return [[self dateModified] mediumString];
278-
} else if ([self isScheduled]) {
279-
return [[self dateCreated] mediumStringWithTime];
280-
} else if ([self shouldPublishImmediately]) {
281-
return NSLocalizedString(@"Publish Immediately",@"A short phrase indicating a post is due to be immedately published.");
282-
}
283-
return [[self dateCreated] mediumString];
284-
}
285-
286264
- (BOOL)isPrivateAtWPCom
287265
{
288266
return self.blog.isPrivateAtWPCom;

Sources/WordPressData/Objective-C/BasePost.m

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#import "BasePost.h"
22
#import "Media.h"
3+
#import "WordPressData-Swift.h"
34

45
@import WordPressShared;
56

@@ -20,16 +21,6 @@ @implementation BasePost
2021
@dynamic suggested_slug;
2122
@dynamic pathForDisplayImage;
2223

23-
- (NSDate *)dateCreated
24-
{
25-
return self.date_created_gmt;
26-
}
27-
28-
- (void)setDateCreated:(NSDate *)localDate
29-
{
30-
self.date_created_gmt = localDate;
31-
}
32-
3324
- (BOOL)hasContent
3425
{
3526
BOOL titleIsEmpty = self.postTitle ? self.postTitle.isEmpty : YES;

Sources/WordPressData/Objective-C/ReaderPost.m

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -234,26 +234,6 @@ + (NSString *)attributionTypeFromTaxonomies:(NSArray *)taxonomies
234234
return nil;
235235
}
236236

237-
- (BOOL)isCrossPost
238-
{
239-
return self.crossPostMeta != nil;
240-
}
241-
242-
- (BOOL)isP2Type
243-
{
244-
NSInteger orgID = [self.organizationID intValue];
245-
return orgID == SiteOrganizationTypeP2 || orgID == SiteOrganizationTypeAutomattic;
246-
}
247-
248-
- (NSString *)authorString
249-
{
250-
if ([self.authorDisplayName length] > 0) {
251-
return self.authorDisplayName;
252-
}
253-
254-
return self.author;
255-
}
256-
257237
- (BOOL)contentIncludesFeaturedImage
258238
{
259239
NSURL *featuredImageURL = [self featuredImageURL];
@@ -279,35 +259,6 @@ - (BOOL)contentIncludesFeaturedImage
279259
return ([content rangeOfString:featuredImage].location != NSNotFound);
280260
}
281261

282-
- (SourceAttributionStyle)sourceAttributionStyle
283-
{
284-
if ([self.sourceAttribution.attributionType isEqualToString:SourcePostAttribution.post]) {
285-
return SourceAttributionStylePost;
286-
} else if ([self.sourceAttribution.attributionType isEqualToString:SourcePostAttribution.site]) {
287-
return SourceAttributionStyleSite;
288-
} else {
289-
return SourceAttributionStyleNone;
290-
}
291-
}
292-
293-
- (NSString *)sourceAuthorNameForDisplay
294-
{
295-
return self.sourceAttribution.authorName;
296-
}
297-
298-
- (NSURL *)sourceAvatarURLForDisplay
299-
{
300-
if (!self.sourceAttribution) {
301-
return nil;
302-
}
303-
return [NSURL URLWithString:self.sourceAttribution.avatarURL];
304-
}
305-
306-
- (NSString *)sourceBlogNameForDisplay
307-
{
308-
return self.sourceAttribution.blogName;
309-
}
310-
311262
- (NSDictionary *)railcarDictionary
312263
{
313264
if (!self.railcar) {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ typedef NS_ENUM(NSUInteger, AbstractPostRemoteStatus) {
8888

8989
#pragma mark - Conveniece Methods
9090
- (BOOL)shouldPublishImmediately;
91-
- (NSString *)authorNameForDisplay;
92-
- (NSString *)dateStringForDisplay;
9391
- (BOOL)isPrivateAtWPCom;
9492

9593

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

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

30-
//date conversion
31-
@property (nonatomic, strong, nullable) NSDate * dateCreated;
32-
3330
// Returns true if title or content is non empty
3431
- (BOOL)hasContent;
3532

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,7 @@ extern NSString * const ReaderPostStoredCommentTextKey;
8181

8282
+ (instancetype)createOrReplaceFromRemotePost:(RemoteReaderPost *)remotePost forTopic:(ReaderAbstractTopic *)topic context:(NSManagedObjectContext *) managedObjectContext;
8383

84-
- (BOOL)isCrossPost;
85-
- (BOOL)isP2Type;
86-
- (NSString *)authorString;
8784
- (BOOL)contentIncludesFeaturedImage;
88-
- (SourceAttributionStyle)sourceAttributionStyle;
89-
- (NSString *)sourceAuthorNameForDisplay;
90-
- (NSURL *)sourceAvatarURLForDisplay;
91-
- (NSString *)sourceBlogNameForDisplay;
9285
- (NSDictionary *)railcarDictionary;
9386

9487
@end

Sources/WordPressData/Swift/AbstractPost.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,19 @@ public extension AbstractPost {
239239
revision.deleteRevision()
240240
}
241241
}
242+
243+
func dateStringForDisplay() -> String? {
244+
if self.originalIsDraft() || self.status == .pending {
245+
return dateModified?.toMediumString()
246+
} else if self.isScheduled() {
247+
return self.dateCreated?.mediumStringWithTime()
248+
} else if self.shouldPublishImmediately() {
249+
return NSLocalizedString("Publish Immediately", comment: "A short phrase indicating a post is due to be immedately published.")
250+
}
251+
return self.dateCreated?.toMediumString()
252+
}
253+
254+
override func contentPreviewForDisplay() -> String? {
255+
mt_excerpt
256+
}
242257
}

Sources/WordPressData/Swift/BasePost.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,13 @@ extension BasePost {
7474

7575
return nil
7676
}
77+
78+
@objc public var dateCreated: Date? {
79+
get {
80+
date_created_gmt
81+
}
82+
set {
83+
date_created_gmt = newValue
84+
}
85+
}
7786
}

Sources/WordPressData/Swift/ReaderPost+PostContentProvider.swift

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import Foundation
22
import WordPressShared
33

44
extension ReaderPost {
5+
public var isCrossPost: Bool {
6+
crossPostMeta != nil
7+
}
8+
9+
public var isP2Type: Bool {
10+
guard let id = organizationID?.intValue, let type = SiteOrganizationType(rawValue: id) else { return false }
11+
return type == .p2 || type == .automattic
12+
}
513

614
@objc public override var featuredImageURL: URL? {
715
if !self.featuredImage.isEmpty {
@@ -38,7 +46,11 @@ extension ReaderPost {
3846
}
3947

4048
public func authorForDisplay() -> String? {
41-
return authorString()
49+
if let name = self.authorDisplayName, !name.isEmpty {
50+
return name
51+
}
52+
53+
return author
4254
}
4355

4456
public func dateForDisplay() -> Date? {
@@ -56,4 +68,31 @@ extension ReaderPost {
5668
public func avatarURLForDisplay() -> URL? {
5769
authorAvatarURL.flatMap(URL.init(string:))
5870
}
71+
72+
public func sourceAuthorNameForDisplay() -> String? {
73+
sourceAttribution?.authorName
74+
}
75+
76+
public func sourceAttributionStyle() -> SourceAttributionStyle {
77+
guard let sourceAttribution else {
78+
return .none
79+
}
80+
81+
if sourceAttribution.attributionType == SourcePostAttribution.post {
82+
return .post
83+
} else if sourceAttribution.attributionType == SourcePostAttribution.site {
84+
return .site
85+
}
86+
87+
return .none
88+
}
89+
90+
public func sourceAvatarURLForDisplay() -> URL? {
91+
sourceAttribution?.avatarURL.flatMap(URL.init(string:))
92+
}
93+
94+
public func sourceBlogNameForDisplay() -> String? {
95+
return sourceAttribution?.blogName
96+
}
97+
5998
}

WordPress/Classes/Models/ReaderPost+Swift.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extension ReaderPost {
1313
if canSubscribeComments {
1414
return true
1515
}
16-
if isP2Type() {
16+
if isP2Type {
1717
return !((topic as? ReaderSiteTopic)?.emailSubscription?.sendComments ?? false)
1818
}
1919
return false

0 commit comments

Comments
 (0)