package com.quikchek.util
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.net.Responder;
import flash.utils.clearInterval;
import flash.utils.setInterval;
import mx.core.Application;
//Tell flex we intend to broadcast two events
[Event("checkVersion")]
[Event("updateVersion")]
public class VersionManager extends EventDispatcher {
private var _pollTime:Number;
private var _applicationVersionNumber:Number;
private var _latestVersionNumber:Number;
private var pollInterval:Number;
/* The requestVersionCheck function is called periodically
via the SetInterval. It dispatches an event asking for
the latest version of the software available */
private function requestVersionCheck():void {
//Cancel our current interval.
//We will wait for a response before we ask again
clearInterval( pollInterval );
//We need to find out the current version of this software
dispatchEvent(new Event("checkVersion"));
}
/*getter/setter functions for the version number
of this application */
public function set applicationVersionNumber( value:Number ):void
{
//This is the version number of our local application
_applicationVersionNumber = value;
}
public function get applicationVersionNumber():Number
{
return _applicationVersionNumber;
}
/* getter/setter functions for the latest version number of
this application according to an outside authority */
public function set latestVersionNumber( value:Number ):void
{
//This is the latest available version number of the application
_latestVersionNumber = value;
//We check if we are up to date
if ( applicationVersionNumber < latestVersionNumber ) {
//We need to update. We inform whomever may be listening
dispatchEvent(new Event("updateVersion"));
} else {
//We have a current version for the moment.
//We will check again later
this.pollInterval = setInterval( requestVersionCheck, pollTime );
}
}
public function get latestVersionNumber():Number {
return _latestVersionNumber;
}
//getter/setter functions for our polling interval
public function set pollTime( value:Number ):void
{
if ( value < 1800000 ) {
/*For our purposes, we are going to constrain the poll
period so that it cannot be less 30 minutes
*/
//default to one hour, 3600 seconds * 1000
value = 1800000;
}
_pollTime = value;
}
public function get pollTime():Number {
return _pollTime;
}
/*We call this method when we are ready to begin
managing the version of our application */
public function manageVersion():void {
/*immediately request a version check on startup.
we want to find out if a new version exists before
the user gets to far into using the application */
requestVersionCheck();
}
private function onFault(obj:Object):void
{
}
}
}