|
26 | 26 | Add a dependency code to your **module**'s `build.gradle` file. |
27 | 27 | ```gradle |
28 | 28 | dependencies { |
29 | | - implementation "com.github.skydoves:only:1.0.4" |
| 29 | + implementation "com.github.skydoves:only:1.0.5" |
30 | 30 | } |
31 | 31 | ``` |
32 | 32 |
|
@@ -209,30 +209,64 @@ Only.init(this) |
209 | 209 | ## Usage in Java |
210 | 210 | Here are some usages for Java developers. |
211 | 211 | ```java |
212 | | -int times = Only.INSTANCE.getOnlyTimes("IntroPopup") ; |
| 212 | +int times = Only.getOnlyTimes("IntroPopup") ; |
213 | 213 | if (times < 3) { |
214 | | - Only.INSTANCE.setOnlyTimes("IntroPopup", times + 1); |
| 214 | + Only.setOnlyTimes("IntroPopup", times + 1); |
215 | 215 | showIntroPopup(); |
216 | 216 | } |
217 | 217 | ``` |
218 | | -Or we can run `Only` in java project using `Only.Builder` and `Function0`. |
| 218 | +### Builder |
| 219 | +we can run `Only` in java project using `Only.Builder` and `Runnable`. |
219 | 220 | ```java |
220 | 221 | new Only.Builder("introPopup", 1) |
221 | | - .onDo(new Function0<Unit>() { |
222 | | - @Override |
223 | | - public Unit invoke() { |
224 | | - doSomethingOnlyOnce(); |
225 | | - return Unit.INSTANCE; |
| 222 | + .onDo(new Runnable() { |
| 223 | + @Override |
| 224 | + public void run() { |
| 225 | + doSomethingOnlyOnce(); |
226 | 226 | } |
227 | 227 | }) |
228 | | - .onDone(new Function0<Unit>() { |
229 | | - @Override |
230 | | - public Unit invoke() { |
231 | | - doSOmethingAfterDone(); |
232 | | - return Unit.INSTANCE; |
233 | | - } |
| 228 | + .onDone(new Runnable() { |
| 229 | + @Override |
| 230 | + public void run() { |
| 231 | + doSOmethingAfterDone(); |
| 232 | + } |
| 233 | + }).run(); // run the Only |
| 234 | +``` |
| 235 | +### Java8 lambda expression |
| 236 | +We can make it more simple using Java8 lambda expression.<br> |
| 237 | +Add below codes on your `build.gradle` file. |
| 238 | +```gradle |
| 239 | +android { |
| 240 | + compileOptions { |
| 241 | + sourceCompatibility JavaVersion.VERSION_1_8 |
| 242 | + targetCompatibility JavaVersion.VERSION_1_8 |
| 243 | + } |
| 244 | +} |
| 245 | +``` |
| 246 | +Then you can run the Only like below. |
| 247 | +```java |
| 248 | +new Only.Builder("introPopup", 1) |
| 249 | + .onDo(() -> { |
| 250 | + doSomethingOnlyOnce(); |
| 251 | + }) |
| 252 | + .onDone(() -> { |
| 253 | + doSOmethingAfterDone(); |
234 | 254 | }).run(); // run the Only |
235 | 255 | ``` |
| 256 | +### Custom util class |
| 257 | +We can create custom util class like what Kotlin's `onlyOnce`. |
| 258 | +```java |
| 259 | +public class OnlyUtils { |
| 260 | + |
| 261 | + public static void onlyOnce( |
| 262 | + String name, Runnable runnableOnDo, Runnable runnableOnDone) { |
| 263 | + new Only.Builder(name, 1) |
| 264 | + .onDo(runnableOnDo) |
| 265 | + .onDone(runnableOnDone) |
| 266 | + .run(); // run the Only |
| 267 | + } |
| 268 | +} |
| 269 | +``` |
236 | 270 |
|
237 | 271 | ## Find this library useful? :heart: |
238 | 272 | Support it by joining __[stargazers](https://github.com/skydoves/only/stargazers)__ for this repository. :star: |
|
0 commit comments