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;
BOOL appRunning = NO;
- (void) applicationWillFinishLaunching:(NSNotification *)notification {
NSLog(@"applicationWillFinishLaunching");
// The following is used to determine is the left or right shift keys were depressed
@ -40,6 +42,10 @@
NSLog(@"Shift or Right Shift");
userDefaults = [TDNUserDefaults sharedInstance];
if (userDefaults.hideDock) {
[self hideDockIcon:TRUE];
}
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
@ -48,36 +54,34 @@
[self requestUserPermissionForNotifications];
if (arrayOfFilesToProcess == nil || arrayOfFilesToProcess.count == 0) {
if (userDefaults.hideDock) {
[self hideDockIcon:TRUE];
}
preferencesWindowController = [[TDNPreferencesWindowController alloc] init];
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 {
unarchiver = [[TDNUnarchiver alloc] init];
unarchiver.quietUnrar = self;
[self requestUserPermissionForNotifications];
if (userDefaults.hideDock) {
[self hideDockIcon:TRUE];
}
for (NSString * filename in arrayOfFilesToProcess) {
BOOL extracted = [unarchiver extractArchiveWithFilename:filename];
if (extracted) {
// post notification based on user preference
if (userDefaults.showNotification && userDefaults.notificationsAllowed) { // if show notification + permission granted ...
[self postNotificationUncompressedFile:filename];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
for (NSString * filename in self->arrayOfFilesToProcess) {
BOOL extracted = [self->unarchiver extractArchiveWithFilename:filename];
if (extracted) {
// post notification based on user preference
if (self->userDefaults.showNotification && self->userDefaults.notificationsAllowed) { // if show notification + permission granted ...
[self postNotificationUncompressedFile:filename];
// maybe don't want to spam lots of notifications if unarchiving a lot of archives
}
}
}
}
[[NSApplication sharedApplication] terminate:self];
[[NSApplication sharedApplication] terminate:self];
});
}
}
@ -98,32 +102,40 @@
arrayOfFilesToProcess = arrayOfFilenames;
// unarchiver = [[TDNUnarchiver alloc] init];
// unarchiver.quietUnrar = self;
//
// userDefaults = [TDNUserDefaults sharedInstance];
//
// [self requestUserPermissionForNotifications];
//
// if (userDefaults.hideDock) {
// [self hideDockIcon:TRUE];
// }
//
// for (NSString * filename in arrayOfFilenames) {
// BOOL extracted = [unarchiver extractArchiveWithFilename:filename];
// 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];
if (appRunning) {
if (!unarchiver) {
unarchiver = [[TDNUnarchiver alloc] init];
unarchiver.quietUnrar = self;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
for (NSString * filename in self->arrayOfFilesToProcess) {
BOOL extracted = [self->unarchiver extractArchiveWithFilename:filename];
if (extracted) {
// post notification based on user preference
if (self->userDefaults.showNotification && self->userDefaults.notificationsAllowed) { // if show notification + permission granted ...
[self postNotificationUncompressedFile:filename];
// maybe don't want to spam lots of notifications if unarchiving a lot of archives
}
}
}
});
}
}
- (BOOL)application:(id)sender openFileWithoutUI:(NSString *)filename {
NSLog(@"called openFileWithoutUI %@", filename);
return YES;
}
- (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
@ -193,12 +205,9 @@
- (void) hideDockIcon: (BOOL) hide {
BOOL result;
if (hide) {
NSLog(@"Setting Policy to Accesosry");
NSLog(@"Setting Policy to Accessory");
result = [NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
NSLog(@"Result of setting ActivationPolicy %d", result);
NSLog(@"%@", [[[NSApplication sharedApplication]delegate] description]);
[self showStatusBarItem:TRUE];
} else {
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
* Store preferences in User Defaults (or mac equvalent)
* Store preferences in User Defaults
* ✅ 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
* Investigate metal warning, something to ignore?
* 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
* reduce menu to only essential - preferences + quit
* 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