Created
January 11, 2012 22:34
-
-
Save srahim/1597196 to your computer and use it in GitHub Desktop.
updated testing file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Titanium.UI.setBackgroundColor('#000'); | |
var tabGroup = Titanium.UI.createTabGroup(); | |
var win = Titanium.UI.createWindow({ | |
title:'Tab 1', | |
backgroundColor:'#fff' | |
}); | |
var label1 = Titanium.UI.createLabel({ | |
color:'#999', | |
text:'Wait about 20 seconds for the image to appear', | |
font:{fontSize:20,fontFamily:'Helvetica Neue'}, | |
textAlign:'center', | |
width:'auto' | |
}); | |
win.add(label1); | |
var tab1 = Titanium.UI.createTab({ | |
icon:'KS_nav_views.png', | |
title:'Tab 1', | |
window:win | |
}); | |
tabGroup.addTab(tab1); | |
/////////////////////////////////////////////////////////////////// | |
// START IMAGE GALLERY CODE | |
/////////////////////////////////////////////////////////////////// | |
//////////////////// | |
// FUNCTIONS | |
//////////////////// | |
function getRemoteFile(filename, url, fn_end, fn_progress, override, eventCallback ) { | |
var file_obj = {file:filename, url:url, path: null}; | |
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename); | |
if ( file.exists() ) { | |
file_obj.path = Titanium.Filesystem.applicationDataDirectory+Titanium.Filesystem.separator; | |
if(override) { | |
file.deleteFile(); | |
} else { | |
return; | |
} | |
} | |
if ( Titanium.Network.online ) { | |
var c = Titanium.Network.createHTTPClient(); | |
c.setTimeout(10000); | |
c.onload = function() | |
{ | |
if (c.status == 200 ) { | |
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename); | |
f.write(this.responseData); | |
if(eventCallback != undefined) { | |
setTimeout(function(){Ti.App.fireEvent(eventCallback);},20000) ; | |
} | |
file_obj.path = Titanium.Filesystem.applicationDataDirectory+Titanium.Filesystem.separator; | |
} else { | |
file_obj.error = 'file not found'; // to set some errors codes | |
} | |
//fn_end(file_obj); | |
}; | |
c.ondatastream = function(e) | |
{ | |
if ( fn_progress ) fn_progress(e.progress); | |
}; | |
c.error = function(e) | |
{ | |
file_obj.error = e.error; | |
fn_end(file_obj); | |
}; | |
c.open('GET',url); | |
c.send(); | |
} else { | |
file_obj.error = 'no internet'; | |
fn_end(file_obj); | |
} | |
}; | |
function deleteRemoteFile(filename) { | |
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename); | |
if ( file.exists() ) { | |
file.deleteFile(); | |
} | |
}; | |
//////////////////// | |
// GALLERY | |
//////////////////// | |
var rssFeed = "http://cph.napp.dk/index.php?option=com_phocagallery&view=napp&format=json"; | |
var dbDataArray = []; | |
var allImagesDownloaded = false; | |
// Create image directory | |
var imageDir = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'gallery'); | |
if(imageDir.exists()){ | |
//TESTING PURPOSE | |
imageDir.deleteDirectory() | |
imageDir.createDirectory(); | |
} else { | |
imageDir.createDirectory(); | |
} | |
function showContent() { | |
Ti.API.info("showContent()" ); | |
//Ti.App.removeEventListener('galeryCallback', function(){ }); | |
var scrollView = Titanium.UI.createScrollView({ | |
contentWidth:'auto', | |
contentHeight:'auto', | |
top:0, | |
left:0, | |
zIndex:10, | |
showVerticalScrollIndicator:true, | |
showHorizontalScrollIndicator:true | |
}); | |
win.add(scrollView); | |
// create table view data object | |
var data = []; | |
var imageView = []; | |
var largeImageArray = []; | |
//image gallery | |
var photoWidth = 75; | |
var photoHeight = 75; | |
var iconSize = 100; | |
var photoPadding = 4; | |
var labelPadding = 5; | |
var labelFontSize = 12; | |
var phoneScreenWidth = Titanium.Platform.displayCaps.platformWidth; | |
// images related to phone width | |
var photoInRow = Math.floor(phoneScreenWidth / (photoWidth+photoPadding)); | |
var x = 0; | |
var count = 0; | |
var Ypos = 0 + photoPadding; | |
var LeftPos = photoPadding; | |
// show gallery | |
var totalImages = dbDataArray.length; | |
var totalRows = totalImages/photoInRow; | |
totalRows = Math.ceil(totalRows); | |
Ti.API.info("showContent totalImages: " + totalImages); | |
for (var i = 0; i < totalRows; i++) { | |
for (var j = 0; j < photoInRow; j++) { | |
if (count < totalImages) { | |
var blob = Titanium.Filesystem.getFile(imageDir.nativePath,dbDataArray[count].imageMedium); | |
//Ti.API.info("Contents of the Tifilesystem"+blob.nativePath); | |
if(blob.exists){ | |
var tempView = Titanium.UI.createImageView({ | |
image: blob.nativePath, | |
width: photoWidth, | |
height: photoHeight, | |
preventDefaultImage: false, //disable default image while loading | |
left: LeftPos, | |
borderColor: '#fff', | |
borderWidth: 1, | |
canScale: false, //android zoom buttons | |
top: Ypos, | |
zIndex: 3, | |
id: count | |
}); | |
LeftPos = LeftPos + photoWidth + photoPadding; | |
var largeblob = Titanium.Filesystem.getFile(imageDir.nativePath,dbDataArray[count].imageLarge); | |
var tempLargeView = Titanium.UI.createImageView({ | |
image: largeblob.nativePath, | |
imagefile: dbDataArray[count].imageLarge, | |
left: 0, | |
top: 0, | |
scalesPageToFit: true, | |
preventDefaultImage: true, //disable default image while loading | |
canScale: true, //android zoom buttons | |
id: count | |
}); | |
imageView.push(tempView); | |
//iterate | |
} | |
else | |
{ | |
alert(blob.nativePath+"doesnot exist"); | |
} | |
count++; | |
//largeImageArray.push(tempLargeView); | |
} | |
} | |
Ypos = Ypos + photoHeight + photoPadding; | |
LeftPos = 0 + photoPadding; | |
} // end for loop | |
for (var p = 0; p < imageView.length; p++) { | |
scrollView.add(imageView[p]); | |
} | |
} | |
function xtractFile(data) { | |
var m = data.match(/(.*)\/([^\/\\]+)(\.\w+)$/); | |
return {path: m[1], file: m[2], extension: m[3]} | |
} | |
//////////////////// | |
// DATA HANDLER | |
//////////////////// | |
//update database with remote data | |
function updateDatabase() { | |
var xhr = Titanium.Network.createHTTPClient(); | |
xhr.onload = function() { | |
try { | |
var items = eval('('+this.responseText+')'); | |
var remoteImagesStarted = 0; | |
var remoteImagesTotal = items.length * 3; //3 images per item | |
//TESTING WITH 20 ITEMS = 60 IMAGES | |
remoteImagesTotal = 60; | |
for (var c=1;c<21;c++) | |
// TEST WITH ALL IMAGES | |
//for (var c=1;c<items.length;c++) | |
{ | |
var id = items[c].id; | |
var imageLarge = items[c].imageLarge; | |
var imageSmall = items[c].imageSmall; | |
var imageMedium = items[c].imageMedium; | |
var pubdate = items[c].pubDate; | |
var category = items[c].category; | |
var lastupdated = items[c].update; | |
//image related content | |
var imageArr = [imageLarge, imageSmall, imageMedium]; | |
var imageNameArr = ['imageLarge_', 'thumb_', 'thumb_']; | |
var imageNativePath = []; | |
for (var i=0; i < imageArr.length; i++) | |
{ | |
var filename = xtractFile(imageArr[i]); | |
var imageName = ''; | |
if(i == imageArr.length-1 ) | |
{ | |
// @2x iphone 4 | |
imageName = '' + imageNameArr[i] + id + '@2x' + filename.extension; | |
} | |
else | |
{ | |
imageName = '' + imageNameArr[i] + id + filename.extension; | |
} | |
remoteImagesStarted++; | |
//remote file save get_remote_file( filename, url, debug, progress, override, callback) | |
getRemoteFile('gallery/'+imageName, imageArr[i], null, null, true, "galeryCallback" ); | |
imageNativePath.push(imageName); | |
}; | |
dbDataArray.push({ | |
id: id, | |
imageLarge: imageNativePath[0], | |
imageSmall: imageNativePath[1], | |
imageMedium: imageNativePath[2], | |
pubdate: pubdate, | |
category: category, | |
lastupdated: lastupdated | |
}); | |
} // feed loop end | |
//Remote image callback Event | |
Ti.App.addEventListener('galeryCallback',function(e) { | |
if (!allImagesDownloaded && (remoteImagesTotal == remoteImagesStarted) ) { | |
Ti.API.info('REMOTE DOWNLOAD DONE: remoteImagesDone: ' + remoteImagesTotal + ' remoteImagesStarted ' + remoteImagesStarted); | |
allImagesDownloaded = true; | |
showContent(); | |
} | |
}); | |
} | |
catch(e) { | |
//something goes wrong - showcontent | |
Ti.API.info("CATCH: " + e.message); | |
} | |
//showContent(); | |
}; | |
xhr.onerror = function(e) { | |
Ti.API.info("onerror: " + e.error); | |
}; | |
xhr.open("GET",rssFeed); | |
xhr.send(); | |
}; | |
// open tab group | |
tabGroup.open(); | |
//start the process | |
updateDatabase(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment