This repository has been archived by the owner on Dec 20, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathRTMHelper.m
188 lines (152 loc) · 5.42 KB
/
RTMHelper.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
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
//
// RTMHelper.m
// SimpleRTM
//
// Created by Greg Allen on 1/28/10.
// Copyright 2010 Apple Inc. All rights reserved.
//
#import "RTMHelper.h"
#import "NSDateHelper.h"
#import "RTMSearch.h"
static int comparePriorities(id obj1, id obj2, void *context) {
NSComparisonResult comp = 0;
int priority1 = [[obj1 objectForKey:@"priority"] intValue];
int priority2 = [[obj2 objectForKey:@"priority"] intValue];
if (priority1 == 0) {
priority1 = 4;
}
if (priority2 == 0) {
priority2 = 4;
}
if (priority1 > priority2) {
comp = NSOrderedDescending;
} else if (priority2 > priority1) {
comp = NSOrderedAscending;
} else {
comp = NSOrderedSame;
}
//NSLog(@"%@ (%d) v %@ (%d) = %d", [obj1 objectForKey:@"name"], priority1, [obj2 objectForKey:@"name"], priority2, comp);
[obj1 retain];
[obj2 retain];
return comp;
}
static int compareDates(id obj1, id obj2, void *context) {
id due1 = [obj1 objectForKey:@"due"];
id due2 = [obj2 objectForKey:@"due"];
NSComparisonResult comp = 0;
if ([due1 isKindOfClass:[NSDate class]] && [due2 isKindOfClass:[NSDate class]]) {
comp = [due1 compare:due2];
} else if ([due1 isKindOfClass:[NSDate class]] && ![due2 isKindOfClass:[NSDate class]]) {
comp = NSOrderedAscending;
} else if ([due2 isKindOfClass:[NSDate class]]) {
comp = NSOrderedDescending;
} else {
comp = NSOrderedSame;
}
//NSLog(@"%@ (%d) v %@ (%d) = %d", [obj1 objectForKey:@"name"], due1, [obj2 objectForKey:@"name"], due2, comp);
[obj1 retain];
[obj2 retain];
return comp;
}
static int compareNames(id obj1, id obj2, void *context) {
NSString *name1 = [obj1 objectForKey:@"name"];
NSString *name2 = [obj2 objectForKey:@"name"];
NSComparisonResult comp = [name1 compare:name2];
NSLog(@"%@ v %@ = %d", name1, name2, comp);
return comp;
}
static int compare(id obj1, id obj2, void *context) {
NSComparisonResult comp = compareDates(obj1, obj2, &context);
if (comp == NSOrderedSame) {
comp = comparePriorities(obj1, obj2, &context);
}
return comp;
}
@implementation RTMHelper
-(NSMutableArray*)getFlatTaskList:(NSDictionary *)rtmResponse {
NSMutableArray *tasks = [[NSMutableArray alloc] init];
NSDictionary *taskList = [rtmResponse objectForKey:@"tasks"];
if (![taskList objectForKey:@"list"])
return tasks;
NSArray *listTasks = [self getArray:[[rtmResponse objectForKey:@"tasks"] objectForKey:@"list"]];
for (NSDictionary *list in listTasks) {
NSArray *taskSeriesList = [self getArray:[list objectForKey:@"taskseries"]];
NSArray* taskSeriesListReversed = [[taskSeriesList reverseObjectEnumerator] allObjects];
for (NSDictionary *taskSeries in taskSeriesListReversed) {
NSArray *taskArray = [self getArray:[taskSeries objectForKey:@"task"]];
for (NSDictionary *t in taskArray) {
id due;
if (![[t objectForKey:@"due"] isEqualToString:@""]) {
NSString *dueDate = [[t objectForKey:@"due"] substringToIndex:10];
due = [NSDate dateWithDateString:dueDate];
} else {
due = @"";
}
NSArray *tags;
id tagNode = [taskSeries objectForKey:@"tags"];
if ([tagNode isKindOfClass:[NSDictionary class]]) {
id tn = [tagNode objectForKey:@"tag"];
if (![tn isKindOfClass:[NSArray class]]) {
tags = [[NSArray alloc] initWithObjects:(NSString*)tn,nil];
} else {
tags = (NSArray*)tn;
}
} else {
tags = [[NSArray alloc] init];
}
NSDictionary *task = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[list objectForKey:@"id"], [taskSeries objectForKey:@"id"],
[t objectForKey:@"id"], [taskSeries objectForKey:@"name"], [t objectForKey:@"priority"], due , tags, nil]
forKeys:[NSArray arrayWithObjects:@"list_id", @"taskseries_id", @"task_id", @"name", @"priority", @"due", @"tags", nil]];
[tasks addObject:task];
}
}
}
//NSLog(@"%@", tasks);
return [self sortTasks:tasks];
}
-(NSMutableArray*)sortTasks:(NSMutableArray*)tasks {
[tasks sortUsingFunction:compare context:nil];
return tasks;
}
-(NSArray*)getArray:(id)obj {
if ([obj isKindOfClass:[NSArray class]]) {
return obj;
} else {
return [NSArray arrayWithObject:obj];
}
}
+ (NSColor *) colorFromHexRGB:(NSString *) inColorString
{
NSColor *result = nil;
unsigned int colorCode = 0;
unsigned char redByte, greenByte, blueByte;
if (nil != inColorString)
{
NSScanner *scanner = [NSScanner scannerWithString:inColorString];
(void) [scanner scanHexInt:&colorCode]; // ignore error
}
redByte = (unsigned char) (colorCode >> 16);
greenByte = (unsigned char) (colorCode >> 8);
blueByte = (unsigned char) (colorCode); // masks off high bits
result = [NSColor
colorWithCalibratedRed: (float)redByte / 0xff
green: (float)greenByte/ 0xff
blue: (float)blueByte / 0xff
alpha:1.0];
return result;
}
+(NSMutableArray*)getLists:(NSDictionary*)rtmResponse {
NSArray* lists = [[rtmResponse objectForKey:@"lists"] objectForKey:@"list"];
NSMutableArray *rtmLists = [[NSMutableArray alloc] init];
for (NSDictionary *list in lists) {
if ([[list objectForKey:@"archived"] intValue] == 0) {
RTMSearch *l = [[RTMSearch alloc] initWithTitle:[list objectForKey:@"name"]
searchType:@"list"
searchParams:[[NSDictionary alloc] initWithObjectsAndKeys:[list objectForKey:@"id"], @"list_id", @"status:incomplete", @"filter", nil]
addParams:[[NSDictionary alloc] initWithObjectsAndKeys:[list objectForKey:@"id"], @"list_id", nil]];
[rtmLists addObject:l];
}
}
return rtmLists;
}
@end