-
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
Blog service lookup blog #19366
Blog service lookup blog #19366
Changes from all commits
25d8a4a
d7c273f
4a4d114
ba698a8
dd51e2a
d715dc1
e0ebe8f
be70b56
274e85d
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Foundation | ||
|
||
extension Blog { | ||
|
||
/// Returns the blog currently flagged as the one last used, or the primary blog, | ||
/// or the first blog in an alphanumerically sorted list, whichever is found first. | ||
@objc(lastUsedOrFirstInContext:) | ||
static func lastUsedOrFirst(in context: NSManagedObjectContext) -> Blog? { | ||
lastUsed(in: context) | ||
?? (try? WPAccount.lookupDefaultWordPressComAccount(in: context))?.defaultBlog | ||
?? firstBlog(in: context) | ||
} | ||
|
||
/// Returns the blog currently flaged as the one last used. | ||
static func lastUsed(in context: NSManagedObjectContext) -> Blog? { | ||
guard let url = RecentSitesService().recentSites.first else { | ||
return nil | ||
} | ||
|
||
return blog(with: NSPredicate(format: "visible = YES AND url = %@", url), in: context) | ||
} | ||
|
||
private static func firstBlog(in context: NSManagedObjectContext) -> Blog? { | ||
blog(with: NSPredicate(format: "visible = YES"), in: context) | ||
} | ||
|
||
private static func blog(with predicate: NSPredicate, in context: NSManagedObjectContext) -> Blog? { | ||
let request = NSFetchRequest<Blog>(entityName: NSStringFromClass(Blog.self)) | ||
request.includesSubentities = false | ||
request.predicate = predicate | ||
request.fetchLimit = 1 | ||
request.sortDescriptors = [NSSortDescriptor(key: "settings.name", ascending: true)] | ||
|
||
do { | ||
return try context.fetch(request).first | ||
} catch { | ||
DDLogError("Couldn't fetch blogs with predicate \(predicate): \(error)") | ||
return nil | ||
} | ||
} | ||
Comment on lines
+27
to
+40
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. Maybe in a future iteration it would be cool to Of course, that's out of scope now and also would require making a choice over which approach to use. Just thinking out loud. |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,35 +26,6 @@ extern NSString *const WPBlogUpdatedNotification; | |
*/ | ||
- (nullable Blog *)blogByHostname:(NSString *)hostname; | ||
|
||
/** | ||
Returns the blog currently flagged as the one last used, or the primary blog, | ||
or the first blog in an alphanumerically sorted list, whichever is found first. | ||
*/ | ||
- (nullable Blog *)lastUsedOrFirstBlog; | ||
|
||
/** | ||
Returns the blog currently flagged as the one last used, or the primary blog, | ||
or the first blog in an alphanumerically sorted list that supports the given | ||
feature, whichever is found first. | ||
*/ | ||
- (nullable Blog *)lastUsedOrFirstBlogThatSupports:(BlogFeature)feature; | ||
Comment on lines
-35
to
-40
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. This method hasn't been converted. But the code builds without it, so it was clearly unused 😄 👍 |
||
|
||
/** | ||
Returns the blog currently flaged as the one last used. | ||
*/ | ||
- (nullable Blog *)lastUsedBlog; | ||
|
||
/** | ||
Returns the first blog in an alphanumerically sorted list. | ||
*/ | ||
- (nullable Blog *)firstBlog; | ||
|
||
/** | ||
Returns the default WPCom blog. | ||
*/ | ||
- (nullable Blog *)primaryBlog; | ||
|
||
|
||
/** | ||
* Sync all available blogs for an acccount | ||
* | ||
|
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 function is translated from
BlogService.blogsWithPredicate