{"id":16482118,"url":"https://github.com/bod/android-kprefs","last_synced_at":"2025-10-27T17:31:52.022Z","repository":{"id":138160868,"uuid":"178729130","full_name":"BoD/android-kprefs","owner":"BoD","description":"Android preferences for WINNERS!  Now optimized for Kotlin™","archived":false,"fork":false,"pushed_at":"2024-02-10T20:24:37.000Z","size":315,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-16T12:23:33.128Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/BoD.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2019-03-31T18:53:36.000Z","updated_at":"2024-02-10T20:34:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"ef5b0001-c0e6-4fa1-9fd8-ca7e550c7c07","html_url":"https://github.com/BoD/android-kprefs","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BoD%2Fandroid-kprefs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BoD%2Fandroid-kprefs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BoD%2Fandroid-kprefs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BoD%2Fandroid-kprefs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BoD","download_url":"https://codeload.github.com/BoD/android-kprefs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219860874,"owners_count":16556011,"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":[],"created_at":"2024-10-11T13:09:41.560Z","updated_at":"2025-10-27T17:31:51.686Z","avatar_url":"https://github.com/BoD.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# KPrefs: pref like a winner™!\n\nA very small library (~400 loc) for Android/Kotlin to reduce\nshared preferences boilerplate.\n\n_This is a much lighter, Kotlin specific, followup to my [Prefs lib](https://github.com/BoD/android-prefs)\nwhich was annotation processor based whereas this one is\nKotlin delegates based._\n\n## Usage\n### 1/ Add the dependencies to your project\n\n```groovy\ndependencies {\n    /* ... */\n    implementation 'org.jraf:kprefs:1.7.2'\n}\n```\n_Note: the artifact is hosted on Maven Central since v1.6.0 - it used to be hosted on JCenter before this version_\n\n### 2/ Define your preferences\nCreate a `Prefs` instance and pass it a `Context`.  Optionally pass it a `fileName`, and a `fileMode`.\n\nYou can also instead pass a `SharedPreferences` which is handy for instance when using the androidx security-crypto library.\n\n```kotlin\n    private val mainPrefs = Prefs(context)\n    \n    private val settingPrefs = Prefs(\n        context,\n        fileName = \"settings_prefs\",\n        fileMode = Context.MODE_PRIVATE\n    )\n\n    private val encryptedPrefs = Prefs(getEncryptedSharedPreferences())\n    \n    // (...)\n```\n\nThen declare your `val`s or `var`s using delegates on the `Prefs` instance:\n\n```kotlin\n    var login by prefs.String()\n\n    var password by prefs.String(Key(KEY_PASSWORD))\n    val passwordLiveData by prefs.StringLiveData(Key(KEY_PASSWORD))\n    val passwordFlow by prefs.StringFlow(Key(KEY_PASSWORD))\n\n    var premium by prefs.Boolean(false)\n    var age by prefs.Int()\n    var preferredColor by prefs.Int(0xFF0000)\n    var weekDays by prefs.StringSet(setOf(\"Friday\", \"Saturday\"))\n```\n\nCurrently, the available types are:\n- Boolean\n- Float\n- Integer\n- Long\n- String\n- StringSet\n\nAnd they can be exposed as:\n- Raw type\n- `MutableLiveData`\n- `MutableStateFlow`\n\n👉 If you pass a `default` value, the attribute type will be non nullable.\u003cbr\u003e\nIn the example above, `age` is `Int?` whereas `preferredColor` is `Int`.\n\n👉 Optionally pass a `key` parameter, which will be used\nto store the preference (useful when migrating from already used preferences).\nBy default the attribute name is used.\n\n### 3/ Be a winner!\nSimply use your class like this:\n```kotlin\n    private val mainPrefs by lazy { MainPrefs(this) }\n    // (...)\n    \n    // Get a preference\n    val login = mainPrefs.login\n    \n    // Put a preference\n    mainPrefs.password = \"p4Ssw0Rd\"\n    \n    // Observe a preference, with LiveData\n    mainPrefs.passwordLiveData.observe(this) {\n        log(\"observed password=$it\")\n    }\n\n    // Update a preference, with LiveData\n    mainPrefs.passwordLiveData.value = \"qwerty\"\n\n    // Observe a preference, with Flow\n    mainPrefs.passwordFlow.onEach {\n        Log.d(TAG, \"observed password=$it\")\n    }.launchIn(scope)\n\n    // Update a preference, with Flow\n    mainPrefs.passwordFlow.value = \"zxcvbn\"\n```\n\nYou can also have a look at the [sample](sample/).\n\n## License\n\n```\nCopyright (C) 2019-present Benoit 'BoD' Lubek (BoD@JRAF.org)\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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbod%2Fandroid-kprefs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbod%2Fandroid-kprefs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbod%2Fandroid-kprefs/lists"}