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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -14,7 +14,7 @@
#import "SHA256Digest.h"
extern NSPersistentContainer *__persistentContainer;
extern NSPersistentContainer *kPersistentContainer;
NSString *getDatabasePath(void) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
@ -1443,7 +1443,7 @@ static SQLiteStore *g_sharedStore = nil;
#endif
- (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) {
entry.error = YES;
@ -1863,7 +1863,7 @@ static SQLiteStore *g_sharedStore = nil;
entry.index = index;
entry.entryId = entryId;
} else {
[__persistentContainer.viewContext deleteObject:entry];
[kPersistentContainer.viewContext deleteObject:entry];
entry = nil;
}
}

View File

@ -17,17 +17,7 @@
#import "PlaylistController.h"
static SandboxBroker *__sharedSandboxBroker = nil;
@interface NSApplication (SandboxBrokerExtension)
- (SandboxBroker *)sharedSandboxBroker;
@end
@implementation NSApplication (SandboxBrokerExtension)
- (SandboxBroker *)sharedSandboxBroker {
return __sharedSandboxBroker;
}
@end
static SandboxBroker *kSharedSandboxBroker = nil;
static NSURL *urlWithoutFragment(NSURL *u) {
if(![u isFileURL]) return u;
@ -111,9 +101,13 @@ static NSURL *urlWithoutFragment(NSURL *u) {
+ (id)sharedSandboxBroker {
static dispatch_once_t 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 {
@ -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];
@ -196,7 +190,6 @@ static NSURL *urlWithoutFragment(NSURL *u) {
- (const void *)beginFolderAccess:(NSURL *)fileUrl {
NSURL *folderUrl = [urlWithoutFragment(fileUrl) URLByDeletingLastPathComponent];
if(![folderUrl isFileURL]) return NULL;
if(![NSApp respondsToSelector:@selector(sharedPersistentContainer)]) return NULL;
SandboxEntry *entry;