Snippets

Adobe Flex - Mini Cookbook update

Posted in Flex, News, Snippets, Tutorials on October 21st, 2008 by Jonnie – Be the first to comment

Hey I just posted another section of the mini cookbook, it is titled Web Service Recipes and solutions include.

I will be making more additions to this when I find some more time. Take a look.

Adobe Flex - My Code is Your Code

Posted in Air, Amfphp, Cairngorm, Flex, Snippets on October 16th, 2008 by Jonnie – 3 Comments

A bunch of code that I tend to re-use and re-use. The contents are listed in the table of contents below.

As changes occur this files will be updated. Keep in mind that this is raw uncut code. So some might be sloppy and some might be clean, some might not work, and some might save your life. ;)

Everything works. Enjoy!

Adobe Air

  1. Connecting to SQLite
  2. Network Connection Manager
  3. SQLite Database Manager
  4. Executing Querys on SQLite
  5. Importing XML into SQLite
  6. Saving Preferences to Local Store
  7. Taking A Screenshot
  8. Update Manager
  9. 30boxes.com API Interface
  10. 30boxes.com API Service
  11. Twitter Amfphp API Service
  12. Del.icio.us API Interface
  13. Del.icio.us API Service
  14. Amfphp Service
  15. Cairngorm Command
  16. Cairngorm Controller
  17. Cairngorm Delegate
  18. Cairngorm Event
  19. Cairngorm Model Locator
  20. Cairngorm Value Object


Adobe Flex

  1. 30boxes.com API Browser
  2. Twitter Amfphp API Browser
  3. Buzznet API Browser
  4. Flex Download Component
  5. Flex Upload Component
  6. Flex Email Component
  7. WordPress API Browser
  8. Service-Config.xml
  9. Del.icious Post Form
  10. Del.icious Post List
  11. Del.icious Post View
  12. Del.icious Tag Form
  13. Del.icious Tag List
  14. Del.icious Tag View
  15. Del.icious User Form
  16. Del.icious User View
  17. Del.icious User Info
  18. Del.icious View


PHP

  1. Amfphp Saving Image
  2. Amfphp Sending Emails
  3. Amfphp CRUD Service
  4. Amfphp DB Connection
  5. Flex File Download
  6. Flex File Upload




Adobe Air - Connection Manager

Posted in Air, Flex, Snippets on October 8th, 2008 by Jonnie – 3 Comments

Here is a very useful script that I re-use and re-use when building Air applications. Its a simple script I am sure you can make out what is going on here.

ConnectionManager.as

   1: package com.jonniespratley.air.managers
   2: {
   3:     import air.net.URLMonitor;
   4:     
   5:     import flash.events.StatusEvent;
   6:     import flash.net.URLRequest;
   7:     
   8:     import mx.controls.Alert;
   9:     
  10:     public class ConnectionManager
  11:     {
  12:         private var statusEvent:StatusEvent;
  13:         private var urlMonitor:URLMonitor;
  14:         private var showAlert:Boolean;
  15:         private var message:String;
  16:         
  17:         [Bindable] public var connectionURL:String;
  18:         [Bindable] public var isConnected:Boolean;    
  19:         
  20:         //3 Arguments, alert, connection URL, and the message to display
  21:         public function ConnectionManager( showAlert:Boolean = true, 
  22:                     connectionURL:String = "http://google.com", 
  23:                     message:String = "This Air Application must be connected to the internet, \nConnection Error" )
  24:         
  25:         {
  26:             this.showAlert = showAlert;
  27:             this.connectionURL = connectionURL;
  28:             this.message = message;
  29:             
  30:             startMonitor();
  31:         }
  32:         
  33:         //Send a request to google, and see if we get a response
  34:         public function startMonitor():void
  35:         {
  36:             var urlRequest:URLRequest = new URLRequest( connectionURL );
  37:                 urlRequest.method = "HEAD";
  38:                 
  39:                 urlMonitor = new URLMonitor( urlRequest );
  40:                 urlMonitor.addEventListener( StatusEvent.STATUS, statusChanged );
  41:                 urlMonitor.start();
  42:         }
  43:         
  44:         //Dispatch a change event, whenever the status of our connection changes
  45:         public function statusChanged( event:StatusEvent ):void
  46:         {
  47:             this.isConnected = urlMonitor.available;
  48:             
  49:             if ( !this.isConnected && this.showAlert )
  50:             {
  51:                 Alert.show( this.message, "Connection Failure" );
  52:             }
  53:             
  54:             statusEvent = new StatusEvent( StatusEvent.STATUS );
  55:             
  56:             dispatchEvent( statusEvent );            
  57:             
  58:         }
  59:  
  60:     }
  61: }

Adobe Flex - Using REST

Posted in Air, Flex, Snippets, Tutorials on October 3rd, 2008 by Jonnie – Be the first to comment

Have you ever wanted to create a really sophisticated application, well connecting to a external data source is just a must with all of the APIs that are available. The social bookmarking scene is one of the biggest movements of the web.

How would you like it to be able to connect to Del.icio.us API and grab all of your bookmarks or posts, well not just yours any one who has an account at Del.icio.us, that would be pretty cool wouldn’t it?

What were are going to do here is get some of the basic information mastered when it comes to connecting to a REST API such as Del.icio.us, and from that you imagination can be the limit to where you go from here.

We need to get this thing started because I am really anxious to see what kind of applications you developers come up with.

Lets Go!

Requirements

Once you have all of those requirements lets go ahead and open up Flex Builder, and create a new project.

 

Show Me The Bookmarks!

Ok I see that you are excited to get this thing up and running, but we are going to have to get some nessaccary items out of the way first for the sake of having some proper objects to hold our data. This enables us to know what is coming and it also gives us the ability extend our applicatoin without having to go back and fix some files.

Here is some things we need to do:

  1. Create a data model to hold all of our data coming from Del.icio.us.
  2. Create a DeliciousPost object to hold our post data.
  3. Create a DeliciousTag object to hold our tag data.
  4. Create a DeliciousUser object to hold our user data and information.
  5. We could create a service class to make the calls for us, but…. O what the hell, lets create a DeliciousService class to hold all of our calls and result handlers.
  6. Then we need to create our views.
  7. Wire up the views.
  8. Test some calls.
  9. And Complete

So when it comes to creating our Delicious objects we are going to have to know some of the values coming from Del.icio.us when we make our calls, so lets go over them.

Here is a sample of the called method, and the result of that method.

Method: posts/recent?

Result:

   1:
"jonniedollas" update="2008-09-13T06:03:26Z" tag="" total="264">
   2:

   3:         href="http://airapps.pbwiki.com/" 
   4:         hash="736123a4e2b3250e6919fa7538a810d1" 
   5:         description="airapps wiki" 
   6:         tag="flex ria web2.0" 
   7:         time="2008-09-13T05:59:12Z" 
   8:         extended=""
   9:         shared="no"/>
  10: 

 

Method: tags/get?

Result:

   1: "1.0" encoding="UTF-8"?>
   2: 
   3:     "1" tag="30boxes"/>
   4:     "1" tag="JavaScript"/>
   5:     "4" tag="Software"/>
   6:     "3" tag="Web_2.0"/>
   7:  

 

Method: posts/update?

Result:

   1: "1.0" encoding="UTF-8"?>
   2:     "2008-09-09T08:45:16Z" inboxnew="0"/>

 

Ok now that we know what is coming back we need to create our data objects. Create 3 new files DeliciousUser.as DeliciousTag.as and DeliciousPost.as make sure that they are classes.

Here is how I created mine.

I am not claiming that this way that I am showing you is the best nor most efficient way of doing things/this, so please if you know of better ways let me know. I love to get better and learn things that could make life easier.

DeliciousTag.as:

   1: package com.jonniespratley.advguidetoflex.model
   2: {
   3:     [Bindable]
   4:     public class DeliciousTag
   5:     {
   6:         public var tag_name:String;
   7:         
   8:         public var tag_count:String;    
   9:                 
  10:         public function DeliciousTag( tag:String = "", 
  11:                                       count:String = "" )
  12:         {
  13:             this.tag_name = tag;
  14:             this.tag_count = count;
  15:         }
  16:     }
  17: }

 

DeliciousPost.as

   1: package com.jonniespratley.advguidetoflex.model
   2: {
   3:     import com.adobe.utils.DateUtil;    
   4:     
   5:     [Bindable]
   6:     public class DeliciousPost
   7:     {
   8:         public var post_url:String;        
   9:         public var post_hash:String;        
  10:         public var post_title:String;        
  11:         public var post_time:String;        
  12:         public var post_description:String;        
  13:         public var post_replace:String;            
  14:         public var post_shared:Boolean;                
  15:         public var post_tags:Array;
  16:         
  17:         public function  DeliciousPost( 
  18:                                         url:String, 
  19:                                         hash:String, 
  20:                                         title:String, 
  21:                                         tags:String = "", 
  22:                                         time:String = "", 
  23:                                         description:String = "", 
  24:                                         shared:Boolean = true )
  25:         {
  26:             this.post_url = url;
  27:             this.post_hash = hash;
  28:             this.post_title = title;
  29:             this.post_tags = String( tags ).split( " " );
  30:             //this.post_time = DateUtil.parseW3CDTF( time ).toDateString();
  31:             this.post_description = description;
  32:             this.post_shared = shared;            
  33:         }
  34:     }
  35: }

 

DeliciousUser.as

   1: package com.jonniespratley.advguidetoflex.model
   2: {
   3:     import com.adobe.utils.DateUtil;
   4:         
   5:  
   6:     [Bindable]
   7:     public class DeliciousUser
   8:     {
   9:         public var user_name:String;
  10:         public var user_pass:String;
  11:         public var user_inbox:Number;                
  12:         public var user_update:String;        
  13:         public var user_feed:String;        
  14:         
  15:         public function  DeliciousUser( username:String, 
  16:                                         password:String, 
  17:                                         updated:String = "", 
  18:                                         messages:Number = 0 )
  19:         {            
  20:             this.user_name = username;
  21:             this.user_pass = password;
  22:             this.user_update = updated;
  23:             this.user_inbox = messages;
  24:             this.user_feed = "http://del.icio.us/rss/" + username;
  25:         }
  26:     }
  27: }

 

Ok now that we have that covered, we need to create our model so we can take our results from the server and add that data to our model so the views can display the data. Create another file called ModelLocator.as this is a ActionScript class.

ModelLocator.as

   1: package com.jonniespratley.advguidetoflex.model
   2: {
   3:     import com.adobe.cairngorm.model.IModelLocator;
   4:     
   5:     import mx.collections.ArrayCollection;
   6:  
   7:     /**
   8:      * The Model Locator pattern is a singleton and was created purely 
   9:      * to be used with Flex/Air application development. In this case, 
  10:      * a singleton is a design pattern that allows for only one 
  11:       * instance of the Model Locator to be present within your a
  12:      * application's memory. Any data that you think is required to live 
  13:      * in the application's state should be stored inside the Model Locator. 
  14:      * The Model Locator creates a central area where all the states 
  15:      * can be held in your Flex/Air application. This allows the view 
  16:      * components to bind to the model or state of the application 
  17:      * and keep everything up to date.
  18:      *
  19:      */
  20:     
  21:     [Bindable]
  22:     public final class ModelLocator implements IModelLocator
  23:     {
  24:         /** Defines the Singleton instance of the Application ModelLocator */
  25:         private static var instance:ModelLocator;        
  26:  
  27:         public function ModelLocator()
  28:         {
  29:             if( instance != null ) throw new Error( "Error: Singletons can only be instantiated via getInstance() method!" );
  30:  
  31:             ModelLocator.instance = this;            
  32:         }
  33:  
  34:         /** Returns the Singleton instance of the Application ModelLocator */
  35:         public static function getInstance():ModelLocator
  36:         {
  37:             if( instance == null )    instance = new ModelLocator();
  38:             return instance;                
  39:         }
  40:     
  41:         // *********** Public Variables that our views are going to bind to ************** \\
  42:  
  43:         /** The data that is comming from Del.icio.us */        
  44:         public var dataCollection:ArrayCollection;
  45: