Skip to content

Instantly share code, notes, and snippets.

@elizaaverywilson
Last active December 29, 2015 07:09
Show Gist options
  • Save elizaaverywilson/7634033 to your computer and use it in GitHub Desktop.
Save elizaaverywilson/7634033 to your computer and use it in GitHub Desktop.
In dev GCAppController.m so those NSChat people can look at it ;)
// Key for the preference file numbers:
const int decConst = 10;
const int hexConst = 16;
const int binConst = 2;
const int decASCII_Const = 9;
const int binASCII_Const = 1;
const int expressionConst = 3;
// Uncomment to enable debugger mode
#define GCDEBUGGER
#import "GCTimeModel.h"
#import "GCLoginItemController.h"
#import "GCAppController.h"
// tgmath.h for CGFloat rounding in the hour section of expression Mode
#include <tgmath.h>
@implementation GCAppController
int preferenceNumber;
BOOL loginBool;
int flashInt;
@synthesize windowController;
#pragma mark startup
- (void) awakeFromNib {
//Handle setup
[self setupStatusItem];
[self disableMenuItemsInitially];
[self turnOffAppsDockIcon];
// If file creation failed for some reason, stops app. Used over [NSApp terminate: nil]; because this forces it to happen at the top of the next pass throught the event loop.
if ([self didCreateDataFileWork] == NO) [NSApp performSelector:@selector(terminate:) withObject:nil afterDelay: 0.0];
[self definePreferences];
[self enableUI_Elements];
//Start clock
[self tickOverview:nil];
}
- (void) setupStatusItem {
// Create the NSStatusItem and set variable length
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength: NSVariableStatusItemLength];
[statusItem setTitle: @"[G-Clock setting up, please wait]"];
// Tells NSStatusItem what menu to load
[statusItem setMenu: statusMenu];
// Sets mouseover
[statusItem setToolTip: @"G-Clock"];
}
- (void) disableMenuItemsInitially {
// Disables all menu items
[_standardMenuItem setEnabled: NO];
[_hexMenuItem setEnabled: NO];
[_binaryMenuItem setEnabled: NO];
[_decASCII_MenuItem setEnabled: NO];
[_binASCII_MenuItem setEnabled: NO];
[_expressionMenuItem setEnabled: NO];
}
- (void) turnOffAppsDockIcon {
// Turn off Dock icon
[NSApp setActivationPolicy:NSApplicationActivationPolicyProhibited];
}
- (BOOL) didCreateDataFileWork {
NSString *dirPath = [@"~/Library/Application Support/GClockData" stringByExpandingTildeInPath];
NSString *filePath = [@"~/Library/Application Support/GClockData/Storage.txt" stringByExpandingTildeInPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL didMethodWork = NO;
if ([fileManager fileExistsAtPath: dirPath] == YES) {
if ([fileManager isWritableFileAtPath: dirPath] == YES) {
if ([fileManager isReadableFileAtPath: dirPath] == NO) {
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE: Permissions error.");
# endif
return didMethodWork;
}
} else {
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE: Permissions error.");
# endif
return didMethodWork;
}
} else {
// Create directory
if ([fileManager createDirectoryAtPath: dirPath withIntermediateDirectories: YES attributes: nil error: NULL] != YES){
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE: Save File Creation Error. (%@)", dirPath);
# endif
return didMethodWork;
}
}
if ([fileManager fileExistsAtPath: filePath] == YES) {
if ([fileManager isWritableFileAtPath: filePath] == YES) {
if ([fileManager isReadableFileAtPath: filePath] == NO) {
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE: Permissions error.");
# endif
return didMethodWork;
} else didMethodWork = YES;
} else {
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE: Permissions error.");
# endif
return didMethodWork;
}
} else {
BOOL aLoginBool = YES;
// Inserts "@" inbetween each variable as a placeholder, later used to separate components of an array
NSString *string = [NSString stringWithFormat:@"%i@%hhd@0", expressionConst,aLoginBool];
NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding];
// Create file
if ([fileManager createFileAtPath: filePath contents: stringData attributes: nil] != YES){
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE: Save File Creation Error. (%@)", dirPath);
# endif
return didMethodWork;
} else didMethodWork = YES;
}
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE: didCreateDataFileWork: %i", didMethodWork);
# endif
return didMethodWork;
}
- (void) definePreferences {
NSString *filePath = [@"~/Library/Application Support/GClockData/Storage.txt" stringByExpandingTildeInPath];
// Gets preferences
NSString *fileContentsString = [NSString stringWithContentsOfFile: filePath encoding: NSUTF8StringEncoding error:NULL];
NSArray *fileContents = [fileContentsString componentsSeparatedByString:@"@"];
// Sets preferencs
preferenceNumber = [fileContents[0] intValue];
loginBool = [fileContents[1] boolValue];
// When setting LoginBool, makes sure its actually a bool, and if not sets flashInt to NO.
bool aFlashBool = [fileContents[2] intValue];
if (aFlashBool == 0) flashInt = 1;
else flashInt = 0;
GCLoginItemController *loginController = [[GCLoginItemController alloc] init];
if (loginBool == YES) [loginController addAppAsLoginItemWithAllUsers: NO];
else if (loginBool == NO) [loginController removeAppFromLoginItemWithAllUsers:NO];
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE: preferenceNumber: %i", preferenceNumber);
# endif
}
- (void) enableUI_Elements {
// Sets loginItem's state
if (loginBool == YES) {
[_loginItem setState: NSOnState];
} else {
[_loginItem setState: NSOffState];
}
// Sets initial disablement/enablement
if (preferenceNumber == decConst) {
[_standardMenuItem setState: NSOnState];
[_standardMenuItem setEnabled: NO];
[_hexMenuItem setState: NSOffState];
[_hexMenuItem setEnabled: YES];
[_binaryMenuItem setState: NSOffState];
[_binaryMenuItem setEnabled: YES];
[_decASCII_MenuItem setState: NSOffState];
[_decASCII_MenuItem setEnabled: YES];
[_binASCII_MenuItem setState: NSOffState];
[_binASCII_MenuItem setEnabled: YES];
[_expressionMenuItem setState: NSOffState];
[_expressionMenuItem setEnabled: YES];
} else if (preferenceNumber == hexConst) {
[_standardMenuItem setState: NSOffState];
[_standardMenuItem setEnabled: YES];
[_hexMenuItem setState: NSOnState];
[_hexMenuItem setEnabled: NO];
[_binaryMenuItem setState: NSOffState];
[_binaryMenuItem setEnabled: YES];
[_decASCII_MenuItem setState: NSOffState];
[_decASCII_MenuItem setEnabled: YES];
[_binASCII_MenuItem setState: NSOffState];
[_binASCII_MenuItem setEnabled: YES];
[_expressionMenuItem setState: NSOffState];
[_expressionMenuItem setEnabled: YES];
} else if (preferenceNumber == binConst) {
[_standardMenuItem setState: NSOffState];
[_standardMenuItem setEnabled: YES];
[_hexMenuItem setState: NSOffState];
[_hexMenuItem setEnabled: YES];
[_binaryMenuItem setState: NSOnState];
[_binaryMenuItem setEnabled: NO];
[_decASCII_MenuItem setState: NSOffState];
[_decASCII_MenuItem setEnabled: YES];
[_binASCII_MenuItem setState: NSOffState];
[_binASCII_MenuItem setEnabled: YES];
[_expressionMenuItem setState: NSOffState];
[_expressionMenuItem setEnabled: YES];
} else if (preferenceNumber == decASCII_Const) {
[_standardMenuItem setState: NSOffState];
[_standardMenuItem setEnabled: YES];
[_hexMenuItem setState: NSOffState];
[_hexMenuItem setEnabled: YES];
[_binaryMenuItem setState: NSOffState];
[_binaryMenuItem setEnabled: YES];
[_decASCII_MenuItem setState: NSOnState];
[_decASCII_MenuItem setEnabled: NO];
[_binASCII_MenuItem setState: NSOffState];
[_binASCII_MenuItem setEnabled: YES];
[_expressionMenuItem setState: NSOffState];
[_expressionMenuItem setEnabled: YES];
} else if (preferenceNumber == binASCII_Const) {
[_standardMenuItem setState: NSOffState];
[_standardMenuItem setEnabled: YES];
[_hexMenuItem setState: NSOffState];
[_hexMenuItem setEnabled: YES];
[_binaryMenuItem setState: NSOffState];
[_binaryMenuItem setEnabled: YES];
[_decASCII_MenuItem setState: NSOffState];
[_decASCII_MenuItem setEnabled: YES];
[_binASCII_MenuItem setState: NSOnState];
[_binASCII_MenuItem setEnabled: NO];
[_expressionMenuItem setState: NSOffState];
[_expressionMenuItem setEnabled: YES];
} else if (preferenceNumber == expressionConst) {
[_standardMenuItem setState: NSOffState];
[_standardMenuItem setEnabled: YES];
[_hexMenuItem setState: NSOffState];
[_hexMenuItem setEnabled: YES];
[_binaryMenuItem setState: NSOffState];
[_binaryMenuItem setEnabled: YES];
[_decASCII_MenuItem setState: NSOffState];
[_decASCII_MenuItem setEnabled: YES];
[_binASCII_MenuItem setState: NSOffState];
[_binASCII_MenuItem setEnabled: YES];
[_expressionMenuItem setState: NSOnState];
[_expressionMenuItem setEnabled: NO];
} else {
[_standardMenuItem setState: NSOffState];
[_standardMenuItem setEnabled: YES];
[_hexMenuItem setState: NSOffState];
[_hexMenuItem setEnabled: YES];
[_binaryMenuItem setState: NSOffState];
[_binaryMenuItem setEnabled: YES];
[_decASCII_MenuItem setState: NSOffState];
[_decASCII_MenuItem setEnabled: YES];
[_binASCII_MenuItem setState: NSOffState];
[_binASCII_MenuItem setEnabled: YES];
[_expressionMenuItem setState: NSOffState];
[_expressionMenuItem setEnabled: YES];
}
// Set flashTimeSeparator's enablement.
if (flashInt == 1) [_flashTimeSeparator setEnabled: YES];
else if (flashInt == 0) [_flashTimeSeparator setEnabled: NO];
}
#pragma mark tick
- (void) tickOverview {
GCTimeModel *timeModel = [[GCTimeModel alloc] init];
NSDate *date = [timeModel getDate];
int second = [timeModel getSecondForTime: date];
[self placeholder];
}
- (NSString*) callModelWithSecond:(int)second {
NSString *stringSecond;
if (preferenceNumber == hexConst)
stringSecond = [NSString stringWithFormat:@"%X", second];
else if (preferenceNumber == decConst)
stringSecond = [NSString stringWithFormat:@"%u", second];
else if (preferenceNumber == binConst) {
if (second == 0) stringSecond = @"0";
else {
NSMutableString *stringCode = [NSMutableString stringWithFormat:@""];
for(NSInteger numberCopy = second; numberCopy > 0; numberCopy >>=1) {
// Prepend "0" or "1", depending on the bit
[stringCode insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
}
stringSecond = [NSString stringWithFormat:@"%@", stringCode];
}
} else if (preferenceNumber == decASCII_Const) {
NSMutableString *stringCode = [NSMutableString stringWithFormat:@""];
NSString *secondString = [NSString stringWithFormat:@"%i", second];
for (int i=0; i<[secondString length]; i++) {
char c = [secondString characterAtIndex:i];
int ASCII_Code = (int)c;
NSString *ASCII_String = [NSString stringWithFormat: @"%i", ASCII_Code];
[stringCode appendString:ASCII_String];
if (i + 1 != [secondString length])
[stringCode appendString:@" "];
}
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE:stringCode second: %@", stringCode);
# endif
stringSecond = stringCode;
} else if (preferenceNumber == binASCII_Const) {
// First, convert to decimal ASCII
NSMutableString * stringCode = [NSMutableString stringWithFormat:@""];
NSString *secondString = [NSString stringWithFormat:@"%i", second];
for (int i=0; i<[secondString length]; i++) {
char c = [secondString characterAtIndex:i];
int ASCII_Code = (int)c;
NSString *ASCII_String = [NSString stringWithFormat: @"%i", ASCII_Code];
[stringCode appendString:ASCII_String];
if (i + 1 != [secondString length])
[stringCode appendString:@" "];
}
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE:stringCode second: %@", stringCode);
# endif
// Now, convert to binary
NSMutableString *str2 = [NSMutableString stringWithFormat:@""];
NSArray *array = [stringCode componentsSeparatedByString: @" "];
NSUInteger numObjects = [array count];
// Converts each object of array into binary separately, so spaces remain intact.
for (int i = 0; i < numObjects; ++i) {
NSString *string = array[i];
NSMutableString *mutStr = [NSMutableString stringWithFormat:@""];
for (NSInteger numberCopy = [string intValue]; numberCopy > 0; numberCopy >>=1) {
// Prepend "0" or "1", depending on the bit
[mutStr insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
}
[str2 appendString: mutStr];
if (numObjects != i + 1) {
// Add space if this is not last run through
[str2 appendString: @" "];
}
}
stringSecond = str2;
} else if (preferenceNumber == expressionConst) {
NSInteger random, random2, numb;
NSString *secondString;
// Addition
if (second == 0)
random = (NSInteger)(arc4random_uniform(121)) - 60;
else random = ((NSInteger)(arc4random_uniform(second * 2 + 1)) - 60);
// Rounds random up to the least multiple of five that is greater or equal to random as multiples of five are easier to add.
BOOL isMultipleOfFive = !(random % 5);
while (isMultipleOfFive != YES) {
++random;
isMultipleOfFive = !(random % 5);
}
numb = second - random;
// Since if second is not a multiple of five, one of the numbers will be a multiple of five (random) and one will not (numb), random2 decides if random goes first in the string or if numb does
random2 = 1 + arc4random_uniform(2);
if (random2 == 1)
secondString = [NSString stringWithFormat: @" %li + %li", numb, random];
else if (random2 == 2)
secondString = [NSString stringWithFormat: @" %li + %li", random, numb];
stringSecond = secondString;
}
}
- (void) placeholder {
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatterMinute = [[NSDateFormatter alloc] init];
[dateFormatterMinute setDateFormat:@"mm"];
NSString *stringFromDateMinute = [dateFormatterMinute stringFromDate: date];
unsigned int minute = [stringFromDateMinute intValue];
NSString *stringMinute;
if (preferenceNumber == hexConst)
stringMinute = [NSString stringWithFormat:@"%X", minute];
else if (preferenceNumber == decConst)
stringMinute = [NSString stringWithFormat:@"%u", minute];
else if (preferenceNumber == binConst){
if (minute == 0) stringMinute = @"0";
else {
NSMutableString *stringCode = [NSMutableString stringWithFormat:@""];
for(NSInteger numberCopy = minute; numberCopy > 0; numberCopy >>=1) {
// Prepend "0" or "1", depending on the bit
[stringCode insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
}
stringMinute = [NSString stringWithFormat:@"%@", stringCode];
}
} else if (preferenceNumber == decASCII_Const) {
NSMutableString * stringCode = [NSMutableString stringWithFormat:@""];
NSString *minuteString = [NSString stringWithFormat:@"%i", minute];
for (int i=0; i<[minuteString length]; i++) {
char c = [minuteString characterAtIndex:i];
int ASCII_Code = (int)c;
NSString *ASCII_String = [NSString stringWithFormat: @"%i", ASCII_Code];
[stringCode appendString:ASCII_String];
if (i + 1 != [minuteString length])
[stringCode appendString:@" "];
}
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE:stringCode minute: %@", stringCode);
# endif
stringMinute = stringCode;
} else if (preferenceNumber == binASCII_Const) {
// First, convert to decimal ASCII
NSMutableString *stringCode = [NSMutableString stringWithFormat:@""];
NSString *minuteString = [NSString stringWithFormat:@"%i", minute];
for (int i=0; i<[minuteString length]; ++i) {
char c = [minuteString characterAtIndex:i];
int ASCII_Code = (int)c;
NSString *ASCII_String = [NSString stringWithFormat: @"%i", ASCII_Code];
[stringCode appendString:ASCII_String];
if (i + 1 != [minuteString length])
[stringCode appendString:@" "];
}
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE:stringCode minute: %@", stringCode);
# endif
// Now, convert to binary
NSMutableString *str2 = [NSMutableString stringWithFormat:@""];
NSArray *array = [stringCode componentsSeparatedByString: @" "];
NSUInteger numObjects = [array count];
// Converts each object of array into binary separately, so spaces remain intact.
for (int i = 0; i < numObjects; ++i) {
// Help for the translator from Martin R. http:// stackoverflow.com/questions/19335367/converting-string-into-binary-with-spaces-intact
NSString *string = array[i];
NSMutableString *mutStr = [NSMutableString stringWithFormat:@""];
for (NSInteger numberCopy = [string intValue]; numberCopy > 0; numberCopy >>=1) {
// Prepend "0" or "1", depending on the bit
[mutStr insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
}
[str2 appendString: mutStr];
if (numObjects != i + 1) {
// Add space if this is not last run through
[str2 appendString: @" "];
}
}
stringMinute = str2;
} else if (preferenceNumber == expressionConst) {
if (_minuteNumber != minute) {
NSInteger random, random2;
NSString *minuteString;
// Decides which section to go through (addition, subtraction, etc)
random = 1 + arc4random_uniform(4);
if (random == 1) {
// Addition
if (minute == 0)
random2 = (NSInteger)(arc4random_uniform(61));
else random2 = (NSInteger)(arc4random_uniform((minute + 1) * 100));
CGFloat random2Float = random2;
CGFloat float1 = random2Float / 1000;
// Gets a NSNumber from float1, as NSNumber auto-truncates insignificant figures
NSNumber *float1numb = [NSNumber numberWithFloat: float1];
NSNumber *numb = [NSNumber numberWithFloat:(minute - float1)];
minuteString = [NSString stringWithFormat: @" %@ + %@ ", numb, float1numb];
_stringForMinute = minuteString;
_minuteNumber = minute;
stringMinute = minuteString;
} else if (random == 2) {
// Subtraction
if (minute == 0)
random2 = (NSInteger)(arc4random_uniform(61));
else random2 = (NSInteger)(arc4random_uniform(minute + 1) * 100);
CGFloat random2Float = random2;
CGFloat float1 = random2Float / 1000;
// Gets a NSNumber from float1, as NSNumber auto-truncates insignificant figures
NSNumber *float1numb = [NSNumber numberWithFloat: float1];
NSNumber *numb = [NSNumber numberWithFloat:(minute + float1)];
minuteString = [NSString stringWithFormat: @" %@ - %@ ", numb, float1numb];
_stringForMinute = minuteString;
_minuteNumber = minute;
stringMinute = minuteString;
} else if (random == 3) {
// Multiplication
if (minute == 0)
random2 = (NSInteger)(arc4random_uniform(15));
else {
if ((minute + 1) <= 15)
random2 = 1 + (NSInteger)(arc4random_uniform(minute + 1));
else random2 = 1 + (NSInteger)(arc4random_uniform(15));
}
NSNumber *numb = [NSNumber numberWithFloat:(minute / random2)];
minuteString = [NSString stringWithFormat: @" %@ * %ld ", numb, random2];
_stringForMinute = minuteString;
_minuteNumber = minute;
stringMinute = minuteString;
} else if (random == 4) {
// Division
if (minute == 0)
random2 = (NSInteger)(arc4random_uniform(15));
else {
if ((minute + 1) <= 15)
random2 = 1 + (NSInteger)(arc4random_uniform(minute + 1));
else random2 = 1 + (NSInteger)(arc4random_uniform(15));
}
NSNumber *numb = [NSNumber numberWithFloat:(minute * random2)];
minuteString = [NSString stringWithFormat: @" %@ / %ld ", numb, random2];
_stringForMinute = minuteString;
_minuteNumber = minute;
stringMinute = minuteString;
}
} else
stringMinute = _stringForMinute;
}
NSDateFormatter *dateFormatterHour = [[NSDateFormatter alloc] init];
[dateFormatterHour setDateFormat:@"hh"];
NSString *stringFromDateHour = [dateFormatterHour stringFromDate: date];
unsigned int hour = [stringFromDateHour intValue];
NSString *stringHour;
if (preferenceNumber == hexConst)
stringHour = [NSString stringWithFormat:@"%X", hour];
else if (preferenceNumber == decConst)
stringHour = [NSString stringWithFormat:@"%i", hour];
else if (preferenceNumber == binConst) {
if (hour == 0) stringHour = @"0";
else {
NSMutableString *stringCode = [NSMutableString stringWithFormat:@""];
for(NSInteger numberCopy = hour; numberCopy > 0; numberCopy >>= 1) {
// Prepend "0" or "1", depending on the bit
[stringCode insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
}
stringHour = [NSString stringWithFormat:@"%@", stringCode];
}
} else if (preferenceNumber == decASCII_Const) {
NSMutableString * stringCode = [NSMutableString stringWithFormat:@""];
NSString *hourString = [NSString stringWithFormat:@"%i", hour];
for (int i = 0; i < [hourString length]; ++i) {
char c = [hourString characterAtIndex:i];
int ASCII_Code = (int)c;
NSString *ASCII_String = [NSString stringWithFormat: @"%i", ASCII_Code];
[stringCode appendString:ASCII_String];
if (i + 1 != [hourString length])
[stringCode appendString:@" "];
}
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE:stringCode hour: %@", stringCode);
# endif
stringHour = stringCode;
} else if (preferenceNumber == binASCII_Const) {
// First, convert to decimal ASCII
NSMutableString * stringCode = [NSMutableString stringWithFormat:@""];
NSString *hourString = [NSString stringWithFormat:@"%i", hour];
for (int i = 0; i < [hourString length]; ++i) {
char c = [hourString characterAtIndex:i];
int ASCII_Code = (int)c;
NSString *ASCII_String = [NSString stringWithFormat: @"%i", ASCII_Code];
[stringCode appendString:ASCII_String];
if (i + 1 != [hourString length])
[stringCode appendString:@" "];
}
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE:stringCode hour: %@", stringCode);
# endif
// Now, convert to binary
NSMutableString *str2 = [NSMutableString stringWithFormat:@""];
NSArray *array = [stringCode componentsSeparatedByString: @" "];
NSUInteger numObjects = [array count];
// Converts each object of array into binary separately, so spaces remain intact.
for (int i = 0; i < numObjects; ++i) {
NSString *string = array[i];
NSMutableString *mutStr = [NSMutableString stringWithFormat:@""];
for (NSInteger numberCopy = [string intValue]; numberCopy > 0; numberCopy >>=1) {
// Prepend "0" or "1", depending on the bit
[mutStr insertString:((numberCopy & 1) ? @"1" : @"0") atIndex:0];
}
[str2 appendString: mutStr];
if (numObjects != i + 1) {
// Add space if this is not last run through
[str2 appendString: @" "];
}
}
stringHour = str2;
} else if (preferenceNumber == expressionConst) {
if (_hourNumber != hour) {
NSInteger random, random3;
NSMutableString *hourString = [NSMutableString stringWithFormat:@" "];
// Declares how many operations will be in hour
random3 = 3 + (NSInteger)(arc4random_uniform(4));
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE: random3: %ld", (long)random3);
# endif
for (int i = 0; i <= random3; ++i) {
// Decides which section to go through (addition & subtraction (50%), multiplication (25%), and division (25%))
random = 1 + (NSInteger)(arc4random_uniform(4));
NSString *appendString;
if (i == 0) _answer = hour;
// Tells what the answer of the expression should be on any runs through the program after the first one.
NSInteger answer = _answer;
if ((random == 1) || (random == 2)) {
// Addition and Subtraction
NSInteger random2 = 49 - (NSInteger)(arc4random_uniform(100));
NSNumber *numb = [NSNumber numberWithInteger: answer - random2];
// Turns numb into string, if positive "+ #" if negative "- #", and if zero a random chance of either.
NSString *numbString;
if ([numb intValue] > 0) {
numbString = [NSString stringWithFormat:@"+ %@", numb];
} else if ([numb intValue] < 0) {
NSMutableString *stringFromNumber = [NSMutableString stringWithFormat:@"%@", numb];
// Adds space right after the negative operator in the string -# so it will later read # - # instead of -#
[stringFromNumber insertString: @" " atIndex:1];
numbString = [NSString stringWithFormat:@"%@", stringFromNumber];
} else if ([numb intValue] == 0) {
NSUInteger randomTemp = 1 + (NSInteger)(arc4random_uniform(2));
if (randomTemp == 1)
numbString = [NSString stringWithFormat:@"+ %@", numb];
else if (randomTemp == 2)
numbString = [NSString stringWithFormat:@"- %@", numb];
}
if (random3 == i) {
// Last run through of for block, replaces the placeholder ("@") with appendString. appendString doesn't get a placeholder ("@"), as another string will not be inserted
appendString = [NSString stringWithFormat: @"%li %@", random2, numbString];
// Finds range of placeholder.
NSRange range = [hourString rangeOfString:@"@"];
// Replaces @ with appendString.
[hourString replaceCharactersInRange: range withString: appendString];
} else if (i == 0) {
// First run through of for block, defines NSMutableString as what would be appendString, as there is nothing yet to be replaced.
hourString = [NSMutableString stringWithFormat: @"@ %@", numbString];
} else {
// In between first and last run of for block, replaces the placeholder ("@") with appendString, and has appendString have a placeholder("@), for the next string to be inserted in.
appendString = [NSString stringWithFormat: @"@ %@", numbString];
// Finds range of placeholder.
NSRange range = [hourString rangeOfString:@"@"];
// Replaces @ with appendString.
[hourString replaceCharactersInRange: range withString: appendString];
}
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE: +- i: %i answer: %ld _answer: %ld random2: %ld numb: %@ numbString: %@ hourString: %@", i, (long)answer, (long)_answer, (long)random2, numb, numbString, hourString);
# endif
_answer = random2;
_hourNumber = hour;
stringHour = hourString;
} else if (random == 3) {
// Multiplication
NSInteger random2 = 1 + (NSInteger)(arc4random_uniform(20));
float random2F= random2;
float answerF = answer;
CGFloat floatForNumb = (answerF / random2F);
// Rounds to nearest hundredth by multiplying by 100, rounding, and then dividing by 100
CGFloat floatForNumbRounded = roundf(floatForNumb * 100) / 100;
// You have to convert it into a NSNumber so it auto-truncates insignificant digits.
NSNumber *numb = [NSNumber numberWithFloat:floatForNumbRounded];
if (random3 == i) {
// Last run through of for block, replaces the placeholder ("@") with appendString. appendString doesn't get a placeholder ("@"), as another string will not be inserted
appendString = [NSString stringWithFormat: @"%ld * %@", (long)random2, numb];
// Finds range of placeholder.
NSRange range = [hourString rangeOfString:@"@"];
// Replaces @ with appendString.
[hourString replaceCharactersInRange: range withString: appendString];
} else if (i == 0) {
// First run through of for block, defines NSMutableString as what would be appendString, as there is nothing yet to be replaced.
hourString = [NSMutableString stringWithFormat: @"(@) * %@", numb];
} else {
// In between first and last run of for block, replaces the placeholder ("@") with appendString, and has appendString have a placeholder("@), for the next string to be inserted in.
appendString = [NSString stringWithFormat: @"(@) * %@", numb];
// Finds range of placeholder.
NSRange range = [hourString rangeOfString:@"@"];
// Replaces @ with appendString.
[hourString replaceCharactersInRange: range withString: appendString];
}
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE: * i: %i answer: %ld _answer: %ld random2: %ld numb: %@ hourString: %@", i, (long)answer, (long)_answer, (long)random2, numb, hourString);
# endif
_answer = random2;
_hourNumber = hour;
stringHour = hourString;
} else if (random == 4) {
// Division
NSInteger random2 = 1 + (NSInteger)(arc4random_uniform(20));
float random2F= random2;
float answerF = answer;
CGFloat floatForNumb = (random2F / answerF);
// Rounds to nearest hundredth by multiplying by 100, rounding, and then dividing by 100
CGFloat floatForNumbRounded = roundf(floatForNumb * 100) / 100;
// You have to convert it into a NSNumber so it auto-truncates insignificant digits.
NSNumber *numb = [NSNumber numberWithFloat:floatForNumbRounded];
if (random3 == i) {
// Last run through of for block, replaces the placeholder ("@") with appendString. appendString doesn't get a placeholder ("@"), as another string will not be inserted
appendString = [NSString stringWithFormat: @"%li / %@", (long)random2, numb];
// Finds range of placeholder.
NSRange range = [hourString rangeOfString:@"@"];
// Replaces @ with appendString.
[hourString replaceCharactersInRange: range withString: appendString];
} else if (i == 0) {
// First run through of for block, defines NSMutableString as what would be appendString, as there is nothing yet to be replaced.
hourString = [NSMutableString stringWithFormat: @"(@) / %@", numb];
} else {
// In between first and last run of for block, replaces the placeholder ("@") with appendString, and has appendString have a placeholder("@), for the next string to be inserted in.
appendString = [NSString stringWithFormat: @"(@) / %@", numb];
// Finds range of placeholder.
NSRange range = [hourString rangeOfString:@"@"];
// Replaces @ with appendString.
[hourString replaceCharactersInRange: range withString: appendString];
}
# ifdef GCDEBUGGER
NSLog (@"GCDEBUGGER MESSAGE: / i: %i answer: %ld _answer: %ld random2: %ld numb: %@ hourString: %@", i, (long)answer, (long)_answer, (long)random2, numb, hourString);
# endif
_answer = random2;
_hourNumber = hour;
stringHour = hourString;
}
}
_stringForHour = hourString;
} else
stringHour = _stringForHour;
}
NSString *string;
if (flashInt == 1) {
// Tell if the time is odd, if so flashes time separators
if ((second % 2) == 1) {
if ((preferenceNumber == decASCII_Const) || (preferenceNumber == decASCII_Const) || (preferenceNumber == binConst))
string = [NSString stringWithFormat: @"(%@) (%@) (%@)", stringHour, stringMinute, stringSecond];
else string = [NSString stringWithFormat: @"%@ %@ %@", stringHour, stringMinute, stringSecond];
} else {
if ((preferenceNumber == decASCII_Const) || (preferenceNumber == binASCII_Const) || (preferenceNumber == binConst))
string = [NSString stringWithFormat: @"(%@):(%@):(%@)", stringHour, stringMinute, stringSecond];
else string = [NSString stringWithFormat: @"%@:%@:%@", stringHour, stringMinute, stringSecond];
}
} else {
if ((preferenceNumber == decASCII_Const) || (preferenceNumber == binASCII_Const) || (preferenceNumber == binConst))
string = [NSString stringWithFormat: @"(%@):(%@):(%@)", stringHour, stringMinute, stringSecond];
else string = [NSString stringWithFormat: @"%@:%@:%@", stringHour, stringMinute, stringSecond];
}
// Sets title to time
[statusItem setTitle: string];
[self performSelector: @selector(tickOverview:) withObject: nil afterDelay: 1.0];
}
#pragma mark - MenuItems
- (IBAction)exit: (id)sender {
// Stops Application.
[NSApp performSelector:@selector(terminate:) withObject:nil afterDelay: 0.0];
}
- (IBAction)loginItem:(id)sender {
if (loginBool == YES) {
loginBool = NO;
[_loginItem setState: NSOffState];
GCLoginItemController *loginController = [[GCLoginItemController alloc] init];
[loginController removeAppFromLoginItemWithAllUsers: NO];
NSString *filePath = [@"~/Library/Application Support/GClockData/Storage.txt" stringByExpandingTildeInPath];
NSString *number = [NSString stringWithFormat: @"%li", (long)preferenceNumber];
BOOL loginBool = NO;
NSString *string = [NSString stringWithFormat:@"%@@%hhd ", number, loginBool];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath: filePath];
[fileHandle writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
} else {
loginBool = YES;
[_loginItem setState: NSOnState];
GCLoginItemController *loginController = [[GCLoginItemController alloc] init];
[loginController addAppAsLoginItemWithAllUsers:YES];
NSString *filePath = [@"~/Library/Application Support/GClockData/Storage.txt" stringByExpandingTildeInPath];
NSString *number = [NSString stringWithFormat: @"%li", (long)preferenceNumber];
BOOL loginBool = YES;
NSString *string = [NSString stringWithFormat:@"%@@%hhd ", number, loginBool];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath: filePath];
[fileHandle writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
}
}
- (IBAction)preferences:(id)sender {
self.windowController = [[NSWindowController alloc] initWithWindowNibName:@"PreferencesWindow"];
[[self windowController] showWindow:self];
}
- (IBAction)standardMode:(id)sender {
preferenceNumber = decConst;
NSString *filePath = [@"~/Library/Application Support/GClockData/Storage.txt" stringByExpandingTildeInPath];
NSString *string = [NSString stringWithFormat:@"%ld@%hhd@%ld ", (long)preferenceNumber, loginBool, (long)flashInt];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath: filePath];
[fileHandle writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
[_standardMenuItem setState: NSOnState];
[_hexMenuItem setState: NSOffState];
[_binaryMenuItem setState: NSOffState];
[_decASCII_MenuItem setState: NSOffState];
[_binASCII_MenuItem setState: NSOffState];
[_expressionMenuItem setState: NSOffState];
[_standardMenuItem setEnabled: NO];
[_hexMenuItem setEnabled: YES];
[_binaryMenuItem setEnabled: YES];
[_decASCII_MenuItem setEnabled: YES];
[_binASCII_MenuItem setEnabled: YES];
[_expressionMenuItem setEnabled: YES];
}
- (IBAction)hexMode:(id)sender {
preferenceNumber = hexConst;
NSString *filePath = [@"~/Library/Application Support/GClockData/Storage.txt" stringByExpandingTildeInPath];
NSString *string = [NSString stringWithFormat:@"%ld@%hhd@%ld ", (long)preferenceNumber, loginBool, (long)flashInt];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath: filePath];
[fileHandle writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
[_standardMenuItem setState: NSOffState];
[_hexMenuItem setState: NSOnState];
[_binaryMenuItem setState: NSOffState];
[_decASCII_MenuItem setState: NSOffState];
[_binASCII_MenuItem setState: NSOffState];
[_expressionMenuItem setState: NSOffState];
[_standardMenuItem setEnabled: YES];
[_hexMenuItem setEnabled: NO];
[_binaryMenuItem setEnabled: YES];
[_decASCII_MenuItem setEnabled: YES];
[_binASCII_MenuItem setEnabled: YES];
[_expressionMenuItem setEnabled: YES];
}
- (IBAction)binaryMode:(id)sender {
preferenceNumber = binConst;
NSString *filePath = [@"~/Library/Application Support/GClockData/Storage.txt" stringByExpandingTildeInPath];
NSString *string = [NSString stringWithFormat:@"%ld@%hhd@%ld ", (long)preferenceNumber, loginBool, (long)flashInt];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath: filePath];
[fileHandle writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
[_standardMenuItem setState: NSOffState];
[_hexMenuItem setState: NSOffState];
[_binaryMenuItem setState: NSOnState];
[_decASCII_MenuItem setState: NSOffState];
[_binASCII_MenuItem setState: NSOffState];
[_expressionMenuItem setState: NSOffState];
[_standardMenuItem setEnabled: YES];
[_hexMenuItem setEnabled: YES];
[_binaryMenuItem setEnabled: NO];
[_decASCII_MenuItem setEnabled: YES];
[_binASCII_MenuItem setEnabled: YES];
[_expressionMenuItem setEnabled: YES];
}
- (IBAction)decASCII_Mode:(id)sender {
preferenceNumber = decASCII_Const;
NSString *filePath = [@"~/Library/Application Support/GClockData/Storage.txt" stringByExpandingTildeInPath];
NSString *string = [NSString stringWithFormat:@"%ld@%hhd@%ld ", (long)preferenceNumber, loginBool, (long)flashInt];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath: filePath];
[fileHandle writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
[_standardMenuItem setState: NSOffState];
[_hexMenuItem setState: NSOffState];
[_binaryMenuItem setState: NSOffState];
[_decASCII_MenuItem setState: NSOnState];
[_binASCII_MenuItem setState: NSOffState];
[_expressionMenuItem setState: NSOffState];
[_standardMenuItem setEnabled: YES];
[_hexMenuItem setEnabled: YES];
[_binaryMenuItem setEnabled: YES];
[_decASCII_MenuItem setEnabled: NO];
[_binASCII_MenuItem setEnabled: YES];
[_expressionMenuItem setEnabled: YES];
}
- (IBAction)binASCII_Mode:(id)sender {
preferenceNumber = binASCII_Const;
NSString *filePath = [@"~/Library/Application Support/GClockData/Storage.txt" stringByExpandingTildeInPath];
NSString *string = [NSString stringWithFormat:@"%ld@%hhd@%ld ", (long)preferenceNumber, loginBool, (long)flashInt];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath: filePath];
[fileHandle writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
[_standardMenuItem setState: NSOffState];
[_hexMenuItem setState: NSOffState];
[_binaryMenuItem setState: NSOffState];
[_decASCII_MenuItem setState: NSOffState];
[_binASCII_MenuItem setState: NSOnState];
[_expressionMenuItem setState: NSOffState];
[_standardMenuItem setEnabled: YES];
[_hexMenuItem setEnabled: YES];
[_binaryMenuItem setEnabled: YES];
[_decASCII_MenuItem setEnabled: YES];
[_binASCII_MenuItem setEnabled: NO];
[_expressionMenuItem setEnabled: YES];
}
- (IBAction)expressionMode:(id)sender {
preferenceNumber = expressionConst;
NSString *filePath = [@"~/Library/Application Support/GClockData/Storage.txt" stringByExpandingTildeInPath];
NSString *string = [NSString stringWithFormat:@"%ld@%hhd@%ld ", (long)preferenceNumber, loginBool, (long)flashInt];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath: filePath];
[fileHandle writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
[_standardMenuItem setState: NSOffState];
[_hexMenuItem setState: NSOffState];
[_binaryMenuItem setState: NSOffState];
[_decASCII_MenuItem setState: NSOffState];
[_binASCII_MenuItem setState: NSOffState];
[_expressionMenuItem setState: NSOnState];
[_standardMenuItem setEnabled: YES];
[_hexMenuItem setEnabled: YES];
[_binaryMenuItem setEnabled: YES];
[_decASCII_MenuItem setEnabled: YES];
[_binASCII_MenuItem setEnabled: YES];
[_expressionMenuItem setEnabled: NO];
}
- (IBAction)flashTimeSeparator: (id)sender {
// Flip flashInt
if (flashInt == 1) flashInt = 0;
else if (flashInt == 0) flashInt = 1;
NSString *filePath = [@"~/Library/Application Support/GClockData/Storage.txt" stringByExpandingTildeInPath];
NSString *string = [NSString stringWithFormat:@"%ld@%hhd@%ld ", (long)preferenceNumber, loginBool, (long)flashInt];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath: filePath];
[fileHandle writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment