Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ final class CustomPostListViewModel: ObservableObject {
self.showsHierarchyIfApplicable = showsHierarchyIfApplicable
self.presentingViewController = presentingViewController

collection = service
collection =
service
.posts()
.createPostMetadataCollectionWithEditContext(
endpointType: details.toPostEndpointType(),
Expand All @@ -94,7 +95,8 @@ final class CustomPostListViewModel: ObservableObject {

withAnimation {
filter.author = author
collection = service
collection =
service
.posts()
.createPostMetadataCollectionWithEditContext(
endpointType: endpoint,
Expand Down Expand Up @@ -254,8 +256,9 @@ final class CustomPostListViewModel: ObservableObject {
}

if endpoint == .pages,
case .staticPage(let homepagePageID) = homepageSetting,
filter.statuses.contains(.publish) || filter.statuses.contains(.custom("any")) {
case .staticPage(let homepagePageID) = homepageSetting,
filter.statuses.contains(.publish) || filter.statuses.contains(.custom("any"))
{
items.markHomepage(id: homepagePageID)
}

Expand All @@ -276,9 +279,11 @@ final class CustomPostListViewModel: ObservableObject {
}

let entries = PageTree.buildHierarchy(from: posts)
let itemMap = Dictionary(uniqueKeysWithValues: items.compactMap { item -> (Int64, CustomPostCollectionItem)? in
return (item.id, item)
})
let itemMap = Dictionary(
uniqueKeysWithValues: items.compactMap { item -> (Int64, CustomPostCollectionItem)? in
(item.id, item)
}
)

indentationMap = Dictionary(uniqueKeysWithValues: entries.map { ($0.id, $0) })
self.items = entries.compactMap { itemMap[$0.id] }
Expand Down Expand Up @@ -397,28 +402,39 @@ final class CustomPostListViewModel: ObservableObject {
wpService: service
)
let viewModel = CustomPostSettingsViewModel(editorService: editorService, blog: blog, isStandalone: true)
viewModel.onEditorPostSaved = { [editorService] in
let postTitle = editorService.post?.title?.raw
let subtitle = (postTitle?.isEmpty == false) ? postTitle : nil
Notice(title: Strings.CustomPostNotice.postUpdated, message: subtitle, feedbackType: .success).post()
}
let settingsVC = PostSettingsViewController(viewModel: viewModel)
let nav = UINavigationController(rootViewController: settingsVC)
vc.present(nav, animated: true)
}
}

func menuNavigation(forBlaze post: AnyPostWithEditContext) -> PostMenuNavigation? {
guard endpoint == .posts
guard
endpoint == .posts
&& BlazeHelper.isBlazeFlagEnabled() && blog.canBlaze
&& post.status == .publish && (post.password ?? "").isEmpty else { return nil }
&& post.status == .publish && (post.password ?? "").isEmpty
else { return nil }
return .blaze(post: post)
}

func menuNavigation(forStats post: AnyPostWithEditContext) -> PostMenuNavigation? {
guard endpoint == .posts
&& blog.supports(.stats) && post.status == .publish else { return nil }
guard
endpoint == .posts
&& blog.supports(.stats) && post.status == .publish
else { return nil }
return .stats(post: post)
}

func menuNavigation(forComments post: AnyPostWithEditContext) -> PostMenuNavigation? {
guard details.supports.supports(feature: .comments)
&& post.status == .publish, let siteID = blog.dotComID else { return nil }
guard
details.supports.supports(feature: .comments)
&& post.status == .publish, let siteID = blog.dotComID
else { return nil }
return .comments(post: post, siteID: siteID)
}

Expand Down Expand Up @@ -551,7 +567,8 @@ struct CustomPostCollectionDisplayPost: Equatable {
self.date = entity.dateGmt
self.modifiedDate = entity.modifiedGmt
self.title = entity.title?.raw
let contentPreview = GutenbergExcerptGenerator
let contentPreview =
GutenbergExcerptGenerator
.firstParagraph(from: entity.content.rendered)
.replacingOccurrences(
of: "[\n]{2,}",
Expand Down Expand Up @@ -783,6 +800,27 @@ private enum Strings {
value: "Settings",
comment: "Menu action to open post settings"
)

/// Localized titles for the success notices emitted when a custom post is
/// persisted to the server. Shared between the editor and the standalone
/// settings sheet (from the Custom Posts list).
enum CustomPostNotice {
static let draftSaved = NSLocalizedString(
"customPost.notice.draftSaved",
value: "Draft saved",
comment: "Success notice shown after a new draft custom post is created on the server."
)
static let postUpdated = NSLocalizedString(
"customPost.notice.postUpdated",
value: "Post updated",
comment: "Success notice shown after an existing custom post is updated on the server."
)
static let postPublished = NSLocalizedString(
"customPost.notice.postPublished",
value: "Post published",
comment: "Success notice shown after a custom post is published on the server."
)
}
}

/// Represents the WordPress "Your homepage displays" setting.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ private extension CustomPostEditorViewController {
guard let self else { return }
switch result {
case .published:
let postTitle = editorService.post?.title?.raw
let subtitle = (postTitle?.isEmpty == false) ? postTitle : nil
Notice(title: Strings.CustomPostNotice.postPublished, message: subtitle, feedbackType: .success)
.post()
completion()
case .cancelled:
break
Expand All @@ -251,6 +255,12 @@ private extension CustomPostEditorViewController {
func save(publish: Bool) async {
SVProgressHUD.show()

// Capture whether this is a brand-new post (not yet on the server) so the
// notice can distinguish "Draft saved" (just created) from "Post updated"
// (edited an existing post). Any save of an already-existing post — draft
// or published — reads as an update from the user's perspective.
let isNewPost = post == nil

do {
let data = try await editorViewController.getTitleAndContent()
try await editorService.save(
Expand All @@ -260,6 +270,17 @@ private extension CustomPostEditorViewController {

dismissHUDWithSuccess()

let title: String
if publish {
title = Strings.CustomPostNotice.postPublished
} else if isNewPost {
title = Strings.CustomPostNotice.draftSaved
} else {
title = Strings.CustomPostNotice.postUpdated
}
let subtitle = data.title.isEmpty ? nil : data.title
Notice(title: title, message: subtitle, feedbackType: .success).post()

if publish {
completion()
}
Expand All @@ -280,3 +301,26 @@ extension CustomPostEditorViewController: CustomPostEditorServiceDelegate {
return EditorContent(title: result.title, content: result.content)
}
}

private enum Strings {
/// Localized titles for the success notices emitted when a custom post is
/// persisted to the server. Shared between the editor and the standalone
/// settings sheet (from the Custom Posts list).
enum CustomPostNotice {
static let draftSaved = NSLocalizedString(
"customPost.notice.draftSaved",
value: "Draft saved",
comment: "Success notice shown after a new draft custom post is created on the server."
)
static let postUpdated = NSLocalizedString(
"customPost.notice.postUpdated",
value: "Post updated",
comment: "Success notice shown after an existing custom post is updated on the server."
)
static let postPublished = NSLocalizedString(
"customPost.notice.postPublished",
value: "Post published",
comment: "Success notice shown after a custom post is published on the server."
)
}
}