Posted By

jonniespratley on 10/27/08


Tagged

textmate AIR as3


Versions (?)

Saving Preferences in Air


 / Published in: Other
 

  1. /** *******************************************************************
  2. * MySnippets
  3. * Free for use
  4. *
  5. * @author Jonnie Spratley
  6. * @contact [email protected]
  7. ******************************************************************* */
  8.  
  9. import com.adobe.air.preferences.PreferenceChangeEvent;
  10. import com.adobe.air.preferences.Preference;
  11.  
  12. /* Create a new prefernce variable and give it a name */
  13. private var prefs:Preference = new Preference( "settings.obj" );
  14.  
  15. /* Create a new bytearry to store the data */
  16. private var byteArray:ByteArray = new ByteArray();
  17.  
  18. /* Create a new fileStream variable to open up the local file system */
  19. private var fileStream:FileStream;
  20.  
  21. /* Register some event listeners to see when the prefrences change */
  22. private function init():void
  23. {
  24. prefs.addEventListener( PreferenceChangeEvent.PREFERENCE_CHANGED_EVENT, onPrefChange );
  25. checkSettings();
  26. }
  27.  
  28. /* When they change, set the action to edit, and update the files */
  29. private function onPrefChange( event:PreferenceChangeEvent ):void
  30. {
  31. if ( event.action == PreferenceChangeEvent.ADD_EDIT_ACTION )
  32. {
  33. //The new value
  34. trace( event.newValue );
  35.  
  36. } else if ( event.action == PreferenceChangeEvent.DELETE_ACTION )
  37. {
  38. //The old value
  39. trace( event.oldValue );
  40. }
  41.  
  42. }
  43.  
  44. /* Save the settings, it the checkbox is selected then lets save it */
  45. private function saveSettings():void
  46. {
  47. if ( cb_remember.selected )
  48. {
  49. /* preferences variable, give it a name, set the value, encyrpted or not */
  50. prefs.setValue( "username", txt_username.text, false );
  51. prefs.setValue( "password", txt_password.text, true );
  52. prefs.setValue( "remember", cb_remember.selected, false );
  53.  
  54. prefs.save();
  55. } else {
  56. //Do nothing
  57. }
  58. //Saved or Edited preferences
  59. trace( prefs );
  60. }
  61.  
  62. /*
  63. When the application loads, check the settings to see if anything has changed, if it has, set the value
  64. of the new values to the values of the inputs
  65. */
  66. private function checkSettings():void
  67. {
  68. prefs.load();
  69. txt_username.text = prefs.getValue( "username" );
  70. txt_password.text = prefs.getValue( "password" );
  71. cb_remember.selected = prefs.getValue( "remember" );
  72. }
  73.  
  74. //Clear settings, and start from scratch
  75. private function clearSettings():void
  76. {
  77. txt_username.text = "";
  78. txt_password.text = "";
  79. cb_remember.selected = false;
  80. }

Report this snippet  

You need to login to post a comment.