Bugfix: unread count needs sorted children! + ManagedContext fix

This commit is contained in:
relikd
2018-08-19 14:40:45 +02:00
parent 733ce7af79
commit 1b118959fd
5 changed files with 37 additions and 32 deletions

View File

@@ -48,7 +48,7 @@
*/ */
- (BOOL)descendantFeedItems:(FeedConfigRecursiveItemsBlock)block { - (BOOL)descendantFeedItems:(FeedConfigRecursiveItemsBlock)block {
if (self.children.count > 0) { if (self.children.count > 0) {
for (FeedConfig *config in self.children) { for (FeedConfig *config in self.sortedChildren) {
if ([config descendantFeedItems:block] == NO) if ([config descendantFeedItems:block] == NO)
return NO; return NO;
} }

View File

@@ -105,14 +105,18 @@
item.refreshUnit = (int16_t)self.refreshUnit.indexOfSelectedItem; item.refreshUnit = (int16_t)self.refreshUnit.indexOfSelectedItem;
if (self.feedResult) { if (self.feedResult) {
[item.managedObjectContext performBlockAndWait:^{
Feed *rss = [StoreCoordinator createFeedFromDictionary:self.feedResult inContext:item.managedObjectContext]; Feed *rss = [StoreCoordinator createFeedFromDictionary:self.feedResult inContext:item.managedObjectContext];
if (item.feed) if (item.feed)
[item.managedObjectContext deleteObject:(NSManagedObject*)item.feed]; [item.managedObjectContext deleteObject:(NSManagedObject*)item.feed];
item.feed = rss; item.feed = rss;
}];
} }
if ([item.managedObjectContext hasChanges]) { if ([item.managedObjectContext hasChanges]) {
self.objectIsModified = YES; self.objectIsModified = YES;
[item.managedObjectContext performBlockAndWait:^{
[item.managedObjectContext refreshObject:item mergeChanges:YES]; [item.managedObjectContext refreshObject:item mergeChanges:YES];
}];
} }
} }
@@ -202,7 +206,9 @@
NSString *name = ((NSTextField*)self.view).stringValue; NSString *name = ((NSTextField*)self.view).stringValue;
if (![item.name isEqualToString: name]) { if (![item.name isEqualToString: name]) {
item.name = name; item.name = name;
[item.managedObjectContext performBlockAndWait:^{
[item.managedObjectContext refreshObject:item mergeChanges:YES]; [item.managedObjectContext refreshObject:item mergeChanges:YES];
}];
[self.delegate modalDidUpdateFeedConfig:item]; [self.delegate modalDidUpdateFeedConfig:item];
} }
} }

View File

@@ -59,14 +59,15 @@ static NSString *dragNodeType = @"baRSS-feed-drag";
self.undoManager = self.dataStore.managedObjectContext.undoManager; self.undoManager = self.dataStore.managedObjectContext.undoManager;
} }
- (void)dealloc {
[self saveAndRebuildMenu];
}
- (void)saveAndRebuildMenu { - (void)saveAndRebuildMenu {
[self.dataStore.managedObjectContext performBlock:^{
[StoreCoordinator saveContext:self.dataStore.managedObjectContext]; [StoreCoordinator saveContext:self.dataStore.managedObjectContext];
[StoreCoordinator saveContext:self.dataStore.managedObjectContext.parentContext];
[[(AppHook*)NSApp barMenu] rebuildMenu]; // updating individual items was way to complicated ... [[(AppHook*)NSApp barMenu] rebuildMenu]; // updating individual items was way to complicated ...
[self.dataStore.managedObjectContext.parentContext performBlock:^{
[StoreCoordinator saveContext:self.dataStore.managedObjectContext.parentContext];
}];
}];
} }
- (IBAction)addFeed:(id)sender { - (IBAction)addFeed:(id)sender {

View File

@@ -31,13 +31,11 @@
/// @c NSMenuItem options that are assigned to the @c tag attribute. /// @c NSMenuItem options that are assigned to the @c tag attribute.
typedef NS_OPTIONS(NSInteger, MenuItemTag) { typedef NS_OPTIONS(NSInteger, MenuItemTag) {
/// Item visible at the very first menu level /// Item visible at the very first menu level
ScopeGlobal = 1, ScopeGlobal = 2,
/// Item visible at each grouping, e.g., multiple feeds in one group /// Item visible at each grouping, e.g., multiple feeds in one group
ScopeGroup = 2, ScopeGroup = 4,
/// Item visible at the deepest menu level (@c FeedItem elements and header) /// Item visible at the deepest menu level (@c FeedItem elements and header)
ScopeLocal = 4, ScopeLocal = 8,
/// @c NSMenuItem is an alternative
ScopeAlternative = 8,
/// ///
TagPreferences = (1 << 4), TagPreferences = (1 << 4),
TagPauseUpdates = (2 << 4), TagPauseUpdates = (2 << 4),
@@ -79,12 +77,12 @@ typedef NS_OPTIONS(NSInteger, MenuItemTag) {
- (void)printUnreadRecurisve:(NSMenu*)menu str:(NSString*)prefix { - (void)printUnreadRecurisve:(NSMenu*)menu str:(NSString*)prefix {
for (NSMenuItem *item in menu.itemArray) { for (NSMenuItem *item in menu.itemArray) {
if (!item.hasUnread) continue;
MenuItemInfo *info = item.representedObject; MenuItemInfo *info = item.representedObject;
if (!info) continue;
id obj = [StoreCoordinator objectWithID:info.objID]; id obj = [StoreCoordinator objectWithID:info.objID];
if ([obj isKindOfClass:[FeedItem class]]) if ([obj isKindOfClass:[FeedItem class]] && ([obj unread] > 0 || item.unreadCount > 0))
NSLog(@"%@ %@ (%d == %d)", prefix, item.title, item.unreadCount, [obj unread]); NSLog(@"%@ %@ (%d == %d)", prefix, item.title, item.unreadCount, [obj unread]);
else else if (item.hasUnread)
NSLog(@"%@ %@ (%d)", prefix, item.title, item.unreadCount); NSLog(@"%@ %@ (%d)", prefix, item.title, item.unreadCount);
if (item.hasSubmenu) { if (item.hasSubmenu) {
[self printUnreadRecurisve:item.submenu str:[NSString stringWithFormat:@" %@", prefix]]; [self printUnreadRecurisve:item.submenu str:[NSString stringWithFormat:@" %@", prefix]];
@@ -242,10 +240,11 @@ typedef NS_OPTIONS(NSInteger, MenuItemTag) {
[self addTitle:NSLocalizedString(@"Mark all unread", nil) selector:@selector(markAllUnread:) toMenu:menu tag:TagMarkAllUnread | scope]; [self addTitle:NSLocalizedString(@"Mark all unread", nil) selector:@selector(markAllUnread:) toMenu:menu tag:TagMarkAllUnread | scope];
[self addTitle:NSLocalizedString(@"Open all unread", nil) selector:@selector(openAllUnread:) toMenu:menu tag:TagOpenAllUnread | scope]; [self addTitle:NSLocalizedString(@"Open all unread", nil) selector:@selector(openAllUnread:) toMenu:menu tag:TagOpenAllUnread | scope];
NSString *alternateTitle = [NSString stringWithFormat:NSLocalizedString(@"Open a few unread (%d)", nil), 3]; NSMenuItem *openSomeUrls = [menu.itemArray.lastObject copy];
[self addTitle:alternateTitle selector:@selector(openAllUnread:) toMenu:menu tag:TagOpenAllUnread | scope | ScopeAlternative]; openSomeUrls.title = [NSString stringWithFormat:NSLocalizedString(@"Open a few unread (%d)", nil), 3];
menu.itemArray.lastObject.alternate = YES; openSomeUrls.alternate = YES;
menu.itemArray.lastObject.keyEquivalentModifierMask = NSEventModifierFlagOption; openSomeUrls.keyEquivalentModifierMask = NSEventModifierFlagOption;
[menu addItem:openSomeUrls];
[menu addItem:[NSMenuItem separatorItem]]; [menu addItem:[NSMenuItem separatorItem]];
return menu; return menu;
@@ -281,7 +280,7 @@ typedef NS_OPTIONS(NSInteger, MenuItemTag) {
NSLocalizedString(@"Preferences", nil)]; NSLocalizedString(@"Preferences", nil)];
// one time token to set reference to nil, which will release window // one time token to set reference to nil, which will release window
NSNotificationCenter * __weak center = [NSNotificationCenter defaultCenter]; NSNotificationCenter * __weak center = [NSNotificationCenter defaultCenter];
id __block token = [center addObserverForName:NSWindowWillCloseNotification object:self.prefWindow.window queue:nil usingBlock:^(NSNotification *note) { __block id token = [center addObserverForName:NSWindowWillCloseNotification object:self.prefWindow.window queue:nil usingBlock:^(NSNotification *note) {
self.prefWindow = nil; self.prefWindow = nil;
[center removeObserver:token]; [center removeObserver:token];
}]; }];

View File

@@ -66,9 +66,8 @@
return [[self getContext] objectWithID:objID]; return [[self getContext] objectWithID:objID];
} }
+ (Feed*)createFeedFromDictionary:(NSDictionary*)obj inContext:(NSManagedObjectContext*)moc { + (Feed*)createFeedFromDictionary:(NSDictionary*)obj inContext:(NSManagedObjectContext*)context {
// NSManagedObjectContext *moc = [self getContext]; Feed *a = [[Feed alloc] initWithEntity:Feed.entity insertIntoManagedObjectContext:context];
Feed *a = [[Feed alloc] initWithEntity:Feed.entity insertIntoManagedObjectContext:moc];
a.title = obj[@"feed"][@"title"]; a.title = obj[@"feed"][@"title"];
a.subtitle = obj[@"feed"][@"subtitle"]; a.subtitle = obj[@"feed"][@"subtitle"];
a.author = obj[@"feed"][@"author"]; a.author = obj[@"feed"][@"author"];
@@ -79,7 +78,7 @@
a.date = obj[@"header"][@"date"]; a.date = obj[@"header"][@"date"];
a.modified = obj[@"header"][@"modified"]; a.modified = obj[@"header"][@"modified"];
for (NSDictionary *entry in obj[@"entries"]) { for (NSDictionary *entry in obj[@"entries"]) {
FeedItem *b = [[FeedItem alloc] initWithEntity:FeedItem.entity insertIntoManagedObjectContext:moc]; FeedItem *b = [[FeedItem alloc] initWithEntity:FeedItem.entity insertIntoManagedObjectContext:context];
b.title = entry[@"title"]; b.title = entry[@"title"];
b.subtitle = entry[@"subtitle"]; b.subtitle = entry[@"subtitle"];
b.author = entry[@"author"]; b.author = entry[@"author"];
@@ -87,7 +86,7 @@
b.published = entry[@"published"]; b.published = entry[@"published"];
b.summary = entry[@"summary"]; b.summary = entry[@"summary"];
for (NSString *tag in entry[@"tags"]) { for (NSString *tag in entry[@"tags"]) {
FeedTag *c = [[FeedTag alloc] initWithEntity:FeedTag.entity insertIntoManagedObjectContext:moc]; FeedTag *c = [[FeedTag alloc] initWithEntity:FeedTag.entity insertIntoManagedObjectContext:context];
c.name = tag; c.name = tag;
[b addTagsObject:c]; [b addTagsObject:c];
} }