Skip to content

Commit

Permalink
One time Index all notes in simplenote on launch
Browse files Browse the repository at this point in the history
  • Loading branch information
charliescheer committed Apr 11, 2024
1 parent db42b63 commit aabd303
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Simplenote.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@
BA4C6D16264CA8C000B723A7 /* SignupRemoteTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA4C6D15264CA8C000B723A7 /* SignupRemoteTests.swift */; };
BA4C6D18264CAAF800B723A7 /* URLRequest+Simplenote.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA4C6D17264CAAF800B723A7 /* URLRequest+Simplenote.swift */; };
BA52005B2BC878F1003F1B75 /* CSSearchable+Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA52005A2BC878F1003F1B75 /* CSSearchable+Helpers.swift */; };
BA52005D2BC88397003F1B75 /* NSManagedObjectContext+Simplenote.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA52005C2BC88397003F1B75 /* NSManagedObjectContext+Simplenote.swift */; };
BA553F0827065E20007737E9 /* FontSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA553F0727065E20007737E9 /* FontSettings.swift */; };
BA553F0927065E20007737E9 /* FontSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA553F0727065E20007737E9 /* FontSettings.swift */; };
BA5F020526BB57F000581E92 /* NSAlert+Simplenote.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA5F020426BB57F000581E92 /* NSAlert+Simplenote.swift */; };
Expand Down Expand Up @@ -870,6 +871,7 @@
BA4C6D15264CA8C000B723A7 /* SignupRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignupRemoteTests.swift; sourceTree = "<group>"; };
BA4C6D17264CAAF800B723A7 /* URLRequest+Simplenote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URLRequest+Simplenote.swift"; sourceTree = "<group>"; };
BA52005A2BC878F1003F1B75 /* CSSearchable+Helpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CSSearchable+Helpers.swift"; sourceTree = "<group>"; };
BA52005C2BC88397003F1B75 /* NSManagedObjectContext+Simplenote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSManagedObjectContext+Simplenote.swift"; sourceTree = "<group>"; };
BA553F0727065E20007737E9 /* FontSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontSettings.swift; sourceTree = "<group>"; };
BA5F020426BB57F000581E92 /* NSAlert+Simplenote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSAlert+Simplenote.swift"; sourceTree = "<group>"; };
BA938CEB26ACFF4A00BE5A1D /* Remote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Remote.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1300,6 +1302,7 @@
BA5F020426BB57F000581E92 /* NSAlert+Simplenote.swift */,
BAFB544F26CCA7F1006E037C /* NSProgressIndicator+Simplenote.swift */,
BA2C65CA26FE996100FA84E1 /* NSButton+Extensions.swift */,
BA52005C2BC88397003F1B75 /* NSManagedObjectContext+Simplenote.swift */,
);
name = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -2159,6 +2162,7 @@
B59EA98124AA5EFA008ABE4B /* NoteMetrics.swift in Sources */,
B5EDF338258A8F1B0066D91D /* TagListFilter.swift in Sources */,
375D293621E033D1007AB25A /* document.c in Sources */,
BA52005D2BC88397003F1B75 /* NSManagedObjectContext+Simplenote.swift in Sources */,
B5609AEC24EEE7200097777A /* SPBucket+Simplenote.swift in Sources */,
B56FA7902437C672002CB9FF /* ColorStudio.swift in Sources */,
B5177CD025EEEEFB00A8D834 /* NSWindow+Transitions.swift in Sources */,
Expand Down
22 changes: 22 additions & 0 deletions Simplenote/NSManagedObjectContext+Simplenote.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// NSManagedObjectContext+Simplenote.swift
// Simplenote
//
// Created by Charlie Scheer on 4/11/24.
// Copyright © 2024 Simperium. All rights reserved.
//

import Foundation
import CoreData

extension NSManagedObjectContext {
@objc(fetchObjectsForEntityName: withPredicate: error:)
func fetchObjects(for entityName: String, withPredicate predicate: NSPredicate) throws -> Array<NSFetchRequestResult> {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>()
let entityDescription = NSEntityDescription.entity(forEntityName: entityName, in: self)

fetchRequest.entity = entityDescription

return try fetch(fetchRequest)
}
}
41 changes: 41 additions & 0 deletions Simplenote/SimplenoteAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
[self cleanupTags];
[self startListeningForThemeNotifications];

[self indexSpotlightItemsIfNeeded];

[SPTracker trackApplicationLaunched];
}

Expand Down Expand Up @@ -570,4 +572,43 @@ - (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window
return [[self managedObjectContext] undoManager];
}


#pragma mark ================================================================================
#pragma mark Spotlight
#pragma mark ================================================================================

- (void)indexSpotlightItemsIfNeeded
{
// This process should be executed *just once*, and only if the user is already logged in (AKA "Upgrade")
NSString *kSpotlightDidRunKey = @"SpotlightDidRunKey";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if ([defaults boolForKey:kSpotlightDidRunKey] == true) {
return;
}

[defaults setBool:true forKey:kSpotlightDidRunKey];
[defaults synchronize];

if (self.simperium.user.authenticated == false) {
return;
}

[self indexSpotlightItems];
}

- (void)indexSpotlightItems
{
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[context setParentContext:self.simperium.managedObjectContext];

[context performBlock:^{
NSArray *deleted = [context fetchObjectsForEntityName:@"Note" withPredicate:[NSPredicate predicateWithFormat:@"deleted == YES"] error:nil];
[[CSSearchableIndex defaultSearchableIndex] deleteSearchableNotes:deleted];

NSArray *notes = [context fetchObjectsForEntityName:@"Note" withPredicate:[NSPredicate predicateWithFormat:@"deleted == NO"] error:nil];
[[CSSearchableIndex defaultSearchableIndex] indexSearchableNotes:notes];
}];
}

@end

0 comments on commit aabd303

Please sign in to comment.