Skip to content

Instantly share code, notes, and snippets.

@pietromalerba
Forked from alanleard/gist:1278595
Created March 27, 2013 08:00
Show Gist options
  • Save pietromalerba/5252527 to your computer and use it in GitHub Desktop.
Save pietromalerba/5252527 to your computer and use it in GitHub Desktop.
var win = Ti.UI.currentWindow;
Titanium.PageFlip = Ti.PageFlip = require('ti.pageflip');
var pdf = 'http://assets.appcelerator.com.s3.amazonaws.com/docs/Appcelerator-IDC-Q1-2011-Mobile-Developer-Report.pdf';
var fileName = pdf.split('/').pop();
var pdfFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, fileName);
function downloadPDF() {
var progressBar = Ti.UI.createProgressBar({ max: 1, min: 0, value: 0, visible: true });
win.add(progressBar);
var xhr = Ti.Network.createHTTPClient({
ondatastream: function(e) {
progressBar.value = e.progress;
},
onreadystatechange: function() {
if (xhr.readyState == 4) {
pdfFile.write(this.responseData);
win.remove(progressBar);
showPDF();
}
}
});
xhr.open('GET', pdf);
xhr.send();
}
function showPDF() {
/**
* "createView" will return a pageflip view. It can be sized and positioned like any other Titanium view. Here we
* call it with a pdf and a transition; it will handle paging the PDF for us.
*/
var scrollView = Ti.UI.createScrollView({
zoomScale:1.0,
minZoomScale:1.0,
maxZoomScale:4.0
});
var pageflip = Ti.PageFlip.createView({
transition: Ti.PageFlip.TRANSITION_FLIP, /* All Options: TRANSITION_FLIP [default], TRANSITION_SLIDE, TRANSITION_FADE */
pdf: pdfFile.nativePath,
tapToAdvanceWidth: '15%'
});
scrollView.add(pageflip);
win.add(scrollView);
scrollView.addEventListener('doubletap', function(){
scrollView.zoomScale = 1.0;
})
function updateWindowTitle(evt) {
win.title = 'PDF, 1 < ' + (evt.currentPage+1) + ' > ' + evt.pageCount;
}
updateWindowTitle(pageflip);
pageflip.addEventListener('change', function(evt) {
scrollView.zoomScale = 1.0;
updateWindowTitle(evt);
});
}
if (pdfFile.exists()) {
showPDF();
}
else {
downloadPDF();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment