-
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
Reader: Local Default Topics #23788
base: trunk
Are you sure you want to change the base?
Reader: Local Default Topics #23788
Changes from all commits
c7cf4d6
9a940e7
7db2e4a
202004b
534a4e9
c84148d
f4e561f
6f712d3
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 |
---|---|---|
|
@@ -4,4 +4,26 @@ import Foundation | |
override open class var TopicType: String { | ||
return "default" | ||
} | ||
|
||
/// - note: Starting with 25.5, the app no longer uses the default topics | ||
/// fetched from `/read/menu`. Instead, it uses the topics created locally. | ||
/// The only reason these topics are needed is to filter entities in Core Data. | ||
static func make(path: Path, in context: NSManagedObjectContext) throws -> ReaderDefaultTopic { | ||
if let topic = context.firstObject(ofType: ReaderDefaultTopic.self, matching: NSPredicate(format: "path == %@", path.rawValue)) { | ||
return topic | ||
} | ||
let topic = ReaderDefaultTopic(entity: ReaderDefaultTopic.entity(), insertInto: context) | ||
topic.type = TopicType | ||
topic.path = path.rawValue | ||
topic.title = "" | ||
try context.save() | ||
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. We should use Also, we don't update and save the main context. |
||
return topic | ||
} | ||
|
||
enum Path: String { | ||
// These are arbitrary values just to identify the entites. | ||
case recommended = "/jpios/discover/recommended" | ||
case firstPosts = "/jpios/discover/first-posts" | ||
case latest = "/jpios/discover/latest" | ||
} | ||
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. Does it mean there should be three unique "default topics" (a.k.a "Discover" topics?) in Core Data? If that's the case, should we delete 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. There are two that are still remaining: one for "Recent" and one for "Likes", so I can't remove them just yet. The "Discover" topic will also be in the database for now, but the app never displays them anywhere, so it's going to be fine. My plan is to ultimately remove 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. Btw, this PR also fixes the following issue: Steps
Expected result:
Actual result:
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,6 @@ | |
@import WordPressKit; | ||
@import WordPressShared; | ||
|
||
NSString * const ReaderTopicFreshlyPressedPathCommponent = @"freshly-pressed"; | ||
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. No longer used. |
||
static NSString * const ReaderTopicCurrentTopicPathKey = @"ReaderTopicCurrentTopicPathKey"; | ||
|
||
@implementation ReaderTopicService | ||
|
||
- (void)fetchReaderMenuWithSuccess:(void (^)(void))success failure:(void (^)(NSError *error))failure | ||
|
@@ -43,73 +40,6 @@ - (void)fetchReaderMenuWithSuccess:(void (^)(void))success failure:(void (^)(NSE | |
} completion:nil onQueue:dispatch_get_main_queue()]; | ||
} | ||
|
||
- (ReaderAbstractTopic *)currentTopicInContext:(NSManagedObjectContext *)context | ||
{ | ||
ReaderAbstractTopic *topic; | ||
topic = [self currentTopicFromSavedPathInContext:context]; | ||
|
||
if (!topic) { | ||
topic = [self currentTopicFromDefaultTopicInContext:context]; | ||
[self setCurrentTopic:topic]; | ||
} | ||
|
||
return topic; | ||
} | ||
|
||
- (ReaderAbstractTopic *)currentTopicFromSavedPathInContext:(NSManagedObjectContext *)context | ||
{ | ||
ReaderAbstractTopic *topic; | ||
NSString *topicPathString = [[UserPersistentStoreFactory userDefaultsInstance] stringForKey:ReaderTopicCurrentTopicPathKey]; | ||
if (topicPathString) { | ||
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:[ReaderAbstractTopic classNameWithoutNamespaces]]; | ||
request.predicate = [NSPredicate predicateWithFormat:@"path = %@", topicPathString]; | ||
|
||
NSError *error; | ||
topic = [[context executeFetchRequest:request error:&error] firstObject]; | ||
if (error) { | ||
DDLogError(@"%@ error fetching topic: %@", NSStringFromSelector(_cmd), error); | ||
} | ||
} | ||
return topic; | ||
} | ||
|
||
- (ReaderAbstractTopic *)currentTopicFromDefaultTopicInContext:(NSManagedObjectContext *)context | ||
{ | ||
// Return the default topic | ||
ReaderAbstractTopic *topic; | ||
NSError *error; | ||
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:[ReaderDefaultTopic classNameWithoutNamespaces]]; | ||
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]; | ||
request.sortDescriptors = @[sortDescriptor]; | ||
NSArray *topics = [context executeFetchRequest:request error:&error]; | ||
if (error) { | ||
DDLogError(@"%@ error fetching topic: %@", NSStringFromSelector(_cmd), error); | ||
return nil; | ||
} | ||
|
||
if ([topics count] == 0) { | ||
return nil; | ||
} | ||
|
||
NSArray *matches = [topics filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"path CONTAINS[cd] %@", ReaderTopicFreshlyPressedPathCommponent]]; | ||
if ([matches count]) { | ||
topic = matches[0]; | ||
} else { | ||
topic = topics[0]; | ||
} | ||
|
||
return topic; | ||
} | ||
|
||
- (void)setCurrentTopic:(ReaderAbstractTopic *)topic | ||
{ | ||
if (!topic) { | ||
[[UserPersistentStoreFactory userDefaultsInstance] removeObjectForKey:ReaderTopicCurrentTopicPathKey]; | ||
} else { | ||
[[UserPersistentStoreFactory userDefaultsInstance] setObject:topic.path forKey:ReaderTopicCurrentTopicPathKey]; | ||
} | ||
} | ||
|
||
- (void)deleteAllSearchTopics | ||
{ | ||
[self.coreDataStack performAndSaveUsingBlock:^(NSManagedObjectContext *context) { | ||
|
@@ -176,7 +106,6 @@ - (void)clearInUseFlags | |
- (void)deleteAllTopics | ||
{ | ||
[self.coreDataStack performAndSaveUsingBlock:^(NSManagedObjectContext *context) { | ||
[self setCurrentTopic:nil]; | ||
NSArray *currentTopics = [ReaderAbstractTopic lookupAllInContext:context error:nil]; | ||
for (ReaderAbstractTopic *topic in currentTopics) { | ||
DDLogInfo(@"Deleting topic: %@", topic.title); | ||
|
@@ -305,8 +234,6 @@ - (void)followTagNamed:(NSString *)topicName | |
NSDictionary *properties = @{@"tag":topicName, @"source":source}; | ||
[WPAnalytics trackReaderStat:WPAnalyticsStatReaderTagFollowed properties:properties]; | ||
[self.coreDataStack performAndSaveUsingBlock:^(NSManagedObjectContext *context) { | ||
[self selectTopicWithID:topicID inContext:context]; | ||
|
||
// mark tag topic as followed | ||
ReaderTagTopic *tag = [ReaderTagTopic lookupWithTagID:topicID inContext:context]; | ||
if (tag) { | ||
|
@@ -597,16 +524,6 @@ - (WordPressComRestApi *)apiForRequest | |
return api; | ||
} | ||
|
||
/** | ||
Finds an existing topic matching the specified topicID and, if found, makes it the | ||
selected topic. | ||
*/ | ||
- (void)selectTopicWithID:(NSNumber *)topicID inContext:(NSManagedObjectContext *)context | ||
{ | ||
ReaderAbstractTopic *topic = [ReaderTagTopic lookupWithTagID:topicID inContext:context]; | ||
[self setCurrentTopic:topic]; | ||
} | ||
|
||
/** | ||
Create a new `ReaderAbstractTopic` or update an existing `ReaderAbstractTopic`. | ||
|
||
|
@@ -862,10 +779,7 @@ - (void)mergeMenuTopics:(NSArray *)topics isLoggedIn:(BOOL)isLoggedIn withSucces | |
if ([currentTopics count] > 0) { | ||
for (ReaderAbstractTopic *topic in currentTopics) { | ||
if (![topic isKindOfClass:[ReaderSiteTopic class]] && ![topicsToKeep containsObject:topic]) { | ||
|
||
if ([topic isEqual:[self currentTopicInContext:context]]) { | ||
self.currentTopic = nil; | ||
} | ||
|
||
if (topic.inUse) { | ||
if (!ReaderHelpers.isLoggedIn && [topic isKindOfClass:ReaderTagTopic.class]) { | ||
DDLogInfo(@"Not unfollowing a locally saved topic: %@", topic.title); | ||
|
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 a required field but it's never used only by other topic types.