Skip to content
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

Fix smart-folder lookup/selection #1868

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Vienna/Sources/Application/AppController+Notifications.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#import "Database.h"
#import "Folder.h"
#import "Vienna-Swift.h"

@implementation AppController (Notifications)

Expand Down Expand Up @@ -59,9 +60,14 @@ - (void)openWindowAndShowUnreadArticles {
}
[self showMainWindow:self];

Folder *unreadArticles = [db folderFromName:NSLocalizedString(@"Unread Articles", nil)];
if (unreadArticles) {
[self selectFolder:unreadArticles.itemId];
Criteria *unreadCriteria =
[[Criteria alloc] initWithField:MA_Field_Read
operatorType:VNACriteriaOperatorEqualTo
value:@"No"];
NSString *predicateFormat = unreadCriteria.predicate.predicateFormat;
Folder *smartFolder = [db folderForPredicateFormat:predicateFormat];
if (smartFolder) {
[self selectFolder:smartFolder.itemId];
}
}

Expand Down
26 changes: 17 additions & 9 deletions Vienna/Sources/Application/AppController.m
Original file line number Diff line number Diff line change
Expand Up @@ -1606,13 +1606,13 @@ -(void)doEditFolder:(Folder *)folder
*/
-(void)handleFolderSelection:(NSNotification *)nc
{
NSInteger newFolderId = ((TreeNode *)nc.object).nodeId;
TreeNode *treeNode = nc.object;

// We don't filter when we switch folders.
self.filterString = @"";

// Call through the controller to display the new folder.
[self.articleController displayFolder:newFolderId];
[self.articleController displayFolder:treeNode.nodeId];
[self updateSearchPlaceholderAndSearchMethod];

// Make sure article viewer is active
Expand All @@ -1624,13 +1624,21 @@ -(void)handleFolderSelection:(NSNotification *)nc

// If the user selects the unread-articles smart folder, then clear the
// relevant user notifications.
if (newFolderId == [db folderFromName:NSLocalizedString(@"Unread Articles", nil)].itemId) {
NSUserNotificationCenter *center = NSUserNotificationCenter.defaultUserNotificationCenter;
[center.deliveredNotifications enumerateObjectsUsingBlock:^(NSUserNotification * notification, NSUInteger idx, BOOL *stop) {
if ([notification.userInfo[UserNotificationContextKey] isEqualToString:UserNotificationContextFetchCompleted]) {
[center removeDeliveredNotification:notification];
}
}];
if (treeNode.folder.type == VNAFolderTypeSmart) {
Criteria *unreadCriteria =
[[Criteria alloc] initWithField:MA_Field_Read
operatorType:VNACriteriaOperatorEqualTo
value:@"No"];
NSString *predicateFormat = unreadCriteria.predicate.predicateFormat;
Folder *smartFolder = [db folderForPredicateFormat:predicateFormat];
if ([smartFolder isEqual:treeNode.folder]) {
NSUserNotificationCenter *center = NSUserNotificationCenter.defaultUserNotificationCenter;
[center.deliveredNotifications enumerateObjectsUsingBlock:^(NSUserNotification *notification, NSUInteger idx, BOOL *stop) {
if ([notification.userInfo[UserNotificationContextKey] isEqualToString:UserNotificationContextFetchCompleted]) {
[center removeDeliveredNotification:notification];
}
}];
}
}
}

Expand Down
19 changes: 18 additions & 1 deletion Vienna/Sources/Database/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,27 @@ extern NSNotificationName const VNADatabaseDidDeleteFolderNotification;
-(NSArray *)arrayOfAllFolders;
-(NSArray *)arrayOfFolders:(NSInteger)parentId;
-(Folder *)folderFromID:(NSInteger)wantedId;
/*!
* folderFromFeedURL
*
* @param wantedFeedURL The feed URL the folder is wanted for
*
* @return An RSSFolder that is subscribed to the specified feed URL.
*/
-(Folder *)folderFromFeedURL:(NSString *)wantedFeedURL;
/*!
* folderFromRemoteId
*
* @param wantedRemoteId The remote identifier the folder is wanted for
*
* @return An OpenReaderFolder that corresponds
*/
-(Folder *)folderFromRemoteId:(NSString *)wantedRemoteId;
-(Folder *)folderFromName:(NSString *)wantedName;
/// Returns a smart folder for the predicate format string.
/// - Parameter predicateFormat: An NSPredicate format string.
/// - Returns: A Folder of type `VNAFolderTypeSmart` or `nil`.
- (Folder *)folderForPredicateFormat:(NSString *)predicateFormat;
-(NSString *)sqlScopeForFolder:(Folder *)folder flags:(VNAQueryScope)scopeFlags field:(NSString *)field;
-(NSInteger)addFolder:(NSInteger)parentId afterChild:(NSInteger)predecessorId folderName:(NSString *)name type:(NSInteger)type canAppendIndex:(BOOL)canAppendIndex;
-(BOOL)deleteFolder:(NSInteger)folderId;
Expand Down Expand Up @@ -100,7 +118,6 @@ extern NSNotificationName const VNADatabaseDidDeleteFolderNotification;
subscriptionURL:(NSString *)url remoteId:(NSString *)remoteId;

// Smart folder functions
-(void)initSmartfoldersDict;
-(NSInteger)addSmartFolder:(NSString *)folderName underParent:(NSInteger)parentId withQuery:(CriteriaTree *)criteriaTree;
-(void)updateSearchFolder:(NSInteger)folderId withFolder:(NSString *)folderName withQuery:(CriteriaTree *)criteriaTree;
-(CriteriaTree *)searchStringForSmartFolder:(NSInteger)folderId;
Expand Down
57 changes: 26 additions & 31 deletions Vienna/Sources/Database/Database.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@
@interface Database ()

@property (nonatomic) BOOL initializedfoldersDict;
@property (nonatomic) BOOL initializedSmartfoldersDict;
@property (nonatomic) NSMutableArray *fieldsOrdered;
@property (nonatomic) NSMutableDictionary *fieldsByName;
@property (nonatomic) NSMutableDictionary *foldersDict;
@property (nonatomic) NSMutableDictionary *smartfoldersDict;
@property (nonatomic) NSMutableDictionary<NSNumber *, CriteriaTree *> *smartfoldersDict;
@property (readwrite, nonatomic) BOOL readOnly;
@property (readwrite, nonatomic) NSInteger countOfUnread;

Expand Down Expand Up @@ -77,12 +76,10 @@ - (instancetype)initWithDatabaseAtPath:(NSString *)dbPath
self = [super init];
if (self) {
_initializedfoldersDict = NO;
_initializedSmartfoldersDict = NO;
_countOfUnread = 0;
_trashFolder = nil;
_searchFolder = nil;
_searchString = @"";
_smartfoldersDict = [[NSMutableDictionary alloc] init];
_foldersDict = [[NSMutableDictionary alloc] init];
[self initaliseFields];
_databaseQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath];
Expand Down Expand Up @@ -1431,14 +1428,6 @@ -(Folder *)folderFromName:(NSString *)wantedName
return folder;
}


/*!
* folderFromFeedURL
*
* @param wantedFeedURL The feed URL the folder is wanted for
*
* @return An RSSFolder that is subscribed to the specified feed URL.
*/
-(Folder *)folderFromFeedURL:(NSString *)wantedFeedURL
{
Folder * folder;
Expand All @@ -1451,14 +1440,6 @@ -(Folder *)folderFromFeedURL:(NSString *)wantedFeedURL
return folder;
}

/*!
* folderFromRemoteId
*
* @param wantedRemoteId The remote identifier the folder is wanted for
*
* @return An OpenReaderFolder that corresponds
*/

-(Folder *)folderFromRemoteId:(NSString *)wantedRemoteId
{
Folder * folder;
Expand All @@ -1471,6 +1452,23 @@ -(Folder *)folderFromRemoteId:(NSString *)wantedRemoteId
return folder;
}

- (Folder *)folderForPredicateFormat:(NSString *)predicateFormat
{
__block Folder *folder;
[self.smartfoldersDict enumerateKeysAndObjectsUsingBlock:^(
NSNumber *folderID, CriteriaTree *criteriaTree, BOOL *stop) {
// Different predicate types that cannot be compared with -isEqual: can
// have the same format string. For example, a comparison predicate and
// an all/any compound predicate with a comparison predicate can result
// in the same format string.
if ([criteriaTree.predicate.predicateFormat isEqualToString:predicateFormat]) {
folder = [self folderFromID:folderID.integerValue];
*stop = YES;
}
}];
return folder;
}

/* handleAutoSortFoldersTreeChange
* Called when preference changes for sorting folders tree.
* Store the new method in the database.
Expand Down Expand Up @@ -1775,12 +1773,12 @@ -(BOOL)deleteArticle:(Article *)article
return NO;
}

/* initSmartfoldersDict
* Preloads all the smart folders into the smartfoldersDict dictionary.
*/
-(void)initSmartfoldersDict
- (NSMutableDictionary<NSNumber *, CriteriaTree *> *)smartfoldersDict
{
if (!self.initializedSmartfoldersDict) {
if (!_smartfoldersDict) {
_smartfoldersDict = [[NSMutableDictionary alloc] init];

// Preload all the smart folders into the dictionary.
FMDatabaseQueue *queue = self.databaseQueue;
// Make sure we have a database queue.
NSAssert(queue, @"Database queue not assigned for this item");
Expand All @@ -1792,12 +1790,12 @@ -(void)initSmartfoldersDict
NSString * search_string = [results stringForColumnIndex:1];

CriteriaTree * criteriaTree = [[CriteriaTree alloc] initWithString:search_string];
self.smartfoldersDict[@(folderId)] = criteriaTree;
_smartfoldersDict[@(folderId)] = criteriaTree;
}
[results close];
}];
self.initializedSmartfoldersDict = YES;
}
return _smartfoldersDict;
}

/* searchStringForSmartFolder
Expand All @@ -1806,7 +1804,6 @@ -(void)initSmartfoldersDict
*/
-(CriteriaTree *)searchStringForSmartFolder:(NSInteger)folderId
{
[self initSmartfoldersDict];
return self.smartfoldersDict[@(folderId)];
}

Expand Down Expand Up @@ -2154,7 +2151,6 @@ -(CriteriaTree *)criteriaForFolder:(NSInteger)folderId
}

if (folder.type == VNAFolderTypeSmart) {
[self initSmartfoldersDict];
return self.smartfoldersDict[@(folderId)];
}

Expand Down Expand Up @@ -2556,11 +2552,10 @@ -(void)close
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.foldersDict removeAllObjects];
[self.smartfoldersDict removeAllObjects];
self.smartfoldersDict = nil;
self.trashFolder = nil;
self.searchFolder = nil;
self.initializedfoldersDict = NO;
self.initializedSmartfoldersDict = NO;
_countOfUnread = 0;
[self.databaseQueue close];
}
Expand Down