31
Jul
08

CPreferencesManager : A preferences Manager for Adobe Flex/AIR

Here’s a small helper class CPreferencesManager which manages application preferences by means of a key/value set, which I added as a comment to coldfusionjedi’s nice blog:

Sample usecase: Make window positions persist after relaunch of an application:

1. Create an instance:

<tracker:CPreferencesManager    id=”thePrefs” />

2. Attach windowMove events to your Window:

windowMove=”thePrefs.setPreference(’main.window.x’,
   this.nativeWindow.x.toString() );
   thePrefs.setPreference(’main.window.y’, this.nativeWindow.y.toString() );”

3. After creationComplete, move the window to its position of the one but last launch:

var posX:int = int( thePrefs.getPreference(’main.window.x’ ) );
var posY:int = int( thePrefs.getPreference(’main.window.y’ ) );
move( posX, posY );      

The implementation:

package com.your.company
{
   import flash.filesystem.File;
   import flash.filesystem.FileMode;
   import flash.filesystem.FileStream;
   import flash.utils.ByteArray;
   import flash.utils.Dictionary;
   import flash.utils.IDataInput;
   import flash.utils.IDataOutput;
   import flash.utils.IExternalizable;
   
   import mx.controls.Alert;
   
   public class CPreferencesManager implements IExternalizable
   {
      
      private var _prefValues:Dictionary;
      private   var   _prefsStream:FileStream;
      
      public function CPreferencesManager()
      {

         _prefValues = new Dictionary();
         loadPreferences();
         
      }
      
      public function loadPreferences():void {
   
         var fp: File = File.applicationStorageDirectory;
         
         fp = fp.resolvePath( ‘prefs.xml’ );
         
         if ( fp.exists ) {
            
            _prefsStream = new FileStream();
            _prefsStream.open( fp, FileMode.READ);
            
            readExternal( _prefsStream );
            _prefsStream.close();
            
         } else {
            _prefValues[ 'appName' ] = ‘tracker’; // test-code
            savePreferences();
         }

      }
      
      public function savePreferences():void {

         var fp: File = File.applicationStorageDirectory;
         
         fp = fp.resolvePath( ‘prefs.xml’ );
         
         _prefsStream = new FileStream();
         _prefsStream.open( fp, FileMode.WRITE );
         
         writeExternal( _prefsStream );
         _prefsStream.close();
         
      }
      
      public function setPreference( name:String, value:Object ):void {
         
         _prefValues[ name ] = value;
         savePreferences();
         
      }

      public function getPreference( name:String ):Object {
         
         return _prefValues[ name ];
         
      }
      
      
      
      public function readExternal(input:IDataInput):void
    {
       
    var bytes:ByteArray = new ByteArray();
       input.readBytes( bytes, 0, input.bytesAvailable );

    var q:Object = bytes.readObject();
    
    _prefValues = new Dictionary();
    
    for( var p:Object in q ) {
       _prefValues[ p ] = q[p];
    }
    
    // Alert.show( _prefValues.toString() );
    
    }
   
      public function writeExternal(output:IDataOutput):void
    {

    var bytes:ByteArray = new ByteArray();
    bytes.writeObject( _prefValues );
    
    output.writeBytes( bytes );
    }

   }
}

 

 

 

 


0 Responses to “CPreferencesManager : A preferences Manager for Adobe Flex/AIR”


  1. No Comments

Leave a Reply