PageRenderTime 46ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Three20UI/Sources/TTSearchDisplayController.m

https://github.com/tomekc/three20
Objective C | 217 lines | 111 code | 53 blank | 53 comment | 8 complexity | 470d09834e1d26ac2397a5ec1c57f19b MD5 | raw file
  1. //
  2. // Copyright 2009-2010 Facebook
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #import "Three20UI/TTSearchDisplayController.h"
  17. // UI
  18. #import "Three20UI/TTTableViewController.h"
  19. #import "Three20UI/TTTableViewDataSource.h"
  20. // UICommon
  21. #import "Three20UICommon/TTGlobalUICommon.h"
  22. // Core
  23. #import "Three20Core/TTCorePreprocessorMacros.h"
  24. const int kTTSearchBarBackgroundTag = 18942;
  25. static const NSTimeInterval kPauseInterval = 0.4;
  26. ///////////////////////////////////////////////////////////////////////////////////////////////////
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. ///////////////////////////////////////////////////////////////////////////////////////////////////
  29. @implementation TTSearchDisplayController
  30. @synthesize searchResultsViewController = _searchResultsViewController;
  31. @synthesize pausesBeforeSearching = _pausesBeforeSearching;
  32. ///////////////////////////////////////////////////////////////////////////////////////////////////
  33. - (id)initWithSearchBar:(UISearchBar*)searchBar contentsController:(UIViewController*)controller {
  34. if (self = [super initWithSearchBar:searchBar contentsController:controller]) {
  35. self.delegate = self;
  36. }
  37. return self;
  38. }
  39. ///////////////////////////////////////////////////////////////////////////////////////////////////
  40. - (void)dealloc {
  41. TT_INVALIDATE_TIMER(_pauseTimer);
  42. TT_RELEASE_SAFELY(_searchResultsDelegate2);
  43. TT_RELEASE_SAFELY(_searchResultsViewController);
  44. [super dealloc];
  45. }
  46. ///////////////////////////////////////////////////////////////////////////////////////////////////
  47. ///////////////////////////////////////////////////////////////////////////////////////////////////
  48. #pragma mark -
  49. #pragma mark Private
  50. ///////////////////////////////////////////////////////////////////////////////////////////////////
  51. - (void)resetResults {
  52. if (_searchResultsViewController.model.isLoading) {
  53. [_searchResultsViewController.model cancel];
  54. }
  55. [_searchResultsViewController.dataSource search:nil];
  56. [_searchResultsViewController viewWillDisappear:NO];
  57. [_searchResultsViewController viewDidDisappear:NO];
  58. _searchResultsViewController.tableView = nil;
  59. }
  60. ///////////////////////////////////////////////////////////////////////////////////////////////////
  61. - (void)restartPauseTimer {
  62. TT_INVALIDATE_TIMER(_pauseTimer);
  63. _pauseTimer = [NSTimer scheduledTimerWithTimeInterval:kPauseInterval target:self
  64. selector:@selector(searchAfterPause) userInfo:nil repeats:NO];
  65. }
  66. ///////////////////////////////////////////////////////////////////////////////////////////////////
  67. - (void)searchAfterPause {
  68. _pauseTimer = nil;
  69. [_searchResultsViewController.dataSource search:self.searchBar.text];
  70. }
  71. ///////////////////////////////////////////////////////////////////////////////////////////////////
  72. ///////////////////////////////////////////////////////////////////////////////////////////////////
  73. #pragma mark -
  74. #pragma mark UISearchDisplayDelegate
  75. ///////////////////////////////////////////////////////////////////////////////////////////////////
  76. - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController*)controller {
  77. self.searchContentsController.navigationItem.rightBarButtonItem.enabled = NO;
  78. UIView* backgroundView = [self.searchBar viewWithTag:kTTSearchBarBackgroundTag];
  79. if (backgroundView) {
  80. [UIView beginAnimations:nil context:nil];
  81. [UIView setAnimationDuration:TT_FAST_TRANSITION_DURATION];
  82. backgroundView.alpha = 0;
  83. [UIView commitAnimations];
  84. }
  85. // if (!self.searchContentsController.navigationController) {
  86. // [UIView beginAnimations:nil context:nil];
  87. // self.searchBar.superview.top -= self.searchBar.screenY - TTStatusHeight();
  88. // [UIView commitAnimations];
  89. // }
  90. }
  91. ///////////////////////////////////////////////////////////////////////////////////////////////////
  92. - (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController*)controller {
  93. [_searchResultsViewController updateView];
  94. }
  95. ///////////////////////////////////////////////////////////////////////////////////////////////////
  96. - (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController*)controller {
  97. self.searchContentsController.navigationItem.rightBarButtonItem.enabled = YES;
  98. UIView* backgroundView = [self.searchBar viewWithTag:kTTSearchBarBackgroundTag];
  99. if (backgroundView) {
  100. [UIView beginAnimations:nil context:nil];
  101. [UIView setAnimationDuration:TT_FAST_TRANSITION_DURATION];
  102. backgroundView.alpha = 1;
  103. [UIView commitAnimations];
  104. }
  105. // if (!self.searchContentsController.navigationController) {
  106. // [UIView beginAnimations:nil context:nil];
  107. // self.searchBar.superview.top += self.searchBar.top - TTStatusHeight();
  108. // [UIView commitAnimations];
  109. // }
  110. }
  111. ///////////////////////////////////////////////////////////////////////////////////////////////////
  112. - (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController*)controller {
  113. [self resetResults];
  114. }
  115. ///////////////////////////////////////////////////////////////////////////////////////////////////
  116. - (void)searchDisplayController:(UISearchDisplayController *)controller
  117. didLoadSearchResultsTableView:(UITableView *)tableView {
  118. }
  119. ///////////////////////////////////////////////////////////////////////////////////////////////////
  120. - (void)searchDisplayController:(UISearchDisplayController *)controller
  121. willUnloadSearchResultsTableView:(UITableView *)tableView {
  122. }
  123. ///////////////////////////////////////////////////////////////////////////////////////////////////
  124. - (void)searchDisplayController:(UISearchDisplayController *)controller
  125. didShowSearchResultsTableView:(UITableView *)tableView {
  126. _searchResultsViewController.tableView = tableView;
  127. [_searchResultsViewController viewWillAppear:NO];
  128. [_searchResultsViewController viewDidAppear:NO];
  129. }
  130. ///////////////////////////////////////////////////////////////////////////////////////////////////
  131. - (void)searchDisplayController:(UISearchDisplayController*)controller
  132. willHideSearchResultsTableView:(UITableView*)tableView {
  133. [self resetResults];
  134. }
  135. ///////////////////////////////////////////////////////////////////////////////////////////////////
  136. - (BOOL)searchDisplayController:(UISearchDisplayController*)controller
  137. shouldReloadTableForSearchString:(NSString*)searchString {
  138. if (_pausesBeforeSearching) {
  139. [self restartPauseTimer];
  140. } else {
  141. [_searchResultsViewController.dataSource search:searchString];
  142. }
  143. return NO;
  144. }
  145. ///////////////////////////////////////////////////////////////////////////////////////////////////
  146. - (BOOL)searchDisplayController:(UISearchDisplayController*)controller
  147. shouldReloadTableForSearchScope:(NSInteger)searchOption {
  148. [_searchResultsViewController invalidateModel];
  149. [_searchResultsViewController.dataSource search:self.searchBar.text];
  150. return NO;
  151. }
  152. ///////////////////////////////////////////////////////////////////////////////////////////////////
  153. ///////////////////////////////////////////////////////////////////////////////////////////////////
  154. #pragma mark -
  155. #pragma mark Public
  156. ///////////////////////////////////////////////////////////////////////////////////////////////////
  157. - (void)setSearchResultsDelegate:(id<UITableViewDelegate>)searchResultsDelegate {
  158. [super setSearchResultsDelegate:searchResultsDelegate];
  159. if (_searchResultsDelegate2 != searchResultsDelegate) {
  160. [_searchResultsDelegate2 release];
  161. _searchResultsDelegate2 = [searchResultsDelegate retain];
  162. }
  163. }
  164. @end