Skip to content
This repository has been archived by the owner on Mar 17, 2024. It is now read-only.

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
josh- committed Jul 30, 2015
1 parent 5414f80 commit 145e85e
Show file tree
Hide file tree
Showing 285 changed files with 7,612 additions and 802 deletions.
10 changes: 10 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[submodule "MonashTimetable/SSKeychain"]
path = MonashTimetable/SSKeychain
url = https://github.com/soffes/sskeychain.git
ignore = dirty
[submodule "CCNStatusItem"]
path = MonashTimetable/CCNStatusItem/
url = https://github.com/phranck/CCNStatusItem.git
[submodule "MonashTimetable/MXLCalendarManager"]
path = MonashTimetable/MXLCalendarManager
url = https://github.com/josh-/MXLCalendarManager.git
290 changes: 272 additions & 18 deletions MonashTimetable.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions MonashTimetable/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
// Copyright (c) 2015 Josh Parnham. All rights reserved.
//

#import <Cocoa/Cocoa.h>
@import Cocoa;

@interface AppDelegate : NSObject <NSApplicationDelegate>
#import "CCNStatusItem.h"

@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (strong, nonatomic) CCNStatusItem *statusItem;

@end

186 changes: 48 additions & 138 deletions MonashTimetable/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,171 +8,81 @@

#import "AppDelegate.h"

#import "LoginViewController.h"
#import "TimetableViewController.h"

#import "CredentialsManager.h"

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
- (IBAction)saveAction:(id)sender;

@property (strong, nonatomic) LoginViewController *loginViewController;
@property (strong, nonatomic) TimetableViewController *timetableViewController;

@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self addObservations];
self.statusItem = [CCNStatusItem sharedInstance];
[self presentViewController];
}

#pragma mark - Core Data stack
#pragma mark - Methods

@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize managedObjectContext = _managedObjectContext;

- (NSURL *)applicationDocumentsDirectory {
// The directory the application uses to store the Core Data store file. This code uses a directory named "com.joshparnham.MonashTimetable" in the user's Application Support directory.
NSURL *appSupportURL = [[[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask] lastObject];
return [appSupportURL URLByAppendingPathComponent:@"com.joshparnham.MonashTimetable"];
- (void)addObservations
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(authorizationStatusChanged) name:@"AuthorizationStatusDidChange" object:nil];
}

- (NSManagedObjectModel *)managedObjectModel {
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
if (_managedObjectModel) {
return _managedObjectModel;
}

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MonashTimetable" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
// 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.)
if (_persistentStoreCoordinator) {
return _persistentStoreCoordinator;
}

NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *applicationDocumentsDirectory = [self applicationDocumentsDirectory];
BOOL shouldFail = NO;
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
- (void)presentViewController
{
NSImage *iconImage = [NSImage imageNamed:@"StatusIcon"];
[iconImage setSize:NSMakeSize(16, 16)];

// Make sure the application files directory is there
NSDictionary *properties = [applicationDocumentsDirectory resourceValuesForKeys:@[NSURLIsDirectoryKey] error:&error];
if (properties) {
if (![properties[NSURLIsDirectoryKey] boolValue]) {
failureReason = [NSString stringWithFormat:@"Expected a folder to store application data, found a file (%@).", [applicationDocumentsDirectory path]];
shouldFail = YES;
}
} else if ([error code] == NSFileReadNoSuchFileError) {
error = nil;
[fileManager createDirectoryAtPath:[applicationDocumentsDirectory path] withIntermediateDirectories:YES attributes:nil error:&error];
if ([CredentialsManager userLoggedIn]) {
[self.statusItem presentStatusItemWithImage:iconImage contentViewController:self.timetableViewController];
}

if (!shouldFail && !error) {
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *url = [applicationDocumentsDirectory URLByAppendingPathComponent:@"OSXCoreDataObjC.storedata"];
if (![coordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:nil error:&error]) {
coordinator = nil;
}
_persistentStoreCoordinator = coordinator;
}

if (shouldFail || error) {
// Report any error we got.
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
dict[NSLocalizedFailureReasonErrorKey] = failureReason;
if (error) {
dict[NSUnderlyingErrorKey] = error;
}
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
[[NSApplication sharedApplication] presentError:error];
else {
[self.statusItem presentStatusItemWithImage:iconImage contentViewController:self.loginViewController];
[self.statusItem showStatusItemWindow];
}
return _persistentStoreCoordinator;
}

- (NSManagedObjectContext *)managedObjectContext {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
if (_managedObjectContext) {
return _managedObjectContext;
#pragma mark - Notification response

- (void)authorizationStatusChanged
{
if ([CredentialsManager userLoggedIn]) {
[self.statusItem updateContentViewController:self.timetableViewController];
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
return nil;
else {
[self.statusItem updateContentViewController:self.loginViewController];
}
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];

return _managedObjectContext;
}

#pragma mark - Core Data Saving and Undo support
#pragma mark - Getters

- (IBAction)saveAction:(id)sender {
// 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.
if (![[self managedObjectContext] commitEditing]) {
NSLog(@"%@:%@ unable to commit editing before saving", [self class], NSStringFromSelector(_cmd));
}

NSError *error = nil;
if ([[self managedObjectContext] hasChanges] && ![[self managedObjectContext] save:&error]) {
[[NSApplication sharedApplication] presentError:error];
- (TimetableViewController *)timetableViewController
{
if (!_timetableViewController) {
_timetableViewController = [[TimetableViewController alloc] init];
_timetableViewController.preferredContentSize = NSMakeSize(500, 500);
}
return _timetableViewController;
}

- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window {
// Returns the NSUndoManager for the application. In this case, the manager returned is that of the managed object context for the application.
return [[self managedObjectContext] undoManager];
}

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
// Save changes in the application's managed object context before the application terminates.

if (!_managedObjectContext) {
return NSTerminateNow;
- (LoginViewController *)loginViewController
{
if (!_loginViewController) {
_loginViewController = [[LoginViewController alloc] init];
_loginViewController.preferredContentSize = NSMakeSize(300, 250);
}

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 == NSAlertFirstButtonReturn) {
return NSTerminateCancel;
}
}

return NSTerminateNow;
return _loginViewController;
}

@end
Loading

0 comments on commit 145e85e

Please sign in to comment.