Last active
May 18, 2016 11:12
-
-
Save roe3p/c8c9ab2739f55d70e8228ccd4bf5e528 to your computer and use it in GitHub Desktop.
A5 Version-checking and self-updating functions for a PhoneGap app
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
//''------------------------- Update Functions | |
//Display current version number | |
function displayVersion() { | |
{dialog.object}.setValue('AppVersion', {dialog.object}.appVars.myVersion); | |
{dialog.object}.setValue('AppVersion1', {dialog.object}.appVars.myVersion); | |
//call this function recursively until version number has been set | |
if ($('version_help').innerHTML != {dialog.object}.appVars.myVersion) { | |
setTimeout(function() { displayVersion(); } , 500); | |
} | |
} | |
//Callback to get latest version number from server | |
function checkVersions (silent) { | |
silent = (typeof silent === 'undefined') ? true : silent; | |
//If not silent, show wait message | |
if(!silent) { | |
//use css3 animations | |
var wo = {}; | |
wo.useCSSAnimation = true; | |
wo.cssAnimationSettings = {}; | |
wo.cssAnimationSettings.size = 32; | |
{dialog.object}.showWaitMessage('page',wo); | |
} | |
//Make callback to server to check latest version | |
{dialog.Object}.ajaxCallback('','','GetLatestVersion','','silent=' + silent,{deviceOfflineFunction: function() { | |
if (!silent) { | |
var msg = getMessage('VERS_NOCONNECTION'); | |
message(msg) | |
} | |
}}); | |
//If silent, hide any wait messages that may have been initiated | |
if(silent == true) { | |
setTimeout(function(){ {dialog.object}.hideWaitMessage();}, 1000); | |
} | |
} | |
//callback response function | |
function checkCurrentVersion (currVersion, updateURL, silent) { | |
silent = (typeof silent === 'undefined') ? true : silent; | |
var myVersion = {dialog.object}.appVars.myVersion; | |
var result = compareVersionNumbers(currVersion,myVersion); | |
{dialog.object}.hideWaitMessage(); | |
if(result==1) { | |
//needs updating - confirm with user in case of unsynced data | |
//alert('update required'); | |
var msg = getMessage('VERS_NEWVERSION').replace('{version}',currVersion); | |
var title = getMessage('VERS_NEWVERSION_TTL'); | |
A5.msgBox.show(title,'<div style="padding: 10px;">' + msg + '</div>','oc',function(button) { | |
if(button == 'ok') { | |
var wnd = window.open(updateURL,'_system'); | |
setTimeout(function() { wnd.close(); }, 5000); | |
return false; | |
} | |
} | |
); | |
} | |
else | |
{ | |
if (!silent) { | |
var msg = getMessage('VERS_CURRENT').replace('{version}',myVersion); | |
var title = getMessage('VERS_CURRENT_TTL'); | |
A5.msgBox.show(title,'<div style="padding: 10px;">' + msg + '</div>','o',function(button) {}); | |
} | |
//alert('You are using the current version'); | |
} | |
} | |
function compareVersionNumbers(a, b) { | |
// Compares two decimal-separated version numbers | |
// Return 1 if a > b | |
// Return -1 if a < b | |
// Return 0 if a == b | |
if (a === b) { | |
return 0; | |
} | |
var a_components = a.split("."); | |
var b_components = b.split("."); | |
var len = Math.min(a_components.length, b_components.length); | |
// loop while the components are equal | |
for (var i = 0; i < len; i++) { | |
// A bigger than B | |
if (parseInt(a_components[i]) > parseInt(b_components[i])) { | |
return 1; | |
} | |
// B bigger than A | |
if (parseInt(a_components[i]) < parseInt(b_components[i])) { | |
return -1; | |
} | |
} | |
// If one's a prefix of the other, the longer one is greater. | |
if (a_components.length > b_components.length) { | |
return 1; | |
} | |
if (a_components.length < b_components.length) { | |
return -1; | |
} | |
// Otherwise they are the same. | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment