diff --git a/README.md b/README.md index b7616a1..487d626 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Add a dependency code to your **module**'s `build.gradle` file. ```gradle dependencies { - implementation "com.github.skydoves:only:1.0.4" + implementation "com.github.skydoves:only:1.0.5" } ``` @@ -209,30 +209,64 @@ Only.init(this) ## Usage in Java Here are some usages for Java developers. ```java -int times = Only.INSTANCE.getOnlyTimes("IntroPopup") ; +int times = Only.getOnlyTimes("IntroPopup") ; if (times < 3) { - Only.INSTANCE.setOnlyTimes("IntroPopup", times + 1); + Only.setOnlyTimes("IntroPopup", times + 1); showIntroPopup(); } ``` -Or we can run `Only` in java project using `Only.Builder` and `Function0`. +### Builder +we can run `Only` in java project using `Only.Builder` and `Runnable`. ```java new Only.Builder("introPopup", 1) - .onDo(new Function0() { - @Override - public Unit invoke() { - doSomethingOnlyOnce(); - return Unit.INSTANCE; + .onDo(new Runnable() { + @Override + public void run() { + doSomethingOnlyOnce(); } }) - .onDone(new Function0() { - @Override - public Unit invoke() { - doSOmethingAfterDone(); - return Unit.INSTANCE; - } + .onDone(new Runnable() { + @Override + public void run() { + doSOmethingAfterDone(); + } + }).run(); // run the Only +``` +### Java8 lambda expression +We can make it more simple using Java8 lambda expression.
+Add below codes on your `build.gradle` file. +```gradle +android { + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } +} +``` +Then you can run the Only like below. +```java +new Only.Builder("introPopup", 1) + .onDo(() -> { + doSomethingOnlyOnce(); + }) + .onDone(() -> { + doSOmethingAfterDone(); }).run(); // run the Only ``` +### Custom util class +We can create custom util class like what Kotlin's `onlyOnce`. +```java +public class OnlyUtils { + + public static void onlyOnce( + String name, Runnable runnableOnDo, Runnable runnableOnDone) { + new Only.Builder(name, 1) + .onDo(runnableOnDo) + .onDone(runnableOnDone) + .run(); // run the Only + } +} +``` ## Find this library useful? :heart: Support it by joining __[stargazers](https://github.com/skydoves/only/stargazers)__ for this repository. :star: diff --git a/app/build.gradle b/app/build.gradle index 4927d10..e6915af 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,6 +12,10 @@ android { versionCode versions.versionCode versionName versions.versionName } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } } dependencies { diff --git a/dependencies.gradle b/dependencies.gradle index fcfa468..493f2ed 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -1,8 +1,8 @@ ext.versions = [ minSdk : 15, compileSdk : 29, - versionCode : 5, - versionName : '1.0.4', + versionCode : 6, + versionName : '1.0.5', gradleBuildTool : '3.6.3', spotlessGradle : '3.28.1', diff --git a/only/src/main/java/com/skydoves/only/Only.kt b/only/src/main/java/com/skydoves/only/Only.kt index 1409822..2aa4629 100644 --- a/only/src/main/java/com/skydoves/only/Only.kt +++ b/only/src/main/java/com/skydoves/only/Only.kt @@ -409,7 +409,6 @@ object Only { editor.apply() } - /** Builder class for creating [Only]. */ @OnlyDsl class Builder(