Skip to content

Commit

Permalink
Move ParentPageSettingsViewController to PostSettingsViewController (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kean authored May 2, 2024
2 parents 9fa967f + bc6d84e commit 720047e
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 245 deletions.
2 changes: 1 addition & 1 deletion RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
24.9
-----

* [*] Add "Parent Page" to "Page Settings" [#23136]

24.8
-----
Expand Down
22 changes: 22 additions & 0 deletions WordPress/Classes/Services/PostRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,28 @@ final class PostRepository {
}
}
}

@MainActor
func buildPageTree(pageIDs: [TaggedManagedObjectID<Page>]? = nil, request: NSFetchRequest<Page>? = nil) async throws -> [(pageID: TaggedManagedObjectID<Page>, hierarchyIndex: Int)] {
assert(pageIDs != nil || request != nil, "`pageIDs` and `request` can not both be nil")

let coreDataStack = ContextManager.shared
return try await coreDataStack.performQuery { context in
var pages = [Page]()

if let pageIDs {
pages = try pageIDs.map(context.existingObject(with:))
} else if let request {
pages = try context.fetch(request)
}

pages = pages.setHomePageFirst()

// The `hierarchyIndex` is not a managed property, so it needs to be returend along with the page object id.
return PageTree.hierarchyList(of: pages)
.map { (TaggedManagedObjectID($0), $0.hierarchyIndex) }
}
}
}

private extension PostRepository {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ extension PageListViewController: InteractivePostViewDelegate {
PostSettingsViewController.showStandaloneEditor(for: post, from: self)
}

func setParent(for apost: AbstractPost) {
guard let page = apost as? Page else { return }
Task {
await setParentPage(for: page)
}
}

func setHomepage(for apost: AbstractPost) {
guard let page = apost as? Page else { return }
WPAnalytics.track(.postListSetAsPostsPageAction)
Expand Down
59 changes: 1 addition & 58 deletions WordPress/Classes/ViewRelated/Pages/PageListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ final class PageListViewController: AbstractPostListViewController {
let pageIDs = pages.map { TaggedManagedObjectID($0) }

do {
self.pages = try await buildPageTree(pageIDs: pageIDs)
self.pages = try await PostRepository().buildPageTree(pageIDs: pageIDs)
.map { pageID, hierarchyIndex in
let page = try coreDataStack.mainContext.existingObject(with: pageID)
page.hierarchyIndex = hierarchyIndex
Expand All @@ -234,29 +234,6 @@ final class PageListViewController: AbstractPostListViewController {
refreshResults()
}

/// Build page hierachy in background, which should not take long (less than 2 seconds for 6000+ pages).
@MainActor
func buildPageTree(pageIDs: [TaggedManagedObjectID<Page>]? = nil, request: NSFetchRequest<Page>? = nil) async throws -> [(pageID: TaggedManagedObjectID<Page>, hierarchyIndex: Int)] {
assert(pageIDs != nil || request != nil, "`pageIDs` and `request` can not both be nil")

let coreDataStack = ContextManager.shared
return try await coreDataStack.performQuery { context in
var pages = [Page]()

if let pageIDs {
pages = try pageIDs.map(context.existingObject(with:))
} else if let request {
pages = try context.fetch(request)
}

pages = pages.setHomePageFirst()

// The `hierarchyIndex` is not a managed property, so it needs to be returend along with the page object id.
return PageTree.hierarchyList(of: pages)
.map { (TaggedManagedObjectID($0), $0.hierarchyIndex) }
}
}

override func syncContentEnded(_ syncHelper: WPContentSyncHelper) {
guard syncHelper.hasMoreContent else {
super.syncContentEnded(syncHelper)
Expand Down Expand Up @@ -429,40 +406,6 @@ final class PageListViewController: AbstractPostListViewController {

// MARK: - Cell Action Handling

@MainActor
func setParentPage(for page: Page) async {
let request = NSFetchRequest<Page>(entityName: Page.entityName())
let filter = PostListFilter.publishedFilter()
request.predicate = filter.predicate(for: blog, author: .everyone)
request.sortDescriptors = filter.sortDescriptors
do {
let context = ContextManager.shared.mainContext
var pages = try await buildPageTree(request: request)
.map { pageID, hierarchyIndex in
let page = try context.existingObject(with: pageID)
page.hierarchyIndex = hierarchyIndex
return page
}
if let index = pages.firstIndex(of: page) {
pages = pages.remove(from: index)
}
let viewController = ParentPageSettingsViewController.navigationController(with: pages, selectedPage: page, onClose: { [weak self] in
self?.updateAndPerformFetchRequestRefreshingResults()
}, onSuccess: { [weak self] in
self?.handleSetParentSuccess()
} )
present(viewController, animated: true)
} catch {
assertionFailure("Failed to fetch pages: \(error)") // This should never happen
}
}

private func handleSetParentSuccess() {
let setParentSuccefullyNotice = NSLocalizedString("Parent page successfully updated.", comment: "Message informing the user that their pages parent has been set successfully")
let notice = Notice(title: setParentSuccefullyNotice, feedbackType: .success)
ActionDispatcher.global.dispatch(NoticeAction.post(notice))
}

func setPageAsHomepage(_ page: Page) {
guard let homePageID = page.postID?.intValue else { return }
beginRefreshingManually()
Expand Down
12 changes: 5 additions & 7 deletions WordPress/Classes/ViewRelated/Pages/PageMenuViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class PageMenuViewModelTests: CoreDataTestCase {
[.view],
[.moveToDraft, .duplicate, .share],
[.blaze],
[.setParent, .setHomepage, .setPostsPage],
[.setHomepage, .setPostsPage],
[.stats, .settings],
[.trash]
]
Expand Down Expand Up @@ -54,7 +54,7 @@ final class PageMenuViewModelTests: CoreDataTestCase {
let expectedButtons: [[AbstractPostButton]] = [
[.view],
[.moveToDraft, .duplicate, .share],
[.setParent, .setHomepage, .setPostsPage],
[.setHomepage, .setPostsPage],
[.stats, .settings],
[.trash]
]
Expand Down Expand Up @@ -82,7 +82,7 @@ final class PageMenuViewModelTests: CoreDataTestCase {
let expectedButtons: [[AbstractPostButton]] = [
[.view],
[.moveToDraft, .duplicate, .share],
[.setParent, .setHomepage, .setPostsPage],
[.setHomepage, .setPostsPage],
[.settings],
[.trash]
]
Expand Down Expand Up @@ -111,7 +111,7 @@ final class PageMenuViewModelTests: CoreDataTestCase {
[.view],
[.duplicate, .share],
[.blaze],
[.setParent, .setPostsPage],
[.setPostsPage],
[.stats, .settings]
]
expect(buttons).to(equal(expectedButtons))
Expand Down Expand Up @@ -139,7 +139,7 @@ final class PageMenuViewModelTests: CoreDataTestCase {
[.view],
[.moveToDraft, .duplicate, .share],
[.blaze],
[.setParent, .setHomepage, .setRegularPage],
[.setHomepage, .setRegularPage],
[.stats, .settings],
[.trash]
]
Expand All @@ -160,7 +160,6 @@ final class PageMenuViewModelTests: CoreDataTestCase {
let expectedButtons: [[AbstractPostButton]] = [
[.view],
[.publish, .duplicate],
[.setParent],
[.settings],
[.trash]
]
Expand All @@ -181,7 +180,6 @@ final class PageMenuViewModelTests: CoreDataTestCase {
let expectedButtons: [[AbstractPostButton]] = [
[.view],
[.moveToDraft],
[.setParent],
[.settings],
[.trash]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ @implementation PageSettingsViewController

- (void)configureSections
{
self.sections = @[@(PostSettingsSectionMeta),@(PostSettingsSectionFeaturedImage)];
self.sections = @[
@(PostSettingsSectionMeta),
@(PostSettingsSectionFeaturedImage),
@(PostSettingsSectionPageAttributes)
];
}

- (Page *)page
Expand Down
51 changes: 8 additions & 43 deletions WordPress/Classes/ViewRelated/Pages/Pages.storyboard
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="22154" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="22505" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22130"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22504"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Navigation Controller-->
<scene sceneID="sxx-aJ-IGT">
<objects>
<navigationController storyboardIdentifier="ParentPageSettings" id="QcV-Yi-qZN" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="i29-WU-Owk">
<rect key="frame" x="0.0" y="20" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<connections>
<segue destination="en5-L6-oun" kind="relationship" relationship="rootViewController" id="vRa-sH-3cd"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="XES-bJ-l3d" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="516" y="1043"/>
</scene>
<!--Parent Page Settings View Controller-->
<scene sceneID="pZd-4V-qlT">
<objects>
<viewController id="en5-L6-oun" customClass="ParentPageSettingsViewController" customModule="WordPress" customModuleProvider="target" sceneMemberID="viewController">
<viewController storyboardIdentifier="ParentPageSettings" id="en5-L6-oun" customClass="ParentPageSettingsViewController" customModule="WordPress" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="iSZ-D8-Et4"/>
<viewControllerLayoutGuide type="bottom" id="AEa-4M-1Cd"/>
Expand All @@ -35,18 +19,17 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="grouped" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="18" sectionFooterHeight="18" translatesAutoresizingMaskIntoConstraints="NO" id="rFc-KI-K05">
<rect key="frame" x="0.0" y="120" width="375" height="547"/>
<color key="backgroundColor" systemColor="groupTableViewBackgroundColor"/>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="insetGrouped" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="18" sectionFooterHeight="18" translatesAutoresizingMaskIntoConstraints="NO" id="rFc-KI-K05">
<rect key="frame" x="0.0" y="76" width="375" height="591"/>
</tableView>
<searchBar contentMode="redraw" translatesAutoresizingMaskIntoConstraints="NO" id="DaX-Id-Nby">
<rect key="frame" x="0.0" y="64" width="375" height="56"/>
<rect key="frame" x="0.0" y="20" width="375" height="56"/>
<textInputTraits key="textInputTraits"/>
</searchBar>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="AEa-4M-1Cd" firstAttribute="top" secondItem="rFc-KI-K05" secondAttribute="bottom" id="B3g-Fs-lQX"/>
<constraint firstAttribute="bottom" secondItem="rFc-KI-K05" secondAttribute="bottom" id="B3g-Fs-lQX"/>
<constraint firstAttribute="trailing" secondItem="rFc-KI-K05" secondAttribute="trailing" id="BNx-y3-pCd"/>
<constraint firstItem="rFc-KI-K05" firstAttribute="top" secondItem="DaX-Id-Nby" secondAttribute="bottom" id="G3L-26-jPU"/>
<constraint firstItem="rFc-KI-K05" firstAttribute="leading" secondItem="2DW-5s-50f" secondAttribute="leading" id="NDe-TA-NNm"/>
Expand All @@ -55,21 +38,8 @@
<constraint firstAttribute="trailing" secondItem="DaX-Id-Nby" secondAttribute="trailing" id="pDw-6f-r70"/>
</constraints>
</view>
<navigationItem key="navigationItem" id="fJK-CD-lVY">
<barButtonItem key="leftBarButtonItem" title="Cancel" id="vsX-CE-sq7">
<connections>
<action selector="cancelAction:" destination="en5-L6-oun" id="7nD-eL-LcM"/>
</connections>
</barButtonItem>
<barButtonItem key="rightBarButtonItem" enabled="NO" title="Done" id="Ybc-8A-PTe">
<connections>
<action selector="doneAction:" destination="en5-L6-oun" id="pGj-nC-QNQ"/>
</connections>
</barButtonItem>
</navigationItem>
<navigationItem key="navigationItem" id="fJK-CD-lVY"/>
<connections>
<outlet property="cancelButton" destination="vsX-CE-sq7" id="nQK-bh-bZX"/>
<outlet property="doneButton" destination="Ybc-8A-PTe" id="dxy-DZ-9VV"/>
<outlet property="searchBar" destination="DaX-Id-Nby" id="3r8-LD-SOe"/>
<outlet property="tableView" destination="rFc-KI-K05" id="BE5-XQ-QGl"/>
</connections>
Expand All @@ -79,9 +49,4 @@
<point key="canvasLocation" x="1240.8" y="1047.5262368815593"/>
</scene>
</scenes>
<resources>
<systemColor name="groupTableViewBackgroundColor">
<color red="0.94901960784313721" green="0.94901960784313721" blue="0.96862745098039216" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
</resources>
</document>
Loading

0 comments on commit 720047e

Please sign in to comment.