Merge pull request #327 from brianantonelli/master

Issue 226
This commit is contained in:
Shazron Abdullah
2012-03-06 10:47:06 -08:00
6 changed files with 87 additions and 43 deletions

View File

@@ -17,7 +17,7 @@ TwitterDemo = {
},
setup:function(){
var tests = ["isAvailable", "isSetup", "tweet", "compose", "timeline", "mentions"];
var tests = ["isAvailable", "isSetup", "tweet1", "tweet2", "tweet3", "tweet4", "tweet5", "tweet6", "timeline", "mentions"];
for(var i=0, l=tests.length; i<l; i++){
this.$(tests[i]).onclick = this[tests[i]];
}
@@ -37,21 +37,66 @@ TwitterDemo = {
});
},
tweet:function(){
tweet1:function(){
TwitterDemo.log("wait..");
window.plugins.twitter.sendTweet(
window.plugins.twitter.composeTweet(
function(s){ TwitterDemo.log("tweet success"); },
function(e){ TwitterDemo.log("tweet failure: " + e); },
"Tweety Poo",
"https://github.com/brianantonelli",
"http://zomgdinosaurs.com/zomg.jpg");
"Text, Image, URL",
{
urlAttach:"https://github.com/brianantonelli",
imageAttach:"http://zomgdinosaurs.com/zomg.jpg"
});
},
compose: function() {
tweet2:function(){
TwitterDemo.log("wait..");
window.plugins.twitter.composeTweet();
window.plugins.twitter.composeTweet(
function(s){ TwitterDemo.log("tweet success"); },
function(e){ TwitterDemo.log("tweet failure: " + e); },
"Text, Remote Image",
{
imageAttach:"http://zomgdinosaurs.com/zomg.jpg"
});
},
tweet6:function(){
TwitterDemo.log("wait..");
window.plugins.twitter.composeTweet(
function(s){ TwitterDemo.log("tweet success"); },
function(e){ TwitterDemo.log("tweet failure: " + e); },
"Text, Local Image",
{
imageAttach:"www/ninja-lolcat.gif"
});
},
tweet3:function(){
TwitterDemo.log("wait..");
window.plugins.twitter.composeTweet(
function(s){ TwitterDemo.log("tweet success"); },
function(e){ TwitterDemo.log("tweet failure: " + e); },
"Text, URL",
{
urlAttach:"https://github.com/brianantonelli"
});
},
tweet4:function(){
TwitterDemo.log("wait..");
window.plugins.twitter.composeTweet(
function(s){ TwitterDemo.log("tweet success"); },
function(e){ TwitterDemo.log("tweet failure: " + e); },
"Text");
},
tweet5:function(){
TwitterDemo.log("wait..");
window.plugins.twitter.composeTweet(
function(s){ TwitterDemo.log("tweet success"); },
function(e){ TwitterDemo.log("tweet failure: " + e); });
},
timeline:function(){
TwitterDemo.log("wait..");
window.plugins.twitter.getPublicTimeline(

View File

@@ -17,8 +17,12 @@
<ol>
<li><a href="#" id="isAvailable">isAvailable</a></li>
<li><a href="#" id="isSetup">isSetup</a></li>
<li><a href="#" id="tweet">tweet through API</a></li>
<li><a href="#" id="compose">compose new tweet popup</a></li>
<li><a href="#" id="tweet1">tweet (text, img, url)</a></li>
<li><a href="#" id="tweet2">tweet (text, remote img)</a></li>
<li><a href="#" id="tweet6">tweet (text, local img)</a></li>
<li><a href="#" id="tweet3">tweet (text, url)</a></li>
<li><a href="#" id="tweet4">tweet (text)</a></li>
<li><a href="#" id="tweet5">tweet (empty)</a></li>
<li><a href="#" id="timeline">timeline</a></li>
<li><a href="#" id="mentions">mentions</a></li>
</ol>

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

View File

@@ -21,8 +21,6 @@
- (void) isTwitterSetup:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
- (void) sendTweet:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
- (void) composeTweet:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
- (void) getPublicTimeline:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;

View File

@@ -36,19 +36,25 @@
[super writeJavascript:[[PluginResult resultWithStatus:PGCommandStatus_OK messageAsInt:canTweet ? 1 : 0] toSuccessCallbackString:callbackId]];
}
- (void) sendTweet:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
- (void) composeTweet:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
// arguments: callback, tweet text, url attachment, image attachment
NSString *callbackId = [arguments objectAtIndex:0];
NSString *tweetText = [arguments objectAtIndex:1];
NSString *urlAttach = [arguments objectAtIndex:2];
NSString *imageAttach = [arguments objectAtIndex:3];
NSString *tweetText = [options objectForKey:@"text"];
NSString *urlAttach = [options objectForKey:@"urlAttach"];
NSString *imageAttach = [options objectForKey:@"imageAttach"];
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
[tweetViewController setInitialText:tweetText];
BOOL ok = YES;
NSString *errorMessage;
if(tweetText != nil){
ok = [tweetViewController setInitialText:tweetText];
if(!ok){
errorMessage = @"Tweet is too long";
}
}
if(urlAttach != nil){
ok = [tweetViewController addURL:[NSURL URLWithString:urlAttach]];
if(!ok){
@@ -58,8 +64,14 @@
if(imageAttach != nil){
// Note that the image is loaded syncronously
UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageAttach]]];
ok = [tweetViewController addImage:img];
if([imageAttach hasPrefix:@"http://"]){
UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageAttach]]];
ok = [tweetViewController addImage:img];
[img release];
}
else{
ok = [tweetViewController addImage:[UIImage imageNamed:imageAttach]];
}
if(!ok){
errorMessage = @"Image could not be added";
}
@@ -93,25 +105,15 @@
[tweetViewController release];
}
- (void) composeTweet:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
TWTweetComposeViewController *tweetComposeViewController = [[TWTweetComposeViewController alloc] init];
[tweetComposeViewController setCompletionHandler: ^(TWTweetComposeViewControllerResult result) {
[[super appViewController] dismissModalViewControllerAnimated:YES];
}];
[[super appViewController] presentModalViewController:tweetComposeViewController animated:YES];
}
- (void) getPublicTimeline:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options{
NSString *callbackId = [arguments objectAtIndex:0];
NSString *url = [NSString stringWithFormat:@"%@statuses/public_timeline.json", TWITTER_URL];
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:url] parameters:nil requestMethod:TWRequestMethodGET];
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:url] parameters:nil requestMethod:TWRequestMethodGET];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *jsResponse;
if([urlResponse statusCode] == 200) {
if([urlResponse statusCode] == 200) {
NSString *dataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSDictionary *dict = [dataString objectFromJSONString];
jsResponse = [[PluginResult resultWithStatus:PGCommandStatus_OK messageAsDictionary:dict] toSuccessCallbackString:callbackId];
@@ -120,10 +122,10 @@
else{
jsResponse = [[PluginResult resultWithStatus:PGCommandStatus_ERROR
messageAsString:[NSString stringWithFormat:@"HTTP Error: %i", [urlResponse statusCode]]]
toErrorCallbackString:callbackId];
toErrorCallbackString:callbackId];
}
[self performCallbackOnMainThreadforJS:jsResponse];
[self performCallbackOnMainThreadforJS:jsResponse];
}];
[postRequest release];

View File

@@ -8,15 +8,10 @@ Twitter.prototype.isTwitterSetup = function(response){
PhoneGap.exec(response, null, "com.phonegap.twitter", "isTwitterSetup", []);
};
Twitter.prototype.sendTweet = function(success, failure, tweetText, urlAttach, imageAttach){
if(typeof urlAttach === "undefined") urlAttach = "";
if(typeof imageAttach === "undefined") imageAttach = "";
PhoneGap.exec(success, failure, "com.phonegap.twitter", "sendTweet", [tweetText, urlAttach, imageAttach]);
};
Twitter.prototype.composeTweet = function() {
PhoneGap.exec(null, null, "com.phonegap.twitter", "composeTweet", []);
Twitter.prototype.composeTweet = function(success, failure, tweetText, options){
options = options || {};
options.text = tweetText;
PhoneGap.exec(success, failure, "com.phonegap.twitter", "composeTweet", [options]);
};
Twitter.prototype.getPublicTimeline = function(success, failure){