Guest User

Untitled

a guest
Jun 16th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. //
  2. // FriendsTimelineController.m
  3. // Twitterena
  4. //
  5. // Created by Andy on 4/06/09
  6. // Copyright 2009 Andrew.Weekes.com. All rights reserved.
  7. //
  8.  
  9. #import "FriendsTimelineController.h"
  10. #import "FriendsTimelineDataSource.h"
  11. #import "TwitterenaAppDelegate.h"
  12. #import "ColorUtils.h"
  13.  
  14. @interface FriendsTimelineController (Private)
  15. - (void)scrollToFirstUnread;
  16. - (void)didLeaveTab:(UINavigationController*)navigationController;
  17. @end
  18.  
  19.  
  20. @implementation FriendsTimelineController
  21.  
  22. //
  23. // UIViewController methods
  24. //
  25. - (void)viewDidLoad
  26. {
  27. if (!isLoaded) {
  28. [self loadTimeline];
  29. }
  30.  
  31. }
  32.  
  33. - (void) dealloc
  34. {
  35. [super dealloc];
  36. }
  37.  
  38. - (void)viewWillAppear:(BOOL)animated
  39. {
  40. [super viewWillAppear:animated];
  41.  
  42. [self.tableView setContentOffset:contentOffset animated:false];
  43. [self.tableView reloadData];
  44.  
  45. self.navigationController.navigationBar.tintColor = [UIColor navigationColorForTab:tab];
  46. self.tableView.separatorColor = [UIColor lightGrayColor];
  47.  
  48. }
  49.  
  50. - (void)viewDidAppear:(BOOL)animated
  51. {
  52. if (firstTimeToAppear) {
  53. firstTimeToAppear = false;
  54. [self scrollToFirstUnread];
  55. }
  56. [super viewDidAppear:animated];
  57. if (stopwatch) {
  58. LAP(stopwatch, @"viewDidAppear");
  59. [stopwatch release];
  60. stopwatch = nil;
  61. }
  62. }
  63.  
  64. - (void)viewWillDisappear:(BOOL)animated
  65. {
  66. contentOffset = self.tableView.contentOffset;
  67. }
  68.  
  69. - (void)viewDidDisappear:(BOOL)animated
  70. {
  71. }
  72.  
  73. - (void)didReceiveMemoryWarning
  74. {
  75. #if 0
  76. TwitterenaAppDelegate *appDelegate = (TwitterenaAppDelegate*)[UIApplication sharedApplication].delegate;
  77. if (appDelegate.selectedTab != [self navigationController].tabBarItem.tag) {
  78. [super didReceiveMemoryWarning];
  79. }
  80. #endif
  81. }
  82.  
  83. //
  84. // Public methods
  85. //
  86. - (void)loadTimeline
  87. {
  88. NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:@"username"];
  89. NSString *password = [[NSUserDefaults standardUserDefaults] stringForKey:@"password"];
  90. if ([username length] != 0 && [password length] != 0) {
  91. self.navigationItem.leftBarButtonItem.enabled = false;
  92. [timelineDataSource getTimeline];
  93. }
  94. isLoaded = true;
  95. }
  96.  
  97. - (void)restoreAndLoadTimeline:(BOOL)load
  98. {
  99. firstTimeToAppear = true;
  100. stopwatch = [[Stopwatch alloc] init];
  101. tab = [self navigationController].tabBarItem.tag;
  102.  
  103. timelineDataSource = [[FriendsTimelineDataSource alloc] initWithController:self tweetType:tab];
  104. self.tableView.dataSource = timelineDataSource;
  105. self.tableView.delegate = timelineDataSource;
  106. if (load) [self loadTimeline];
  107. }
  108.  
  109. - (IBAction) reload:(id) sender
  110. {
  111. self.navigationItem.leftBarButtonItem.enabled = false;
  112. [timelineDataSource getTimeline];
  113. }
  114.  
  115. - (void)autoRefresh
  116. {
  117. [self reload:nil];
  118. }
  119. // sound notification
  120. -(void)playSound
  121. {BOOL flag = [[NSUserDefaults standardUserDefaults] boolForKey:@"tweetsounds"];
  122. if (flag == false) return;
  123. {itootSound *mentionssound = [[itootSound alloc] initWithFileName:@"mentionssound.aif"];
  124. [mentionssound play];
  125. }}
  126. - (void)postViewAnimationDidFinish
  127. {
  128. if (self.navigationController.topViewController != self) return;
  129.  
  130.  
  131. if (tab == TAB_FRIENDS) {
  132. //
  133. // Do animation if the controller displays friends timeline or sent direct messages.
  134. //
  135. NSArray *indexPaths = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:0], nil];
  136. [self.tableView beginUpdates];
  137. [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop, [self playSound]];
  138. [self.tableView endUpdates];
  139. }
  140.  
  141. }
  142.  
  143. - (void)postTweetDidSucceed:(Status*)status
  144. {
  145. if (tab == TAB_FRIENDS) {
  146. [timelineDataSource.timeline insertStatus:status atIndex:0];
  147. }
  148. }
  149.  
  150. //
  151. // TwitterenaApPDelegate delegate
  152. //
  153. - (void)didLeaveTab:(UINavigationController*)navigationController
  154. {
  155. navigationController.tabBarItem.badgeValue = nil;
  156. for (int i = 0; i < [timelineDataSource.timeline countStatuses]; ++i) {
  157. Status* sts = [timelineDataSource.timeline statusAtIndex:i];
  158. sts.unread = false;
  159. }
  160. unread = 0;
  161. }
  162.  
  163.  
  164. - (void) removeStatus:(Status*)status
  165. {
  166. [timelineDataSource.timeline removeStatus:status];
  167. [self.tableView reloadData];
  168. }
  169.  
  170. - (void) updateFavorite:(Status*)status
  171. {
  172. [timelineDataSource.timeline updateFavorite:status];
  173. }
  174.  
  175. - (void)scrollToFirstUnread
  176. {
  177. BOOL flag = [[NSUserDefaults standardUserDefaults] boolForKey:@"autoScrollToFirstUnread"];
  178. if (flag == false) return;
  179.  
  180. if (unread) {
  181. if (unread < [timelineDataSource.timeline countStatuses]) {
  182. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:unread inSection:0];
  183. [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition: UITableViewScrollPositionBottom animated:true];
  184. }
  185. }
  186. }
  187. //
  188. // TimelineDelegate
  189. //
  190. - (void)timelineDidUpdate:(FriendsTimelineDataSource*)sender count:(int)count insertAt:(int)position
  191. {
  192. self.navigationItem.leftBarButtonItem.enabled = true;
  193.  
  194. if (self.navigationController.tabBarController.selectedIndex == tab &&
  195. self.navigationController.topViewController == self) {
  196.  
  197. [self.tableView beginUpdates];
  198. if (position) {
  199. NSMutableArray *deletion = [[[NSMutableArray alloc] init] autorelease];
  200. [deletion addObject:[NSIndexPath indexPathForRow:position inSection:0]];
  201. [self.tableView deleteRowsAtIndexPaths:deletion withRowAnimation:UITableViewRowAnimationBottom];
  202. }
  203. if (count != 0) {
  204. NSMutableArray *insertion = [[[NSMutableArray alloc] init] autorelease];
  205.  
  206. int numInsert = count;
  207. // Avoid to create too many table cell.
  208. if (numInsert > 8) numInsert = 8;
  209. for (int i = 0; i < numInsert; ++i) {
  210. [insertion addObject:[NSIndexPath indexPathForRow:position + i inSection:0]];
  211. }
  212. [self.tableView insertRowsAtIndexPaths:insertion withRowAnimation:UITableViewRowAnimationTop];
  213. }
  214. [self.tableView endUpdates];
  215.  
  216. if (position == 0 && unread == 0) {
  217. [self performSelector:@selector(scrollToFirstUnread) withObject:nil afterDelay:0.4];
  218. }
  219. }
  220. if (count) {
  221. unread += count;
  222. [self navigationController].tabBarItem.badgeValue = [NSString stringWithFormat:@"%d", unread];
  223.  
  224. }
  225.  
  226. }
  227.  
  228.  
  229. - (void)timelineDidFailToUpdate:(FriendsTimelineDataSource*)sender position:(int)position
  230. {
  231. self.navigationItem.leftBarButtonItem.enabled = true;
  232. }
  233. @end
Add Comment
Please, Sign In to add comment