Skip to content

Commit 18dd66a

Browse files
authored
Custom Posts: Show "Post updated" notice (#25576)
* Show success notice when a custom post is saved to the site Post a Notice toast on every HTTP write to the site from the Custom Post editor (save draft, update, publish) and from the standalone settings sheet reached from the Custom Posts list. The notice's title reflects the action: "Draft saved" only on the first save of a brand-new post, "Post updated" for any save of an existing post, and "Post published" via the publishing sheet. The post title is the notice's subtitle (omitted when empty). Localized strings live in a shared CustomPostNotice enum. * Reformat PublishPostViewController with swift-format * Show the published notice for both custom post publishing flows Move the success notice into PublishPostViewController.show(editorService:blog:from:completion:) so both the custom post editor and the Custom Posts list show it after publishing through the sheet. Consolidate the duplicated notice strings into a shared CustomPostNoticeStrings type. * Remove the unused publish argument from CustomPostEditorViewController.save The method is only ever called to persist a draft or update, so drop the always-false publish parameter and the dead publish-only branches.
1 parent 9758ecf commit 18dd66a

4 files changed

Lines changed: 125 additions & 30 deletions

File tree

WordPress/Classes/ViewRelated/CustomPostTypes/CustomPostListViewModel.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,11 @@ final class CustomPostListViewModel: ObservableObject {
417417
wpService: service
418418
)
419419
let viewModel = CustomPostSettingsViewModel(editorService: editorService, blog: blog, isStandalone: true)
420+
viewModel.onEditorPostSaved = { [editorService] in
421+
let postTitle = editorService.post?.title?.raw
422+
let subtitle = (postTitle?.isEmpty == false) ? postTitle : nil
423+
Notice(title: CustomPostNoticeStrings.postUpdated, message: subtitle, feedbackType: .success).post()
424+
}
420425
let settingsVC = PostSettingsViewController(viewModel: viewModel)
421426
let nav = UINavigationController(rootViewController: settingsVC)
422427
vc.present(nav, animated: true)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Foundation
2+
3+
/// Localized titles for the success notices emitted when a custom post is
4+
/// persisted to the server. Shared between the editor, the publishing sheet,
5+
/// and the Custom Posts list.
6+
enum CustomPostNoticeStrings {
7+
static let draftSaved = NSLocalizedString(
8+
"customPost.notice.draftSaved",
9+
value: "Draft saved",
10+
comment: "Success notice shown after a new draft custom post is created on the server."
11+
)
12+
static let postUpdated = NSLocalizedString(
13+
"customPost.notice.postUpdated",
14+
value: "Post updated",
15+
comment: "Success notice shown after an existing custom post is updated on the server."
16+
)
17+
static let postPublished = NSLocalizedString(
18+
"customPost.notice.postPublished",
19+
value: "Post published",
20+
comment: "Success notice shown after a custom post is published on the server."
21+
)
22+
}

WordPress/Classes/ViewRelated/NewGutenberg/CustomPostEditorViewController.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private extension CustomPostEditorViewController {
143143
style: .default,
144144
handler: { [weak self] _ in
145145
Task {
146-
await self?.save(publish: false)
146+
await self?.save()
147147
}
148148
}
149149
)
@@ -170,7 +170,7 @@ private extension CustomPostEditorViewController {
170170
attributes: enabled ? [] : [.disabled]
171171
) { [weak self] _ in
172172
Task {
173-
await self?.save(publish: false)
173+
await self?.save()
174174
}
175175
}
176176
resolve([saveDraft])
@@ -197,7 +197,7 @@ private extension CustomPostEditorViewController {
197197
} else {
198198
return UIAction(title: PostEditorStrings.update) { [weak self] _ in
199199
Task {
200-
await self?.save(publish: false)
200+
await self?.save()
201201
}
202202
}
203203
}
@@ -228,12 +228,8 @@ private extension CustomPostEditorViewController {
228228
blog: blog,
229229
from: self,
230230
completion: { [weak self] result in
231-
guard let self else { return }
232-
switch result {
233-
case .published:
234-
completion()
235-
case .cancelled:
236-
break
231+
if case .published = result {
232+
self?.completion()
237233
}
238234
}
239235
)
@@ -248,21 +244,27 @@ private extension CustomPostEditorViewController {
248244

249245
private extension CustomPostEditorViewController {
250246

251-
func save(publish: Bool) async {
247+
func save() async {
252248
SVProgressHUD.show()
253249

250+
// Capture whether this is a brand-new post (not yet on the server) so the
251+
// notice can distinguish "Draft saved" (just created) from "Post updated"
252+
// (edited an existing post). Any save of an already-existing post (draft
253+
// or published) reads as an update from the user's perspective.
254+
let isNewPost = post == nil
255+
254256
do {
255257
let data = try await editorViewController.getTitleAndContent()
256258
try await editorService.save(
257259
content: EditorContent(title: data.title, content: data.content),
258-
publish: publish
260+
publish: false
259261
)
260262

261263
dismissHUDWithSuccess()
262264

263-
if publish {
264-
completion()
265-
}
265+
let title = isNewPost ? CustomPostNoticeStrings.draftSaved : CustomPostNoticeStrings.postUpdated
266+
let subtitle = data.title.isEmpty ? nil : data.title
267+
Notice(title: title, message: subtitle, feedbackType: .success).post()
266268
} catch {
267269
SVProgressHUD.showError(withStatus: error.localizedDescription)
268270
}

WordPress/Classes/ViewRelated/Post/Publishing/PublishPostViewController.swift

Lines changed: 82 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,14 @@ final class PublishPostViewController<ViewModel: PostSettingsViewModelProtocol>:
8181
editorService: editorService,
8282
blog: blog
8383
)
84-
publishVC.onCompletion = completion
84+
publishVC.onCompletion = { result in
85+
if case .published = result {
86+
let postTitle = editorService.post?.title?.raw
87+
let subtitle = (postTitle?.isEmpty == false) ? postTitle : nil
88+
Notice(title: CustomPostNoticeStrings.postPublished, message: subtitle, feedbackType: .success).post()
89+
}
90+
completion(result)
91+
}
8592
let navigationVC = UINavigationController(rootViewController: publishVC)
8693
navigationVC.sheetPresentationController?.detents = [
8794
.custom(identifier: .medium, resolver: { _ in 526 }),
@@ -221,23 +228,82 @@ private typealias Strings = PrepublishingSheetStrings
221228

222229
enum PrepublishingSheetStrings {
223230
static let title = NSLocalizedString("prepublishing.title", value: "Publishing", comment: "Navigation title")
224-
static let publishingTo = NSLocalizedString("prepublishing.publishingTo", value: "Publishing to", comment: "Label in the header in the pre-publishing sheet")
225-
static let publish = NSLocalizedString("prepublishing.publish", value: "Publish", comment: "Primary button label in the pre-publishing sheet")
226-
static let schedule = NSLocalizedString("prepublishing.schedule", value: "Schedule", comment: "Primary button label in the pre-publishing shee")
227-
static let publishDate = NSLocalizedString("prepublishing.publishDate", value: "Publish Date", comment: "Label for a cell in the pre-publishing sheet")
228-
static let visibility = NSLocalizedString("prepublishing.visibility", value: "Visibility", comment: "Label for a cell in the pre-publishing sheet")
229-
static let categories = NSLocalizedString("prepublishing.categories", value: "Categories", comment: "Label for a cell in the pre-publishing sheet")
230-
static let tags = NSLocalizedString("prepublishing.tags", value: "Tags", comment: "Label for a cell in the pre-publishing sheet")
231-
static let jetpackSocial = NSLocalizedString("prepublishing.jetpackSocial", value: "Jetpack Social", comment: "Label for a cell in the pre-publishing sheet")
232-
static let immediately = NSLocalizedString("prepublishing.publishDateImmediately", value: "Immediately", comment: "Placeholder value for a publishing date in the prepublishing sheet when the date is not selected")
233-
static let uploadingMedia = NSLocalizedString("prepublishing.uploadingMedia", value: "Uploading media", comment: "Title for a publish button state in the pre-publishing sheet")
234-
private static let uploadMediaOneItemRemaining = NSLocalizedString("prepublishing.uploadMediaOneItemRemaining", value: "%@ item remaining", comment: "Details label for a publish button state in the pre-publishing sheet")
235-
private static let uploadMediaManyItemsRemaining = NSLocalizedString("prepublishing.uploadMediaManyItemsRemaining", value: "%@ items remaining", comment: "Details label for a publish button state in the pre-publishing sheet")
231+
static let publishingTo = NSLocalizedString(
232+
"prepublishing.publishingTo",
233+
value: "Publishing to",
234+
comment: "Label in the header in the pre-publishing sheet"
235+
)
236+
static let publish = NSLocalizedString(
237+
"prepublishing.publish",
238+
value: "Publish",
239+
comment: "Primary button label in the pre-publishing sheet"
240+
)
241+
static let schedule = NSLocalizedString(
242+
"prepublishing.schedule",
243+
value: "Schedule",
244+
comment: "Primary button label in the pre-publishing shee"
245+
)
246+
static let publishDate = NSLocalizedString(
247+
"prepublishing.publishDate",
248+
value: "Publish Date",
249+
comment: "Label for a cell in the pre-publishing sheet"
250+
)
251+
static let visibility = NSLocalizedString(
252+
"prepublishing.visibility",
253+
value: "Visibility",
254+
comment: "Label for a cell in the pre-publishing sheet"
255+
)
256+
static let categories = NSLocalizedString(
257+
"prepublishing.categories",
258+
value: "Categories",
259+
comment: "Label for a cell in the pre-publishing sheet"
260+
)
261+
static let tags = NSLocalizedString(
262+
"prepublishing.tags",
263+
value: "Tags",
264+
comment: "Label for a cell in the pre-publishing sheet"
265+
)
266+
static let jetpackSocial = NSLocalizedString(
267+
"prepublishing.jetpackSocial",
268+
value: "Jetpack Social",
269+
comment: "Label for a cell in the pre-publishing sheet"
270+
)
271+
static let immediately = NSLocalizedString(
272+
"prepublishing.publishDateImmediately",
273+
value: "Immediately",
274+
comment: "Placeholder value for a publishing date in the prepublishing sheet when the date is not selected"
275+
)
276+
static let uploadingMedia = NSLocalizedString(
277+
"prepublishing.uploadingMedia",
278+
value: "Uploading media",
279+
comment: "Title for a publish button state in the pre-publishing sheet"
280+
)
281+
private static let uploadMediaOneItemRemaining = NSLocalizedString(
282+
"prepublishing.uploadMediaOneItemRemaining",
283+
value: "%@ item remaining",
284+
comment: "Details label for a publish button state in the pre-publishing sheet"
285+
)
286+
private static let uploadMediaManyItemsRemaining = NSLocalizedString(
287+
"prepublishing.uploadMediaManyItemsRemaining",
288+
value: "%@ items remaining",
289+
comment: "Details label for a publish button state in the pre-publishing sheet"
290+
)
236291
static func uploadMediaRemaining(count: Int) -> String {
237-
String(format: count == 1 ? Strings.uploadMediaOneItemRemaining : Strings.uploadMediaManyItemsRemaining, count.description)
292+
String(
293+
format: count == 1 ? Strings.uploadMediaOneItemRemaining : Strings.uploadMediaManyItemsRemaining,
294+
count.description
295+
)
238296
}
239-
static let mediaUploadFailedTitle = NSLocalizedString("prepublishing.mediaUploadFailedTitle", value: "Failed to upload media", comment: "Title for a publish button state in the pre-publishing sheet")
240-
static let mediaUploadFailedDetailsMultipleFailures = NSLocalizedString("prepublishing.mediaUploadFailedDetails", value: "%@ items failed to upload", comment: "Details for a publish button state in the pre-publishing sheet; count as a parameter")
297+
static let mediaUploadFailedTitle = NSLocalizedString(
298+
"prepublishing.mediaUploadFailedTitle",
299+
value: "Failed to upload media",
300+
comment: "Title for a publish button state in the pre-publishing sheet"
301+
)
302+
static let mediaUploadFailedDetailsMultipleFailures = NSLocalizedString(
303+
"prepublishing.mediaUploadFailedDetails",
304+
value: "%@ items failed to upload",
305+
comment: "Details for a publish button state in the pre-publishing sheet; count as a parameter"
306+
)
241307

242308
static let discardChangesTitle = NSLocalizedString(
243309
"prepublishing.discardChanges.title",

0 commit comments

Comments
 (0)