Skip to content

Instantly share code, notes, and snippets.

@bclinkinbeard
Created June 23, 2011 14:32
Show Gist options
  • Save bclinkinbeard/1042646 to your computer and use it in GitHub Desktop.
Save bclinkinbeard/1042646 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
applicationComplete="init(event)">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
import spark.components.Button;
import spark.events.IndexChangeEvent;
private var loader:URLLoader;
private var file:File;
private var fileName:String;
[Bindable]
private var alerts:ArrayCollection = new ArrayCollection();
protected function displayPDF( url:String ):void
{
fileName = url.substr( url.lastIndexOf( "/" ) + 1 );
file = File.applicationStorageDirectory.resolvePath( fileName );
if( file.exists )
{
trace( "loading", fileName, "from disk!" );
displayLocalPDF( new File( file.nativePath ).url );
return;
}
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener( Event.COMPLETE, onComplete );
loader.load( new URLRequest( url ) );
}
private function onComplete( event:Event ):void
{
trace( "loaded", fileName, "from the server" );
file = File.applicationStorageDirectory.resolvePath( fileName );
if( file.exists )
file.deleteFile();
var fs:FileStream = new FileStream();
fs.open( file, FileMode.WRITE );
fs.writeBytes( loader.data );
fs.close();
displayLocalPDF( new File( file.nativePath ).url );
}
protected function displayLocalPDF( url:String ):void
{
var sv:StageWebView = new StageWebView();
sv.stage = stage;
sv.viewPort = new Rectangle( 0, 300, stage.stageWidth, stage.stageHeight - 300 );
sv.loadURL( url );
}
protected function init(event:FlexEvent):void
{
var svc:URLLoader = new URLLoader();
svc.addEventListener( Event.COMPLETE, onXMLLoad );
svc.load( new URLRequest( "http://www.meridiancp.com/api/alerts/" ) );
}
private function onXMLLoad(event:Event):void
{
var xml:XML = new XML( event.target.data );
for each( var node:XML in xml.alert )
{
alerts.addItem( { label: node.title, url: node.url } );
}
}
protected function alertsList_changeHandler(event:IndexChangeEvent):void
{
displayPDF( alertsList.selectedItem.url );
}
]]>
</fx:Script>
<s:List id="alertsList" dataProvider="{ alerts }" width="100%" height="300" horizontalScrollPolicy="off" change="alertsList_changeHandler(event)" />
</s:Application>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment