forked from joaomoreno/thyme
/
Session.m
74 lines (58 loc) · 2.03 KB
/
Session.m
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
//
// Session.m
// Thyme
//
// Created by João Moreno on 6/4/10.
//
#import "Session.h"
#import "ThymeAppDelegate.h"
#define AppDelegate ((ThymeAppDelegate*) [[NSApplication sharedApplication] delegate])
@interface Session(hidden)
- (NSString*)formattedDate;
@end
@implementation Session
@dynamic hours;
@dynamic minutes;
@dynamic seconds;
@dynamic date;
- (NSString*)formatDate
{
static NSDateFormatter *dateFormatter = nil;
if (dateFormatter == nil)
{
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
}
return [dateFormatter stringFromDate:self.date];
}
+ (NSArray*)allSessions
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[[AppDelegate.managedObjectModel entitiesByName] valueForKey:@"Session"]];
NSArray *result = [AppDelegate.managedObjectContext executeFetchRequest:request error:nil];
[request release];
return result;
}
+ (Session*)sessionWithSeconds:(NSInteger)_seconds minutes:(NSInteger)_minutes hours:(NSInteger)_hours
{
Session* session = (Session*) [NSEntityDescription insertNewObjectForEntityForName:@"Session"
inManagedObjectContext:AppDelegate.managedObjectContext];
session.hours = [NSNumber numberWithInt:_hours];
session.minutes = [NSNumber numberWithInt:_minutes];
session.seconds = [NSNumber numberWithInt:_seconds];
session.date = [NSDate date];
return session;
}
- (NSString*)timeStringRepresentation
{
if ([self.hours intValue] > 0)
return [NSString stringWithFormat:@"%02d:%02d:%02d", [self.hours intValue], [self.minutes intValue], [self.seconds intValue]];
else
return [NSString stringWithFormat:@"%02d:%02d", [self.minutes intValue], [self.seconds intValue]];
}
- (NSString*)stringRepresentation
{
return [NSString stringWithFormat:@"%@ - %@", [self timeStringRepresentation], [self formatDate]];
}
@end