Skip to content

Instantly share code, notes, and snippets.

@dasher
Created July 9, 2010 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dasher/469440 to your computer and use it in GitHub Desktop.
Save dasher/469440 to your computer and use it in GitHub Desktop.
var win = Titanium.UI.currentWindow;
var xhr;
Ti.App.globalImage = null;
function alert(title,message){
var alertDialog = Titanium.UI.createAlertDialog({
title: title,
message: message,
buttonNames: ['OK']
});
alertDialog.show();
}
//Check If The Form Is Empty
function validate(){
if (whatText.value != '' && whereText.value != ''){
return true;
} else {
alert('Hold On','Something is Missing');
return false;
}
}
// Helper for updating the interface
function networkUpdateSuccess() {
alert('Success Uploaded',Ti.App.globalImage);
win.rightNavButton = null;
Ti.App.globalImage = null;
whatText.value = '';
whereText.value = '';
actInd.hide();
upload.hide();
previewImage.image = '../images/upload_image.png';
}
function initXHR() {
// Create the xhr object
xhr = Titanium.Network.createHTTPClient();
// Set the static properties on the xhr object
xhr.setTimeout(20000);
// First setup the event handlers
xhr.onerror = function(e) {
Ti.UI.createAlertDialog({title:'Error', message:e.error}).show();
};
xhr.onload = function(e) {
// Sucessful operation from the send
if (xhr.readyState == 4) {
networkUpdateSuccess();
} else {
// Houston - there was a problem
// Although the onerror is probably going to be called
}
Ti.API.info('IN ONLOAD ' + this.status + ' readyState ' + this.readyState);
};
xhr.onsendstream = function(e) {
Ti.API.info('ONSENDSTREAM - PROGRESS: ' + e.progress);
};
}
function postPic(whatText, whereText) {
// Ok - now let's get down to business
// You may have these already elsewhere
initXHR();
actInd.show();
// open the client - it's probably best to do this fresh with each upload
// it'll ensure that the readystate is set accordingly - not sure if it's reset
// with the open or the send
xhr.open('POST','https://twitpic.com/api/uploadAndPost');
// send the data
xhr.send({media:Ti.App.globalImage, message:whatText.value+' @ '+whereText.value});
}
//Listener to Upload
uploadSmall.addEventListener('click',function(e){
if(validate() != false){
postPic(whatText.value,whereText.value);
}
});
//Listener to Upload Large Button
upload.addEventListener('click',function(e){
if(validate() != false){
postPic(whatText.value,whereText.value);
}
});
//Listener to Option Group
Ti.App.optionGroup.addEventListener('click',function(e){
if(e.index == 0) {
//Opens Photo Gallery
Titanium.Media.openPhotoGallery({
success:function(event)
{
var image = event.media;
Ti.App.globalImage = image;
previewImage.image =Ti.App.globalImage;
win.rightNavButton = uploadSmall;
upload.show();
},
cancel:function()
{
},
error:function(error)
{
},
allowImageEditing:true
});
}
if (e.index == 1) {
//Open Camera
Titanium.Media.showCamera({
success:function(event)
{
var image = event.media;
Ti.App.globalImage = image;
previewImage.image = Ti.App.globalImage;
win.rightNavButton = uploadSmall;
upload.show();
},
cancel:function()
{
},
error:function(error)
{
// create alert
var a = Titanium.UI.createAlertDialog({title:'Camera'});
// set message
if (error.code == Titanium.Media.NO_CAMERA)
{
a.setMessage('Device does not have video recording capabilities');
}
else
{
a.setMessage('Unexpected error: ' + error.code);
}
// show alert
a.show();
},
saveToPhotoGallery:true,
allowImageEditing:true
});
}
});
previewImage.addEventListener('click',function(e){
Ti.App.optionGroup.show();
});
//Create Stack
formView.add(whatText);
formView.add(whereText);
formView.add(atLabel);
formView.add(previewImage);
formView.add(actInd);
formView.add(upload);
win.add(formView);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment