{"id":15626100,"url":"https://github.com/skydoves/only","last_synced_at":"2025-04-06T04:16:29.362Z","repository":{"id":48969342,"uuid":"193119682","full_name":"skydoves/Only","owner":"skydoves","description":":bouquet: An easy way to persist and run code block only as many times as necessary on Android.","archived":false,"fork":false,"pushed_at":"2021-02-05T15:55:22.000Z","size":566,"stargazers_count":482,"open_issues_count":1,"forks_count":27,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-30T03:07:33.389Z","etag":null,"topics":["android","dsl","kotlin","kotlin-dsl","only","onlyonce","persistence","preference","skydoves"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/skydoves.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null},"funding":{"github":"skydoves","custom":["https://www.paypal.me/skydoves","https://www.buymeacoffee.com/skydoves"]}},"created_at":"2019-06-21T15:16:52.000Z","updated_at":"2024-11-15T14:02:41.000Z","dependencies_parsed_at":"2022-08-30T06:11:16.888Z","dependency_job_id":null,"html_url":"https://github.com/skydoves/Only","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skydoves%2FOnly","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skydoves%2FOnly/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skydoves%2FOnly/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skydoves%2FOnly/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/skydoves","download_url":"https://codeload.github.com/skydoves/Only/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247430966,"owners_count":20937875,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","dsl","kotlin","kotlin-dsl","only","onlyonce","persistence","preference","skydoves"],"created_at":"2024-10-03T10:10:23.393Z","updated_at":"2025-04-06T04:16:29.239Z","avatar_url":"https://github.com/skydoves.png","language":"Kotlin","readme":"# Only\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://opensource.org/licenses/Apache-2.0\"\u003e\u003cimg alt=\"License\" src=\"https://img.shields.io/badge/License-Apache%202.0-blue.svg\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://android-arsenal.com/api?level=15\"\u003e\u003cimg alt=\"API\" src=\"https://img.shields.io/badge/API-15%2B-brightgreen.svg?style=flat\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://www.codacy.com/app/skydoves/Only?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=skydoves/Only\u0026amp;utm_campaign=Badge_Grade\"\u003e\u003cimg alt=\"API\" src=\"https://api.codacy.com/project/badge/Grade/3e04de5613974807a8eede8ccebc0a7d\"/\u003e\u003c/a\u003e\n    \u003ca href=\"https://github.com/skydoves/Only/actions\"\u003e\u003cimg alt=\"Build Status\" src=\"https://github.com/skydoves/Only/workflows/Android%20CI/badge.svg\"/\u003e\u003c/a\u003e\u003cbr\u003e\n  \u003ca href=\"https://androidweekly.net/issues/issue-370\"\u003e\u003cimg alt=\"Javadoc\" src=\"https://img.shields.io/badge/Android%20Weekly-%23370-orange.svg\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://us12.campaign-archive.com/?u=f39692e245b94f7fb693b6d82\u0026id=b12259cf32\"\u003e\u003cimg alt=\"KotlinWeekly\" src=\"https://img.shields.io/badge/KotlinWeekly-%23159-4E71E6\"/\u003e\u003c/a\u003e\n  \u003ca href=\"https://skydoves.github.io/libraries/only/html/only/com.skydoves.only/index.html\"\u003e\u003cimg alt=\"Javadoc\" src=\"https://img.shields.io/badge/Javadoc-Only-yellow.svg\"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n:bouquet: An easy way to persist and run code block only as many times as necessary on Android.\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://github.com/skydoves/Only/blob/master/art/showcase.png\" width=\"720\" height=\"307\"/\u003e\n\u003c/p\u003e\n\n## Download\n[![Maven Central](https://img.shields.io/maven-central/v/com.github.skydoves/only.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.github.skydoves%22%20AND%20a:%22only%22)\n[![Jitpack](https://jitpack.io/v/skydoves/Only.svg)](https://jitpack.io/#skydoves/Only)\n\n#### Gradle\nAdd below codes to your **root** `build.gradle` file (not your module build.gradle file).\n```gradle\nallprojects {\n    repositories {\n        mavenCentral()\n    }\n}\n```\nAnd add a dependency code to your **module**'s `build.gradle` file.\n```gradle\ndependencies {\n    implementation \"com.github.skydoves:only:1.0.8\"\n}\n```\n\n## Usage\n### Initialize\nFirst, initialize the `Only` using `init()` method like below. \u003cbr\u003e\nThis code can be initialized on `Application` class only once.\n```kotlin\nOnly.init(context)\n```\n\n### onDo\n\nBelow codes will run the `showIntroPopup()` only three times using `onDo` method.\n```kotlin\nOnly.onDo(\"introPopup\", times = 3) {\n  showIntroPopup()\n}\n\n// kotlin dsl\nonly(\"introPopup\", times = 3) {\n  onDo { showIntroPopup() }\n}\n```\nHere is the Java codes.\n```java\nOnly.onDo(\"introPopup\", 1, () -\u003e showIntroPopup());\n```\n\n### onDone\nBelow codes will run the `doSomethingAfterDone()` and `toast(\"done\")` after run the `onDo` block codes three times.\n\n```kotlin\nOnly.onDo(\"introPopup\", times = 3,\n  onDo = {\n    showIntroPopup()\n    toast(\"onDo only three times\")\n  },\n  onDone = {\n    doSomethingAfterDone()\n    toast(\"done\")\n  })\n\n// kotlin dsl\nonly(\"introPopup\", times = 3) {\n  onDo {\n    showIntroPopup()\n    toast(\"onDo only three times\")\n   }\n  onDone {\n    doSomeThingAfterDone()\n    toast(\"done\")\n  }\n}\n```\nHere is the Java codes.\n```java\nOnly.onDo(\"introPopup\", 1,\n    () -\u003e doSomethingOnlyOnce(), // onDo\n    () -\u003e doSOmethingAfterDone() // onDone\n);\n```\n\n### onLastDo, onBeforeDone\nWe can do pre and post-processing using `onLastDo`, `onBeforeDone` options.\n```kotlin\nonly(\"Intro\", times = 3) {\n  onDo {\n    showIntroPopup()\n    toast(\"onDo only three times\")\n  }\n  onLastDo { // executes only once after finished onDo block 3 times.\n    toast(\"finished onDo\")\n  }\n  onBeforeDone { // executes only once before run onDone block.\n    toast(\"starts onDo\")\n  }\n  onDone {\n    doSomethingAfterDone()\n    toast(\"done\")\n  }\n}\n```\nWe can apply it for repeating x times.\u003cbr\u003e\nBelow codes shows review-popup 3 times and checks the user reviewed or not in `onLastDo` block.\u003cbr\u003e\nIf not, clear times using the `Only.clearOnly` method, and repeat it the first time again.\n```kotlin\nonly(\"Intro\", times = 3) {\n  onDo { showReviewRequestPopup() }\n  onLastDo { // executes only once after finished onDo block 3 times.\n    if (!isRequested) {\n      Only.clearOnly(this@only.name)\n    }\n  }\n}\n```\n\n### Version Control\nWe can renew the persistence times for controlling the version using `version` option. \u003cbr\u003e\nIf the version is different from the old version, run times will be initialized 0.\u003cbr\u003e\n\n```kotlin\nOnly.onDo(\"introPopup\", times = 3,\n  onDo = { showIntroPopup() },\n  onDone = { doSomethingAfterDone() },\n  version = BuildConfig.VERSION_NAME // we can set manually. e.g. \"1.1.1.1\"\n)\n\n// kotlin dsl\nonly(\"introPopup\", times = 3) {\n  onDo { showIntroPopup() }\n  onDone { doSomethingAfterDone() }\n  version(\"1.1.1.1\")\n}\n```\n\n### Create Using Builder\nWe can run Only using `Only.Builder` class like below.\n```kotlin\nOnly.Builder(\"introPopup\", times = 3)\n  .onDo { showIntroPopup() }\n  .onDone { doSomethingAfterDone() }\n  .version(BuildConfig.VERSION_NAME)\n  .run()\n```\n\n### OnlyOnce, OnlyTwice, OnlyThrice\nHere is some useful kotlin-dsl functions.\n\n```kotlin\nonlyOnce(\"onlyOnce\") { // run the onDo block codes only once.\n  onDo { doSomethingOnlyOnce() }\n  onDone { doSomethingAfterDone() }\n}\n\nonlyTwice(\"onlyTwice\") { // run the onDo block codes only twice.\n  onDo { doSomethingOnlyTwice() }\n  onDone { doSomethingAfterDone() }\n}\n\nonlyThrice(\"onlyThrice\") { // run the onDo block codes only three times.\n  onDo { doSomethingOnlyThrice() }\n  onDone { doSomethingAfterDone() }\n  version(\"1.1.1.1\")\n}\n```\n\n### Clear Data\nWe can optionally delete the stored `Only` target data or delete the whole `Only` data.\n```kotlin\nOnly.clearOnly(\"introPopup\") // clears a saved target Only data.\nOnly.clearAllOnly() // clears all of the target Only data on the application.\n```\n\n### View Extension\nBelow codes will show the `button` view only once.\n```kotlin\nbutton.onlyVisibility(name = \"myButton\", times = 1, visible = true)\n```\n\n### Toast Extension\nBelow codes will show toast only x times.\n```kotlin\nonlyToast(\"toast\", 3, \"This toast will be shown only three times.\")\nonlyOnceToast(\"toast1\", \"This toast will be shown only once.\")\nonlyTwiceToast(\"toast2\", \"This toast will be shown only twice.\")\nonlyThriceToast(\"toast3\", \"This toast will be shown only thrice.\")\n```\n\n### Marking\nWe can mark data to the Only target.\n```kotlin\nonly(\"introPopup\", times = 3) {\n  onDo { showIntroPopup() }\n  onDone { doSomethingAfterDone() }\n  mark(\"abc\") // marks only once when run by kotlin dsl or builder class.\n}\n\nOnly.mark(\"introPopup\", 3) // changes marking using mark method.\nval marking = Only.getMarking(\"introPopup\") // gets the marked data.\n```\n\n### Debug Mode\nSometimes on debug, we don't need to persist data and replay onDone block. \u003cbr\u003e\n`onlyOnDoDebugMode` helps that ignore persistence data and onDone block when initialization. It runs only onDo block.\n```kotlin\nval only = Only.init(application)\nif (BuildConfig.DEBUG) {\n  only.onlyOnDoDebugMode(true)\n}\n```\n\n## Usage in Java\nHere are some usages for Java developers.\n```java\nint times = Only.getOnlyTimes(\"IntroPopup\") ;\nif (times \u003c 3) {\n    Only.setOnlyTimes(\"IntroPopup\", times + 1);\n    showIntroPopup();\n}\n```\n### Java Supports\nwe can run `Only` in our java project.\n```java\nOnly.onDo(\"introPopup\", 1,\n    new Runnable() {\n  @Override\n  public void run() {\n    doSomethingOnlyOnce();\n  }\n}, new Runnable() {\n  @Override\n  public void run() {\n    doSOmethingAfterDone();\n  }\n});\n```\nOr we can run using `Only.Builder` like below.\n```java\nnew Only.Builder(\"introPopup\", 1)\n  .onDo(new Runnable() {\n    @Override\n    public void run() {\n        doSomethingOnlyOnce();     \n      }\n    })\n   .onDone(new Runnable() {\n     @Override\n     public void run() {\n       doSOmethingAfterDone();\n     }\n   }).run(); // run the Only\n```\n### Java8 lambda expression\nWe can make it more simple using Java8 lambda expression.\u003cbr\u003e\nAdd below codes on your `build.gradle` file.\n```gradle\nandroid {\n  compileOptions {\n      sourceCompatibility JavaVersion.VERSION_1_8\n      targetCompatibility JavaVersion.VERSION_1_8\n  }\n}\n```\nThen you can run the Only like below.\n```java\nOnly.onDo(\"introPopup\", 1,\n    () -\u003e doSomethingOnlyOnce(), // onDo\n    () -\u003e doSOmethingAfterDone() // onDone\n);\n```\n### Custom util class\nWe can create custom util class like what Kotlin's `onlyOnce`.\n```java\npublic class OnlyUtils {\n\n  public static void onlyOnce(\n      String name, Runnable runnableOnDo, Runnable runnableOnDone) {\n    new Only.Builder(name, 1)\n        .onDo(runnableOnDo)\n        .onDone(runnableOnDone)\n        .run(); // run the Only\n  }\n}\n```\n\n## Find this library useful? :heart:\nSupport it by joining __[stargazers](https://github.com/skydoves/only/stargazers)__ for this repository. :star: \u003cbr\u003e\nAnd __[follow](https://github.com/skydoves)__ me for my next creations! 🤩\n\n# License\n```xml\nCopyright 2019 skydoves (Jaewoong Eum)\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n   http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n```\n","funding_links":["https://github.com/sponsors/skydoves","https://www.paypal.me/skydoves","https://www.buymeacoffee.com/skydoves"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskydoves%2Fonly","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskydoves%2Fonly","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskydoves%2Fonly/lists"}