Skip to content

Commit

Permalink
Merge pull request #21963 from wordpress-mobile/task/posts-pages-set-…
Browse files Browse the repository at this point in the history
…regular-page

Posts & Pages: Allow setting a posts page back to a regular page
  • Loading branch information
momo-ozawa authored Nov 6, 2023
2 parents ae33467 + 0bb5b5a commit 3f1f1c3
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 20 deletions.
5 changes: 5 additions & 0 deletions WordPress/Classes/Utility/Analytics/WPAnalyticsEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ import Foundation
case postListCommentsAction
case postListSetAsPostsPageAction
case postListSetHomePageAction
case postListSetAsRegularPageAction

// Page List
case pageListEditHomepageTapped
Expand Down Expand Up @@ -967,6 +968,8 @@ import Foundation
return "post_list_button_pressed"
case .postListSetHomePageAction:
return "post_list_button_pressed"
case .postListSetAsRegularPageAction:
return "post_list_button_pressed"

// Page List
case .pageListEditHomepageTapped:
Expand Down Expand Up @@ -1424,6 +1427,8 @@ import Foundation
return ["button": "set_posts_page"]
case .postListSetHomePageAction:
return ["button": "set_homepage"]
case .postListSetAsRegularPageAction:
return ["button": "set_regular_page"]
default:
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,20 @@ extension PageListViewController: InteractivePostViewDelegate {

func setHomepage(for apost: AbstractPost) {
guard let page = apost as? Page else { return }
WPAnalytics.track(.postListSetAsPostsPageAction)
setPageAsHomepage(page)
}

func setPostsPage(for apost: AbstractPost) {
guard let page = apost as? Page else { return }
setPageAsPostsPage(page)
WPAnalytics.track(.postListSetHomePageAction)
togglePageAsPostsPage(page)
}

func setRegularPage(for apost: AbstractPost) {
guard let page = apost as? Page else { return }
WPAnalytics.track(.postListSetAsRegularPageAction)
togglePageAsPostsPage(page)
}

// MARK: - Helpers
Expand Down
23 changes: 11 additions & 12 deletions WordPress/Classes/ViewRelated/Pages/PageListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,9 @@ class PageListViewController: AbstractPostListViewController, UIViewControllerRe
}

func setPageAsHomepage(_ page: Page) {
guard let pageID = page.postID?.intValue else { return }

guard let homePageID = page.postID?.intValue else { return }
beginRefreshingManually()
WPAnalytics.track(.postListSetHomePageAction)
homepageSettingsService?.setHomepageType(.page, homePageID: pageID, success: { [weak self] in
homepageSettingsService?.setHomepageType(.page, homePageID: homePageID, success: { [weak self] in
self?.refreshAndReload()
self?.handleHomepageSettingsSuccess()
}, failure: { [weak self] error in
Expand All @@ -444,14 +442,13 @@ class PageListViewController: AbstractPostListViewController, UIViewControllerRe
})
}

func setPageAsPostsPage(_ page: Page) {
guard let pageID = page.postID?.intValue else { return }

func togglePageAsPostsPage(_ page: Page) {
let newValue = !page.isSitePostsPage
let postsPageID = page.isSitePostsPage ? 0 : (page.postID?.intValue ?? 0)
beginRefreshingManually()
WPAnalytics.track(.postListSetAsPostsPageAction)
homepageSettingsService?.setHomepageType(.page, withPostsPageID: pageID, success: { [weak self] in
homepageSettingsService?.setHomepageType(.page, withPostsPageID: postsPageID, success: { [weak self] in
self?.refreshAndReload()
self?.handleHomepagePostsPageSettingsSuccess()
self?.handleHomepagePostsPageSettingsSuccess(isPostsPage: newValue)
}, failure: { [weak self] error in
self?.refreshControl.endRefreshing()
self?.handleHomepageSettingsFailure()
Expand All @@ -463,8 +460,9 @@ class PageListViewController: AbstractPostListViewController, UIViewControllerRe
ActionDispatcher.global.dispatch(NoticeAction.post(notice))
}

private func handleHomepagePostsPageSettingsSuccess() {
let notice = Notice(title: HomepageSettingsText.updatePostsPageSuccessTitle, feedbackType: .success)
private func handleHomepagePostsPageSettingsSuccess(isPostsPage: Bool) {
let title = isPostsPage ? HomepageSettingsText.updatePostsPageSuccessTitle : HomepageSettingsText.updatePageSuccessTitle
let notice = Notice(title: title, feedbackType: .success)
ActionDispatcher.global.dispatch(NoticeAction.post(notice))
}

Expand All @@ -483,6 +481,7 @@ class PageListViewController: AbstractPostListViewController, UIViewControllerRe
struct HomepageSettingsText {
static let updateErrorTitle = NSLocalizedString("Unable to update homepage settings", comment: "Error informing the user that their homepage settings could not be updated")
static let updateErrorMessage = NSLocalizedString("Please try again later.", comment: "Prompt for the user to retry a failed action again later")
static let updatePageSuccessTitle = NSLocalizedString("pages.updatePage.successTitle", value: "Page successfully updated", comment: "Message informing the user that their static homepage page was set successfully")
static let updateHomepageSuccessTitle = NSLocalizedString("Homepage successfully updated", comment: "Message informing the user that their static homepage page was set successfully")
static let updatePostsPageSuccessTitle = NSLocalizedString("Posts page successfully updated", comment: "Message informing the user that their static homepage for posts was set successfully")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,9 @@ final class PageMenuViewModelTests: CoreDataTestCase {
.map { $0.buttons }
let expectedButtons: [[AbstractPostButton]] = [
[.view],
[.moveToDraft, .duplicate],
[.duplicate],
[.blaze],
[.setParent, .setPostsPage],
[.trash]
[.setParent, .setPostsPage]
]
expect(buttons).to(equal(expectedButtons))
}
Expand All @@ -136,7 +135,7 @@ final class PageMenuViewModelTests: CoreDataTestCase {
[.view],
[.moveToDraft, .duplicate],
[.blaze],
[.setParent, .setHomepage],
[.setParent, .setHomepage, .setRegularPage],
[.trash]
]
expect(buttons).to(equal(expectedButtons))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ extension AbstractPostButton: AbstractPostMenuAction {
case .setParent: return UIImage(systemName: "text.append")
case .setHomepage: return UIImage(systemName: "house")
case .setPostsPage: return UIImage(systemName: "text.word.spacing")
case .setRegularPage: return UIImage(systemName: "arrow.uturn.backward")
case .pageAttributes: return UIImage(systemName: "doc")
}
}
Expand Down Expand Up @@ -123,6 +124,7 @@ extension AbstractPostButton: AbstractPostMenuAction {
case .setParent: return Strings.setParent
case .setHomepage: return Strings.setHomepage
case .setPostsPage: return Strings.setPostsPage
case .setRegularPage: return Strings.setRegularPage
case .pageAttributes: return Strings.pageAttributes
}
}
Expand Down Expand Up @@ -157,6 +159,8 @@ extension AbstractPostButton: AbstractPostMenuAction {
delegate.setHomepage(for: post)
case .setPostsPage:
delegate.setPostsPage(for: post)
case .setRegularPage:
delegate.setRegularPage(for: post)
case .pageAttributes:
break
}
Expand All @@ -178,6 +182,7 @@ extension AbstractPostButton: AbstractPostMenuAction {
static let setParent = NSLocalizedString("posts.setParent.actionTitle", value: "Set parent", comment: "Set the parent page for the selected page.")
static let setHomepage = NSLocalizedString("posts.setHomepage.actionTitle", value: "Set as homepage", comment: "Set the selected page as the homepage.")
static let setPostsPage = NSLocalizedString("posts.setPostsPage.actionTitle", value: "Set as posts page", comment: "Set the selected page as a posts page.")
static let setRegularPage = NSLocalizedString("posts.setRegularPage.actionTitle", value: "Set as regular page", comment: "Set the selected page as a regular page.")
static let pageAttributes = NSLocalizedString("posts.pageAttributes.actionTitle", value: "Page attributes", comment: "Opens a submenu for page attributes.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ enum AbstractPostButton: Equatable {
case setParent
case setHomepage
case setPostsPage
case setRegularPage
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ protocol InteractivePostViewDelegate: AnyObject {
func setParent(for post: AbstractPost)
func setHomepage(for post: AbstractPost)
func setPostsPage(for post: AbstractPost)
func setRegularPage(for post: AbstractPost)
}

extension InteractivePostViewDelegate {
func setParent(for post: AbstractPost) {}
func setHomepage(for post: AbstractPost) {}
func setPostsPage(for post: AbstractPost) {}
func setRegularPage(for post: AbstractPost) {}

func trash(_ post: AbstractPost) {
self.trash(post, completion: {})
Expand Down
16 changes: 13 additions & 3 deletions WordPress/Classes/ViewRelated/Post/PageMenuViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ final class PageMenuViewModel: AbstractPostMenuViewModel {
private func createSecondarySection() -> AbstractPostButtonSection {
var buttons = [AbstractPostButton]()

if page.status != .draft {
if page.status != .draft && !isSiteHomepage {
buttons.append(.moveToDraft)
}

Expand Down Expand Up @@ -88,18 +88,28 @@ final class PageMenuViewModel: AbstractPostMenuViewModel {

buttons.append(.setParent)

if page.status == .publish, !isSiteHomepage {
guard page.status == .publish else {
return AbstractPostButtonSection(buttons: buttons)
}

if !isSiteHomepage {
buttons.append(.setHomepage)
}

if page.status == .publish, !isSitePostsPage {
if !isSitePostsPage {
buttons.append(.setPostsPage)
} else {
buttons.append(.setRegularPage)
}

return AbstractPostButtonSection(buttons: buttons, submenuButton: .pageAttributes)
}

private func createTrashSection() -> AbstractPostButtonSection {
guard !isSiteHomepage else {
return AbstractPostButtonSection(buttons: [])
}

return AbstractPostButtonSection(buttons: [.trash])
}
}

0 comments on commit 3f1f1c3

Please sign in to comment.