-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTeam.m
More file actions
166 lines (138 loc) · 5.15 KB
/
Team.m
File metadata and controls
166 lines (138 loc) · 5.15 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
//
// Team.m
// TwentyFirstCenturyTag
//
// Created by Christopher Ballinger on 7/7/11.
// Copyright 2011. All rights reserved.
//
#import "Team.h"
#import "ASIHTTPRequest.h"
#import "APIUtil.h"
@implementation Team
@synthesize name;
@synthesize leader;
@synthesize users;
@synthesize venues;
@synthesize history;
@synthesize points;
@synthesize poiPoints;
@synthesize motto;
@synthesize avatar;
-(id)init
{
self = [super init];
if(self)
{
APITYPE = @"Team";
NAME = @"name";
LEADER = @"leader";
USERS = @"members";
VENUES = @"venues";
HISTORY = @"events";
MOTTO = @"moto";
POINTS = @"points";
POIPOINTS = @"poi_pts";
MOTTO = @"motto";
AVATAR = @"avatar";
}
return self;
}
//iAPI methods
-(void) parseDictionary:(NSDictionary *)fields
{
[super parseDictionary:fields];
self.name = [fields objectForKey:NAME];
self.leader = [fields objectForKey:LEADER];
//self.users = [NSSet setWithArray:[fields objectForKey:USERS]];
//self.venues = [NSSet setWithArray:[fields objectForKey:VENUES]];
self.motto = [fields objectForKey:MOTTO];
NSLog(@"fields: %@",[fields objectForKey:AVATAR]);
self.avatar = [fields objectForKey:AVATAR];
NSArray * rawHistory = [fields objectForKey:HISTORY];
NSMutableArray * tempHistory = [[NSMutableArray alloc] initWithCapacity:[rawHistory count]];
for (int i =0; i<[rawHistory count]; i++)
{
Event * event = [[Event alloc] initWithDictionary:[rawHistory objectAtIndex:i]];
[tempHistory addObject:event];
}
history = tempHistory;
NSArray *rawPoiPoints = [fields objectForKey:POIPOINTS];
NSMutableDictionary *pointsDictionary = [[NSMutableDictionary alloc] initWithCapacity:[rawPoiPoints count]];
for(int i = 0; i < [rawPoiPoints count]; i++)
{
NSString * tempPoiId = [NSString stringWithFormat:@"%@",[[rawPoiPoints objectAtIndex:i] objectForKey:@"poi"]];
NSString * tempPoiPoints = [NSString stringWithFormat:@"%@", [[rawPoiPoints objectAtIndex:i] objectForKey:@"pts"]];
NSLog(@"input poi pts: %@",[tempPoiId class]);
if(tempPoiId)
[pointsDictionary setObject:tempPoiPoints forKey:tempPoiId];
}
poiPoints = pointsDictionary;
self.points = [fields objectForKey:POINTS];
NSArray *userFieldsArray = [fields objectForKey:USERS];
if(userFieldsArray)
{
NSMutableSet *usersSet = [[NSMutableSet alloc] initWithCapacity:[userFieldsArray count]];
for(int i = 0; i < [userFieldsArray count]; i++)
{
NSLog(@"Users: %@",[userFieldsArray objectAtIndex:i]);
User *user = [[User alloc] initWithDictionary:[userFieldsArray objectAtIndex:i]];
[usersSet addObject:user];
}
users = usersSet;
}
NSArray *venueFieldsArray = [fields objectForKey:VENUES];
if(venueFieldsArray)
{
NSMutableSet *venuesSet = [[NSMutableSet alloc] initWithCapacity:[venueFieldsArray count]];
for(int i = 0; i < [venueFieldsArray count]; i++)
{
Venue *venue = [[Venue alloc] initWithDictionary:[venueFieldsArray objectAtIndex:i]];
[venuesSet addObject:venue];
}
venues = venuesSet;
}
}
-(UIImage *)getTeamImage
{
if(self.avatar == @"")
{
UIImage *teamImage = nil;
return teamImage;
}
return [Team getTeamImageWithId:[self getId]];
}
+ (UIImage *)getTeamImageWithId: (NSString *)teamId
{
NSString* documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* filePath = [NSString stringWithFormat:@"%@/%@.jpeg",documentsDirectory,teamId];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) //get local image
{
NSLog(@"get local image");
NSData * imageData = [[NSFileManager defaultManager] contentsAtPath:filePath];
UIImage *teamImage=[UIImage imageWithData:imageData];
return teamImage;
}
else //fetch from server
{
NSLog(@"get server image");
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://21tag.com/media/team_avatars/%@.jpg",teamId ]]; //V1 "/createteam"
NSData * urlData = [NSData dataWithContentsOfURL:url];
if(urlData)
{
NSLog(@"There is data");
UIImage *teamImage = [[UIImage alloc] initWithData:urlData];
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *jpegFilePath = [NSString stringWithFormat:@"%@/%@.jpeg",docDir,teamId];
NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(teamImage, 1.0f)];//1.0f = 100% quality
[data writeToFile:jpegFilePath atomically:YES];
return teamImage;
}
}
UIImage * teamImage = nil;
return teamImage;
}
-(NSString *) description
{
return [NSString stringWithFormat:@"Name: %@ Members: %@ Venues: %@",name,users,venues];
}
@end