Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // TweetPricesAppDelegate.m
- // TweetPrices
- //
- // Created by Matthew Wilkinson on 20/07/2009.
- // Copyright __MyCompanyName__ 2009. All rights reserved.
- //
- #import "TweetPricesAppDelegate.h"
- #import "Manage.h"
- #import "ManageDetails.h"
- @implementation TweetPricesAppDelegate
- @synthesize window;
- @synthesize tabBarController;
- @synthesize navController;
- @synthesize twitterEngine;
- @synthesize received_friend_timeline;
- @synthesize received_images_small;
- @synthesize received_images_large;
- @synthesize received_user;
- @synthesize received_replies;
- @synthesize cached_users;
- @synthesize loggedIn;
- - (void)applicationDidFinishLaunching:(UIApplication *)application {
- received_friend_timeline = [(NSMutableArray*)[NSMutableArray alloc] init];
- received_replies = [(NSMutableArray*)[NSMutableArray alloc] init];
- received_images_small = [(NSMutableDictionary*)[NSMutableDictionary alloc] init];
- received_images_large = [(NSMutableDictionary*)[NSMutableDictionary alloc] init];
- cached_users = [(NSMutableDictionary*)[NSMutableDictionary alloc] init];
- #pragma mark Create User Interface
- // Setup the login view
- loginView = [[LoginViewController alloc] initWithNibName:@"Login" bundle:[NSBundle mainBundle]];
- // Push login view
- [window addSubview:loginView.view];
- // Override point for customization after application launch
- [window makeKeyAndVisible];
- }
- - (void)goHome {
- [self getStatus:0 count:20 refresh:NO];
- [window addSubview:tabBarController.view];
- }
- - (void)setEngine:(NSString *)username password:(NSString *)password {
- #pragma mark setup MGTwitterEngineDelegate
- _username = username;
- _password = password;
- // Most API calls require a name and password to be set...
- if (! username || ! password) {
- NSLog(@"You forgot to specify your username/password in AppController.m, things might not work!");
- NSLog(@"And if things are mysteriously working without the username/password, it's because NSURLConnection is using a session cookie from another connection.");
- }
- // Create a TwitterEngine and set our login details.
- twitterEngine = [[MGTwitterEngine alloc] initWithDelegate:self];
- [twitterEngine setUsername:username password:password];
- [twitterEngine checkUserCredentials];
- [twitterEngine getUpdate:3454995098];
- }
- - (void)dealloc {
- [loginView release];
- [twitterEngine release];
- [window release];
- [super dealloc];
- }
- - (void)getStatus:(int)page count:(int)messages_count refresh:(Boolean)refresh_status {
- NSLog(@"Get Status");
- if (refresh_status==YES) {
- [received_friend_timeline removeAllObjects];
- }
- //[twitterEngine getFollowedTimelineSinceID:0 startingAtPage:page count:messages_count];
- [twitterEngine getUserTimelineFor:_username sinceID:0 startingAtPage:page count:messages_count];
- }
- - (void)getReplies:(int)page count:(int)messages_count refresh:(Boolean)refresh_status {
- NSLog(@"Get Replies");
- if (refresh_status==YES) {
- [received_replies removeAllObjects];
- }
- //[twitterEngine getFollowedTimelineSinceID:0 startingAtPage:page count:messages_count];
- [twitterEngine getRepliesSinceID:0 startingAtPage:page count:messages_count];
- }
- #pragma mark MGTwitterEngineDelegate methods
- - (void)requestSucceeded:(NSString *)connectionIdentifier
- {
- NSLog(@"Request succeeded for connectionIdentifier = %@", connectionIdentifier);
- [[NSNotificationCenter defaultCenter] postNotificationName:@"requestSucceeded"
- object:nil];
- }
- - (void)requestFailed:(NSString *)connectionIdentifier withError:(NSError *)error
- {
- NSLog(@"Request failed for connectionIdentifier = %@, error = %@ (%@)",
- connectionIdentifier,
- [error localizedDescription],
- [error userInfo]);
- [[NSNotificationCenter defaultCenter] postNotificationName:@"requestFailed"
- object:nil
- userInfo:[NSDictionary dictionaryWithObject:[error localizedDescription]
- forKey:@"description"]];
- }
- - (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier
- {
- //NSLog(@"status started");
- int i, count;
- count = [statuses count];
- for (i = 0; i < count; i++)
- {
- NSDictionary *status = [statuses objectAtIndex: i];
- Status *st = [(Status*)[Status alloc] init];
- st.created_at = [status valueForKey:@"created_at"];
- st.Ident = [status valueForKey:@"id"];
- st.text = [status valueForKey:@"text"];
- st.source = [status valueForKey:@"source"];
- st.truncated = (int)[status valueForKey:@"truncated"];
- st.favorited = (int)[status valueForKey:@"favorited"];
- st.in_reply_to_status_id = [status valueForKey:@"in_reply_to_status_id"];
- st.in_reply_to_status_id = [status valueForKey:@"in_reply_to_user_id"];
- //get user profile
- NSDictionary *user = [status valueForKey:@"user"];
- User *u = [(User*)[User alloc] init];
- u.created_at = [user valueForKey:@"created_at"];
- u.favourites_count = (NSNumber*)[user valueForKey:@"favourites_count"];
- u.followers_count = (NSNumber*)[user valueForKey:@"followers_count"];
- u.following = (int)[user valueForKey:@"following"];
- u.friends_count = (NSNumber*)[user valueForKey:@"friends_count"];
- u.ident = (NSNumber*)[user valueForKey:@"id"];
- u.name = [user valueForKey:@"name"];
- u.description = [user valueForKey:@"description"];
- u.location = [user valueForKey:@"location"];
- u.notifications = (int)[user valueForKey:@"notifications"];
- u.profile_background_color = [user valueForKey:@"profile_background_color"];
- u.profile_background_image_url = [user valueForKey:@"profile_background_image_url"];
- u.profile_background_tile = [user valueForKey:@"profile_background_tile"];
- u.profile_image_url = [user valueForKey:@"profile_image_url"];
- u.profile_link_color = [user valueForKey:@"profile_link_color"];
- u.profile_sidebar_border_color = [user valueForKey:@"profile_sidebar_border_color"];
- u.profile_sidebar_fill_color = [user valueForKey:@"profile_sidebar_fill_color"];
- u.profile_text_color = [user valueForKey:@"profile_text_color"];
- u.protected = (int)[user valueForKey:@"protected"];
- u.screen_name = [user valueForKey:@"screen_name"];
- u.statuses_count = (NSNumber*)[user valueForKey:@"statuses_count"];
- //add user to cache for later use
- [cached_users setObject:u forKey:u.ident];
- st.name = [user valueForKey:@"name"];
- st.screen_name = [user valueForKey:@"screen_name"];
- st.userID = u.ident;
- //get the image object from received_image_small
- UIImage *img = (UIImage*)[received_images_small objectForKey:st.userID];
- if (img == nil) { //if image does not exist, so we fetch
- NSString *profile_image_url = [user valueForKey:@"profile_image_url"];
- //profile_image_url = [profile_image_url stringByReplacingOccurrencesOfString:@"_" withString:@""];
- NSURL *url = [NSURL URLWithString:profile_image_url];
- // TODO: fix the nil image url
- NSError *er = [(NSError*)[NSError alloc] init];
- img = [UIImage imageWithData: [NSData dataWithContentsOfURL:url options:NSUncachedRead error:&er]];
- NSLog(@"Image got: %@",profile_image_url);
- if (img==nil) {
- NSLog(@"Nil image");
- NSLog(@"Error code: %@",[er description]);
- }
- else {
- NSLog(@"Begin image set");
- [received_images_small setObject:img forKey:st.userID];
- NSLog(@"End image set");
- }
- [er release];
- } else { //if image already there, so we do nothing
- //NSLog(@"Ignore once");
- }
- if ([[self.navController topViewController] isKindOfClass:[Manage class]]) {
- [received_friend_timeline addObject:st];
- //[[(Manage *)[(UINavigationController *)[self.tabBarController selectedViewController] topViewController] tableView] reloadData];
- //NSLog(st.text);
- } else if ([[self.navController topViewController] isKindOfClass:[ManageDetails class]]){
- [received_replies addObject:st];
- //[[(ManageDetails *)[(UINavigationController *)[self.tabBarController selectedViewController] topViewController] tableView] reloadData];
- //NSLog(st.text);
- }
- }
- NSLog(@"Friend Timeline got: %d", [received_friend_timeline count]);
- [[NSNotificationCenter defaultCenter] postNotificationName:@"statusesReceived"
- object:nil
- userInfo:nil];
- }
- - (void)directMessagesReceived:(NSArray *)messages forRequest:(NSString *)connectionIdentifier
- {
- NSLog(@"Got direct messages for %@:\r%@", connectionIdentifier, messages);
- }
- - (void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)connectionIdentifier
- {
- NSLog(@"Got user info for %@:\r%@", connectionIdentifier, userInfo);
- }
- - (void)miscInfoReceived:(NSArray *)miscInfo forRequest:(NSString *)connectionIdentifier
- {
- NSLog(@"Got misc info for %@:\r%@", connectionIdentifier, miscInfo);
- }
- - (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)connectionIdentifier
- {
- NSLog(@"Got search results for %@:\r%@", connectionIdentifier, searchResults);
- }
- - (void)imageReceived:(UIImage *)image forRequest:(NSString *)connectionIdentifier
- {
- NSLog(@"Got an image for %@: %@", connectionIdentifier, image);
- // Save image to the Desktop.
- //NSString *path = [[NSString stringWithFormat:@"~/Desktop/%@.tiff", connectionIdentifier] stringByExpandingTildeInPath];
- //[[image TIFFRepresentation] writeToFile:path atomically:NO];
- }
- - (void)connectionFinished:(NSString *)connectionIdentifier
- {
- NSLog(@"Connection finished %@", connectionIdentifier);
- if ([twitterEngine numberOfConnections] == 0)
- {
- //[NSApp terminate:self];
- }
- }
- #if YAJL_AVAILABLE
- - (void)receivedObject:(NSDictionary *)dictionary forRequest:(NSString *)connectionIdentifier
- {
- NSLog(@"Got an object for %@: %@", connectionIdentifier, dictionary);
- }
- #endif
- @end
Add Comment
Please, Sign In to add comment