Guest User

Untitled

a guest
Jul 31st, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.70 KB | None | 0 0
  1. //
  2. // TweetPricesAppDelegate.m
  3. // TweetPrices
  4. //
  5. // Created by Matthew Wilkinson on 20/07/2009.
  6. // Copyright __MyCompanyName__ 2009. All rights reserved.
  7. //
  8.  
  9. #import "TweetPricesAppDelegate.h"
  10. #import "Manage.h"
  11. #import "ManageDetails.h"
  12.  
  13.  
  14. @implementation TweetPricesAppDelegate
  15.  
  16. @synthesize window;
  17. @synthesize tabBarController;
  18. @synthesize navController;
  19. @synthesize twitterEngine;
  20. @synthesize received_friend_timeline;
  21. @synthesize received_images_small;
  22. @synthesize received_images_large;
  23. @synthesize received_user;
  24. @synthesize received_replies;
  25. @synthesize cached_users;
  26. @synthesize loggedIn;
  27.  
  28. - (void)applicationDidFinishLaunching:(UIApplication *)application {
  29.  
  30. received_friend_timeline = [(NSMutableArray*)[NSMutableArray alloc] init];
  31. received_replies = [(NSMutableArray*)[NSMutableArray alloc] init];
  32. received_images_small = [(NSMutableDictionary*)[NSMutableDictionary alloc] init];
  33. received_images_large = [(NSMutableDictionary*)[NSMutableDictionary alloc] init];
  34. cached_users = [(NSMutableDictionary*)[NSMutableDictionary alloc] init];
  35.  
  36. #pragma mark Create User Interface
  37.  
  38. // Setup the login view
  39.  
  40. loginView = [[LoginViewController alloc] initWithNibName:@"Login" bundle:[NSBundle mainBundle]];
  41.  
  42. // Push login view
  43.  
  44. [window addSubview:loginView.view];
  45.  
  46. // Override point for customization after application launch
  47. [window makeKeyAndVisible];
  48. }
  49.  
  50. - (void)goHome {
  51. [self getStatus:0 count:20 refresh:NO];
  52. [window addSubview:tabBarController.view];
  53. }
  54.  
  55. - (void)setEngine:(NSString *)username password:(NSString *)password {
  56.  
  57. #pragma mark setup MGTwitterEngineDelegate
  58.  
  59. _username = username;
  60. _password = password;
  61.  
  62. // Most API calls require a name and password to be set...
  63. if (! username || ! password) {
  64. NSLog(@"You forgot to specify your username/password in AppController.m, things might not work!");
  65. NSLog(@"And if things are mysteriously working without the username/password, it's because NSURLConnection is using a session cookie from another connection.");
  66. }
  67.  
  68. // Create a TwitterEngine and set our login details.
  69. twitterEngine = [[MGTwitterEngine alloc] initWithDelegate:self];
  70. [twitterEngine setUsername:username password:password];
  71. [twitterEngine checkUserCredentials];
  72.  
  73. [twitterEngine getUpdate:3454995098];
  74. }
  75.  
  76.  
  77. - (void)dealloc {
  78. [loginView release];
  79. [twitterEngine release];
  80. [window release];
  81. [super dealloc];
  82. }
  83.  
  84. - (void)getStatus:(int)page count:(int)messages_count refresh:(Boolean)refresh_status {
  85. NSLog(@"Get Status");
  86.  
  87. if (refresh_status==YES) {
  88. [received_friend_timeline removeAllObjects];
  89. }
  90. //[twitterEngine getFollowedTimelineSinceID:0 startingAtPage:page count:messages_count];
  91. [twitterEngine getUserTimelineFor:_username sinceID:0 startingAtPage:page count:messages_count];
  92. }
  93.  
  94. - (void)getReplies:(int)page count:(int)messages_count refresh:(Boolean)refresh_status {
  95. NSLog(@"Get Replies");
  96.  
  97. if (refresh_status==YES) {
  98. [received_replies removeAllObjects];
  99. }
  100.  
  101. //[twitterEngine getFollowedTimelineSinceID:0 startingAtPage:page count:messages_count];
  102. [twitterEngine getRepliesSinceID:0 startingAtPage:page count:messages_count];
  103. }
  104.  
  105.  
  106. #pragma mark MGTwitterEngineDelegate methods
  107.  
  108.  
  109. - (void)requestSucceeded:(NSString *)connectionIdentifier
  110. {
  111. NSLog(@"Request succeeded for connectionIdentifier = %@", connectionIdentifier);
  112.  
  113. [[NSNotificationCenter defaultCenter] postNotificationName:@"requestSucceeded"
  114. object:nil];
  115. }
  116.  
  117.  
  118. - (void)requestFailed:(NSString *)connectionIdentifier withError:(NSError *)error
  119. {
  120. NSLog(@"Request failed for connectionIdentifier = %@, error = %@ (%@)",
  121. connectionIdentifier,
  122. [error localizedDescription],
  123. [error userInfo]);
  124.  
  125. [[NSNotificationCenter defaultCenter] postNotificationName:@"requestFailed"
  126. object:nil
  127. userInfo:[NSDictionary dictionaryWithObject:[error localizedDescription]
  128. forKey:@"description"]];
  129. }
  130.  
  131.  
  132. - (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier
  133. {
  134. //NSLog(@"status started");
  135.  
  136. int i, count;
  137. count = [statuses count];
  138. for (i = 0; i < count; i++)
  139. {
  140. NSDictionary *status = [statuses objectAtIndex: i];
  141. Status *st = [(Status*)[Status alloc] init];
  142. st.created_at = [status valueForKey:@"created_at"];
  143. st.Ident = [status valueForKey:@"id"];
  144. st.text = [status valueForKey:@"text"];
  145. st.source = [status valueForKey:@"source"];
  146. st.truncated = (int)[status valueForKey:@"truncated"];
  147. st.favorited = (int)[status valueForKey:@"favorited"];
  148. st.in_reply_to_status_id = [status valueForKey:@"in_reply_to_status_id"];
  149. st.in_reply_to_status_id = [status valueForKey:@"in_reply_to_user_id"];
  150. //get user profile
  151. NSDictionary *user = [status valueForKey:@"user"];
  152. User *u = [(User*)[User alloc] init];
  153. u.created_at = [user valueForKey:@"created_at"];
  154. u.favourites_count = (NSNumber*)[user valueForKey:@"favourites_count"];
  155. u.followers_count = (NSNumber*)[user valueForKey:@"followers_count"];
  156. u.following = (int)[user valueForKey:@"following"];
  157. u.friends_count = (NSNumber*)[user valueForKey:@"friends_count"];
  158. u.ident = (NSNumber*)[user valueForKey:@"id"];
  159. u.name = [user valueForKey:@"name"];
  160. u.description = [user valueForKey:@"description"];
  161. u.location = [user valueForKey:@"location"];
  162. u.notifications = (int)[user valueForKey:@"notifications"];
  163. u.profile_background_color = [user valueForKey:@"profile_background_color"];
  164. u.profile_background_image_url = [user valueForKey:@"profile_background_image_url"];
  165. u.profile_background_tile = [user valueForKey:@"profile_background_tile"];
  166. u.profile_image_url = [user valueForKey:@"profile_image_url"];
  167. u.profile_link_color = [user valueForKey:@"profile_link_color"];
  168. u.profile_sidebar_border_color = [user valueForKey:@"profile_sidebar_border_color"];
  169. u.profile_sidebar_fill_color = [user valueForKey:@"profile_sidebar_fill_color"];
  170. u.profile_text_color = [user valueForKey:@"profile_text_color"];
  171. u.protected = (int)[user valueForKey:@"protected"];
  172. u.screen_name = [user valueForKey:@"screen_name"];
  173. u.statuses_count = (NSNumber*)[user valueForKey:@"statuses_count"];
  174. //add user to cache for later use
  175. [cached_users setObject:u forKey:u.ident];
  176. st.name = [user valueForKey:@"name"];
  177. st.screen_name = [user valueForKey:@"screen_name"];
  178. st.userID = u.ident;
  179. //get the image object from received_image_small
  180. UIImage *img = (UIImage*)[received_images_small objectForKey:st.userID];
  181. if (img == nil) { //if image does not exist, so we fetch
  182. NSString *profile_image_url = [user valueForKey:@"profile_image_url"];
  183. //profile_image_url = [profile_image_url stringByReplacingOccurrencesOfString:@"_" withString:@""];
  184. NSURL *url = [NSURL URLWithString:profile_image_url];
  185. // TODO: fix the nil image url
  186. NSError *er = [(NSError*)[NSError alloc] init];
  187. img = [UIImage imageWithData: [NSData dataWithContentsOfURL:url options:NSUncachedRead error:&er]];
  188. NSLog(@"Image got: %@",profile_image_url);
  189. if (img==nil) {
  190. NSLog(@"Nil image");
  191. NSLog(@"Error code: %@",[er description]);
  192. }
  193. else {
  194. NSLog(@"Begin image set");
  195. [received_images_small setObject:img forKey:st.userID];
  196. NSLog(@"End image set");
  197. }
  198. [er release];
  199. } else { //if image already there, so we do nothing
  200. //NSLog(@"Ignore once");
  201. }
  202.  
  203. if ([[self.navController topViewController] isKindOfClass:[Manage class]]) {
  204. [received_friend_timeline addObject:st];
  205.  
  206. //[[(Manage *)[(UINavigationController *)[self.tabBarController selectedViewController] topViewController] tableView] reloadData];
  207. //NSLog(st.text);
  208. } else if ([[self.navController topViewController] isKindOfClass:[ManageDetails class]]){
  209. [received_replies addObject:st];
  210. //[[(ManageDetails *)[(UINavigationController *)[self.tabBarController selectedViewController] topViewController] tableView] reloadData];
  211. //NSLog(st.text);
  212. }
  213. }
  214.  
  215. NSLog(@"Friend Timeline got: %d", [received_friend_timeline count]);
  216.  
  217. [[NSNotificationCenter defaultCenter] postNotificationName:@"statusesReceived"
  218. object:nil
  219. userInfo:nil];
  220. }
  221.  
  222.  
  223. - (void)directMessagesReceived:(NSArray *)messages forRequest:(NSString *)connectionIdentifier
  224. {
  225. NSLog(@"Got direct messages for %@:\r%@", connectionIdentifier, messages);
  226. }
  227.  
  228.  
  229. - (void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)connectionIdentifier
  230. {
  231. NSLog(@"Got user info for %@:\r%@", connectionIdentifier, userInfo);
  232. }
  233.  
  234.  
  235. - (void)miscInfoReceived:(NSArray *)miscInfo forRequest:(NSString *)connectionIdentifier
  236. {
  237. NSLog(@"Got misc info for %@:\r%@", connectionIdentifier, miscInfo);
  238. }
  239.  
  240. - (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)connectionIdentifier
  241. {
  242. NSLog(@"Got search results for %@:\r%@", connectionIdentifier, searchResults);
  243. }
  244.  
  245.  
  246. - (void)imageReceived:(UIImage *)image forRequest:(NSString *)connectionIdentifier
  247. {
  248. NSLog(@"Got an image for %@: %@", connectionIdentifier, image);
  249.  
  250. // Save image to the Desktop.
  251. //NSString *path = [[NSString stringWithFormat:@"~/Desktop/%@.tiff", connectionIdentifier] stringByExpandingTildeInPath];
  252. //[[image TIFFRepresentation] writeToFile:path atomically:NO];
  253. }
  254.  
  255. - (void)connectionFinished:(NSString *)connectionIdentifier
  256. {
  257. NSLog(@"Connection finished %@", connectionIdentifier);
  258.  
  259. if ([twitterEngine numberOfConnections] == 0)
  260. {
  261. //[NSApp terminate:self];
  262. }
  263. }
  264.  
  265. #if YAJL_AVAILABLE
  266.  
  267. - (void)receivedObject:(NSDictionary *)dictionary forRequest:(NSString *)connectionIdentifier
  268. {
  269. NSLog(@"Got an object for %@: %@", connectionIdentifier, dictionary);
  270. }
  271.  
  272. #endif
  273.  
  274.  
  275. @end
Add Comment
Please, Sign In to add comment