Only

An easy way to persistence and run block codes only as many times as necessary on Android.

Download

Download
Jitpack

Gradle

And add a dependency code to your module's build.gradle file.

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

Usage

Initialize

Fisrt, initialize the Only using init() method like below.

This code can be initialized on Application class only once.

Only.init(context)

onDo

Below codes will run the showIntroPopup() only three times using onDo method.

Only.onDo("introPopup", times = 3) {
  showIntroPopup()
}

// kotlin dsl
only("introPopup", times = 3) {
  onDo {
    showIntroPopup()
  }
}

onDone

Below codes will run the doSomeThingAfterDone() and toast("done") after run the onDo block codes three times.

Only.onDo("introPopup", times = 3,
  onDo = {
    showIntroPopup()
    toast("onDo only three times")
  },
  onDone = {
    doSomethingAfterDone()
    toast("done")
  })

// kotlin dsl
only("introPopup", times = 3) {
  onDo {
    showIntroPopup()
    toast("onDo only three times")
   }
  onDone {
    doSomeThingAfterDone()
    toast("done")
  }
}

Version Control

We can renew the persistence times for controlling the version using version option.

If the version is different from the old version, run times will be initialized 0.

Only.onDo("introPopup", times = 3,
  onDo = { showIntroPopup() },
  onDone = { doSomethingAfterDone() },
  version = "1.1.1.1"
)

// kotlin dsl
only("introPopup", times = 3) {
  onDo { showIntroPopup() }
  onDone { doSomethingAfterDone() }
  version("1.1.1.1")
}

Create Using Builder

We can run Only using Only.Builder class like below.

Only.Builder("introPopup", times = 3)
  .onDo { showIntroPopup() }
  .onDone { doSomethingAfterDone() }
  .version("1.1.1.1")
  .run()

OnlyOnce, OnlyTwice, OnlyThrice

Here is some useful kotlin-dsl functions.

onlyOnce("onlyOnce") { // run the onDo block codes only once.
  onDo { doSomethingOnlyOnce() }
  onDone { doSomethingAfterDone() }
}

onlyTwice("onlyTwice") { // run the onDo block codes only twice.
  onDo { doSomethingOnlyTwice() }
  onDone { doSomethingAfterDone() }
}

onlyThrice("onlyThrice") { // run the onDo block codes only three times.
  onDo { doSomethingOnlyThrice() }
  onDone { doSomethingAfterDone() }
  version("1.1.1.1")
}

Clear Times

You can optionally delete the stored Only times data or delete the entire Only times data.

Only.clearOnly("introPopup") // clear one saved times data.
Only.clearAllOnly() // clear all of the times data on the application.

View Extension

Below codes will show the button view only once.

button.onlyVisibility(name = "myButton", times = 1, visible = true)

Debug Mode

Sometimes on debug, we don't need to persist data and replay onDone block.

onlyOnDoDebugMode helps that ignore persistence data and onDone block when initialization. It runs only onDo block.

Only.init(this)
   .onlyOnDoDebugMode(true)

Usage in Java

We can run Only in java project using Only.Builder and Function0.

new Only.Builder("introPopup", 1)
    .onDo(new Function0<Unit>() {
      @Override
      public Unit invoke() {
        doSomethingOnlyOnce();
        return Unit.INSTANCE;
      }
    })
    .onDone(new Function0<Unit>() {
      @Override
      public Unit invoke() {
        doSOmethingAfterDone();
        return Unit.INSTANCE;
      }
    }).run(); // run the Only

GitHub