Changeset 782

Show
Ignore:
Timestamp:
07/26/08 22:28:39 (4 months ago)
Author:
kaz
Message:
  • Refactoring twitter network client
Location:
trunk/TwitterFon
Files:
2 removed
6 modified
2 moved

Legend:

Unmodified
Added
Removed
  • trunk/TwitterFon/Classes/Controllers/PostViewController.h

    r781 r782  
    88 
    99#import <UIKit/UIKit.h> 
    10 #import "PostTweet.h" 
     10#import "TwitterClient.h" 
    1111#import "Message.h" 
    1212#import "SendingWindow.h" 
     
    1818    IBOutlet NSObject*   delegate; 
    1919    IBOutlet NSObject*   appDelegate; 
    20     PostTweet*           post; 
     20    TwitterClient*       post; 
    2121    BOOL                 didPost; 
    2222    NSRange              textRange; 
  • trunk/TwitterFon/Classes/Controllers/PostViewController.m

    r781 r782  
    121121- (IBAction) send: (id) sender 
    122122{ 
    123     post = [[PostTweet alloc] initWithDelegate:self]; 
     123    post = [[TwitterClient alloc] initWithDelegate:self]; 
    124124    [post post:text.text]; 
    125125    [sendingWindow show]; 
     
    143143} 
    144144 
    145 - (void)postTweetDidSucceed:(NSDictionary*)dic 
    146 { 
     145- (void)twitterClientDidSucceed:(TwitterClient*)sender messages:(NSObject*)obj; 
     146{ 
     147    NSDictionary *dic = nil; 
     148    if ([obj isKindOfClass:[NSDictionary class]]) { 
     149        dic = (NSDictionary*)obj;     
     150    } 
     151     
    147152    [sendingWindow hide]; 
    148153     
     
    156161    [post autorelease]; 
    157162    [self cancel:self]; 
    158     didPost = true; 
    159 } 
    160  
    161 - (void)postTweetDidFail:(NSString*)error 
     163    didPost = (dic) ? true : false; 
     164} 
     165 
     166- (void)twitterClientDidFail:(TwitterClient*)sender error:(NSString*)error 
    162167{ 
    163168    [sendingWindow fail:error]; 
  • trunk/TwitterFon/Classes/Controllers/TimelineViewController.m

    r775 r782  
    244244 
    245245// 
    246 // TimelineDownloaderDelegate 
     246// TimelineDelegate 
    247247// 
    248248- (void)timelineDidReceiveNewMessage:(Message*)msg 
  • trunk/TwitterFon/Classes/Network/TwitterClient.h

    r771 r782  
    33#import "Message.h" 
    44 
    5 @interface TimelineDownloader : TFConnection 
     5@interface TwitterClient : TFConnection 
    66{ 
    77} 
    88 
    99- (void)get:(MessageType)type since:(NSString*)since; 
     10- (void)post:(NSString*)tweet; 
    1011 
    1112@end 
  • trunk/TwitterFon/Classes/Network/TwitterClient.m

    r780 r782  
    11// 
    2 //  TimelineDownloader.m 
    3 //  TwitterPhox 
     2//  TwitterClient.m 
     3//  TwitterFon 
    44// 
    55//  Created by kaz on 7/13/08. 
     
    77// 
    88 
    9 #import "TimelineDownloader.h" 
     9#import "TwitterClient.h" 
     10#import "StringUtil.h" 
    1011#import "JSON.h" 
    1112#import "Message.h" 
     
    2021//#define DEBUG_WITH_PUBLIC_TIMELINE 
    2122 
    22 @interface NSObject (TimelineDownloaderDelegate) 
    23 - (void)timelineDownloaderDidSucceed:(TimelineDownloader*)sender messages:(NSArray*)messages; 
    24 - (void)timelineDownloaderDidFail:(TimelineDownloader*)sender error:(NSError*)error; 
     23@interface NSObject (TwitterClientDelegate) 
     24- (void)twitterClientDidSucceed:(TwitterClient*)sender messages:(NSObject*)messages; 
     25- (void)twitterClientDidFail:(TwitterClient*)sender error:(NSString*)error; 
    2526@end 
    2627 
    27 @implementation TimelineDownloader 
     28@implementation TwitterClient 
    2829 
    2930- (void)get:(MessageType)type since:(NSString*)since 
     
    5758} 
    5859 
     60- (void)post:(NSString*)tweet 
     61{ 
     62     
     63        NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:@"username"]; 
     64        NSString *password = [[NSUserDefaults standardUserDefaults] stringForKey:@"password"]; 
     65     
     66        NSString* url = [NSString stringWithFormat:@"https://%@:%@@twitter.com/statuses/update.json", 
     67                     username, password]; 
     68     
     69    NSLog(@"%@", url); 
     70     
     71    NSString *postString = [NSString stringWithFormat:@"status=%@&source=TwitterFon", [tweet encodeAsURIComponent]]; 
     72     
     73    [self post:url body:postString]; 
     74     
     75} 
     76 
    5977- (void)TFConnectionDidFailWithError:(NSError*)error 
    6078{ 
    6179    [self alertError:@"Connection Failed" withMessage:[error localizedDescription]]; 
    62     [delegate timelineDownloaderDidFail:self error:error]; 
     80    [delegate twitterClientDidFail:self error:[error localizedDescription]]; 
    6381} 
    6482 
     
    93111     
    94112    if ([obj isKindOfClass:[NSDictionary class]]) { 
     113        NSDictionary* dic = (NSDictionary*)obj; 
     114        NSString *msg = [dic objectForKey:@"error"]; 
     115        if (msg) { 
     116            NSLog(@"Twitter returns an error: %@", msg); 
     117            [self alertError:@"Server error" withMessage:msg]; 
     118            [delegate twitterClientDidFail:self error:msg]; 
     119        } 
     120        else { 
     121            [delegate twitterClientDidSucceed:self messages:obj]; 
     122        } 
     123    } 
     124    else if ([obj isKindOfClass:[NSArray class]]) { 
     125        [delegate twitterClientDidSucceed:self messages:obj]; 
     126    } 
     127    else { 
     128        NSLog(@"Null or wrong response: %@", content); 
     129        [delegate twitterClientDidSucceed:self messages:nil]; 
     130    } 
     131     
     132#if 0 
     133    if ([obj isKindOfClass:[NSDictionary class]]) { 
    95134        NSLog(@"%@", content); 
    96135        NSDictionary* dic = (NSDictionary*)obj; 
    97136        NSString *msg = [dic objectForKey:@"error"]; 
    98         if (msg == nil) msg = @""; 
     137        if (msg == nil) { 
     138            msg = @""; 
     139        } 
    99140        NSLog(@"Twitter returns an error: %@", msg); 
    100         [self alertError:@"Server error" withMessage:msg]; 
    101                 [delegate timelineDownloaderDidFail:self error:nil]; 
     141//        [self alertError:@"Server error" withMessage:msg]; 
     142                [delegate twitterClientDidFail:self error:nil]; 
    102143    } 
    103144    else if ([obj isKindOfClass:[NSArray class]]) { 
    104145        NSArray *ary = (NSArray*)obj; 
    105146        NSLog(@"received %d objects", [ary count]); 
    106         [delegate timelineDownloaderDidSucceed:self messages:ary]; 
     147        [delegate twitterClientDidSucceed:self messages:ary]; 
    107148    } 
    108149    else { 
    109150        NSLog(@"Null or wrong response: %@", content); 
    110         [delegate timelineDownloaderDidSucceed:self messages:nil]; 
     151        [delegate twitterClientDidSucceed:self messages:nil]; 
    111152    } 
     153#endif 
    112154} 
    113155 
  • trunk/TwitterFon/Classes/Twitter/Timeline.h

    r721 r782  
    11#import <UIKit/UIKit.h> 
    22#import "Message.h" 
    3 #import "TimelineDownloader.h" 
     3#import "TwitterClient.h" 
    44 
    55@interface Timeline : NSObject 
     
    77        IBOutlet NSObject*  delegate; 
    88        NSMutableArray*     messages; 
    9         TimelineDownloader* timelineConn; 
     9        TwitterClient*      twitterClient; 
    1010    MessageType         type; 
    1111} 
  • trunk/TwitterFon/Classes/Twitter/Timeline.m

    r771 r782  
    2626{ 
    2727        [messages release]; 
    28         [timelineConn release]; 
     28        [twitterClient release]; 
    2929        [super dealloc]; 
    3030} 
     
    4747- (void)update:(MessageType)aType 
    4848{ 
    49         if (timelineConn) return; 
     49        if (twitterClient) return; 
    5050     
    5151    type = aType; 
    5252 
    53         timelineConn = [[TimelineDownloader alloc] initWithDelegate:self]; 
     53        twitterClient = [[TwitterClient alloc] initWithDelegate:self]; 
    5454 
    5555        NSString* lastMessageDate = nil; 
     
    5757            lastMessageDate = ((Message*)[messages lastObject]).createdAt; 
    5858    } 
    59         [timelineConn get:type since:lastMessageDate]; 
     59        [twitterClient get:type since:lastMessageDate]; 
    6060} 
    6161 
     
    8080} 
    8181 
    82 - (void)timelineDownloaderDidSucceed:(TimelineDownloader*)sender messages:(NSArray*)ary 
     82- (void)twitterClientDidSucceed:(TwitterClient*)sender messages:(NSObject*)obj 
    8383{ 
    84         [timelineConn autorelease]; 
    85         timelineConn = nil; 
     84        [twitterClient autorelease]; 
     85        twitterClient = nil; 
     86 
     87    if (obj == nil) return; 
    8688     
    87     if (ary == nil) { 
     89    NSArray *ary = nil; 
     90    if ([obj isKindOfClass:[NSArray class]]) { 
     91        ary = (NSArray*)obj; 
     92    } 
     93    else { 
    8894        return; 
    8995    } 
     
    115121} 
    116122 
    117 - (void)timelineDownloaderDidFail:(TimelineDownloader*)sender error:(NSError*)error 
     123- (void)twitterClientDidFail:(TwitterClient*)sender error:(NSString*)error 
    118124{ 
    119         [timelineConn autorelease]; 
    120         timelineConn = nil; 
     125        [twitterClient autorelease]; 
     126        twitterClient = nil; 
    121127} 
    122128 
  • trunk/TwitterFon/TwitterFon.xcodeproj/project.pbxproj

    r775 r782  
    1616                6E2BD0C10E35AE04003A8F69 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6E2BD0C00E35AE04003A8F69 /* QuartzCore.framework */; }; 
    1717                6E2BD11F0E35B3D2003A8F69 /* Default.png in Resources */ = {isa = PBXBuildFile; fileRef = 6E2BD11E0E35B3D2003A8F69 /* Default.png */; }; 
     18                6E335EA40E3BBE0C008B15BB /* TwitterClient.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E335EA30E3BBE0C008B15BB /* TwitterClient.m */; }; 
    1819                6EAF29400E36F97E00129692 /* alert.png in Resources */ = {isa = PBXBuildFile; fileRef = 6EAF293F0E36F97E00129692 /* alert.png */; }; 
    1920                6ED17FA70E38D3A0006E8AB7 /* forward.png in Resources */ = {isa = PBXBuildFile; fileRef = 6ED17FA50E38D3A0006E8AB7 /* forward.png */; }; 
     
    2526                6EDD27740E3B5429002E09A0 /* TinyURL.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EDD27730E3B5429002E09A0 /* TinyURL.m */; }; 
    2627                6EEBF7000E3B1F390003EA63 /* ImageDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EEBF6FA0E3B1F390003EA63 /* ImageDownloader.m */; }; 
    27                 6EEBF7010E3B1F390003EA63 /* PostTweet.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EEBF6FC0E3B1F390003EA63 /* PostTweet.m */; }; 
    28                 6EEBF7020E3B1F390003EA63 /* TimelineDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EEBF6FE0E3B1F390003EA63 /* TimelineDownloader.m */; }; 
    2928                6EEBF70C0E3B1FD40003EA63 /* ColorUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EEBF7040E3B1FD40003EA63 /* ColorUtils.m */; }; 
    3029                6EEBF70D0E3B1FD40003EA63 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EEBF7050E3B1FD40003EA63 /* main.m */; }; 
     
    7170                6E2BD0C00E35AE04003A8F69 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; }; 
    7271                6E2BD11E0E35B3D2003A8F69 /* Default.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Default.png; sourceTree = "<group>"; }; 
     72                6E335EA20E3BBE0C008B15BB /* TwitterClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TwitterClient.h; path = Network/TwitterClient.h; sourceTree = "<group>"; }; 
     73                6E335EA30E3BBE0C008B15BB /* TwitterClient.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TwitterClient.m; path = Network/TwitterClient.m; sourceTree = "<group>"; }; 
    7374                6EAF293F0E36F97E00129692 /* alert.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = alert.png; sourceTree = "<group>"; }; 
    7475                6ED17FA50E38D3A0006E8AB7 /* forward.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = forward.png; sourceTree = "<group>"; }; 
     
    8384                6EEBF6F90E3B1F390003EA63 /* ImageDownloader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ImageDownloader.h; path = Network/ImageDownloader.h; sourceTree = "<group>"; }; 
    8485                6EEBF6FA0E3B1F390003EA63 /* ImageDownloader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ImageDownloader.m; path = Network/ImageDownloader.m; sourceTree = "<group>"; }; 
    85                 6EEBF6FB0E3B1F390003EA63 /* PostTweet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PostTweet.h; path = Network/PostTweet.h; sourceTree = "<group>"; }; 
    86                 6EEBF6FC0E3B1F390003EA63 /* PostTweet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PostTweet.m; path = Network/PostTweet.m; sourceTree = "<group>"; }; 
    87                 6EEBF6FD0E3B1F390003EA63 /* TimelineDownloader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TimelineDownloader.h; path = Network/TimelineDownloader.h; sourceTree = "<group>"; }; 
    88                 6EEBF6FE0E3B1F390003EA63 /* TimelineDownloader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TimelineDownloader.m; path = Network/TimelineDownloader.m; sourceTree = "<group>"; }; 
    8986                6EEBF7030E3B1FD40003EA63 /* ColorUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ColorUtils.h; path = Classes/OtherSources/ColorUtils.h; sourceTree = "<group>"; }; 
    9087                6EEBF7040E3B1FD40003EA63 /* ColorUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ColorUtils.m; path = Classes/OtherSources/ColorUtils.m; sourceTree = "<group>"; }; 
     
    301298                                6EEBF6F90E3B1F390003EA63 /* ImageDownloader.h */, 
    302299                                6EEBF6FA0E3B1F390003EA63 /* ImageDownloader.m */, 
    303                                 6EEBF6FB0E3B1F390003EA63 /* PostTweet.h */, 
    304                                 6EEBF6FC0E3B1F390003EA63 /* PostTweet.m */, 
    305                                 6EEBF6FD0E3B1F390003EA63 /* TimelineDownloader.h */, 
    306                                 6EEBF6FE0E3B1F390003EA63 /* TimelineDownloader.m */, 
    307300                                6E092C390E3B41D40039FEE3 /* TFConnection.h */, 
    308301                                6E092C3A0E3B41D40039FEE3 /* TFConnection.m */, 
     302                                6E335EA20E3BBE0C008B15BB /* TwitterClient.h */, 
     303                                6E335EA30E3BBE0C008B15BB /* TwitterClient.m */, 
    309304                                6EDD27720E3B5429002E09A0 /* TinyURL.h */, 
    310305                                6EDD27730E3B5429002E09A0 /* TinyURL.m */, 
     
    450445                                6EDC01D10E344B5600FF142E /* DBConnection.m in Sources */, 
    451446                                6EEBF7000E3B1F390003EA63 /* ImageDownloader.m in Sources */, 
    452                                 6EEBF7010E3B1F390003EA63 /* PostTweet.m in Sources */, 
    453                                 6EEBF7020E3B1F390003EA63 /* TimelineDownloader.m in Sources */, 
    454447                                6EEBF70C0E3B1FD40003EA63 /* ColorUtils.m in Sources */, 
    455448                                6EEBF70D0E3B1FD40003EA63 /* main.m in Sources */, 
     
    467460                                6E092C3B0E3B41D40039FEE3 /* TFConnection.m in Sources */, 
    468461                                6EDD27740E3B5429002E09A0 /* TinyURL.m in Sources */, 
     462                                6E335EA40E3BBE0C008B15BB /* TwitterClient.m in Sources */, 
    469463                        ); 
    470464                        runOnlyForDeploymentPostprocessing = 0;