-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use PostRepository to fetch pages in the Menu screen #21949
Changes from all commits
fb3fad8
0e63b7f
187e134
53d634a
5339b86
ed781a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,6 @@ | |
#import "WordPress-Swift.h" | ||
@import WordPressKit; | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was this removed? I'm guessing because it's not necessary in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed. No benefit whatsoever in using it in .m files. |
||
|
||
@implementation MenusService | ||
|
||
#pragma mark - Menus availability | ||
|
@@ -161,47 +159,21 @@ - (void)deleteMenu:(Menu *)menu | |
failure:failure]; | ||
} | ||
|
||
- (void)generateDefaultMenuItemsForBlog:(Blog *)blog | ||
success:(nullable void(^)(NSArray <MenuItem *> * _Nullable defaultItems))success | ||
failure:(nullable MenusServiceFailureBlock)failure | ||
{ | ||
// Get the latest list of Pages available to the site. | ||
PostServiceSyncOptions *options = [[PostServiceSyncOptions alloc] init]; | ||
options.statuses = @[PostStatusPublish]; | ||
options.order = PostServiceResultsOrderAscending; | ||
options.orderBy = PostServiceResultsOrderingByTitle; | ||
PostService *postService = [[PostService alloc] initWithManagedObjectContext:self.managedObjectContext]; | ||
[postService syncPostsOfType:PostServiceTypePage | ||
withOptions:options | ||
forBlog:blog | ||
success:^(NSArray *pages) { | ||
[self.managedObjectContext performBlock:^{ | ||
|
||
if (!pages.count) { | ||
success(nil); | ||
return; | ||
} | ||
NSMutableArray *items = [NSMutableArray arrayWithCapacity:pages.count]; | ||
// Create menu items for the top parent pages. | ||
for (Page *page in pages) { | ||
if ([page.parentID integerValue] > 0) { | ||
continue; | ||
} | ||
MenuItem *pageItem = [NSEntityDescription insertNewObjectForEntityForName:[MenuItem entityName] inManagedObjectContext:self.managedObjectContext]; | ||
pageItem.contentID = page.postID; | ||
pageItem.name = page.titleForDisplay; | ||
pageItem.type = MenuItemTypePage; | ||
[items addObject:pageItem]; | ||
} | ||
|
||
[[ContextManager sharedInstance] saveContext:self.managedObjectContext withCompletionBlock:^{ | ||
if (success) { | ||
success(items); | ||
} | ||
} onQueue:dispatch_get_main_queue()]; | ||
}]; | ||
} | ||
failure:failure]; | ||
- (MenuItem *)createItemWithPageID:(NSManagedObjectID *)pageObjectID inContext:(NSManagedObjectContext *)context { | ||
Page *page = [context existingObjectWithID:pageObjectID error:nil]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you considered passing an error object? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That didn't cross my mind. That's a good idea though: turning this Objective-C function to a throwing Swift function. But unfortunately, after giving it go, I noticed Swift turns the function to |
||
if (page == nil) { | ||
return nil; | ||
} | ||
|
||
if ([page.parentID integerValue] > 0) { | ||
return nil; | ||
} | ||
|
||
MenuItem *pageItem = [NSEntityDescription insertNewObjectForEntityForName:[MenuItem entityName] inManagedObjectContext:self.managedObjectContext]; | ||
pageItem.contentID = page.postID; | ||
pageItem.name = page.titleForDisplay; | ||
pageItem.type = MenuItemTypePage; | ||
return pageItem; | ||
} | ||
|
||
#pragma mark - private | ||
|
@@ -435,5 +407,3 @@ - (RemoteMenuItem *)remoteItemFromItem:(MenuItem *)item withItems:(NSOrderedSet< | |
} | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,6 @@ | |
#import <WordPressShared/WPFontManager.h> | ||
#import <WordPressShared/WPStyleGuide.h> | ||
|
||
|
||
|
||
static CGFloat const ScrollViewOffsetAdjustmentPadding = 10.0; | ||
|
||
@interface MenusViewController () <UIScrollViewDelegate, MenuHeaderViewControllerDelegate, MenuDetailsViewControllerDelegate, MenuItemsViewControllerDelegate> | ||
|
@@ -32,9 +30,6 @@ @interface MenusViewController () <UIScrollViewDelegate, MenuHeaderViewControlle | |
@property (nonatomic, strong, readonly) UILabel *itemsLoadingLabel; | ||
@property (nonatomic, strong, readonly) UIBarButtonItem *saveButtonItem; | ||
|
||
@property (nonatomic, strong, readonly) Blog *blog; | ||
@property (nonatomic, strong, readonly) MenusService *menusService; | ||
|
||
@property (nonatomic, strong) MenuLocation *selectedMenuLocation; | ||
@property (nonatomic, strong) Menu *updatedMenuForSaving; | ||
@property (nonatomic, strong) Menu *initialMenuSelection; | ||
|
@@ -45,6 +40,8 @@ @interface MenusViewController () <UIScrollViewDelegate, MenuHeaderViewControlle | |
@property (nonatomic, assign) BOOL needsSave; | ||
@property (nonatomic, assign) BOOL isSaving; | ||
|
||
@property (nonatomic, strong) void (^fetchAllPagesCancellationHandler)(void); | ||
|
||
@end | ||
|
||
@implementation MenusViewController | ||
|
@@ -176,6 +173,15 @@ - (void)viewWillDisappear:(BOOL)animated | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; | ||
} | ||
|
||
- (void)viewDidDisappear:(BOOL)animated | ||
{ | ||
[super viewDidDisappear:animated]; | ||
|
||
if (self.navigationController == nil && self.fetchAllPagesCancellationHandler != nil) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does the code check for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make sure we only cancel the fetching when user taps the Back button. |
||
self.fetchAllPagesCancellationHandler(); | ||
} | ||
} | ||
|
||
#pragma mark - Views setup | ||
|
||
- (void)updateScrollViewContentSize | ||
|
@@ -355,9 +361,7 @@ - (void)loadDefaultMenuItemsIfNeeded | |
void(^failureBlock)(NSError *) = ^(NSError * __unused error) { | ||
weakSelf.itemsLoadingLabel.text = NSLocalizedString(@"An error occurred loading the menu, please check your internet connection.", @"Menus error message seen when an error occurred loading a specific menu."); | ||
}; | ||
[self.menusService generateDefaultMenuItemsForBlog:self.blog | ||
success:successBlock | ||
failure:failureBlock]; | ||
self.fetchAllPagesCancellationHandler = [self fetchAllPagesWithSuccess:successBlock failure:failureBlock]; | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Foundation | ||
|
||
extension MenusViewController { | ||
|
||
/// Fetch all pages from the site. | ||
/// | ||
/// - Returns: A block that can be used to cancel the fetching. | ||
@objc func fetchAllPages(success: @escaping ([MenuItem]) -> Void, failure: @escaping (Error) -> Void) -> () -> Void { | ||
let coreDataStack = ContextManager.shared | ||
let repository = PostRepository(coreDataStack: coreDataStack) | ||
let fetchAllPagesTask = repository.fetchAllPages(statuses: [.publish], in: TaggedManagedObjectID(blog)) | ||
|
||
// Wait for the fetching all pages task to complete and pass its result to the success or failure block. | ||
Task { [weak self] in | ||
let allPages: [TaggedManagedObjectID<Page>] | ||
do { | ||
allPages = try await fetchAllPagesTask.value | ||
} catch { | ||
failure(error) | ||
return | ||
} | ||
|
||
guard let menusService = self?.menusService else { return } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was going to suggest moving the check at the start of the |
||
|
||
await menusService.managedObjectContext.perform { | ||
let items = allPages.compactMap { | ||
menusService.createItem(withPageID: $0.objectID, in: menusService.managedObjectContext) | ||
} | ||
success(items) | ||
} | ||
} | ||
|
||
return fetchAllPagesTask.cancel | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick. There docs in the previous method has
@param
s. The new one doesn't. One could see this as a regression.At the same time, I personally prefer this style of documentation, which I interpret as "only document was is not obvious."
What do you think? Is it worth using this as an example to start a team-wide conversation to define a standard?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is exactly what I was thinking. I don't think those param docs in the original function is really useful.