Created
August 11, 2012 00:52
-
-
Save jcuffe/3319570 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Cocoa/Cocoa.h> | |
#import "DDHotKeyCenter.h" | |
@interface TwiddleAppDelegate : NSObject <NSApplicationDelegate> | |
@property (assign) IBOutlet NSWindow *window; | |
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; | |
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; | |
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; | |
- (void)registerIntervalHotKey; | |
- (IBAction)saveAction:(id)sender; | |
- (IBAction)startInterval:(id)sender; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "TwiddleAppDelegate.h" | |
#import "Interval+IntervalExtension.h" | |
@implementation TwiddleAppDelegate | |
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator; | |
@synthesize managedObjectModel = _managedObjectModel; | |
@synthesize managedObjectContext = _managedObjectContext; | |
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification | |
{ | |
[self registerIntervalHotKey]; | |
NSManagedObjectContext *moc = [self managedObjectContext]; | |
Interval *interval = [NSEntityDescription insertNewObjectForEntityForName:@"Interval" inManagedObjectContext:moc]; | |
interval.startTime = [NSDate date]; | |
NSError *error = nil; | |
[moc save:&error]; | |
} | |
- (void)registerIntervalHotKey { | |
DDHotKeyCenter *startInterval = [[DDHotKeyCenter alloc] init]; | |
DDHotKeyCenter *stopInterval = [[DDHotKeyCenter alloc] init]; | |
DDHotKeyTask startIntervalTask = ^(NSEvent *hkEvent) { | |
[Interval startInterval]; | |
}; | |
DDHotKeyTask stopIntervalTask = ^(NSEvent *hkEvent) { | |
[Interval stopInterval]; | |
}; | |
if (![startInterval registerHotKeyWithKeyCode:21 modifierFlags:(NSControlKeyMask | NSCommandKeyMask) task:startIntervalTask]) { | |
return; | |
} | |
if (![stopInterval registerHotKeyWithKeyCode:22 modifierFlags:(NSControlKeyMask | NSCommandKeyMask) task:stopIntervalTask]) { | |
return; | |
} | |
} | |
// Returns the directory the application uses to store the Core Data store file. This code uses a directory named "cuffeco.twiddle" in the user's Application Support directory. | |
- (NSURL *)applicationFilesDirectory | |
{ | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
NSURL *appSupportURL = [[fileManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask] lastObject]; | |
return [appSupportURL URLByAppendingPathComponent:@"cuffeco.twiddle"]; | |
} | |
// Creates if necessary and returns the managed object model for the application. | |
- (NSManagedObjectModel *)managedObjectModel | |
{ | |
if (_managedObjectModel) { | |
return _managedObjectModel; | |
} | |
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"twiddle" withExtension:@"momd"]; | |
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; | |
return _managedObjectModel; | |
} | |
// Returns the persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. (The directory for the store is created, if necessary.) | |
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator | |
{ | |
if (_persistentStoreCoordinator) { | |
return _persistentStoreCoordinator; | |
} | |
NSManagedObjectModel *mom = [self managedObjectModel]; | |
if (!mom) { | |
NSLog(@"%@:%@ No model to generate a store from", [self class], NSStringFromSelector(_cmd)); | |
return nil; | |
} | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
NSURL *applicationFilesDirectory = [self applicationFilesDirectory]; | |
NSError *error = nil; | |
NSDictionary *properties = [applicationFilesDirectory resourceValuesForKeys:@[NSURLIsDirectoryKey] error:&error]; | |
if (!properties) { | |
BOOL ok = NO; | |
if ([error code] == NSFileReadNoSuchFileError) { | |
ok = [fileManager createDirectoryAtPath:[applicationFilesDirectory path] withIntermediateDirectories:YES attributes:nil error:&error]; | |
} | |
if (!ok) { | |
[[NSApplication sharedApplication] presentError:error]; | |
return nil; | |
} | |
} else { | |
if (![properties[NSURLIsDirectoryKey] boolValue]) { | |
// Customize and localize this error. | |
NSString *failureDescription = [NSString stringWithFormat:@"Expected a folder to store application data, found a file (%@).", [applicationFilesDirectory path]]; | |
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; | |
[dict setValue:failureDescription forKey:NSLocalizedDescriptionKey]; | |
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:101 userInfo:dict]; | |
[[NSApplication sharedApplication] presentError:error]; | |
return nil; | |
} | |
} | |
NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"twiddle.storedata"]; | |
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom]; | |
if (![coordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:nil error:&error]) { | |
[[NSApplication sharedApplication] presentError:error]; | |
return nil; | |
} | |
_persistentStoreCoordinator = coordinator; | |
return _persistentStoreCoordinator; | |
} | |
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) | |
- (NSManagedObjectContext *)managedObjectContext | |
{ | |
if (_managedObjectContext) { | |
return _managedObjectContext; | |
} | |
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; | |
if (!coordinator) { | |
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; | |
[dict setValue:@"Failed to initialize the store" forKey:NSLocalizedDescriptionKey]; | |
[dict setValue:@"There was an error building up the data file." forKey:NSLocalizedFailureReasonErrorKey]; | |
NSError *error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict]; | |
[[NSApplication sharedApplication] presentError:error]; | |
return nil; | |
} | |
_managedObjectContext = [[NSManagedObjectContext alloc] init]; | |
[_managedObjectContext setPersistentStoreCoordinator:coordinator]; | |
return _managedObjectContext; | |
} | |
// Returns the NSUndoManager for the application. In this case, the manager returned is that of the managed object context for the application. | |
- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window | |
{ | |
return [[self managedObjectContext] undoManager]; | |
} | |
// Performs the save action for the application, which is to send the save: message to the application's managed object context. Any encountered errors are presented to the user. | |
- (IBAction)saveAction:(id)sender | |
{ | |
NSError *error = nil; | |
if (![[self managedObjectContext] commitEditing]) { | |
NSLog(@"%@:%@ unable to commit editing before saving", [self class], NSStringFromSelector(_cmd)); | |
} | |
if (![[self managedObjectContext] save:&error]) { | |
[[NSApplication sharedApplication] presentError:error]; | |
} | |
} | |
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender | |
{ | |
// Save changes in the application's managed object context before the application terminates. | |
if (!_managedObjectContext) { | |
return NSTerminateNow; | |
} | |
if (![[self managedObjectContext] commitEditing]) { | |
NSLog(@"%@:%@ unable to commit editing to terminate", [self class], NSStringFromSelector(_cmd)); | |
return NSTerminateCancel; | |
} | |
if (![[self managedObjectContext] hasChanges]) { | |
return NSTerminateNow; | |
} | |
NSError *error = nil; | |
if (![[self managedObjectContext] save:&error]) { | |
// Customize this code block to include application-specific recovery steps. | |
BOOL result = [sender presentError:error]; | |
if (result) { | |
return NSTerminateCancel; | |
} | |
NSString *question = NSLocalizedString(@"Could not save changes while quitting. Quit anyway?", @"Quit without saves error question message"); | |
NSString *info = NSLocalizedString(@"Quitting now will lose any changes you have made since the last successful save", @"Quit without saves error question info"); | |
NSString *quitButton = NSLocalizedString(@"Quit anyway", @"Quit anyway button title"); | |
NSString *cancelButton = NSLocalizedString(@"Cancel", @"Cancel button title"); | |
NSAlert *alert = [[NSAlert alloc] init]; | |
[alert setMessageText:question]; | |
[alert setInformativeText:info]; | |
[alert addButtonWithTitle:quitButton]; | |
[alert addButtonWithTitle:cancelButton]; | |
NSInteger answer = [alert runModal]; | |
if (answer == NSAlertAlternateReturn) { | |
return NSTerminateCancel; | |
} | |
} | |
return NSTerminateNow; | |
} | |
- (void)startInterval:(id)sender { | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment