forked from joaomoreno/thyme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Session.m
116 lines (88 loc) · 3.69 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
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
//
// 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"]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date"
ascending:YES
comparator:^NSComparisonResult(NSDate* a, NSDate* b) {
return [b compare:a];
}];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSArray *result = [AppDelegate.managedObjectContext executeFetchRequest:request error:nil];
[request release];
return result;
}
+ (NSArray*)allSessionsAsDictionaries
{
NSArray *sessions = [Session allSessions];
NSMutableArray *result = [NSMutableArray arrayWithCapacity:sessions.count];
[sessions enumerateObjectsUsingBlock:^(Session *session, NSUInteger index, BOOL *stop) {
[result addObject:[session asDictionary]];
}];
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]];
}
- (NSDictionary*)asDictionary
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
NSNumber *duration = [NSNumber numberWithUnsignedInteger:[self.seconds unsignedIntegerValue]
+ (([self.minutes unsignedIntegerValue] + ([self.hours unsignedIntegerValue] * 60)) * 60)];
NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:
[dateFormatter stringFromDate:self.date], @"date",
duration, @"duration",
nil];
[dateFormatter release];
return result;
}
@end