Kripton Live Data — First contact

Francesco Benincasa
6 min readMay 16, 2018

--

If you work on Android, you already read or work with Architectural Component. Kripton Persistence Library, since version 4, support the generation of Live Data object starting from DAO queries. I’ve talked about Kripton Persistence Library V. 4 in this post.

In this article, I want to show you how to integrate Kripton with View Model and Live Data. To accomplish this task, I realized a simple RSS Reader available on github.

For simplicity, RSS Reader reads RSS only from BBC channel. The app has a simple flow: at the application start, RSS feeds are downloaded from BBC site and store in a SQLite database. You can force RSS feed check with the refresh button. Every article shows its title, description and image.

Once articles are downloaded, they are displayed in a collection of cards. If you click on an article, the app open browser to related link. Every clicked article is marked as read. The article list can be filtered in three mode:

  • No filter: all articles are showed
  • Read item: only read item are shown
  • Unread item: only unread item are shown

Read articles are marked with a green check.

Ok, I know.. the app is very simple, but at the same time, it allows me to show you some Kripton’s feature:

  • RSS feed are downloaded by a REST service. As you know, RSS is a particular type of XML data format. Kripton can be easily combined with Retrofit to consume this kind of services.
  • RSS feeds are locally stored in a SQLite database
  • Data model for REST service and local SQLite database are managed by Kripton in a uniform way.
  • SQLite access are managed by Kripton with DAO pattern. Develper have to write data-source and DAO interfaces. All boiler-plate code is generated by Kripton.
  • In the activity, we need to filter articles list with a filter. In out app, filters are implemented with a dynamic where condition that is calculate at runtime.

Configuration

First of all, let’s see the configuration.

At the moment the latest version of Kripton is 4, release candidate 14. In a couple of weeks, Kripton 4 final will be released.

Kripton is imported including its annotation processor and kripton-android-library.

REST Data Model

The REST web service that app consumes return an XML response like this:

From the RSS point of view, the data model class diagram is:

This model is derived directlyl from RSS’s XML (some attributes are omitted for simplicity).

ORM Data model

Once data are downloaded from web, we need to persist data into SQLite database. The entity relationship diagram is:

SQLite schema

Comparing REST data model and SQLite data model you can notice that main differences are:

  • Every entity in SQLite data model has an id attribute
  • In SQLite data model some entities are stored as embedded items (Image and Thumbnail).

Defining data model with Java classes

Now is time to translate REST data model and SQLite data model in Java classes. The follwing classes represent the Java classes that represent the two models.

For the REST data model, the attributes datePub and lastPubDate are managed with DateAdapter, that allow to convert the string value in a Date object instance.

With @BindXml annotation, XML information are associated to specific attributes. For example, the thumbnail attribute in Article class is associated to media XML namespace.

The same Java classes that define the data model for REST service, defines the SQLite data model. For SQLite data model, @BindSqlType and @BindSqlColumn are used to customize bean and attribute behaviour.

Every class, that must be persisted as table in SQLite database, have an id column (it is a Kripton requirement). To avoid field declaration duplication (I know, I’m lazy), every class of data model was derived from Entity class that contains id field (the primary key). It is no mandatory. Image and Thumbnail does not have its tables: they are embedded in Channel and Article tables.

Kripton and Retrofit: define REST client

You probably already know how to use Retrofit to define REST client (otherwise consult the official documentation)

Kripton is integrated with Retrofit using KriptonBinderConverterFactory during Retrofit client creation.

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL) .addConverterFactory(KriptonBinderConverterFactory
.create(BinderType.XML))
.client(httpClient.build()).build();

In our case we work with XML. KriptonBinderConverterFactory allows to specify data format used in REST service client (JSON is default).

Kripton ORM: define data source and DAOs interfaces

We need to specify DAOs and DataSource interface. Kripton will generate for us implementation code.

The important method definition is DaoArticle#selectByChannel that expose result as LiveData and accept a dynamic where condition, that will be provided at runtime. We will see how this method is used soon.

Usually, when a DAO method is invoked, you have to open and then close the SQLite connection. It’s important to know that when a DAO method returns a LiveData, the open-close database operations are done inside the method.

Kripton, Live Data and ViewModel

Now, is time to see how Kripton can be integrated with a ViewModel. To do this, let’s create a Repository class and a ViewModel to use in the main activity of RSS Reader app.

RssViewModel exposes all method necessary to read article (with or without filters), mark article as read and to download articles from the web.

The article list is subject to modification of article list and filter type. This double dependency is implementented with the switchMap used in the RssViewModel constructor.

Activity and dynamic filtering

As state at the beginning of this article, articles can be filtered or can be showed all together. To implements filters, query that retrieve articles accept a dynamic where condition that is retrieve from a enumeration type. The Activity code:

The method RSSViewModel#updateField allows to specifiy which filter is applyed. RSSViewModel#getArticles retrieve Live Data for articles. This Live Data is modified by filter modification and from article table rows modification (add or delete or update).

Conclusion

The sample project is available on github.

This example showed you how simple is to:

  • have an uniform persistence model usable in different context (SQLite and REST in this case)
  • Integrate Kripton with Architectural componet.

The dynamic sql where conditions allows to manage in a easy way situations where you need to modify executed SQL. To better understand the mechanism does not esitate to run application, debug, and view log information (Kripton loves writing logs..). Just another Kripton’s feature is the capability to generate SQL log on log cat. You can always remove them, but I think that in developement stage, them are a goodness.

If you like Kripton, please give it a star on github and give a clap to this article. Thank you in advance.

Happy coding!

Francesco

--

--