PageRenderTime 52ms CodeModel.GetById 27ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes/UVProfileViewController.m

https://github.com/appsocial/uservoice-iphone-sdk
Objective C | 335 lines | 256 code | 58 blank | 21 comment | 20 complexity | ab0ad5a6604c3b36fe99f927df18ec3a MD5 | raw file
  1//
  2//  UVProfileViewController.m
  3//  UserVoice
  4//
  5//  Created by UserVoice on 11/12/09.
  6//  Copyright 2009 UserVoice Inc. All rights reserved.
  7//
  8
  9#import <QuartzCore/QuartzCore.h>
 10#import "UVProfileViewController.h"
 11#import "UVUser.h"
 12#import "UVClientConfig.h"
 13#import "UVSession.h"
 14#import "UVSuggestionDetailsViewController.h"
 15#import "UVProfileEditViewController.h"
 16#import "UVProfileIdeaListViewController.h"
 17#import "UVStyleSheet.h"
 18#import "UVUserChickletView.h"
 19
 20#define UV_PROFILE_TAG_NAME 1
 21#define UV_PROFILE_TAG_EMAIL 2
 22#define UV_PROFILE_TAG_CHICKLET 3
 23#define UV_PROFILE_TAG_MEMBER_SINCE 4
 24
 25#define UV_PROFILE_ROW_SUPPORTING_IDEAS 0
 26#define UV_PROFILE_ROW_CREATED_IDEAS 1
 27#define UV_PROFILE_ROW_EDIT 2
 28
 29
 30@implementation UVProfileViewController
 31
 32@synthesize userId;
 33@synthesize userName;
 34@synthesize user;
 35@synthesize avatarUrl;
 36@synthesize message;
 37
 38- (id)initWithUserId:(NSInteger)theUserId name:(NSString *)theUserName {
 39	return [self initWithUserId:theUserId name:theUserName avatarUrl:nil];
 40}
 41
 42- (id)initWithUserId:(NSInteger)theUserId name:(NSString *)theUserName avatarUrl:(NSString *)theAvatarUrl {
 43	if (self = [super init]) {
 44		self.userId = theUserId;
 45		self.userName = theUserName;
 46		self.avatarUrl = theAvatarUrl;
 47	}
 48	return self;
 49}
 50
 51- (id)initWithUVUser:(UVUser *)theUser {
 52	if (self = [super init]) {
 53		self.user = theUser;
 54		self.userId = theUser.userId;
 55		self.userName = theUser.displayName;
 56		self.avatarUrl = theUser.avatarUrl;
 57	}
 58	return self;
 59}
 60
 61// Determines whether we're viewing the logged-in user's profile
 62- (BOOL)isSelf {
 63	return self.userId == [UVSession currentSession].user.userId;
 64}
 65
 66- (NSString *)backButtonTitle {
 67	return [self isSelf] ? @"My Profile" : self.userName;
 68}
 69
 70- (NSString *)memberSince {
 71	static NSDateFormatter* dateFormatter = nil;
 72	if (!dateFormatter) {
 73		dateFormatter = [[NSDateFormatter alloc] init];
 74		[dateFormatter setDateFormat:@"'Member since 'MMM yyyy"];
 75	}
 76	return [dateFormatter stringFromDate:self.user.createdAt];
 77}
 78
 79- (UIView *)createHeaderView 
 80{
 81	CGFloat screenWidth = [UVClientConfig getScreenWidth];
 82	UIView *header = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 84)] autorelease];
 83	header.backgroundColor = [UIColor clearColor];
 84	
 85	// Name
 86	UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(70, 10, 240, 16)];
 87	label.tag = UV_PROFILE_TAG_NAME;
 88	label.text = self.userName ? self.userName : @"Anonymous";
 89	label.font = [UIFont boldSystemFontOfSize:16];
 90	label.textColor = [UVStyleSheet veryDarkGrayColor];
 91	label.backgroundColor = [UIColor clearColor];
 92	[header addSubview:label];
 93	[label release];
 94	
 95	CGFloat prevY = 26.0;
 96	
 97	if ([self isSelf]) {
 98		// Email
 99		label = [[UILabel alloc] initWithFrame:CGRectMake(70, 30, 240, 14)];
100		label.tag = UV_PROFILE_TAG_EMAIL;
101		label.text = self.user.email;
102		label.font = [UIFont systemFontOfSize:14];
103		label.textColor = [UVStyleSheet veryDarkGrayColor];
104		label.backgroundColor = [UIColor clearColor];
105		[header addSubview:label];
106		[label release];
107		
108		prevY = 44.0;
109	}
110
111	// Date
112	label = [[UILabel alloc] initWithFrame:CGRectMake(70, prevY + 4, 240, 11)];
113	label.tag = UV_PROFILE_TAG_MEMBER_SINCE;
114	label.text = self.user ? [self memberSince] : @"";
115	label.font = [UIFont boldSystemFontOfSize:11];
116	label.textColor = [UIColor darkGrayColor];
117	label.backgroundColor = [UIColor clearColor];
118	[header addSubview:label];
119	[label release];
120
121	// Avatar
122	NSInteger karma = self.user ? self.user.karmaScore : 0;
123	UVUserChickletView *chicklet = [UVUserChickletView userChickletViewWithOrigin:CGPointMake(10, 10)
124																	   controller:self
125																			style:UVUserChickletStyleDetail
126																		   userId:self.userId
127																			 name:self.userName
128																		avatarUrl:self.avatarUrl
129																			admin:NO
130																	   karmaScore:karma];
131	chicklet.tag = UV_PROFILE_TAG_CHICKLET;
132	[chicklet enableButton:NO];
133	[header addSubview:chicklet];
134
135	return header;
136}
137
138- (void)updateHeaderView {
139	UIView *header = self.tableView.tableHeaderView;
140
141	// Name
142	UILabel *label = (UILabel *)[header viewWithTag:UV_PROFILE_TAG_NAME];
143	label.text = self.userName ? self.userName : @"Anonymous";
144
145	// Email
146	label = (UILabel *)[header viewWithTag:UV_PROFILE_TAG_EMAIL];
147	label.text = self.user.email;
148	
149	// Date
150	label = (UILabel *)[header viewWithTag:UV_PROFILE_TAG_MEMBER_SINCE];
151	label.text = [self memberSince];
152
153	// User Chicklet
154	UVUserChickletView *chicklet = (UVUserChickletView *)[header viewWithTag:UV_PROFILE_TAG_CHICKLET];
155	[chicklet updateWithAvatarUrl:self.avatarUrl karmaScore:self.user.karmaScore];
156}
157
158- (void)didRetrieveUser:(UVUser *)theUser {
159	self.user = theUser;
160	self.userName = theUser.displayName;
161	self.userId = theUser.userId;
162	self.avatarUrl = theUser.avatarUrl;
163	
164	[self updateHeaderView];
165	[self.tableView reloadData];
166	[self hideActivityIndicator];
167}
168
169- (void)editButtonTapped {
170	UVProfileEditViewController *next = [[UVProfileEditViewController alloc] init];
171	[self.navigationController pushViewController:next animated:YES];
172	[next release];
173}
174
175#pragma mark ===== table cells =====
176
177- (void)customizeCellForProfile:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
178	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
179	cell.selectionStyle = UITableViewCellSelectionStyleBlue;
180	NSString *text = @"";
181	switch (indexPath.row) {
182		case UV_PROFILE_ROW_SUPPORTING_IDEAS: {
183			NSInteger count = self.user.supportedSuggestionsCount;
184			text = [NSString stringWithFormat:@"Supporting %d %@", count, count == 1 ? @"idea" : @"ideas"];
185			if (count == 0) {
186				cell.accessoryType = UITableViewCellAccessoryNone;
187				cell.selectionStyle = UITableViewCellSelectionStyleNone;
188			}
189			break;
190		}
191		case UV_PROFILE_ROW_CREATED_IDEAS: {
192			NSInteger count = self.user.createdSuggestionsCount;
193			text = [NSString stringWithFormat:@"Created %d %@", count, count == 1 ? @"idea" : @"ideas"];
194			if (count == 0) {
195				cell.accessoryType = UITableViewCellAccessoryNone;
196				cell.selectionStyle = UITableViewCellSelectionStyleNone;
197			}
198			break;
199		}
200		case UV_PROFILE_ROW_EDIT:
201			text = @"Edit my profile";
202			break;
203		default:
204			break;
205	}
206	cell.textLabel.text = text;
207}
208
209#pragma mark ===== UITableViewDataSource Methods =====
210
211- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
212	return [self createCellForIdentifier:@"Profile"
213							   tableView:theTableView
214							   indexPath:indexPath
215								   style:UITableViewCellStyleDefault
216							  selectable:YES];
217}
218
219- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView {
220	return 1;
221}
222
223- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
224	return [self isSelf] ? 3 : 2;
225}
226
227#pragma mark ===== UITableViewDelegate Methods =====
228
229- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
230	[theTableView deselectRowAtIndexPath:indexPath animated:YES];
231
232	UIViewController *next = nil;
233	
234	switch (indexPath.row) {
235		case UV_PROFILE_ROW_SUPPORTING_IDEAS:
236			if (self.user.supportedSuggestionsCount > 0) {
237				next = [[UVProfileIdeaListViewController alloc] initWithUVUser:self.user 
238																	  andTitle:@"Ideas Supported"
239																showingCreated:NO];
240			}
241			break;
242		case UV_PROFILE_ROW_CREATED_IDEAS:
243			if (self.user.createdSuggestionsCount > 0) {
244				next = [[UVProfileIdeaListViewController alloc] initWithUVUser:self.user 
245																	  andTitle:@"Ideas Created"
246																showingCreated:YES];
247			}
248			break;
249		case UV_PROFILE_ROW_EDIT:
250			next = [UVProfileEditViewController alloc];
251			break;
252		default:
253			break;
254	}
255	
256	if (next) {
257		[self.navigationController pushViewController:next animated:YES];
258		[next release];
259	}
260}
261
262#pragma mark ===== Basic View Methods =====
263
264// Implement loadView to create a view hierarchy programmatically, without using a nib.
265- (void)loadView {
266	[super loadView];
267	
268	self.navigationItem.title = [self isSelf] ? @"My Profile" : @"User Profile";
269
270	CGRect frame = [self contentFrame];
271	UIView *contentView = [[UIView alloc] initWithFrame:frame];
272	CGFloat screenWidth = [UVClientConfig getScreenWidth];
273	CGFloat screenHeight = [UVClientConfig getScreenHeight];
274
275	UITableView *theTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight-64) style:UITableViewStyleGrouped];
276	theTableView.dataSource = self;
277	theTableView.delegate = self;
278	theTableView.backgroundColor = [UIColor clearColor];
279	
280	theTableView.tableHeaderView = [self createHeaderView];
281	
282	[contentView addSubview:theTableView];
283	self.tableView = theTableView;
284	[theTableView release];
285	
286	self.view = contentView;
287	[contentView release];
288	
289	[self addGradientBackground];
290}
291
292- (void)viewWillAppear:(BOOL)animated {
293	[super viewWillAppear:animated];
294		
295	if (!self.user) {	
296		if ([self isSelf]) {
297			self.user = [UVSession currentSession].user;
298			self.userName = self.user.displayName;
299			self.avatarUrl = self.user.avatarUrl;
300			[self updateHeaderView];
301			
302		} else {
303			[self showActivityIndicator];
304			[UVUser getWithUserId:self.userId delegate:self];
305		}		
306	} else {
307		[self.tableView reloadData];
308		[self updateHeaderView];
309	}
310}
311
312- (void)didReceiveMemoryWarning {
313	// Releases the view if it doesn't have a superview.
314    [super didReceiveMemoryWarning];
315	
316	// Release any cached data, images, etc that aren't in use.
317}
318
319- (void)viewDidUnload {
320	// Release any retained subviews of the main view.
321	// e.g. self.myOutlet = nil;
322	self.tableView = nil;
323}
324
325
326- (void)dealloc {
327	self.user = nil;
328	self.userName = nil;
329	self.avatarUrl = nil;
330	self.tableView = nil;
331	
332    [super dealloc];
333}
334
335@end