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