Some cleanup + behaviour improvements - code smell

Code smell atm, duplicated code, needs improvement
Will now appear on status bar if unarchiving a file, disappear on completion
Now shows preferences window when in hide in dock mode (if opening app without a file)
Currently wont close the app if in hidedock mode and user closes the window. -- Not sure about this behaviour. Should probably close
Will unarchive files if the app is already open (loose testing works, but needs proper testing)
This commit is contained in:
Robert McGovern 2021-05-31 22:31:45 +01:00
parent e7b7a375e4
commit b1337f11ca
2 changed files with 59 additions and 48 deletions

View File

@ -30,6 +30,8 @@
@synthesize window, passwordView, passwordField, preferencesWindowController, unarchiver, userDefaults, statusBarItem, arrayOfFilesToProcess; @synthesize window, passwordView, passwordField, preferencesWindowController, unarchiver, userDefaults, statusBarItem, arrayOfFilesToProcess;
BOOL appRunning = NO;
- (void) applicationWillFinishLaunching:(NSNotification *)notification { - (void) applicationWillFinishLaunching:(NSNotification *)notification {
NSLog(@"applicationWillFinishLaunching"); NSLog(@"applicationWillFinishLaunching");
// The following is used to determine is the left or right shift keys were depressed // The following is used to determine is the left or right shift keys were depressed
@ -40,6 +42,10 @@
NSLog(@"Shift or Right Shift"); NSLog(@"Shift or Right Shift");
userDefaults = [TDNUserDefaults sharedInstance]; userDefaults = [TDNUserDefaults sharedInstance];
if (userDefaults.hideDock) {
[self hideDockIcon:TRUE];
}
} }
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
@ -48,36 +54,34 @@
[self requestUserPermissionForNotifications]; [self requestUserPermissionForNotifications];
if (arrayOfFilesToProcess == nil || arrayOfFilesToProcess.count == 0) { if (arrayOfFilesToProcess == nil || arrayOfFilesToProcess.count == 0) {
if (userDefaults.hideDock) {
[self hideDockIcon:TRUE];
}
preferencesWindowController = [[TDNPreferencesWindowController alloc] init]; preferencesWindowController = [[TDNPreferencesWindowController alloc] init];
preferencesWindowController.quietUnrar = self; preferencesWindowController.quietUnrar = self;
[preferencesWindowController showWindow:nil]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self->preferencesWindowController.window makeKeyAndOrderFront:self];
[NSApp activateIgnoringOtherApps:YES];
});
appRunning = YES;
} else { } else {
unarchiver = [[TDNUnarchiver alloc] init]; unarchiver = [[TDNUnarchiver alloc] init];
unarchiver.quietUnrar = self; unarchiver.quietUnrar = self;
[self requestUserPermissionForNotifications]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
for (NSString * filename in self->arrayOfFilesToProcess) {
if (userDefaults.hideDock) { BOOL extracted = [self->unarchiver extractArchiveWithFilename:filename];
[self hideDockIcon:TRUE]; if (extracted) {
} // post notification based on user preference
if (self->userDefaults.showNotification && self->userDefaults.notificationsAllowed) { // if show notification + permission granted ...
for (NSString * filename in arrayOfFilesToProcess) { [self postNotificationUncompressedFile:filename];
BOOL extracted = [unarchiver extractArchiveWithFilename:filename]; // maybe don't want to spam lots of notifications if unarchiving a lot of archives
if (extracted) { }
// post notification based on user preference
if (userDefaults.showNotification && userDefaults.notificationsAllowed) { // if show notification + permission granted ...
[self postNotificationUncompressedFile:filename];
} }
} }
}
[[NSApplication sharedApplication] terminate:self]; [[NSApplication sharedApplication] terminate:self];
});
} }
} }
@ -98,32 +102,40 @@
arrayOfFilesToProcess = arrayOfFilenames; arrayOfFilesToProcess = arrayOfFilenames;
// unarchiver = [[TDNUnarchiver alloc] init]; if (appRunning) {
// unarchiver.quietUnrar = self; if (!unarchiver) {
// unarchiver = [[TDNUnarchiver alloc] init];
// userDefaults = [TDNUserDefaults sharedInstance]; unarchiver.quietUnrar = self;
// }
// [self requestUserPermissionForNotifications]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// for (NSString * filename in self->arrayOfFilesToProcess) {
// if (userDefaults.hideDock) { BOOL extracted = [self->unarchiver extractArchiveWithFilename:filename];
// [self hideDockIcon:TRUE]; if (extracted) {
// } // post notification based on user preference
// if (self->userDefaults.showNotification && self->userDefaults.notificationsAllowed) { // if show notification + permission granted ...
// for (NSString * filename in arrayOfFilenames) { [self postNotificationUncompressedFile:filename];
// BOOL extracted = [unarchiver extractArchiveWithFilename:filename]; // maybe don't want to spam lots of notifications if unarchiving a lot of archives
// if (extracted) { }
// // post notification based on user preference }
// if (userDefaults.showNotification && userDefaults.notificationsAllowed) { // if show notification + permission granted ... }
// [self postNotificationUncompressedFile:filename]; });
// } }
// } }
// }
// - (BOOL)application:(id)sender openFileWithoutUI:(NSString *)filename {
// [[NSApplication sharedApplication] terminate:self]; NSLog(@"called openFileWithoutUI %@", filename);
return YES;
} }
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender { - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
return YES; // Possibly not the behaviour wanted
if (userDefaults.hideDock) {
NSLog(@"applicationShouldTerminateAfterLastWindowClosed -- NO");
return NO;
} else {
NSLog(@"applicationShouldTerminateAfterLastWindowClosed -- YES");
return YES;
}
} }
#pragma mark UI Methods #pragma mark UI Methods
@ -193,12 +205,9 @@
- (void) hideDockIcon: (BOOL) hide { - (void) hideDockIcon: (BOOL) hide {
BOOL result; BOOL result;
if (hide) { if (hide) {
NSLog(@"Setting Policy to Accesosry"); NSLog(@"Setting Policy to Accessory");
result = [NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory]; result = [NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
NSLog(@"Result of setting ActivationPolicy %d", result); NSLog(@"Result of setting ActivationPolicy %d", result);
NSLog(@"%@", [[[NSApplication sharedApplication]delegate] description]);
[self showStatusBarItem:TRUE]; [self showStatusBarItem:TRUE];
} else { } else {
NSLog(@"Setting Policy to Regular"); NSLog(@"Setting Policy to Regular");

View File

@ -8,9 +8,9 @@ Original was written in 2009 as a little thing for me, and now its getting some
## TO DO ## TO DO
* Store preferences in User Defaults (or mac equvalent) * Store preferences in User Defaults
* ✅ Move code handling un archiving into seperate class * ✅ Move code handling un archiving into seperate class
* add model code for preferences * add model code for preferences
* add support for 7zip https://github.com/OlehKulykov/PLzmaSDK * add support for 7zip https://github.com/OlehKulykov/PLzmaSDK
* Investigate metal warning, something to ignore? * Investigate metal warning, something to ignore?
* Add testing * Add testing
@ -18,6 +18,8 @@ Original was written in 2009 as a little thing for me, and now its getting some
* if keeping my extractRARArchiveWithFilename method rather than unrarkit, swap to using the wide text process method * if keeping my extractRARArchiveWithFilename method rather than unrarkit, swap to using the wide text process method
* reduce menu to only essential - preferences + quit * reduce menu to only essential - preferences + quit
* about box with thanks & liecense info * about box with thanks & liecense info
* post notification on finishing
* what to do if app open and user unarchives a file? (apart from not make the preferences window front and central)
### Metal Warning ### Metal Warning