Thursday, October 9, 2014

Automate Save State Feature in Firemonkey

Hi!
One of the many exciting new features of Delphi XE7 is the Save State in Firemonkey.
The FireMonkey Save State feature allows you to save data describing the state of the application before it closes. The Save State data can be used to recover the state after the application is restarted.
This is a very cool feature but the way in which you save and restore the data is not so refined (for more info see this http://docwiki.embarcadero.com/RADStudio/XE7/en/FireMonkey_Save_State ).
What happen if I need to save 10,15 or more components? I need to specify the save and the restore for each one.
So I decided to write a class helper to automate this process!
Using JSON serialization all the components of type TFMXObject are saved. In particular is stored the Data property.

You can find the demo in my github repository .

This is a snippet of the demo

uses SaveStateHelper;

procedure TForm1.FormCreate(Sender: TObject);
begin
  LoadFormState;
end;

procedure TForm1.FormSaveState(Sender: TObject);
begin
  SaveFormState;
end;

I hope you enjoy it!!

Tuesday, June 3, 2014

Access Marvel Developer API with Delphi XE6



Marvel has recent made available The Marvel Comics API, that allows developers everywhere to access information about Marvel Universe. "The Marvel Comics API is a RESTful service which provides methods for accessing specific resources at canonical URLs and for searching and filtering sets of resources by various criteria. All representations are encoded as JSON objects".



So my idea is to access these API with the Delphi REST Client Library and create an awesome app.
Let's start!
At First we need to register to the developers program, once registered we will be given 2 keys one public and the other private that will be used to call the Marvel API. For more information about the service, resources, authentication and so on I refer you to the official Pages: General Info - Getting Started - Interactive API tester.
You can access six resource types using the API:

  • Comics
  • Comic Series
  • Comic Stories
  • Comic events and crossovers
  • Creators
  • Characters

In this first demo app I get information only about characters of Marvel Universe.




I created an HeaderFooter Mobile Application by the wizard, modified the text of prebuilt label for the Title ("Marvel Character"), added a TLabel for the name of the Character, a TImage that is the representative image for the character and finally I insert the mainly 3 components of the REST Client Library: RESTClient, RESTRequest and RESTResponse.
The Marvel Comics API’s base endpoint is http://gateway.marvel.com/v1/public so I put this URL in the BaseURL property of the RESTClient.



The RESTRequest represent the resource in the REST pattern and in this case represent the character. I defined the Resource property in this way: characters?limit=1&offset={OFFSET}&ts={TS}&apikey={APIKEY}&hash={HASH}.
There are 3 parameters:
  • limit - that represent the number of the result, in this case 1 result for time because we can see 1 character at time on the form
  • offset - that indicate the distance from the first character
  • ts - a timestamp (or other long string which can change on a request-by-request basis)
  •  apikey - your public key
  • hash - a md5 digest of the ts parameter, your private key and your public key (e.g. md5(ts+privateKey+publicKey)
This is the snippet code of request:

procedure THeaderFooterForm.ExecuteRequest;
var
  TS: string;
  imd5: TIdHashMessageDigest;
  HASHStr: string;
begin
  // StartWait show a progress dialog until the request is finished
  StartWait;
  TS := IntToStr(DateTimeToUnix(Now));
  RESTRequest1.Params.ParameterByName('OFFSET').Value := FOffSet.ToString;
  RESTRequest1.Params.ParameterByName('TS').Value := TS;
  RESTRequest1.Params.ParameterByName('APIKEY').Value := PUBLIC_KEY;
  imd5 := TIdHashMessageDigest5.Create;
  try
    HASHStr := TS + PRIVATE_KEY + PUBLIC_KEY;
    RESTRequest1.Params.ParameterByName('HASH').Value :=
      imd5.HashStringAsHex(HASHStr).ToLower;
    RESTRequest1.ExecuteAsync(OnAfterRequest);
  finally
    imd5.Free;
  end;
end;

Once the call give a result will need to parse the JSON response to take the name of the character and its image, obviously respecting the JSON structure described in the official documentation:

procedure THeaderFooterForm.OnAfterRequest;
var
  RetObject: TJSONObject;
  RetData: TJSONObject;
  MResult: TJSONObject;
  Loader: TImageLoaderThread;
  Thumbnail: TJSONObject;
begin
  try
    RetObject := TJSONObject.ParseJSONValue(RESTResponse1.Content)
      as TJSONObject;
    RetData := RetObject.GetValue('data') as TJSONObject;
    MResult := (RetData.GetValue('results') as TJSONArray).Get(0)
      as TJSONObject;
    Thumbnail := MResult.GetValue('thumbnail') as TJSONObject;
    Text1.Text := MResult.GetValue('name').Value;
    // TImageLoaderThread is a custom class to retrieve the image in a background thread
    // and 2 anonymous methods to notify when thread finish download image and when bitmap is ready  
    Loader := TImageLoaderThread.Create(true, nil, OnBitmapLoaded);
    Loader.ImgURL := Thumbnail.GetValue('path').Value + '.' +
      Thumbnail.GetValue('extension').Value;
    Loader.Start;
  finally
    StopWait;
  end;
end;

The image is provided by another URL so I have to make another request for the image.
Both the request (info character + image) are completed by background threads so it doesn't block the GUI.
Here some screeenshot about final app





The app run on Android device but the code work on Android, iOS, Windows and Mac.
In the next post I will improve the app creating a master-detail structure that allowing to search character by other criteria!
The code was loaded on my github repository.

Friday, April 11, 2014

My First Appmethod App


In the "bit Time Lab" me and my colleague Daniele Teti have developed an app to control a car via bluetooth with Appmethod, for Android and iOS (released soon) with the same codebase.

The main topics covered by the team are the multitouch UI and the bluetooth hardware interface using a true native app (no VMs, no scripting, no compromises). Below the picture of the app and the cars used.


Collegamento permanente dell'immagine integrata


The most interesting note was the time: we spent 7/8 hours to develop and build the entire system!
This project will be shown at the Rome Codemotion.
Here the video presentation of this project by bit Time Lab

Tuesday, March 11, 2014

Gideros Mobile - A game development tool

Gideros is a set of software packages created and managed by a company named  Gideros Mobile, it provides developers with the ability to create 2D games for multiple platforms by reusing the same code.

At italian DROIDCON in Turin, I held a speech about the Gideros Mobile framework (the slides will be available shortly).

We appreciate this tool so much that in bit Time Software we decided to make a game using Gideros: Math Brain. It's free and available for Android and iOS devices.


To start developing with Gideros I suggest the online tutorials and the book Gideros Mobile Game Development. Here you can find some considerations about the book written by me and my colleague Daniele Teti.

I hope to write soon new post about this tool.