By

Swipe Gestures using Flex 4.5 Hero/Burrito on Android

I’ve been playing a little with the pre-release of Flex 4.5 Hero and checking out what it’s mobile support is like.

I ended up mocking up a simple swiping example – swiping in both vertical and horizontal directions – and quickly found that all the info floating around on the net is not quite right for this release – seems they have changed around the classes a bit.

The one thing that I’ve noticed is swiping in Flex is somewhat more primative than in Android native – in android the swipe speed is more or less dictated by your fingers speed across the screen – you swipe slower, so does the app. You can pause half way and see half of each screen. In Flex 4.5, once it detects a swipe event, across it goes at the speed specified by the code.

I would be interested to see if the native style swipe is possible, as it is a much nicer implementation – probably causes a lot more overhead, so it may be too much of a performance hit.

These sort of little differences, in my opinion, will be what will brand Flex based apps as an inferior product, so it would be good to see some movement there. That being said, this is a pre-release so I’ll give them the benefit of the doubt.

All in all it seems pretty good… close enough to native feel to justify looking into using flex on mobile further.

swipe code:-

Create a new mobile project using the prerelease of Flash Builder Burrito (you’ll need to sign up for the prerelease program for this) and using Flex 4.5 Hero runtime (should be bundled with Burrito)

There will be 5 files in total, Main.mxml, PrevView.mxml, NextView.mxml, TopView.mxml and BottomView.mxml. I will provide the code for Main.mxml and NextView.mxml. The other three files are essentially the same as NextView, just will be using a different event.offsetX/offsetY value and slideViewTransition.direction value, so I’m sure you can work it out.

Main.mxml:-

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
		xmlns:s="library://ns.adobe.com/flex/spark" 
		title="Swiping Demo" gestureSwipe="handleSwipe(event)">
	
	<fx:Script>
		<![CDATA[
			import spark.transitions.SlideViewTransition;
			import spark.transitions.SlideViewTransitionMode;
			import spark.transitions.ViewTransitionDirection;
			
			private function handleSwipe(event:TransformGestureEvent):void
			{
				// Swipe was to the right
				if (event.offsetX == 1 ) {
					// push the PreviousView without any data using default
					// ViewTransition
					var slideViewTransition:SlideViewTransition = new SlideViewTransition();
					slideViewTransition.duration = 300;
					slideViewTransition.direction = ViewTransitionDirection.RIGHT
					navigator.pushView( PrevView, null,null, slideViewTransition);
				}
					// Swipe was to the left
				else if (event.offsetX == -1 ) {
					// push the NextView withour any data using 
					var slideViewTransition2:SlideViewTransition = new SlideViewTransition();
					slideViewTransition2.duration = 300;
					slideViewTransition2.direction = ViewTransitionDirection.LEFT; 
					navigator.pushView( NextView, null,null, slideViewTransition2);
				}
				
				if (event.offsetY == 1 ) {
					var topTrans:SlideViewTransition = new SlideViewTransition();
					topTrans.duration = 300;
					topTrans.direction = ViewTransitionDirection.DOWN
					navigator.pushView( TopView, null, null, topTrans);
				}
				else if (event.offsetY == -1 ) 
				{
					var bottomTrans:SlideViewTransition = new SlideViewTransition();
					bottomTrans.duration = 300;
					bottomTrans.direction = ViewTransitionDirection.UP
					navigator.pushView( BottomView, null, null, bottomTrans);
				}
			}
			
		]]>
	</fx:Script>
	
	<s:layout>
		<s:VerticalLayout verticalAlign="middle" horizontalAlign="center"/>
	</s:layout>
	
	<s:Label text="main screen" />
	
</s:View>

NextView.mxml:-

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
		xmlns:s="library://ns.adobe.com/flex/spark" 
		title="Next Screen" gestureSwipe="handleSwipe(event)">
	
	<fx:Script>
		<![CDATA[
                        import spark.transitions.SlideViewTransition;
			import spark.transitions.SlideViewTransitionMode;
			import spark.transitions.ViewTransitionDirection;
			
			private function handleSwipe(event:TransformGestureEvent):void
			{
	
					// Swipe was to the left
				 if (event.offsetX == 1 ) {
					// push the NextView withour any data using 
					var slideViewTransition2:SlideViewTransition = new SlideViewTransition();
					slideViewTransition2.duration = 300;
					slideViewTransition2.direction = ViewTransitionDirection.RIGHT; 
					navigator.pushView( ConvulsingChild, null, null, slideViewTransition2);
				}
			}
	</fx:Script>
	
	<s:layout>
		<s:VerticalLayout verticalAlign="middle" horizontalAlign="center"/>
	</s:layout>
	
	<s:Label text="Next screen" />
	
</s:View>

By

Flex Itemrenderer content overflow

There seems to be a problem with MXItemRenderer derivative classes resizing percentage-based widths.

datagrid:-

<mx:DataGrid id="dataGrid" dataProvider="{dataProvider}"
includeIn="data"
sortableColumns="false"
resizableColumns="false"
draggableColumns="false"
verticalScrollPolicy="on"
wordWrap="true"
variableRowHeight="true"
rowCount="10" maxHeight="500" width="100%">
<mx:columns>
<mx:DataGridColumn headerText="new data" itemRenderer="com.test.NewItemRenderer"/>
</mx:columns>
</mx:DataGrid>

newItemRenderer.mxml:-

<s:MXDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx"  focusEnabled="true" width="100%">
<s:Label text="{data}" lineBreak="toFit" width="100%"/>
</s:MXDataGridItemRenderer>

will cause the content to spill over into the next row (or clip if clipAndEnableScrolling is true) when the itemrenderer is recycled during scrolling the datagrid.
If I set the width to a pixel size it will draw correctly.

Only thing similar I have come across is this issue on stackoverflow

If anyone has a better workaround that can keep the percentage based width I’d love to hear it!

By

BlazeDS and Dynamic Remote Endpoints

make sure you are sitting down, you might fall over -  I’m about to make a tech post!

I’ve been working on setting dynamic endpoints for our Flex Remoting server BlazeDS for the last day or two and it hasn’t been easy to find info on how to do it so I thought I should mention it here.

What we want to do is have an xml file with our configuration settings that will be read on initialisation with the endpoint URL, whether we want to use ssl and the name of the channel

<?xml version="1.0" encoding="UTF-8"?>
<config>
<channel id="my-amf" secureChannel="false">
<endpoint url="http://localhost:8080/eq-remoting-amf/messagebroker/amf" secureEndpoint="false"/>
<properties>
<pollingEnabled>false</pollingEnabled>
<addNoCacheHeaders>false</addNoCacheHeaders>
</properties>
</channel>
</config>

I have this saved in the root of the application.

Then in the application you need to parse the xml and set the channel in the remoteObject.

here’s my example:-

<?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"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application2_creationCompleteHandler(event)" >
<fx:Declarations>
<s:RemoteObject id="ro" destination="mockService" result="ro_resultHandler(event)" />
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import mx.messaging.ChannelSet;
import mx.messaging.channels.AMFChannel;
import mx.messaging.events.MessageAckEvent;
import mx.messaging.events.MessageEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.xml.SimpleXMLDecoder;

protected function ro_resultHandler(event:ResultEvent):void
{
label.text = event.result.toString();
}

private var channelSet:ChannelSet;

private var myService:String;

private var XML_URL:String = "flex-server.xml";
private var myXMLURL:URLRequest;
private var myLoader:URLLoader;

protected function application2_creationCompleteHandler(event:FlexEvent):void
{
//load the xml file
myXMLURL = new URLRequest(XML_URL);
myLoader = new URLLoader(myXMLURL);
//event to fire on load complete
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
}

private function xmlLoaded(event:Event):void
{
//parse xml
var xmlDoc:XMLDocument = new XMLDocument(String(event.data));
var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
var configObject:Object = decoder.decodeXML(xmlDoc);
var channelSet:ChannelSet = new ChannelSet();
var channelConfig:Object = configObject.config.channel;
var channel:AMFChannel;
if(channelConfig.secureChannel)
{
channel = new SecureAMFChannel(channelConfig.id, channelConfig.endpoint.url);
}
else
{
channel = new AMFChannel(channelConfig.id, channelConfig.endpoint.url);
}
channel.pollingEnabled = false;
channelSet.addChannel(channel);
//set the channel to the remoteObject
ro.channelSet = channelSet;
}

]]>
</fx:Script>
<s:layout>
<s:VerticalLayout />
</s:layout>
<s:Button label="Get Data" click="ro.testcall('hello', ' world')"/>
<s:Label id="label" />

</s:Application>

substituting your server call obviously and disabling the flex server in the preferences of your application (set to none/other).

By

Thoughts on Flex 4

I have been working on a Flex 4 project for the past 3 or so months using the Potomac Framework (an OSGi-like framework for flash). We started off with the Flex 4 Beta 2 framework and have now moved to stable, and I have to say, for a 4th iteration, the IDE still seems years behind Java in Eclipse.

There’s a few nifty features like the design view which will let you filter by state but things like errors being underlined as well as a gutter mark make things a lot easier and formatting code seems to be limited to ctrl+a the file and then fix indentation. All the magic functions like refactoring out methods and variables that us Java programmers take for granted these days are staples of Eclipse, IntelliJ and Netbeans. Sure, you can go without all the fluff and I know guys who are adamant that fully featured IDEs are the work of Satan, however they save me several hours over the course of a week spent coding.

What is there in Flash Builder is also pretty buggy…. auto organise imports has a life of its own, often deleting used imports at random and the build path often leaves classes un-included – especially when you copy/paste classes from other modules. That’s the two main offenders that plague me day-to-day, there are several more however they could also be related to the Potomac Framework which is currently in Beta.

There are a couple of plugins that address one or some of these issues:- one such option is SourceMate (written by the same folks who wrote the potomac framework) but spending an extra 80 bucks on top of the flash builder license seems a bit steep, and my last play with the beta had all those methods in their own menu rather than under refactor/source menus. It does what it says it will, and if the extra cash doesn’t rankle it’s worth a look.

I’m surprised Adobe didn’t just dump a pile of money in their laps and just shunt it into the flash builder code, and chances are they are working on the same functionality as we speak meaning SourceMate will need to keep a step or three in front of adobe in order for it to remain a viable investment.

No doubt building an IDE to this point is no mean feat, however I would have thought that Adobe would have thrown the extra resources at it to provide a more polished product when the likes of Silverlight, HTML5 and JavaFX (if it survives the Oracle merger) are gaining serious interest among developers and designers.