Android Guide
This guide will show you how to use LeanEngine on an Android device. All the examples are taken from our example Android app which is a part of LeanEngine for Android. You can find it here. For information on how to obtain and run this project please visit the Quick start section of this documentation.Initialization
Before you can start using all the goodies of LeanEngine you need to initialize it. It needs to know the URL of your server. You do this by calling it's LeanEngine.init() method. This has to be done only once in you app. For instance you can call it in the onCreate() method of your main activity. Once it has been initialized you can start calling it's static methods.
LeanEngine.init(this, "http://lean-engine.appspot.com");
Login
Login is done by invoking LoginDialog object. There are three possible outcomes of a login attempt:
- login was successful
- user has cancelled the login
- there was an error
Currently different login methods are supported. You can login your users with Facebook, Google or Yahoo. You tell the dialog which method you want by providing it with a loginUri parameter. Of course your user needs to have an account at the provider he chooses to use.
If the login outcome was successful a user token is set. If at any point you need to check whether you user is currently logged in or not you can do so by calling LeanAccount.isTokenAvailable(). The token in stored in the preferences. That means that your app will remember if the user was logged in even after the app is restarted. You can read more about logging out here.
Facebook login example
This example shows how to make a Facebook login after the user has clicked a button. First we obtain a login URI for Facebook. Then we make a new LoginDialog with a new LoginListener (with overrided methods for each of the possible outcomes). At the end we show the dialog.
loginFacebookButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Uri loginUri = LeanEngine.getFacebookLoginUri(); LoginDialog fbDialog = new LoginDialog(LoginActivity.this, loginUri.toString(), new LoginListener() { @Override public void onSuccess() { // do your thing here } @Override public void onCancel() { // do your thing here } @Override public void onError(LeanError error) { // do your thing here } }); fbDialog.show(); } });
Logout
After a successful login a token is stored in LeanEngine's preferences. When you logout this token is removed from the preferences. At this point the server also gets notified about the logout therefore the previously stored token is not valid any more anyways. The example shows how to logout your user when logoutButton has been clicked. Notice the two methods you need to override here for the two possible outcome scenarios on logout.
logoutButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { LeanAccount.logoutInBackground(new NetworkCallback() { @Override public void onResult(Boolean... result) { // your code here } @Override public void onFailure(LeanError error) { // your code here } }); } });
LeanEntity
LeanEngine stores data in objects called LeanEntity. LeanEntity has the following fields:
- kind
- properties
- id
- accountID
LeanEntity catEntity = LeanEntity.init("cat"); catEntity.put("name", "Mookie"); catEntity.put("color", "white"); catEntity.put("age", 2);
When you need to get a property from an entity you need to use the correct method for the type of object you have stored. For instance if you need to read a cat's name you would call it's getString() method
// this will return the value "Mookie" if you call it on the // catEntity created in the example above String catName = catEntity.getString("name");
Supported types
LeanEntity property values can be of one of the following Java objest types:
- String
- Long
- Double
- Date
- Boolean
Storing entities
After you have created your LeanEntity you can send it to the server. This is done by calling saveInBackground() method.
Saving is done in background so it does not block the UI thread. When saving LeanEntity it gets a unique ID and the server returns this ID upon a successful save.entity.saveInBackground(new NetworkCallback() { @Override public void onResult(Long... result) { // your code here // inform the user saving was successful } @Override public void onFailure(LeanError error) { // your code here // inform the user something went wrong } });
Querying entities
You get your data form the server by making a LeanQuery. When making a query you need to specify the kind of entities you are looking for. You can also define filters for specific properties of the searched entity. The example shows how to make a query to find all cat entities with age 2 or more and sort them ascending.
LeanQuery query = new LeanQuery("cat"); query.addFilter("age", LeanQuery.FilterOperator.GREATER_THAN_OR_EQUAL, 2); query.addSort("age", LeanQuery.SortDirection.ASCENDING); query.limit(2); LeanEntity[] result = new LeanEntity[0]; try { result = query.fetch(); } catch (LeanException e) { // alert user something went wrong return; }
Deleting entities
If you already have an instance of the entity you can delete it simply by calling ti's delete() method. Otherwise you need to provide the kind and the id of the entity to be deleted.
try { LeanEntity.delete("cat", entitiyID); } catch (LeanException e) { // alert user something went wrong return; }