An easy way to persist and run code-block on Android.

Jaewoong Eum
3 min readAug 10, 2019

--

Only showcase

Hi, Today I’m gonna post about a library Only that I published a month ago.

The main purpose of this library is ..
💐 An easy way to persist and run code block only as many times as necessary on Android.

You can check about the simple usage details about the library “Only” at the below link.

When is it useful?

Sometimes we should implement something show up only one-or-two times. And it needs to be persisted using SharedPreferences, database or something many kinds of ways. But it is a really annoying thing which we have to waste our time for implementing perform only a few times. And it makes our codes messy. For example, we want to show up a popup only once. then how we should implement it? As we already know the easiest way is SharedPreferences like below.

If the project is already structured well about persistence things we do not need to consider it. but in our side project or something small-sized personal project, implementing the full persistence structure is a hassle thing. So here is some nice way to implement it fastly.

Configuration

We should import the library via adding a dependency on your gradle file.

dependencies {
implementation "com.github.skydoves:only:1.0.3"
}

And we should initialize the library using the init method.
It is recommended to declare on your Application class’s onCreate method, but it is doesn’t matter initialize on your Activity’s onCreate method. Anyway, it should be initialized before to use it.

Only.init(context)

How to use?

The basic usage is really simple.

The only keyword is a kotlin-dsl based keyword. onDo lambda block will perform showIntroPopup() method 3-times. When this code runs for the fourth time, onDo block will not be executed then, onDone block will be executed.

And here are some useful kotlin-dsl functions.

onlyOnce keyword executes onDo block only one time. So we don’t need to set time parameter. And there are more: onlyTwice, onlyThrice.

onLastDo, onBeforeDone

we can do pre&post-processing using onLastDo, onBeforeDone options.

When this code runs for the third time, after executed onDo block lastly, the onLastDo block will be executed. And the fourth time, onBeforeDone block will be executed only once, and next onDone block will be executed.

We can apply it for repeating x times.
If we want to show review-request popup 3 times and checks the user confirmed or not. If not, we should clear the persisted times. Then we can implement is using Only.clearOnly method.

We can check the confirmed or not in onLastDo block.
If not, clear times using the Only.clearOnly method, and repeat it the first time again.

Version Control

If our application is already released and we want to show a newly updated popup to old users. Then how should we implement it? Changes time parameter value 3 to 4? No, If we increment the value, the old user could see the popup one more time but a newbie user have to see the popup 4-times. So there is a version control way.

We can clear the persisted data using version option. If the version value is different to before, the onDo block will be executed three times again, even if the block is already executed three times in the past.
If we want to show updated-list popup every released, we can apply it like below.

Debug Mode

Sometimes on debug, we don’t need to persist data and replay onDone block. onlyOnDoDebugMode option helps that ignore persistence data and onDone block when initialization. It runs only onDo block.

Only.init(this)
.onlyOnDoDebugMode(true)

If you find this library is useful, support it by joining stargazers for this repository. ⭐️

--

--

Jaewoong Eum
Jaewoong Eum

Written by Jaewoong Eum

Senior Android Developer Advocate @ Stream 🥑 • GDE for Android • OSS engineer. Love psychology, magic tricks, and writing poems. https://github.com/skydoves

No responses yet