From aabd303a1109145fdac4017f02826fe7343c6056 Mon Sep 17 00:00:00 2001 From: Charlie Scheer Date: Thu, 11 Apr 2024 14:54:31 -0600 Subject: [PATCH] One time Index all notes in simplenote on launch --- Simplenote.xcodeproj/project.pbxproj | 4 ++ .../NSManagedObjectContext+Simplenote.swift | 22 ++++++++++ Simplenote/SimplenoteAppDelegate.m | 41 +++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 Simplenote/NSManagedObjectContext+Simplenote.swift diff --git a/Simplenote.xcodeproj/project.pbxproj b/Simplenote.xcodeproj/project.pbxproj index 838f41f73..a92b75158 100644 --- a/Simplenote.xcodeproj/project.pbxproj +++ b/Simplenote.xcodeproj/project.pbxproj @@ -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 */; }; @@ -870,6 +871,7 @@ BA4C6D15264CA8C000B723A7 /* SignupRemoteTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignupRemoteTests.swift; sourceTree = ""; }; BA4C6D17264CAAF800B723A7 /* URLRequest+Simplenote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URLRequest+Simplenote.swift"; sourceTree = ""; }; BA52005A2BC878F1003F1B75 /* CSSearchable+Helpers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CSSearchable+Helpers.swift"; sourceTree = ""; }; + BA52005C2BC88397003F1B75 /* NSManagedObjectContext+Simplenote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSManagedObjectContext+Simplenote.swift"; sourceTree = ""; }; BA553F0727065E20007737E9 /* FontSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontSettings.swift; sourceTree = ""; }; BA5F020426BB57F000581E92 /* NSAlert+Simplenote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSAlert+Simplenote.swift"; sourceTree = ""; }; BA938CEB26ACFF4A00BE5A1D /* Remote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Remote.swift; sourceTree = ""; }; @@ -1300,6 +1302,7 @@ BA5F020426BB57F000581E92 /* NSAlert+Simplenote.swift */, BAFB544F26CCA7F1006E037C /* NSProgressIndicator+Simplenote.swift */, BA2C65CA26FE996100FA84E1 /* NSButton+Extensions.swift */, + BA52005C2BC88397003F1B75 /* NSManagedObjectContext+Simplenote.swift */, ); name = Extensions; sourceTree = ""; @@ -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 */, diff --git a/Simplenote/NSManagedObjectContext+Simplenote.swift b/Simplenote/NSManagedObjectContext+Simplenote.swift new file mode 100644 index 000000000..d106b2a5b --- /dev/null +++ b/Simplenote/NSManagedObjectContext+Simplenote.swift @@ -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 { + let fetchRequest = NSFetchRequest() + let entityDescription = NSEntityDescription.entity(forEntityName: entityName, in: self) + + fetchRequest.entity = entityDescription + + return try fetch(fetchRequest) + } +} diff --git a/Simplenote/SimplenoteAppDelegate.m b/Simplenote/SimplenoteAppDelegate.m index 1aa263cc5..61909dd0b 100644 --- a/Simplenote/SimplenoteAppDelegate.m +++ b/Simplenote/SimplenoteAppDelegate.m @@ -123,6 +123,8 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification [self cleanupTags]; [self startListeningForThemeNotifications]; + [self indexSpotlightItemsIfNeeded]; + [SPTracker trackApplicationLaunched]; } @@ -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