{"id":13902347,"url":"https://github.com/tommus/ktx-prefs","last_synced_at":"2025-04-14T19:13:17.057Z","repository":{"id":57716352,"uuid":"169000678","full_name":"tommus/ktx-prefs","owner":"tommus","description":"Shared preferences made easy. With a little bit of reactive extensions.","archived":false,"fork":false,"pushed_at":"2020-09-28T21:30:46.000Z","size":322,"stargazers_count":9,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T19:13:11.687Z","etag":null,"topics":["android","annotation-processor","annotations","compile-time","kotlin","kotlin-library","sharedpreferences","utility"],"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/tommus.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-03T22:26:22.000Z","updated_at":"2023-11-23T10:00:33.000Z","dependencies_parsed_at":"2022-08-26T09:30:24.822Z","dependency_job_id":null,"html_url":"https://github.com/tommus/ktx-prefs","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tommus%2Fktx-prefs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tommus%2Fktx-prefs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tommus%2Fktx-prefs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tommus%2Fktx-prefs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tommus","download_url":"https://codeload.github.com/tommus/ktx-prefs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248943461,"owners_count":21186958,"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","annotation-processor","annotations","compile-time","kotlin","kotlin-library","sharedpreferences","utility"],"created_at":"2024-08-06T22:01:06.794Z","updated_at":"2025-04-14T19:13:17.033Z","avatar_url":"https://github.com/tommus.png","language":"Kotlin","funding_links":[],"categories":["Kotlin"],"sub_categories":[],"readme":"# Ktx Preferences\n\n[![Maven Central][mavenbadge-svg]][mavencentral] [![Travis (.org) branch][travisci-svg]][travisci] [![API][apibadge-svg]][apioverview] [![GitHub][license-svg]][license]\n\nThis library incorporates annotation processing to ensure the compile time verification for user-defined shared\npreferences.\n\n## Usage\n\n### Add dependencies\n\nAdd dependencies to the *Kotlin-based* project:\n\n```groovy\ndependencies {\n    implementation \"co.windly:ktx-prefs:1.5.0\"\n    kapt \"co.windly:ktx-prefs-compiler:1.5.0\"\n}\n```\n\n### Define shared preferences\n\nUse the `@Prefs` annotation on any POJO. All (non static) fields will be considered a preference.\n\nMinimal example:\n\n```kotlin\n@Prefs(value = \"UserCachePreferences\")\nclass UserCache(\n\n  //region Id\n\n  @DefaultLong(value = 0L)\n  internal val id: Long,\n\n  //endregion\n\n  //region Name\n\n  @DefaultString(value = \"\")\n  internal val firstName: String,\n\n  @DefaultString(value = \"\")\n  internal val lastName: String,\n\n  @DefaultString(value = \"\")\n  internal val password: String,\n\n  //endregion\n\n  //region Active\n\n  @DefaultBoolean(value = false)\n  internal val active: Boolean\n\n  //endregion\n)\n```\n\nAccepted shared preference field types are:\n\n* Boolean\n* Float\n* Integer\n* Long\n* String\n\n### Use generated wrapper class\n\nA class named `\u003cYourClassName\u003ePrefs` will be generated in the same package (at compile time).  Use it like this:\n\n```kotlin\n// Get access to shared preferences wrapper.\nval cache = UserCachePrefs.get(this)\n\n// ...or using an extension method.\n// val cache = requireUserCache()\n\n// Put a single value (apply() is automatically called).\ncache\n  .putId(1L)\n\n// Put several values in one transaction.\ncache\n  .edit()\n  .putFirstName(\"John\")\n  .putLastName(\"Snow\")\n  .putPassword(\"WinterIsComing\")\n  .putActive(true)\n  .commit()\n\n// Check if a value is set.\nif (cache.containsFirstName()) {\n  Log.d(TAG, \"First name is set.\")\n}\n\n// Access preferences one by one.\nLog.d(TAG, \"id -\u003e \" + cache.getId())\nLog.d(TAG, \"first name -\u003e \" + cache.getFirstName())\nLog.d(TAG, \"last name -\u003e \" + cache.getLastName())\nLog.d(TAG, \"password -\u003e \" + cache.getPassword())\nLog.d(TAG, \"active -\u003e \" + cache.isActive())\n\n// Access all preferences.\nLog.d(TAG, \"cache -\u003e \" + cache.getAll())\n\n// Remove a value.\ncache.removeFirstName()\n\n// Clear all preferences.\ncache.clear()\n```\n\n### Extension methods\n\nFor all classes annotated with `@Prefs` there would be `\u003cYourClassName\u003eExt` file generated which contains\ntwo extension methods that simplifies accessing generated shared preferences wrapper.\n\nAbove mentioned extension methods will be called `require\u003cYourClassName\u003e()` like:\n\n```kotlin\ninline fun Context.requireUserCache(): UserCachePrefs =\n    UserCachePrefs.get(this)\n\ninline fun Fragment.requireUserCache(): UserCachePrefs =\n    requireContext().requireUserCache()\n``` \n\n### Reactive Extensions\n\nLibrary supports generation of reactive methods by default. You can disable this feature either by:\n\n- annotating class with `@Reactive(value = false)`,\n- annotating field with `@Reactive(value = false)`\n\nAll shared property changes are emitted to given stream using `distinctUntilChanged()` method. You can configure this\nbehavior in `@Reactive` annotation (property `distinctUntilChanged`) for entire class or for each field separately.\n\n## License\n\n    Copyright 2019 Tomasz Dzieniak\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n\n[apibadge-svg]: https://img.shields.io/badge/API-14%2B-brightgreen.svg?color=97ca00\n[apioverview]: https://developer.android.com/about/versions/android-4.0\n[license-svg]: https://img.shields.io/github/license/tommus/ktx-prefs.svg?color=97ca00\n[license]: http://www.apache.org/licenses/LICENSE-2.0\n[mavenbadge-svg]: https://img.shields.io/maven-central/v/co.windly/ktx-prefs.svg?color=97ca00\n[mavencentral]: https://search.maven.org/artifact/co.windly/ktx-prefs\n[travisci-svg]: https://img.shields.io/travis/tommus/ktx-prefs/master.svg?color=97ca00\n[travisci]: https://travis-ci.org/tommus/ktx-prefs\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftommus%2Fktx-prefs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftommus%2Fktx-prefs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftommus%2Fktx-prefs/lists"}