Saturday 30 January 2010

Android Development – emulator problems

I just had my first go at android development. SO far I have managed to get Hello World running on my phone in debug mode but unfortunately I can’t get the emulator running which is going to make development harder.

I’ve posted a question on stack overflow so hopefully some helpful person will be able to help me out.

Thursday 21 January 2010

AsyncTokenWrapper

I have to confess that I lied ever so slightly in that last post. I said that the service code was exactly the same as it was without using the Command metadatatag.

This isn’t strictly true – I had to write a wrapper for AsyncToken to handle the xml translation. I was hoping that I would be able to use an interceptor to intercept the result event, translate the xml and then re-dispatch the event. Jens said that this wasn’t possible though so I needed a neat and tidy way to translate the results of the async call before it made it back to the model.

I did this by wrapping the async token that I get from whatever async operation I am performing. I can then pass this token to parsley so my code can do what it likes to the result before parsley routes it to it’s destination.

AsyncTokenWrapper does just that, it extends AsyncToken and wraps another AsyncToken. When it gets a result or a fault from the original token it applies an optional function to the result or fault before passing this new ResultEvent or FaultEvent onto it’s responders.

Code Below:

package com.pricklythistle.common.service
{
import mx.core.mx_internal;
import mx.messaging.messages.IMessage;
import mx.rpc.AsyncToken;
import mx.rpc.Fault;
import mx.rpc.IResponder;
import mx.rpc.Responder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

use namespace mx_internal

public class AsyncTokenWrapper extends AsyncToken
{

//---------------------------------
//
// Constructor
//
//---------------------------------

public function AsyncTokenWrapper(
token : AsyncToken,
resultProcessingFunction : Function = null,
faultProcessingFunction : Function = null )
{
super();

_token = token;

addResponders();

this.resultProcessingFunction = resultProcessingFunction;
this.faultProcessingFunction = faultProcessingFunction;
}

//---------------------------------
//
// Private Variables
//
//---------------------------------
private var _token : AsyncToken;

//---------------------------------
//
// Properties
//
//---------------------------------

public var resultProcessingFunction : Function;
public var faultProcessingFunction : Function;

//---------------------------------
//
// Public Methods
//
//---------------------------------
private function onResult( data : Object ) : void
{
var originalResultEvent : ResultEvent = ResultEvent( data );
var result : Object = originalResultEvent.result;
if( resultProcessingFunction != null )
{
result = resultProcessingFunction( result );
}

var newResultEvent : ResultEvent = new ResultEvent(
ResultEvent.RESULT,
originalResultEvent.bubbles,
originalResultEvent.cancelable,
result,
this,
originalResultEvent.message );

applyResult( newResultEvent );

}

private function onFault( info : Object ) : void
{
var originalFaultEvent : FaultEvent = FaultEvent( info );
var fault : Fault = originalFaultEvent.fault;
if( faultProcessingFunction != null )
{
fault = faultProcessingFunction( fault ) as Fault;
}

var newFaultEvent : FaultEvent = new FaultEvent(
FaultEvent.FAULT,
originalFaultEvent.bubbles,
originalFaultEvent.cancelable,
fault,
this,
originalFaultEvent.message
);

applyFault( newFaultEvent );
}

//---------------------------------
//
// Private Functions
//
//---------------------------------
private function addResponders() : void
{
_token.addResponder(
new Responder( onResult, onFault ) );
}
}
}

Sorry for the way the code gets mashed up there but you can click on the little button top right to get it in a new window.

Asynchronous Commands in Parsley 2.2

I’ve been working with the latest version of parsley at work over the last few days and it’s got a really nice new feature borrowed from Cairngorm 3.0. It makes it much easier to deal with asynchronous operations. Previously if I was going to load something I might need all these files:

  • model that wants the data
  • loadData event – possibly with payload
  • Action to receive load event
  • service
  • ResultEvent
  • FaultEvent

Now we can get rid of a lot of these classes and end up with:

  • model that wants the data
  • loadDataEvent
  • service

You don’t actually need a custom event for the load either as you can just use a normal event and use string based matching but I prefer to match on class rather than string.

Let me give an example. In my Picasa API demo I had the following code in my LoadPicasaAlbumList action:

[MessageHandler]
public function loadAlbumList( event : ListPicasaAlbumsEvent ) : void
{
var token : AsyncToken = service.loadUserEntries( event.userID, PicasaService.KIND_ALBUM );
token.addResponder( this );
}
public function result( data : Object ) : void
{
var resultEvent : ResultEvent = ResultEvent( data );

var collection : ArrayCollection = PicasaTranslator.desearialiseXML( XML( resultEvent.result ) );

dispatchEvent( new ResultEvent( LIST_ALBUM_RESULT, false, true, collection, resultEvent.token, resultEvent.message ) );
}

public function fault( info : Object ) : void
{
var faultEvent : FaultEvent = FaultEvent( info );

dispatchEvent( new FaultEvent( LIST_ALBUM_FAULT, false, true, faultEvent.fault, faultEvent.token, faultEvent.message ) );
}

Along with a load of meta data tags at the top of the class. This class is no longer required – which is good as it’s a load of boring boilerplate code that doesn’t really do very much and is tedious to write.

Now we have the following:

Model:

protected function loadFeed() : void
{
albums = null;
dispatchEvent( new ListPicasaAlbumsEvent( userID.text ) );
}

Service:

[Command( type="com.pricklythistle.picasa.event.ListPicasaAlbumsEvent", messageProperties="userID,kind" )]
public function loadUserEntries( userID : String, kind : String = null ) : AsyncToken
{
if( !kind )kind = KIND_ALBUM;

if( ArrayUtil.getItemIndex( kind, KIND_ARRAY ) < 0 )
{
throw new Error( "kind must be found in KIND_ARRAY" );
}

url = StringUtil.substitute( BASE_URL, userID );

return new AsyncTokenWrapper( send( { kind : kind } ), PicasaTranslator.desearialiseXML );
}

Which is exactly the same code I had in the service originally except for the Command metadata tag. It instructs parsley to fire that function when an event of the correct type is received and tells parsley what properties to pass into the function.

Model:

[CommandResult]
public function onAlbums( albums : ArrayCollection, event : ListPicasaAlbumsEvent ) : void
{
this.albums = albums;
}

[CommandError]
public function onAlbumsFault( fault : FaultEvent, event : ListPicasaAlbumsEvent ) : void
{
trace( "fault" );
}
}

Parsley calls these functions when the command completes or fails and passes either a ResultEvent or your data in according to how you type the arguments.

And the really clever bit:

[Bindable]
[CommandStatus( type="com.pricklythistle.picasa.event.ListPicasaAlbumsEvent" )]
public var loadingAlbum : Boolean;

This populates the property with a Boolean value of true when the data is loading so that you can disable buttons / show a loader or do whatever you want.

For more details about the Command tags take a look here.

Sunday 17 January 2010

Picasa API Implementation v 0.1

Today I finally managed to get some time to carry on working on the Picasa API that I started in October or whenever it was (ok, I had to get up at about 5:00 but I couldn’t sleep!).

You can have a look at progress so far here. At the moment it only lists albums and allows you to click on them to go through the Picasa page. Put your own username in at the top and you’ll get your albums listed.

The next stage is to load the images for each album and display them.

I am pretty pleased with the progress I made today. Most of it was spent writing the translators for the XML which I did with some stub data and TDD which worked pretty well. In the past writing tests for xml translation didn’t work especially well and they always broke – but that was because the service layer changed. I am hoping that the Google APIs will be a bit more stable.

The UI code here is pretty ugly with no presentation models or anything like that, just enough to get what you see displayed. At the moment the effort is going into the API code.

I am hoping to eventually add support to this for other photo hosting services such as flickr. The model that I am using here is closely based on the xml format that comes back from Picasa. When I move to include other services there might have to be a few tweaks to make it work with more than one service.

Sunday 3 January 2010

Implementing Lazy Loading of XML with ArrayCollection

I am still gradually working through my implementation of the Picasa public API to load images into Flex. One of the first apps I want to build is a simple signature app for forums that will loop through images in a Picasa album and display them one by one.

A Picasa album can contain 1000 images and the xml is pretty verbose so this would be a very large download. This would download each time the signature appeared on a page and every time the page was refreshed so downloading the whole lot is really bad idea.

I want to mimic the fill method of DataService. I would ask for an ArrayCollection to be filled but only the first 5 or however many records are specified would initially be loaded. The length of the collection would be reported as the number of items in the album and not the number of items downloaded. Additional items would be loaded as they are required.

Looking at ArrayCollection the getItemAt method has a prefetch argument specifying the number of items to fetch if the item is not local. There is also the ItemPendingError that is despatched if the item that is requested is not local but there isn’t much documentation about this and it’s not clear exactly how this is handled.

I had hoped that List and DataGroup for example would listen for ItemPendingError and add responders but they don’t seem to so it might not be as useful to implement the behaviour in this way.

If anyone has any clues as to how all this holds together I’d be very interested to hear them. I am particularly interested in how the DataService class interacts with ListCollectionView. Somehow DataService has to get ListCollectionView to report the length of the remote list and not the local list.
Unfortunately the source for DataService doesn’t seem to be available.

I could possibly implement this with some specific implementation of IList that is passed to a ListCollectionView but I would rather do it a similar way to the framework.

Any help much appreciated.