PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes/UVWelcomeViewController.m

https://gitlab.com/lisit1003/uservoice-ios-sdk
Objective C | 373 lines | 318 code | 48 blank | 7 comment | 113 complexity | 1039849ed49b037534032d2995d8ff83 MD5 | raw file
  1. //
  2. // UVWelcomeViewController.m
  3. // UserVoice
  4. //
  5. // Created by UserVoice on 12/15/09.
  6. // Copyright 2009 UserVoice Inc. All rights reserved.
  7. //
  8. #import <QuartzCore/QuartzCore.h>
  9. #import "UVWelcomeViewController.h"
  10. #import "UVStyleSheet.h"
  11. #import "UVSession.h"
  12. #import "UVForum.h"
  13. #import "UVClientConfig.h"
  14. #import "UVSubdomain.h"
  15. #import "UVContactViewController.h"
  16. #import "UVSuggestionListViewController.h"
  17. #import "UVSuggestion.h"
  18. #import "UVArticle.h"
  19. #import "UVSuggestionDetailsViewController.h"
  20. #import "UVArticleViewController.h"
  21. #import "UVHelpTopic.h"
  22. #import "UVHelpTopicViewController.h"
  23. #import "UVConfig.h"
  24. #import "UVPostIdeaViewController.h"
  25. #import "UVBabayaga.h"
  26. #import "UVUtils.h"
  27. @implementation UVWelcomeViewController {
  28. NSInteger _filter;
  29. }
  30. - (BOOL)showArticles {
  31. return [UVSession currentSession].config.topicId || [[UVSession currentSession].topics count] == 0;
  32. }
  33. #pragma mark ===== table cells =====
  34. - (void)initCellForContact:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
  35. cell.backgroundColor = [UIColor whiteColor];
  36. cell.textLabel.text = NSLocalizedStringFromTableInBundle(@"Send us a message", @"UserVoice", [UserVoice bundle], nil);
  37. if (IOS7) {
  38. cell.textLabel.textColor = cell.textLabel.tintColor;
  39. }
  40. }
  41. - (void)initCellForPostIdea:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
  42. cell.backgroundColor = [UIColor whiteColor];
  43. cell.textLabel.text = NSLocalizedStringFromTableInBundle(@"Post an idea", @"UserVoice", [UserVoice bundle], nil);
  44. if (IOS7) {
  45. cell.textLabel.textColor = cell.textLabel.tintColor;
  46. }
  47. }
  48. - (void)initCellForForum:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
  49. cell.backgroundColor = [UIColor whiteColor];
  50. cell.textLabel.text = NSLocalizedStringFromTableInBundle(@"Feedback Forum", @"UserVoice", [UserVoice bundle], nil);
  51. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  52. }
  53. - (void)customizeCellForForum:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
  54. NSString *detail;
  55. if ([UVSession currentSession].forum.suggestionsCount == 1) {
  56. detail = NSLocalizedStringFromTableInBundle(@"1 idea", @"UserVoice", [UserVoice bundle], nil);
  57. } else {
  58. detail = [NSString stringWithFormat:NSLocalizedStringFromTableInBundle(@"%@ ideas", @"UserVoice", [UserVoice bundle], nil), [UVUtils formatInteger:[UVSession currentSession].forum.suggestionsCount]];
  59. }
  60. cell.detailTextLabel.text = detail;
  61. }
  62. - (void)customizeCellForTopic:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
  63. cell.backgroundColor = [UIColor whiteColor];
  64. if (indexPath.row == [[UVSession currentSession].topics count]) {
  65. cell.textLabel.text = NSLocalizedStringFromTableInBundle(@"All Articles", @"UserVoice", [UserVoice bundle], nil);
  66. cell.detailTextLabel.text = nil;
  67. } else {
  68. UVHelpTopic *topic = [[UVSession currentSession].topics objectAtIndex:indexPath.row];
  69. cell.textLabel.text = topic.name;
  70. cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", (int)topic.articleCount];
  71. }
  72. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  73. }
  74. - (void)customizeCellForArticle:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
  75. cell.backgroundColor = [UIColor whiteColor];
  76. UVArticle *article = [[UVSession currentSession].articles objectAtIndex:indexPath.row];
  77. cell.textLabel.text = article.question;
  78. cell.imageView.image = [UVUtils imageNamed:@"uv_article.png"];
  79. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  80. cell.textLabel.numberOfLines = 2;
  81. cell.textLabel.font = [UIFont boldSystemFontOfSize:13.0];
  82. }
  83. - (void)initCellForFlash:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
  84. cell.backgroundColor = [UIColor whiteColor];
  85. cell.textLabel.text = NSLocalizedStringFromTableInBundle(@"View idea", @"UserVoice", [UserVoice bundle], nil);
  86. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  87. }
  88. - (void)initCellForArticleResult:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
  89. [_instantAnswerManager initCellForArticle:cell finalCondition:indexPath == nil];
  90. }
  91. - (void)customizeCellForArticleResult:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
  92. id model = [self.searchResults objectAtIndex:indexPath.row];
  93. [_instantAnswerManager customizeCell:cell forArticle:(UVArticle *)model];
  94. }
  95. - (void)initCellForSuggestionResult:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
  96. [_instantAnswerManager initCellForSuggestion:cell finalCondition:indexPath == nil];
  97. }
  98. - (void)customizeCellForSuggestionResult:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
  99. id model = [self.searchResults objectAtIndex:indexPath.row];
  100. [_instantAnswerManager customizeCell:cell forSuggestion:(UVSuggestion *)model];
  101. }
  102. #pragma mark ===== UITableViewDataSource Methods =====
  103. - (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  104. NSString *identifier = @"";
  105. NSInteger style = UITableViewCellStyleValue1;
  106. if (theTableView == _searchController.searchResultsTableView || _searching) {
  107. id model = [self.searchResults objectAtIndex:indexPath.row];
  108. if ([model isMemberOfClass:[UVArticle class]]) {
  109. identifier = @"ArticleResult";
  110. } else {
  111. identifier = @"SuggestionResult";
  112. }
  113. style = UITableViewCellStyleDefault;
  114. } else {
  115. if (indexPath.section == 0 && indexPath.row == 0 && [UVSession currentSession].config.showContactUs)
  116. identifier = @"Contact";
  117. else if (indexPath.section == 0 && [UVSession currentSession].config.showForum)
  118. identifier = @"Forum";
  119. else if (indexPath.section == 0 && [UVSession currentSession].config.showPostIdea)
  120. identifier = @"PostIdea";
  121. else if ([self showArticles])
  122. identifier = @"Article";
  123. else
  124. identifier = @"Topic";
  125. }
  126. return [self createCellForIdentifier:identifier tableView:theTableView indexPath:indexPath style:style selectable:YES];
  127. }
  128. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  129. if (tableView == _searchController.searchResultsTableView || _searching) {
  130. NSString *identifier;
  131. id model = [self.searchResults objectAtIndex:indexPath.row];
  132. if ([model isMemberOfClass:[UVArticle class]]) {
  133. identifier = @"ArticleResult";
  134. } else {
  135. identifier = @"SuggestionResult";
  136. }
  137. return [self heightForDynamicRowWithReuseIdentifier:identifier indexPath:indexPath];
  138. } else {
  139. return 44;
  140. }
  141. }
  142. - (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView {
  143. if (theTableView == _searchController.searchResultsTableView || _searching) {
  144. return 1;
  145. } else {
  146. int sections = 0;
  147. if ([UVSession currentSession].config.showKnowledgeBase && ([[UVSession currentSession].topics count] > 0 || [[UVSession currentSession].articles count] > 0))
  148. sections++;
  149. if ([UVSession currentSession].config.showForum || [UVSession currentSession].config.showContactUs || [UVSession currentSession].config.showPostIdea)
  150. sections++;
  151. return sections;
  152. }
  153. }
  154. - (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section {
  155. if (theTableView == _searchController.searchResultsTableView || _searching) {
  156. return self.searchResults.count;
  157. } else {
  158. if (section == 0 && ([UVSession currentSession].config.showForum || [UVSession currentSession].config.showContactUs || [UVSession currentSession].config.showPostIdea))
  159. return (([UVSession currentSession].config.showForum || [UVSession currentSession].config.showPostIdea) && [UVSession currentSession].config.showContactUs) ? 2 : 1;
  160. else if ([self showArticles])
  161. return [[UVSession currentSession].articles count];
  162. else
  163. return [[UVSession currentSession].topics count] + 1;
  164. }
  165. }
  166. - (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  167. if (theTableView == _searchController.searchResultsTableView || _searching) {
  168. [_instantAnswerManager pushViewFor:[self.searchResults objectAtIndex:indexPath.row] parent:self];
  169. } else {
  170. if (indexPath.section == 0 && indexPath.row == 0 && [UVSession currentSession].config.showContactUs) {
  171. [self presentModalViewController:[UVContactViewController new]];
  172. } else if (indexPath.section == 0 && [UVSession currentSession].config.showForum) {
  173. UVSuggestionListViewController *next = [UVSuggestionListViewController new];
  174. [self.navigationController pushViewController:next animated:YES];
  175. } else if (indexPath.section == 0 && [UVSession currentSession].config.showPostIdea) {
  176. [self presentModalViewController:[UVPostIdeaViewController new]];
  177. } else if ([self showArticles]) {
  178. UVArticle *article = (UVArticle *)[[UVSession currentSession].articles objectAtIndex:indexPath.row];
  179. UVArticleViewController *next = [UVArticleViewController new];
  180. next.article = article;
  181. [self.navigationController pushViewController:next animated:YES];
  182. } else {
  183. UVHelpTopic *topic = nil;
  184. if (indexPath.row < [[UVSession currentSession].topics count])
  185. topic = (UVHelpTopic *)[[UVSession currentSession].topics objectAtIndex:indexPath.row];
  186. UVHelpTopicViewController *next = [[UVHelpTopicViewController alloc] initWithTopic:topic];
  187. [self.navigationController pushViewController:next animated:YES];
  188. }
  189. }
  190. [theTableView deselectRowAtIndexPath:indexPath animated:YES];
  191. }
  192. - (NSString *)tableView:(UITableView *)theTableView titleForHeaderInSection:(NSInteger)section {
  193. if (theTableView == _searchController.searchResultsTableView || _searching)
  194. return nil;
  195. else if (section == 0 && ([UVSession currentSession].config.showForum || [UVSession currentSession].config.showContactUs || [UVSession currentSession].config.showPostIdea))
  196. return nil;
  197. else if ([UVSession currentSession].config.topicId)
  198. return [((UVHelpTopic *)[[UVSession currentSession].topics objectAtIndex:0]) name];
  199. else
  200. return NSLocalizedStringFromTableInBundle(@"Knowledge Base", @"UserVoice", [UserVoice bundle], nil);
  201. }
  202. - (CGFloat)tableView:(UITableView *)theTableView heightForHeaderInSection:(NSInteger)section {
  203. return theTableView == _searchController.searchResultsTableView || _searching ? 0 : 30;
  204. }
  205. - (void)logoTapped {
  206. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.uservoice.com/ios"]];
  207. }
  208. #pragma mark ===== UISearchBarDelegate Methods =====
  209. - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
  210. [searchBar setShowsCancelButton:YES animated:YES];
  211. _filter = IA_FILTER_ALL;
  212. searchBar.showsScopeBar = YES;
  213. searchBar.selectedScopeButtonIndex = 0;
  214. [searchBar sizeToFit];
  215. if (FORMSHEET) {
  216. _tableView.tableHeaderView = searchBar;
  217. _searching = YES;
  218. [_tableView reloadData];
  219. } else {
  220. [_searchController setActive:YES animated:YES];
  221. _searchController.searchResultsTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  222. }
  223. return YES;
  224. }
  225. - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope {
  226. _filter = searchBar.selectedScopeButtonIndex;
  227. if (FORMSHEET) {
  228. [_tableView reloadData];
  229. } else {
  230. [_searchController.searchResultsTableView reloadData];
  231. }
  232. }
  233. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
  234. _instantAnswerManager.searchText = searchBar.text;
  235. [_instantAnswerManager search];
  236. }
  237. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  238. _searchBar.showsScopeBar = NO;
  239. if (FORMSHEET) {
  240. [_searchBar setShowsCancelButton:NO animated:YES];
  241. _searchBar.text = @"";
  242. _instantAnswerManager.instantAnswers = [NSArray array];
  243. [_searchBar resignFirstResponder];
  244. _searching = NO;
  245. [_searchBar sizeToFit];
  246. _tableView.tableHeaderView = _searchBar;
  247. [_tableView reloadData];
  248. }
  249. }
  250. #pragma mark ===== Search handling =====
  251. - (void)didUpdateInstantAnswers {
  252. if (FORMSHEET) {
  253. if (_searching)
  254. [_tableView reloadData];
  255. } else {
  256. if (_searchController.active)
  257. [_searchController.searchResultsTableView reloadData];
  258. }
  259. }
  260. - (NSArray *)searchResults {
  261. switch (_filter) {
  262. case IA_FILTER_ALL:
  263. return _instantAnswerManager.instantAnswers;
  264. case IA_FILTER_ARTICLES:
  265. return _instantAnswerManager.articles;
  266. case IA_FILTER_IDEAS:
  267. return _instantAnswerManager.ideas;
  268. default:
  269. return nil;
  270. }
  271. }
  272. #pragma mark ===== Basic View Methods =====
  273. - (void)loadView {
  274. [super loadView];
  275. [UVBabayaga track:VIEW_KB];
  276. _instantAnswerManager = [UVInstantAnswerManager new];
  277. _instantAnswerManager.delegate = self;
  278. self.navigationItem.title = NSLocalizedStringFromTableInBundle(@"Feedback & Support", @"UserVoice", [UserVoice bundle], nil);
  279. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedStringFromTableInBundle(@"Close", @"UserVoice", [UserVoice bundle], nil)
  280. style:UIBarButtonItemStylePlain
  281. target:self
  282. action:@selector(dismiss)];
  283. [self setupGroupedTableView];
  284. if ([UVSession currentSession].config.showKnowledgeBase) {
  285. _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
  286. _searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  287. _searchBar.placeholder = NSLocalizedStringFromTableInBundle(@"Search", @"UserVoice", [UserVoice bundle], nil);
  288. _searchBar.delegate = self;
  289. _searchBar.showsScopeBar = NO;
  290. if ([UVSession currentSession].config.showForum) {
  291. _searchBar.scopeButtonTitles = @[NSLocalizedStringFromTableInBundle(@"All", @"UserVoice", [UserVoice bundle], nil), NSLocalizedStringFromTableInBundle(@"Articles", @"UserVoice", [UserVoice bundle], nil), NSLocalizedStringFromTableInBundle(@"Ideas", @"UserVoice", [UserVoice bundle], nil)];
  292. }
  293. _tableView.tableHeaderView = _searchBar;
  294. if (!FORMSHEET) {
  295. _searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
  296. _searchController.searchResultsDelegate = self;
  297. _searchController.searchResultsDataSource = self;
  298. }
  299. }
  300. if (![UVSession currentSession].clientConfig.whiteLabel) {
  301. _tableView.tableFooterView = self.poweredByView;
  302. }
  303. [_tableView reloadData];
  304. }
  305. - (void)dismiss {
  306. _instantAnswerManager.delegate = nil;
  307. [super dismiss];
  308. }
  309. - (void)viewWillAppear:(BOOL)animated {
  310. [_tableView reloadData];
  311. [super viewWillAppear:animated];
  312. }
  313. - (void)dealloc {
  314. if (_instantAnswerManager) {
  315. _instantAnswerManager.delegate = nil;
  316. }
  317. if (_searchBar) {
  318. _searchBar.delegate = nil;
  319. }
  320. if (_searchController) {
  321. _searchController.searchResultsDelegate = nil;
  322. _searchController.searchResultsDataSource = nil;
  323. }
  324. }
  325. @end