-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainMenuController.m
More file actions
283 lines (226 loc) · 10.1 KB
/
MainMenuController.m
File metadata and controls
283 lines (226 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#import "MainMenuController.h"
#import "PreferencesController.h"
#import "FTTimerFormatter.h"
#import "FTSavedTimerDescriptionTransformer.h"
@implementation MainMenuController
@synthesize workSeconds;
@synthesize breakSeconds;
@synthesize cycles;
@synthesize timer;
+ (void) initialize {
NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
NSMutableArray *savedTimers = [NSMutableArray array];
NSMutableDictionary *pomodoroTimer = [NSMutableDictionary dictionary];
[pomodoroTimer setObject: @"The Pomodoro Technique" forKey: @"name"];
[pomodoroTimer setObject: [NSNumber numberWithInt: 1500] forKey: @"workSeconds"];
[pomodoroTimer setObject: [NSNumber numberWithInt: 300] forKey: @"breakSeconds"];
[pomodoroTimer setObject: [NSNumber numberWithInt: 4] forKey: @"cycles"];
[savedTimers addObject: pomodoroTimer];
NSMutableDictionary *procrastinationTimer = [NSMutableDictionary dictionary];
[procrastinationTimer setObject: @"The Procrastination Hack" forKey: @"name"];
[procrastinationTimer setObject: [NSNumber numberWithInt: 600] forKey: @"workSeconds"];
[procrastinationTimer setObject: [NSNumber numberWithInt: 120] forKey: @"breakSeconds"];
[procrastinationTimer setObject: [NSNumber numberWithInt: 5] forKey: @"cycles"];
[savedTimers addObject: procrastinationTimer];
[defaultValues setObject: savedTimers forKey: @"savedTimers"];
[defaultValues setObject: @"Use Growl Notifications" forKey: @"timerAlerts"];
[[NSUserDefaults standardUserDefaults] registerDefaults: defaultValues];
}
- (NSDictionary *) registrationDictionaryForGrowl {
NSMutableArray *notifications = [NSMutableArray array];
[notifications addObject: @"Work Timer Finished"];
[notifications addObject: @"Break Timer Finished"];
[notifications addObject: @"All Cycles Complete"];
NSDictionary *regDict = [NSDictionary dictionaryWithObjectsAndKeys:
notifications, GROWL_NOTIFICATIONS_ALL,
notifications, GROWL_NOTIFICATIONS_DEFAULT, nil];
return regDict;
}
- (id) init {
if(self = [super init]) {
workSeconds = 1500;
breakSeconds = 300;
cycles = 4;
isWorkTime = NO;
isBreakTime = NO;
cyclesCompleted = 0;
FTSavedTimerDescriptionTransformer *stTransformer = [[FTSavedTimerDescriptionTransformer alloc] init];
[NSValueTransformer setValueTransformer: stTransformer forName: @"FTSavedTimerDescriptionTransformer"];
}
return self;
}
- (void) dealloc {
[self removeObserver: self forKeyPath: @"workSeconds"];
[self removeObserver: self forKeyPath: @"breakSeconds"];
[super dealloc];
}
- (void) awakeFromNib {
[GrowlApplicationBridge setGrowlDelegate: self];
[stopButton setEnabled: NO];
[self addObserver: self forKeyPath: @"workSeconds" options: NSKeyValueObservingOptionNew context: NULL];
[self addObserver: self forKeyPath: @"breakSeconds" options: NSKeyValueObservingOptionNew context: NULL];
}
- (IBAction) showPreferencesPanel: (id) sender {
[[PreferencesController sharedPrefsWindowController] showWindow:nil];
(void)sender;
}
- (void) updateTimerFieldsFromPreset: (id) sender {
NSArray *savedTimers = [[NSUserDefaults standardUserDefaults] objectForKey: @"savedTimers"];
NSDictionary *selectedPreset = [savedTimers objectAtIndex: ([sender indexOfSelectedItem] - 1)];
self.workSeconds = [[selectedPreset objectForKey: @"workSeconds"] intValue];
self.breakSeconds = [[selectedPreset objectForKey: @"breakSeconds"] intValue];
self.cycles = [[selectedPreset objectForKey: @"cycles"] intValue];
}
- (void) observeValueForKeyPath: (NSString *) keyPath ofObject: (id) object change: (NSDictionary *) change context: (void *) context {
if([keyPath isEqual: @"workSeconds"]) {
[workTextField validateEditing];
} else if([keyPath isEqual: @"breakSeconds"]) {
[breakTextField validateEditing];
}
}
- (IBAction) startTimer: (id) sender {
[startButton setEnabled: NO];
[stopButton setEnabled: YES];
[workTextField setEnabled: NO];
[breakTextField setEnabled: NO];
[cyclesTextField setEnabled: NO];
cyclesCompleted = 0;
[self startWorkTimer];
}
- (void) startWorkTimer {
NSLog(@"starting work timer");
isWorkTime = YES;
isBreakTime = NO;
[timerLabel setIntValue: workSeconds];
if(timer) [timer invalidate];
[self setTimer: [NSTimer scheduledTimerWithTimeInterval: 1.0
target: self
selector: @selector(updateTimer:)
userInfo: [NSDate date]
repeats: YES]];
}
- (void) startBreakTimer {
NSLog(@"starting break timer");
isWorkTime = NO;
isBreakTime = YES;
[timerLabel setIntValue: breakSeconds];
if(timer) [timer invalidate];
[self setTimer: [NSTimer scheduledTimerWithTimeInterval: 1.0
target: self
selector: @selector(updateTimer:)
userInfo: [NSDate date]
repeats: YES]];
}
- (IBAction) stopTimer: (id) sender {
NSLog(@"stopping cycle");
[timer invalidate];
[timerLabel setIntValue: 0];
isWorkTime ? [self stopWorkTimer] : [self stopBreakTimer];
isWorkTime = NO;
isBreakTime = NO;
[startButton setEnabled: YES];
[stopButton setEnabled: NO];
[workTextField setEnabled: YES];
[breakTextField setEnabled: YES];
[cyclesTextField setEnabled: YES];
}
- (void) stopWorkTimer {
NSLog(@"stopping work timer");
[timer invalidate];
}
- (void) stopBreakTimer {
NSLog(@"stopping break timer");
[timer invalidate];
}
- (void) updateTimer: (NSTimer *) notificationTimer {
NSInteger secondsSinceTimerStarted = [[notificationTimer userInfo] timeIntervalSinceNow] * -1;
NSInteger secondsLeftOnTimer = ( (isWorkTime ? workSeconds : breakSeconds) - secondsSinceTimerStarted );
[timerLabel setIntValue: secondsLeftOnTimer];
if(secondsLeftOnTimer == 0) {
if(isBreakTime) cyclesCompleted++;
NSString *timerAlerts = [[NSUserDefaults standardUserDefaults] objectForKey: @"timerAlerts"];
if(isWorkTime && ![self hasCompletedAllCycles]) {
[self stopWorkTimer];
[self alertEndOfWork];
if(timerAlerts == @"Use OS Alert Dialogs") {
[self startBreakTimer];
}
} else if(isBreakTime && ![self hasCompletedAllCycles]) {
[self stopBreakTimer];
[self alertEndOfBreak];
if(timerAlerts == @"Use OS Alert Dialogs") {
[self startWorkTimer];
}
} else {
[self stopTimer: self];
[self alertEndOfCycle];
}
}
}
- (BOOL) hasCompletedAllCycles {
return cyclesCompleted == cycles;
}
- (void) alertEndOfWork {
NSSound *alertSound = [NSSound soundNamed: @"Glass"];
if(alertSound) [alertSound play];
NSString *timerAlerts = [[NSUserDefaults standardUserDefaults] objectForKey: @"timerAlerts"];
if(timerAlerts == @"Use Growl Notifications") {
[GrowlApplicationBridge notifyWithTitle: @"Work Timer Finished" description: @"Time to take a break!"
notificationName: @"Work Timer Finished" iconData: nil
priority: 0 isSticky: YES clickContext: @"StartBreakClick"];
} else {
NSAlert *alert = [NSAlert alertWithMessageText: @"Time to take a break!" defaultButton: nil alternateButton: nil otherButton: nil informativeTextWithFormat: @"" ];
[alert runModal];
}
}
- (void) alertEndOfBreak {
NSSound *alertSound = [NSSound soundNamed: @"Glass"];
if(alertSound) [alertSound play];
NSString *timerAlerts = [[NSUserDefaults standardUserDefaults] objectForKey: @"timerAlerts"];
if(timerAlerts == @"Use Growl Notifications") {
[GrowlApplicationBridge notifyWithTitle: @"Break Timer Finished" description: @"Time to get back to work!"
notificationName: @"Break Timer Finished" iconData: nil
priority: 0 isSticky: YES clickContext: @"StartWorkClick"];
} else {
NSAlert *alert = [NSAlert alertWithMessageText: @"Time to get back to work!" defaultButton: nil alternateButton: nil otherButton: nil informativeTextWithFormat: @"" ];
[alert runModal];
}
}
- (void) alertEndOfCycle {
NSInteger totalTimeInSeconds = (workSeconds * cycles) + (breakSeconds * cycles);
NSLog(@"totalTime in seconds: %d", totalTimeInSeconds);
NSString *totalTimeString;
if(totalTimeInSeconds > 3600) {
int hours = floor(totalTimeInSeconds / 3600);
totalTimeInSeconds -= (hours * 3600);
int minutes = floor(totalTimeInSeconds / 60);
totalTimeString = [NSString stringWithFormat: @"%d hour %d minute", hours, minutes];
} else if(totalTimeInSeconds > 60) {
int minutes = floor(totalTimeInSeconds / 60);
totalTimeString = [NSString stringWithFormat: @"%d minute", minutes];
} else {
totalTimeString = [NSString stringWithFormat: @"%d second", totalTimeInSeconds];
}
NSSound *alertSound = [NSSound soundNamed: @"Submarine"];
if(alertSound) [alertSound play];
NSString *timerAlerts = [[NSUserDefaults standardUserDefaults] objectForKey: @"timerAlerts"];
if(timerAlerts == @"Use Growl Notifications") {
NSString *description = [NSString stringWithFormat: @"You've finished a %@ cycle!", totalTimeString];
[GrowlApplicationBridge notifyWithTitle: @"All Cycles Complete" description: description
notificationName: @"All Cycles Complete" iconData: nil
priority: 0 isSticky: YES clickContext: nil];
} else {
NSAlert *alert = [NSAlert alertWithMessageText: @"Finished" defaultButton: nil alternateButton: nil otherButton: nil informativeTextWithFormat: @"You've finished a %@ cycle", totalTimeString ];
[alert runModal];
}
}
- (void) growlNotificationWasClicked: (id) clickContext {
NSLog(@"got click: %@", clickContext);
if([@"StartBreakClick" isEqualToString: clickContext]) {
[self startBreakTimer];
} else if([@"StartWorkClick" isEqualToString: clickContext]) {
[self startWorkTimer];
}
}
- (BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication *) theApplication { return YES; }
@end