diff --git a/Sources/WordPressData/Objective-C/AbstractPost.m b/Sources/WordPressData/Objective-C/AbstractPost.m index 78ed98857609..99d47e6eadb5 100644 --- a/Sources/WordPressData/Objective-C/AbstractPost.m +++ b/Sources/WordPressData/Objective-C/AbstractPost.m @@ -256,33 +256,11 @@ - (BOOL)shouldPublishImmediately return [self originalIsDraft] && [self dateCreatedIsNilOrEqualToDateModified]; } -- (NSString *)authorNameForDisplay -{ - return [self.author makePlainText]; -} - - (NSURL *)blogURL { return [NSURL URLWithString:self.blog.url]; } -- (NSString *)contentPreviewForDisplay -{ - return self.mt_excerpt; -} - -- (NSString *)dateStringForDisplay -{ - if ([self originalIsDraft] || [self.status isEqualToString:PostStatusPending]) { - return [[self dateModified] mediumString]; - } else if ([self isScheduled]) { - return [[self dateCreated] mediumStringWithTime]; - } else if ([self shouldPublishImmediately]) { - return NSLocalizedString(@"Publish Immediately",@"A short phrase indicating a post is due to be immedately published."); - } - return [[self dateCreated] mediumString]; -} - - (BOOL)isPrivateAtWPCom { return self.blog.isPrivateAtWPCom; diff --git a/Sources/WordPressData/Objective-C/BasePost.m b/Sources/WordPressData/Objective-C/BasePost.m index cd04500ae595..c8dbb15ab750 100644 --- a/Sources/WordPressData/Objective-C/BasePost.m +++ b/Sources/WordPressData/Objective-C/BasePost.m @@ -1,5 +1,6 @@ #import "BasePost.h" #import "Media.h" +#import "WordPressData-Swift.h" @import WordPressShared; @@ -20,16 +21,6 @@ @implementation BasePost @dynamic suggested_slug; @dynamic pathForDisplayImage; -- (NSDate *)dateCreated -{ - return self.date_created_gmt; -} - -- (void)setDateCreated:(NSDate *)localDate -{ - self.date_created_gmt = localDate; -} - - (BOOL)hasContent { BOOL titleIsEmpty = self.postTitle ? self.postTitle.isEmpty : YES; diff --git a/Sources/WordPressData/Objective-C/ReaderPost.m b/Sources/WordPressData/Objective-C/ReaderPost.m index 6f96cdfc508e..dfde53164d2f 100644 --- a/Sources/WordPressData/Objective-C/ReaderPost.m +++ b/Sources/WordPressData/Objective-C/ReaderPost.m @@ -234,26 +234,6 @@ + (NSString *)attributionTypeFromTaxonomies:(NSArray *)taxonomies return nil; } -- (BOOL)isCrossPost -{ - return self.crossPostMeta != nil; -} - -- (BOOL)isP2Type -{ - NSInteger orgID = [self.organizationID intValue]; - return orgID == SiteOrganizationTypeP2 || orgID == SiteOrganizationTypeAutomattic; -} - -- (NSString *)authorString -{ - if ([self.authorDisplayName length] > 0) { - return self.authorDisplayName; - } - - return self.author; -} - - (BOOL)contentIncludesFeaturedImage { NSURL *featuredImageURL = [self featuredImageURL]; @@ -279,35 +259,6 @@ - (BOOL)contentIncludesFeaturedImage return ([content rangeOfString:featuredImage].location != NSNotFound); } -- (SourceAttributionStyle)sourceAttributionStyle -{ - if ([self.sourceAttribution.attributionType isEqualToString:SourcePostAttribution.post]) { - return SourceAttributionStylePost; - } else if ([self.sourceAttribution.attributionType isEqualToString:SourcePostAttribution.site]) { - return SourceAttributionStyleSite; - } else { - return SourceAttributionStyleNone; - } -} - -- (NSString *)sourceAuthorNameForDisplay -{ - return self.sourceAttribution.authorName; -} - -- (NSURL *)sourceAvatarURLForDisplay -{ - if (!self.sourceAttribution) { - return nil; - } - return [NSURL URLWithString:self.sourceAttribution.avatarURL]; -} - -- (NSString *)sourceBlogNameForDisplay -{ - return self.sourceAttribution.blogName; -} - - (NSDictionary *)railcarDictionary { if (!self.railcar) { diff --git a/Sources/WordPressData/Objective-C/include/AbstractPost.h b/Sources/WordPressData/Objective-C/include/AbstractPost.h index 2ba249b030aa..9e45e0df765b 100644 --- a/Sources/WordPressData/Objective-C/include/AbstractPost.h +++ b/Sources/WordPressData/Objective-C/include/AbstractPost.h @@ -88,8 +88,6 @@ typedef NS_ENUM(NSUInteger, AbstractPostRemoteStatus) { #pragma mark - Conveniece Methods - (BOOL)shouldPublishImmediately; -- (NSString *)authorNameForDisplay; -- (NSString *)dateStringForDisplay; - (BOOL)isPrivateAtWPCom; diff --git a/Sources/WordPressData/Objective-C/include/BasePost.h b/Sources/WordPressData/Objective-C/include/BasePost.h index bb009eb6c456..079fccbef7ec 100644 --- a/Sources/WordPressData/Objective-C/include/BasePost.h +++ b/Sources/WordPressData/Objective-C/include/BasePost.h @@ -27,9 +27,6 @@ NS_ASSUME_NONNULL_BEGIN */ @property (nonatomic, strong, nullable) NSString *pathForDisplayImage; -//date conversion -@property (nonatomic, strong, nullable) NSDate * dateCreated; - // Returns true if title or content is non empty - (BOOL)hasContent; diff --git a/Sources/WordPressData/Objective-C/include/ReaderPost.h b/Sources/WordPressData/Objective-C/include/ReaderPost.h index 54873174bdd1..e380de41cda2 100644 --- a/Sources/WordPressData/Objective-C/include/ReaderPost.h +++ b/Sources/WordPressData/Objective-C/include/ReaderPost.h @@ -81,14 +81,7 @@ extern NSString * const ReaderPostStoredCommentTextKey; + (instancetype)createOrReplaceFromRemotePost:(RemoteReaderPost *)remotePost forTopic:(ReaderAbstractTopic *)topic context:(NSManagedObjectContext *) managedObjectContext; -- (BOOL)isCrossPost; -- (BOOL)isP2Type; -- (NSString *)authorString; - (BOOL)contentIncludesFeaturedImage; -- (SourceAttributionStyle)sourceAttributionStyle; -- (NSString *)sourceAuthorNameForDisplay; -- (NSURL *)sourceAvatarURLForDisplay; -- (NSString *)sourceBlogNameForDisplay; - (NSDictionary *)railcarDictionary; @end diff --git a/Sources/WordPressData/Swift/AbstractPost.swift b/Sources/WordPressData/Swift/AbstractPost.swift index 21fc427b55e1..f90e82db4dff 100644 --- a/Sources/WordPressData/Swift/AbstractPost.swift +++ b/Sources/WordPressData/Swift/AbstractPost.swift @@ -239,4 +239,19 @@ public extension AbstractPost { revision.deleteRevision() } } + + func dateStringForDisplay() -> String? { + if self.originalIsDraft() || self.status == .pending { + return dateModified?.toMediumString() + } else if self.isScheduled() { + return self.dateCreated?.mediumStringWithTime() + } else if self.shouldPublishImmediately() { + return NSLocalizedString("Publish Immediately", comment: "A short phrase indicating a post is due to be immedately published.") + } + return self.dateCreated?.toMediumString() + } + + override func contentPreviewForDisplay() -> String? { + mt_excerpt + } } diff --git a/Sources/WordPressData/Swift/BasePost.swift b/Sources/WordPressData/Swift/BasePost.swift index 5b1408327074..26cd64df7ebf 100644 --- a/Sources/WordPressData/Swift/BasePost.swift +++ b/Sources/WordPressData/Swift/BasePost.swift @@ -74,4 +74,13 @@ extension BasePost { return nil } + + @objc public var dateCreated: Date? { + get { + date_created_gmt + } + set { + date_created_gmt = newValue + } + } } diff --git a/Sources/WordPressData/Swift/ReaderPost+PostContentProvider.swift b/Sources/WordPressData/Swift/ReaderPost+PostContentProvider.swift index fcb52be036ad..6ef44aced1e8 100644 --- a/Sources/WordPressData/Swift/ReaderPost+PostContentProvider.swift +++ b/Sources/WordPressData/Swift/ReaderPost+PostContentProvider.swift @@ -2,6 +2,14 @@ import Foundation import WordPressShared extension ReaderPost { + public var isCrossPost: Bool { + crossPostMeta != nil + } + + public var isP2Type: Bool { + guard let id = organizationID?.intValue, let type = SiteOrganizationType(rawValue: id) else { return false } + return type == .p2 || type == .automattic + } @objc public override var featuredImageURL: URL? { if !self.featuredImage.isEmpty { @@ -38,7 +46,11 @@ extension ReaderPost { } public func authorForDisplay() -> String? { - return authorString() + if let name = self.authorDisplayName, !name.isEmpty { + return name + } + + return author } public func dateForDisplay() -> Date? { @@ -56,4 +68,31 @@ extension ReaderPost { public func avatarURLForDisplay() -> URL? { authorAvatarURL.flatMap(URL.init(string:)) } + + public func sourceAuthorNameForDisplay() -> String? { + sourceAttribution?.authorName + } + + public func sourceAttributionStyle() -> SourceAttributionStyle { + guard let sourceAttribution else { + return .none + } + + if sourceAttribution.attributionType == SourcePostAttribution.post { + return .post + } else if sourceAttribution.attributionType == SourcePostAttribution.site { + return .site + } + + return .none + } + + public func sourceAvatarURLForDisplay() -> URL? { + sourceAttribution?.avatarURL.flatMap(URL.init(string:)) + } + + public func sourceBlogNameForDisplay() -> String? { + return sourceAttribution?.blogName + } + } diff --git a/WordPress/Classes/Models/ReaderPost+Swift.swift b/WordPress/Classes/Models/ReaderPost+Swift.swift index 9b6fcc2d641c..b3d6d61f0a8a 100644 --- a/WordPress/Classes/Models/ReaderPost+Swift.swift +++ b/WordPress/Classes/Models/ReaderPost+Swift.swift @@ -13,7 +13,7 @@ extension ReaderPost { if canSubscribeComments { return true } - if isP2Type() { + if isP2Type { return !((topic as? ReaderSiteTopic)?.emailSubscription?.sendComments ?? false) } return false diff --git a/WordPress/Classes/ViewRelated/Post/PostSettings/PostSettingsViewModel.swift b/WordPress/Classes/ViewRelated/Post/PostSettings/PostSettingsViewModel.swift index 77c184761848..e92b5d5f121d 100644 --- a/WordPress/Classes/ViewRelated/Post/PostSettings/PostSettingsViewModel.swift +++ b/WordPress/Classes/ViewRelated/Post/PostSettings/PostSettingsViewModel.swift @@ -46,7 +46,7 @@ final class PostSettingsViewModel: NSObject, ObservableObject { } var authorDisplayName: String { - settings.author?.displayName ?? post.authorNameForDisplay() + settings.author?.displayName ?? post.author?.makePlainText() ?? "" } var authorAvatarURL: URL? { diff --git a/WordPress/Classes/ViewRelated/Post/Views/PostListItemViewModel.swift b/WordPress/Classes/ViewRelated/Post/Views/PostListItemViewModel.swift index 3859eb9d3eea..d7e8a1cbd287 100644 --- a/WordPress/Classes/ViewRelated/Post/Views/PostListItemViewModel.swift +++ b/WordPress/Classes/ViewRelated/Post/Views/PostListItemViewModel.swift @@ -28,7 +28,7 @@ final class PostListItemViewModel { private func makeAccessibilityLabel(for post: Post, statusViewModel: PostCardStatusViewModel) -> String? { let titleAndDateChunk: String = { - return String(format: Strings.Accessibility.titleAndDateChunkFormat, post.titleForDisplay(), post.dateStringForDisplay()) + return String(format: Strings.Accessibility.titleAndDateChunkFormat, post.titleForDisplay(), post.dateStringForDisplay() ?? "") }() let authorChunk: String? = { diff --git a/WordPress/Classes/ViewRelated/Reader/Controllers/ReaderStreamViewController.swift b/WordPress/Classes/ViewRelated/Reader/Controllers/ReaderStreamViewController.swift index c4676169699c..1e5148a2759b 100644 --- a/WordPress/Classes/ViewRelated/Reader/Controllers/ReaderStreamViewController.swift +++ b/WordPress/Classes/ViewRelated/Reader/Controllers/ReaderStreamViewController.swift @@ -1329,7 +1329,7 @@ extension ReaderStreamViewController: WPTableViewHandlerDelegate { return cell } - if post.isCross() { + if post.isCrossPost { let cell = tableConfiguration.crossPostCell(tableView) cell.isCompact = isCompact cell.isSeparatorHidden = !showsSeparator diff --git a/WordPress/Classes/ViewRelated/Reader/Detail/ReaderDetailCoordinator.swift b/WordPress/Classes/ViewRelated/Reader/Detail/ReaderDetailCoordinator.swift index 1c31b20caa81..bc20c638dcee 100644 --- a/WordPress/Classes/ViewRelated/Reader/Detail/ReaderDetailCoordinator.swift +++ b/WordPress/Classes/ViewRelated/Reader/Detail/ReaderDetailCoordinator.swift @@ -554,7 +554,7 @@ class ReaderDetailCoordinator { let characterSet = CharacterSet(charactersIn: "/") guard let post, - post.isCross(), + post.isCrossPost, let crossPostMeta = post.crossPostMeta, let crossPostURL = URL(string: crossPostMeta.postURL.trimmingCharacters(in: characterSet)), let selectedURL = URL(string: url.absoluteString.trimmingCharacters(in: characterSet)) else { diff --git a/WordPress/Classes/ViewRelated/Reader/Detail/ReaderDetailViewController.swift b/WordPress/Classes/ViewRelated/Reader/Detail/ReaderDetailViewController.swift index 30a06cd767b1..fa815c1bd501 100644 --- a/WordPress/Classes/ViewRelated/Reader/Detail/ReaderDetailViewController.swift +++ b/WordPress/Classes/ViewRelated/Reader/Detail/ReaderDetailViewController.swift @@ -274,7 +274,7 @@ class ReaderDetailViewController: UIViewController, ReaderDetailView { webView.postURL = postURL } - webView.isP2 = post.isP2Type() + webView.isP2 = post.isP2Type if post.content?.hasSuffix("[…]") == true { let viewMoreView = ReaderReadMoreView(post: post) @@ -871,7 +871,7 @@ class ReaderDetailViewController: UIViewController, ReaderDetailView { post.sourceAttribution.postID != nil && post.sourceAttribution.blogID != nil { return ReaderDetailViewController.controllerWithPostID(post.sourceAttribution.postID!, siteID: post.sourceAttribution.blogID!) - } else if post.isCross() { + } else if post.isCrossPost { return ReaderDetailViewController.controllerWithPostID(post.crossPostMeta.postID, siteID: post.crossPostMeta.siteID) } else { let controller = ReaderDetailViewController.loadFromStoryboard()