[Sandbox / Core Data] Refine global variable usage

Refine the global variable usage, eliminating double underscore prefixes
and trusting that static global variables will exist in the object where
the class was created. Got rid of that nasty NSApplication extension
hack that was previously in use.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
swiftingly
Christopher Snowhill 2022-06-21 00:01:07 -07:00
parent 4ac566c308
commit 5cb08291bf
8 changed files with 48 additions and 68 deletions

View File

@ -19,10 +19,6 @@
@class SpotlightWindowController; @class SpotlightWindowController;
@class PlaybackController; @class PlaybackController;
@interface NSApplication (CoreDataStorageExtension)
- (NSPersistentContainer *_Nonnull)sharedPersistentContainer;
@end
typedef NS_ENUM(NSInteger, RepeatMode) { typedef NS_ENUM(NSInteger, RepeatMode) {
RepeatModeNoRepeat = 0, RepeatModeNoRepeat = 0,
RepeatModeRepeatOne, RepeatModeRepeatOne,
@ -69,7 +65,6 @@ typedef NS_ENUM(NSInteger, URLOrigin) {
@property(retain) NSString *_Nullable totalTime; @property(retain) NSString *_Nullable totalTime;
@property(retain) NSString *_Nullable currentStatus; @property(retain) NSString *_Nullable currentStatus;
@property(strong, nonatomic, readonly) NSOperationQueue *_Nonnull persistentContainerQueue;
@property(strong, nonatomic, readonly) NSPersistentContainer *_Nonnull persistentContainer; @property(strong, nonatomic, readonly) NSPersistentContainer *_Nonnull persistentContainer;
@property(strong, nonatomic, readonly) NSMutableDictionary<NSString *, AlbumArtwork *> *_Nonnull persistentArtStorage; @property(strong, nonatomic, readonly) NSMutableDictionary<NSString *, AlbumArtwork *> *_Nonnull persistentArtStorage;
@ -139,6 +134,8 @@ typedef NS_ENUM(NSInteger, URLOrigin) {
- (void)readQueueFromDataStore; - (void)readQueueFromDataStore;
- (void)readShuffleListFromDataStore; - (void)readShuffleListFromDataStore;
+ (NSPersistentContainer *_Nonnull)sharedPersistentContainer;
// reload metadata of selection // reload metadata of selection
- (IBAction)reloadTags:(id _Nullable)sender; - (IBAction)reloadTags:(id _Nullable)sender;

View File

@ -27,13 +27,7 @@
extern BOOL kAppControllerShuttingDown; extern BOOL kAppControllerShuttingDown;
NSPersistentContainer *__persistentContainer = nil; NSPersistentContainer *kPersistentContainer = nil;
@implementation NSApplication (CoreDataStorageExtension)
- (NSPersistentContainer *_Nonnull)sharedPersistentContainer {
return __persistentContainer;
}
@end
@implementation PlaylistController @implementation PlaylistController
@ -43,7 +37,7 @@ NSPersistentContainer *__persistentContainer = nil;
static NSArray *cellIdentifiers = nil; static NSArray *cellIdentifiers = nil;
NSMutableDictionary<NSString *, AlbumArtwork *> *__artworkDictionary = nil; NSMutableDictionary<NSString *, AlbumArtwork *> *kArtworkDictionary = nil;
static void *playlistControllerContext = &playlistControllerContext; static void *playlistControllerContext = &playlistControllerContext;
@ -126,16 +120,20 @@ static void *playlistControllerContext = &playlistControllerContext;
} }
}]; }];
__persistentContainer = self.persistentContainer; kPersistentContainer = self.persistentContainer;
self.persistentContainer.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy; self.persistentContainer.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
_persistentArtStorage = [[NSMutableDictionary alloc] init]; _persistentArtStorage = [[NSMutableDictionary alloc] init];
__artworkDictionary = self.persistentArtStorage; kArtworkDictionary = self.persistentArtStorage;
return self; return self;
} }
+ (NSPersistentContainer *)sharedPersistentContainer {
return kPersistentContainer;
}
- (void)awakeFromNib { - (void)awakeFromNib {
[super awakeFromNib]; [super awakeFromNib];

View File

@ -16,8 +16,8 @@
#import "SHA256Digest.h" #import "SHA256Digest.h"
#import "SecondsFormatter.h" #import "SecondsFormatter.h"
extern NSPersistentContainer *__persistentContainer; extern NSPersistentContainer *kPersistentContainer;
extern NSMutableDictionary<NSString *, AlbumArtwork *> *__artworkDictionary; extern NSMutableDictionary<NSString *, AlbumArtwork *> *kArtworkDictionary;
@implementation PlaylistEntry (Extension) @implementation PlaylistEntry (Extension)
@ -344,7 +344,7 @@ extern NSMutableDictionary<NSString *, AlbumArtwork *> *__artworkDictionary;
@dynamic albumArtInternal; @dynamic albumArtInternal;
- (NSData *)albumArtInternal { - (NSData *)albumArtInternal {
NSString *imageCacheTag = self.artHash; NSString *imageCacheTag = self.artHash;
return [__artworkDictionary objectForKey:imageCacheTag].artData; return [kArtworkDictionary objectForKey:imageCacheTag].artData;
} }
- (void)setAlbumArtInternal:(NSData *)albumArtInternal { - (void)setAlbumArtInternal:(NSData *)albumArtInternal {
@ -354,12 +354,12 @@ extern NSMutableDictionary<NSString *, AlbumArtwork *> *__artworkDictionary;
self.artHash = imageCacheTag; self.artHash = imageCacheTag;
if(![__artworkDictionary objectForKey:imageCacheTag]) { if(![kArtworkDictionary objectForKey:imageCacheTag]) {
AlbumArtwork *art = [NSEntityDescription insertNewObjectForEntityForName:@"AlbumArtwork" inManagedObjectContext:__persistentContainer.viewContext]; AlbumArtwork *art = [NSEntityDescription insertNewObjectForEntityForName:@"AlbumArtwork" inManagedObjectContext:kPersistentContainer.viewContext];
art.artHash = imageCacheTag; art.artHash = imageCacheTag;
art.artData = albumArtInternal; art.artData = albumArtInternal;
[__artworkDictionary setObject:art forKey:imageCacheTag]; [kArtworkDictionary setObject:art forKey:imageCacheTag];
} }
} }
@ -505,7 +505,7 @@ NSURL *_Nullable urlForPath(NSString *_Nullable path) {
request.predicate = predicate; request.predicate = predicate;
NSError *error = nil; NSError *error = nil;
NSArray *results = [__persistentContainer.viewContext executeFetchRequest:request error:&error]; NSArray *results = [kPersistentContainer.viewContext executeFetchRequest:request error:&error];
if(!results || [results count] < 1) { if(!results || [results count] < 1) {
NSPredicate *filenamePredicate = [NSPredicate predicateWithFormat:@"filename == %@", self.filename]; NSPredicate *filenamePredicate = [NSPredicate predicateWithFormat:@"filename == %@", self.filename];
@ -513,7 +513,7 @@ NSURL *_Nullable urlForPath(NSString *_Nullable path) {
request = [NSFetchRequest fetchRequestWithEntityName:@"PlayCount"]; request = [NSFetchRequest fetchRequestWithEntityName:@"PlayCount"];
request.predicate = filenamePredicate; request.predicate = filenamePredicate;
results = [__persistentContainer.viewContext executeFetchRequest:request error:&error]; results = [kPersistentContainer.viewContext executeFetchRequest:request error:&error];
} }
if(!results || [results count] < 1) return nil; if(!results || [results count] < 1) return nil;

View File

@ -41,7 +41,7 @@
@import Firebase; @import Firebase;
extern NSMutableDictionary<NSString *, AlbumArtwork *> *__artworkDictionary; extern NSMutableDictionary<NSString *, AlbumArtwork *> *kArtworkDictionary;
@implementation PlaylistLoader @implementation PlaylistLoader
@ -839,7 +839,7 @@ NSURL *_Nullable urlForPath(NSString *_Nullable path);
} }
for(AlbumArtwork *art in results) { for(AlbumArtwork *art in results) {
[__artworkDictionary setObject:art forKey:art.artHash]; [kArtworkDictionary setObject:art forKey:art.artHash];
} }
request = [NSFetchRequest fetchRequestWithEntityName:@"PlaylistEntry"]; request = [NSFetchRequest fetchRequestWithEntityName:@"PlaylistEntry"];

View File

@ -29,8 +29,7 @@
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"path" ascending:YES]; NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"path" ascending:YES];
[self setSortDescriptors:@[sortDescriptor]]; [self setSortDescriptors:@[sortDescriptor]];
if([NSApp respondsToSelector:@selector(sharedPersistentContainer)]) { NSPersistentContainer *pc = [NSClassFromString(@"PlaylistController") sharedPersistentContainer];
NSPersistentContainer *pc = [NSApp sharedPersistentContainer];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"SandboxToken"]; NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"SandboxToken"];
@ -49,7 +48,6 @@
[self addObject:@{ @"path": token.path, @"valid": (isStale ? NSLocalizedPrefString(@"ValidNo") : NSLocalizedPrefString(@"ValidYes")) }]; [self addObject:@{ @"path": token.path, @"valid": (isStale ? NSLocalizedPrefString(@"ValidNo") : NSLocalizedPrefString(@"ValidYes")) }];
} }
} }
}
} }
- (void)addUrl:(NSURL *)url { - (void)addUrl:(NSURL *)url {
@ -60,10 +58,7 @@
return; return;
} }
if(![NSApp respondsToSelector:@selector(sharedPersistentContainer)]) NSPersistentContainer *pc = [NSClassFromString(@"PlaylistController") sharedPersistentContainer];
return;
NSPersistentContainer *pc = [NSApp sharedPersistentContainer];
SandboxToken *token = [NSEntityDescription insertNewObjectForEntityForName:@"SandboxToken" inManagedObjectContext:pc.viewContext]; SandboxToken *token = [NSEntityDescription insertNewObjectForEntityForName:@"SandboxToken" inManagedObjectContext:pc.viewContext];
@ -81,10 +76,7 @@
} }
- (void)removePath:(NSString *)path { - (void)removePath:(NSString *)path {
if(![NSApp respondsToSelector:@selector(sharedPersistentContainer)]) NSPersistentContainer *pc = [NSClassFromString(@"PlaylistController") sharedPersistentContainer];
return;
NSPersistentContainer *pc = [NSApp sharedPersistentContainer];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"path == %@", path]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"path == %@", path];

View File

@ -13,7 +13,7 @@
// with format (entryKey, transformerName) // with format (entryKey, transformerName)
static NSDictionary *importKeys; static NSDictionary *importKeys;
extern NSPersistentContainer *__persistentContainer; extern NSPersistentContainer *kPersistentContainer;
@implementation SpotlightPlaylistEntry @implementation SpotlightPlaylistEntry
@ -41,7 +41,7 @@ extern NSPersistentContainer *__persistentContainer;
} }
+ (PlaylistEntry *)playlistEntryWithMetadataItem:(NSMetadataItem *)metadataItem { + (PlaylistEntry *)playlistEntryWithMetadataItem:(NSMetadataItem *)metadataItem {
PlaylistEntry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"PlaylistEntry" inManagedObjectContext:__persistentContainer.viewContext]; PlaylistEntry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"PlaylistEntry" inManagedObjectContext:kPersistentContainer.viewContext];
entry.deLeted = YES; entry.deLeted = YES;

View File

@ -14,7 +14,7 @@
#import "SHA256Digest.h" #import "SHA256Digest.h"
extern NSPersistentContainer *__persistentContainer; extern NSPersistentContainer *kPersistentContainer;
NSString *getDatabasePath(void) { NSString *getDatabasePath(void) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
@ -1443,7 +1443,7 @@ static SQLiteStore *g_sharedStore = nil;
#endif #endif
- (PlaylistEntry *_Nonnull)getTrack:(int64_t)trackId { - (PlaylistEntry *_Nonnull)getTrack:(int64_t)trackId {
PlaylistEntry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"PlaylistEntry" inManagedObjectContext:__persistentContainer.viewContext]; PlaylistEntry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"PlaylistEntry" inManagedObjectContext:kPersistentContainer.viewContext];
if(trackId < 0) { if(trackId < 0) {
entry.error = YES; entry.error = YES;
@ -1863,7 +1863,7 @@ static SQLiteStore *g_sharedStore = nil;
entry.index = index; entry.index = index;
entry.entryId = entryId; entry.entryId = entryId;
} else { } else {
[__persistentContainer.viewContext deleteObject:entry]; [kPersistentContainer.viewContext deleteObject:entry];
entry = nil; entry = nil;
} }
} }

View File

@ -17,17 +17,7 @@
#import "PlaylistController.h" #import "PlaylistController.h"
static SandboxBroker *__sharedSandboxBroker = nil; static SandboxBroker *kSharedSandboxBroker = nil;
@interface NSApplication (SandboxBrokerExtension)
- (SandboxBroker *)sharedSandboxBroker;
@end
@implementation NSApplication (SandboxBrokerExtension)
- (SandboxBroker *)sharedSandboxBroker {
return __sharedSandboxBroker;
}
@end
static NSURL *urlWithoutFragment(NSURL *u) { static NSURL *urlWithoutFragment(NSURL *u) {
if(![u isFileURL]) return u; if(![u isFileURL]) return u;
@ -111,9 +101,13 @@ static NSURL *urlWithoutFragment(NSURL *u) {
+ (id)sharedSandboxBroker { + (id)sharedSandboxBroker {
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
__sharedSandboxBroker = [[self alloc] init]; kSharedSandboxBroker = [[self alloc] init];
}); });
return [NSApp sharedSandboxBroker]; return kSharedSandboxBroker;
}
+ (NSPersistentContainer *)sharedPersistentContainer {
return [NSClassFromString(@"PlaylistController") sharedPersistentContainer];
} }
- (id)init { - (id)init {
@ -156,7 +150,7 @@ static NSURL *urlWithoutFragment(NSURL *u) {
} }
} }
NSPersistentContainer *pc = [NSApp sharedPersistentContainer]; NSPersistentContainer *pc = [SandboxBroker sharedPersistentContainer];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"path.length" ascending:NO]; NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"path.length" ascending:NO];
@ -196,7 +190,6 @@ static NSURL *urlWithoutFragment(NSURL *u) {
- (const void *)beginFolderAccess:(NSURL *)fileUrl { - (const void *)beginFolderAccess:(NSURL *)fileUrl {
NSURL *folderUrl = [urlWithoutFragment(fileUrl) URLByDeletingLastPathComponent]; NSURL *folderUrl = [urlWithoutFragment(fileUrl) URLByDeletingLastPathComponent];
if(![folderUrl isFileURL]) return NULL; if(![folderUrl isFileURL]) return NULL;
if(![NSApp respondsToSelector:@selector(sharedPersistentContainer)]) return NULL;
SandboxEntry *entry; SandboxEntry *entry;