Skip to content

Instantly share code, notes, and snippets.

@Josh-Miller
Forked from anonymous/landing.js
Created May 26, 2011 21:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Josh-Miller/994180 to your computer and use it in GitHub Desktop.
Save Josh-Miller/994180 to your computer and use it in GitHub Desktop.
updating data on first window load
/* UPDATING DATA */
// 1. compare dates in two update files (if OK internet connection)
var update = Titanium.App.Properties.getString('update');
if (Titanium.Network.online && update == 'YES') {
var newUpdatesFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'newUpdates.txt');
var savedFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory+Titanium.Filesystem.separator,'lastUpdated.txt');
var lastUpdatedDate = savedFile.read();
var newUpdatesDate = newUpdatesFile.read();
if(lastUpdatedDate.text == newUpdatesDate.text){
newUpdatesFile.deleteFile(true);
} else {
var updateAlert = Titanium.UI.createAlertDialog({
title:'New events have been added!',
message:'Would you like to update now?',
buttonNames: ['No','Yes']
});
// activity indicator
var actInd = Titanium.UI.createActivityIndicator({
height:45,
width:10,
top:'auto',
left:90,
style:Titanium.UI.iPhone.ActivityIndicatorStyle.PLAIN
});
actInd.show();
uploadActInd = Titanium.UI.createLabel({
font:{fontFamily:'Helvetica Neue', fontSize:15,fontWeight:'normal'},
color:'#FFFFFF',
text: 'Updating...',
textAlign: 'center'
});
uploadActIndBackground = Titanium.UI.createView({
width:320,
height:480,
top:0,
opacity: 0.8,
backgroundColor: '#000000'
});
uploadActIndBackground.add(actInd);
uploadActIndBackground.add(uploadActInd);
// update databases
function updateImagesDatabase(){
var xhr = Titanium.Network.createHTTPClient();
xhr.setTimeout(10000);
xhr.onload = function()
{
var currentImagesDB = Ti.Filesystem.getFile(Ti.Filesystem.applicationSupportDirectory + '/database/events_images.sql');
if (currentImagesDB.exists()) {
currentImagesDB.deleteFile(true);
}
if(this.readyState == 4 && this.status == "200" )
{
currentImagesDB.write(this.responseData);
}
xhr.onerror = function()
{
win.remove(uploadActIndBackground);
alert("Your internet connection is slow or unstable. Please try again later.");
};
};
xhr.open('GET','URL/dbImages.sql');
xhr.send();
}
function updateVenuesDatabase(){
var xhr = Titanium.Network.createHTTPClient();
xhr.setTimeout(10000);
xhr.onload = function()
{
var currentVenuesDB = Ti.Filesystem.getFile(Ti.Filesystem.applicationSupportDirectory + '/database/venues.sql');
if (currentVenuesDB.exists()) {
currentVenuesDB.deleteFile(true);
}
if(this.readyState == 4 && this.status == "200" )
{
currentVenuesDB.write(this.responseData);
}
xhr.onerror = function()
{
win.remove(uploadActIndBackground);
alert("Your internet connection is slow or unstable. Please try again later.");
};
};
xhr.open('GET','URL/dbVenues.sql');
xhr.send();
}
function updateVideosDatabase(){
var xhr = Titanium.Network.createHTTPClient();
xhr.setTimeout(10000);
xhr.onload = function()
{
var currentVideosDB = Ti.Filesystem.getFile(Ti.Filesystem.applicationSupportDirectory + '/database/events_videos.sql');
if (currentVideosDB.exists()) {
currentVideosDB.deleteFile(true);
}
if(this.readyState == 4 && this.status == "200" )
{
currentVideosDB.write(this.responseData);
}
xhr.onerror = function()
{
win.remove(uploadActIndBackground);
alert("Your internet connection is slow or unstable. Please try again later.");
};
};
xhr.open('GET','URL/dbVideos.sql');
xhr.send();
}
// if images directories already exist, delete them
function deleteImageDirectories(){
var eventsDirectory = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'events');
if(eventsDirectory.exists()){
eventsDirectory.deleteDirectory(true);
}
var reviewersDirectory = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'reviewers');
if(reviewersDirectory.exists()){
reviewersDirectory.deleteDirectory(true);
}
var venuesDirectory = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'venues');
if(venuesDirectory.exists()){
venuesDirectory.deleteDirectory(true);
}
}
// then download images folders
var zipfile = require("zipfile");
function downloadEventImages(){
var xhr = Titanium.Network.createHTTPClient();
xhr.setTimeout(10000);
xhr.onload = function()
{
var events = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'events.zip');
if(this.readyState == 4 && this.status == "200" )
{
events.write(this.responseData);
zipfile.extract(Ti.Filesystem.applicationDataDirectory+'/events.zip', Ti.Filesystem.applicationDataDirectory);
}
xhr.onerror = function()
{
win.remove(uploadActIndBackground);
alert("Your internet connection is slow or unstable. Please try again later.");
};
};
xhr.open('GET','URL/events.zip');
xhr.send();
}
function downloadReviewerImages(){
var xhr = Titanium.Network.createHTTPClient();
xhr.setTimeout(10000);
xhr.onload = function()
{
var reviewers = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'reviewers.zip');
if(this.readyState == 4 && this.status == "200" )
{
reviewers.write(this.responseData);
zipfile.extract(Ti.Filesystem.applicationDataDirectory+'/reviewers.zip', Ti.Filesystem.applicationDataDirectory);
}
xhr.onerror = function()
{
win.remove(uploadActIndBackground);
alert("Your internet connection is slow or unstable. Please try again later.");
};
};
xhr.open('GET','URL/reviewers.zip');
xhr.send();
}
function downloadVenueImages(){
var xhr = Titanium.Network.createHTTPClient();
xhr.setTimeout(10000);
xhr.onload = function()
{
var venues = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'venues.zip');
if(this.readyState == 4 && this.status == "200" )
{
venues.write(this.responseData);
zipfile.extract(Ti.Filesystem.applicationDataDirectory+'/venues.zip', Ti.Filesystem.applicationDataDirectory);
}
xhr.onerror = function()
{
win.remove(uploadActIndBackground);
alert("Your internet connection is slow or unstable. Please try again later.");
};
};
xhr.open('GET','URL/venues.zip');
xhr.send();
}
function updateTextFiles(){
var lastUpdatedFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory+Titanium.Filesystem.separator,'lastUpdated.txt');
lastUpdatedFile.deleteFile(true);
var newUpdatesFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory+Titanium.Filesystem.separator,'newUpdates.txt');
newUpdatesFile.rename('lastUpdated.txt');
}
function updateEventsDatabase(){
var xhr = Titanium.Network.createHTTPClient();
xhr.setTimeout(10000);
xhr.onload = function()
{
var currentEventsDB = Ti.Filesystem.getFile(Ti.Filesystem.applicationSupportDirectory + '/database/events.sql');
if (currentEventsDB.exists()) {
currentEventsDB.deleteFile(true);
}
currentEventsDB.write(this.responseData);
eventTable.setData([]);
// open events database
db = Titanium.Database.open('events');
dbRows = db.execute('SELECT * FROM events ORDER BY eventID DESC LIMIT 1');
// variables
newEventData = [];
var appDirectoryImg = "/events/" + dbRows.field(3);
newImageUrl = Titanium.Filesystem.applicationDataDirectory + appDirectoryImg;
newTitle = dbRows.field(1);
newEventID = dbRows.field(0);
// image
image.backgroundImage = newImageUrl;
detailLabel.text = newTitle;
var newEventRow = Ti.UI.createTableViewRow({
height:191,
backgroundImage: '../images/styling/landingEventBg.png',
selectedBackgroundImage: '../images/styling/landingEventBgActive.png',
id: newEventID,
detailTitle: newTitle
});
newEventRow.add(image);
newEventRow.add(headingLabel);
newEventRow.add(detailLabel);
newEventRow.add(rightArrow);
newEventData.push(newEventRow);
dbRows.close();
db.close();
if(this.readyState == 4 && this.status == "200" )
{
eventTable.setData(newEventData);
win.remove(uploadActIndBackground);
}
xhr.onerror = function()
{
win.remove(uploadActIndBackground);
alert("Your internet connection is slow or unstable. Please try again later.");
};
};
xhr.open('GET','URL/dbEvents.sql');
xhr.send();
}
// 'yes' clicked
updateAlert.addEventListener('click', function(e) {
if (e.index == 1) {
win.add(uploadActIndBackground);
updateImagesDatabase();
updateVenuesDatabase();
updateVideosDatabase();
deleteImageDirectories();
downloadEventImages();
downloadReviewerImages();
downloadVenueImages();
updateTextFiles();
setTimeout(function()
{
updateEventsDatabase();
},15000);
}
});
updateAlert.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment