This is an example app built using Contentstack’s Android SDK, Sync API, and Persistence Library. You can try out and play with our Sync API and data persistence with this example app, before building bigger and better applications.
The Persistence Library lets you store data on the device’s local storage, helping you create apps that can work offline too. Perform the steps given below to use the app.
Perform the steps given below to use this app.
In this tutorial, we will first go through the steps involved in configuring Contentstack and then look at the steps required to customize and use the presentation layer.
Log in to your Contentstack account, and create a new stack. Read more about stack.
Add a publishing environment to publish your content in Contentstack. Provide the necessary details as per your requirement. Read more about environments.
For this app, we need just one content type: Session. Here’s what it’s needed for:
- Session: Lets you add the session content to your app
For quick integration, we have already created the content type. Download the content type and import it to your stack. (If needed, you can create your own content types. Read more about Content Types).
Now that all the content types are ready, let’s add some content for your Stack.
Create and publish entries for the ‘Session’ content type.
Now that we have created the sample data, it’s time to use and configure the presentation layer.
To get your app up and running quickly, we have created a sample app. Clone the Github repo given below and change the configuration as per your need:
$ git clone https://github.com/contentstack/contentstack-android-persistence-example.git
Now add your Contentstack API Key, Delivery Token, and Environment to the project during the SDK initialization step. (Find your Stack's API Key and Delivery Token.)
Stack stack = Contentstack.stack(context,“api_key”, “delivery_token”, “environment");
This will initiate your project.
We need to first download Realm and install it. To do so, perform the steps given below:
- Add the latest version of Realm library in your project and follow Installation gulde to complete setup.
Now that your Realm installation is ready, let's look at the steps involved in setting up your Contentstack SDK.
-
Download and set up the Contentstack android SDK. Read the Contentstack android SDK Documentation for more details.
-
You will find the "syncwrapper" folder, which contains the following four files keep it in your src folder:
- RealmStore.java
- SyncManager.java
- SyncPersistable.java
- SyncStore.java
There are three important items to be mapped in our Synchronization process:
- Sync token/pagination token
- Entries
- Assets
Let’s look at how to persist Sync Token & Pagination Token
To save Sync Token and Pagination Token, we need SyncStore class file which will manage the storage and retrieval of updated Sync Token and Pagination Token.
if (stackResponse.getPaginationToken()!=null){
persistsToken(stackStack.getPaginationToken());
}else{
persistsToken(stackStack.getSyncToken());
}
To begin with, let’s consider an example of our Example app. Let’s say we have content type: Session. Let’s see how to implement this example.
Create a table class named Session extending RealmObject, and add following code to implement EntityFields as shown below:
// @RealmClass accepts ("name= "content_type_uid"")
@RealmClass(name = "session")
public class Session extends RealmObject {
*************************************
// Mandatory fields
@PrimaryKey
@RealmField(name = "uid")
private String uid;
*************************************
//Note: the annotation name will be content_type's field id
@RealmField(name="title")
//user defined fields may be anything of user liking.
private String mTitle;
@RealmField(name="is_popular")
private String mIsPopular;
@RealmField(name="type")
private String mType;
@RealmField(name="session_id")
private String mId;
@RealmField(name="tags")
private String mTags;
@RealmField(name="locale")
private String mLocale;
@RealmField(name="speakers")
private RealmList<Speaker> speaker;
@RealmField(name="track")
private String mTrack;
@RealmField(name="start_time")
private String mStartTime;
@RealmField(name="end_time")
private String mEndTime;
@RealmField(name="room")
private Room mRoom;
... Generated getters and setters ...
}
You also need to implement the fieldMapping function which returns the mapping of attributes and entry fields in Contentstack.
Similarly, we can add other entries and mapping for each entry.
To map Assets, you need to create a table for assets named SysAssets and extend RealmObject. Add the following code to implement AssetProtocol.
@RealmClass(name="sys_assets")
public class SysAssets extends RealmObject {
******************************
// Mandatory fields
@PrimaryKey
@RealmField(name = "uid")
private String uid;
******************************
// Note: the annotation name will be content_type's field id
@RealmField(name="created_at")
//user defined fields may be anything of user liking.
private String created_at;
@RealmField(name = "updated_at")
private String updated_at;
@RealmField(name = "created_by")
private String created_by;
@RealmField(name = "updated_by")
private String updated_by;
@RealmField(name = "content_type")
private String content_type;
@RealmField(name = "file_size")
private String file_size;
@RealmField(name = "tags")
private String tags;
@RealmField(name = "filename")
private String filename;
@RealmField(name = "url")
private String url;
@RealmField(name = "is_dir")
private String is_dir;
@RealmField(name = "parent_uid")
private String parent_uid;
@RealmField(name = "_version")
private String version;
@RealmField(name = "title")
private String title;
@RealmField(name = "publish_details")
private String publish_details;
... Generated getters and setters ...
}
Now, our final step is to initiate SyncManager and begin with the Sync content type.
Finally, after setting up the content mapping, initiate SyncManager. It takes Stack instance and RealmPersistenceHelper class instance as follows:
//Get stack instance like below
Stack stack = Contentstack.stack(context, "api_key", "access_token", "environment");
//Get realm instance like below
Realm realmInstance = Realm.getDefaultInstance();
//Get realmStore instance by passing realmInsatance to its constructor like below.
RealmStore realmStore = new RealmStore(realmInstance);
SyncManager syncManager = new SyncManager(realmStore, stack);
syncManager.stackRequest();
Screenshot