{"id":37026460,"url":"https://github.com/quebin31/preferences-helper","last_synced_at":"2026-01-14T03:04:52.261Z","repository":{"id":44760238,"uuid":"512980691","full_name":"quebin31/preferences-helper","owner":"quebin31","description":"Minimal add-on library which provides a nicer API  for Preferences Datastore","archived":false,"fork":false,"pushed_at":"2022-07-16T15:27:26.000Z","size":131,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2023-07-13T17:45:59.134Z","etag":null,"topics":["android","datastore-android","datastore-preferences","jetpack","jetpack-android","kotlin","kotlin-android"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/quebin31.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2022-07-12T03:05:56.000Z","updated_at":"2023-07-13T17:45:59.135Z","dependencies_parsed_at":"2022-08-29T22:51:13.536Z","dependency_job_id":null,"html_url":"https://github.com/quebin31/preferences-helper","commit_stats":null,"previous_names":[],"tags_count":1,"template":null,"template_full_name":null,"purl":"pkg:github/quebin31/preferences-helper","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quebin31%2Fpreferences-helper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quebin31%2Fpreferences-helper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quebin31%2Fpreferences-helper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quebin31%2Fpreferences-helper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/quebin31","download_url":"https://codeload.github.com/quebin31/preferences-helper/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quebin31%2Fpreferences-helper/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28408800,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T01:52:23.358Z","status":"online","status_checked_at":"2026-01-14T02:00:06.678Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","datastore-android","datastore-preferences","jetpack","jetpack-android","kotlin","kotlin-android"],"created_at":"2026-01-14T03:04:51.526Z","updated_at":"2026-01-14T03:04:52.242Z","avatar_url":"https://github.com/quebin31.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Preferences Helper\n\nMinimal add-on library that provides a nicer API for [Preferences DataStore](https://developer.android.com/topic/libraries/architecture/datastore#preferences-datastore) \nwhich is more readable and convenient. I tend to write this same wrapper in all my projects and I've got\ntired of doing it manually so I decided to make it a library in case anyone else finds it useful. \n\n## Getting started\nJust include the following dependency in your Gradle script, no need to add any extra repository\nif you're already using the Maven Central repository (which in most cases you'll be using):\n\n```kotlin\nimplementation(\"com.quebin31:preferences-helper:1.0.0\")\n```\n\nFor a comprehensive list of examples head to the [Guide](docs/guide.md).\n\n## Increment counter example\nIn order to see the benefits of using this library let's compare it to Preferences DataStore,\nas per the official Preferences DataStore documentation you'd write the following to update a value:\n\n```kotlin\nval Context.dataStore by preferencesDatastore(name = \"datastore\") // top file declaration\n\nsuspend fun incrementCounter() {\n    val keyCounter = intPreferencesKey(name = \"counter\")\n    context.dataStore.edit { mutablePreferences -\u003e\n        val currentValue = mutablePreferences[keyCounter] ?: 0\n        mutablePreferences[keyCounter] = currentValue + 1\n    }\n}\n```\n\nThat's a lot of boilerplate just to update a value, compare it to what you'd write if you were using \nthis library:\n\n```kotlin\nval Context.dataStore by preferencesDatastore(name = \"datastore\") // top file declaration\n\n// Somewhere you have access to the context, perhaps in a `PreferencesManager` that can be injected\n// with Hilt\nval helper = PreferencesHelper(context.dataStore) \n\nsuspend fun incrementCounter() {\n    val keyCounter = intPreferencesKey(name = \"counter\")\n    helper.update(keyCounter, default = 0) { it + 1 }\n}\n```\n\n## Performance\nBy calling `PreferencesHelper(datastore)` we're just creating a thin wrapper around our preferences\ndatastore, all calls are transformed to the operations you'd normally do if you were using the \n`edit` function directly, though there's an **important catch**, each function declared in the \n`PreferencesHelper` interface is atomic and separated from the others, you don't have to commit \nyour changes, this has the advantage of providing an easy way to update, save, read or delete a value\nin very few lines.\n\nHowever, the disadvantage is that doing multiple separate atomic operations is too expensive, each time \na value in the internal data is updated the whole preferences map is updated, the solution to this is \nusing the `batch` operation whenever you want to update multiple values at once in a single place, \nthis is pretty similar to using `edit` but with the benefit of having scoped operations identical to the\nones found in the `PreferencesHelper` interface:\n\n### Example\n\nInstead of doing this 3 separate atomic operations:\n```kotlin\nhelper.save(keyA, 3)\nhelper.delete(keyB)\nhelper.update(keyC, default = 0) { it + 1 }\n```\n\nYou can use `batch` to reduce the number of atomic operations from 3 to 1:\n```kotlin\nhelper.batch { // single transaction\n    save(keyA, 3)\n    delete(keyB)\n    update(keyC, default = 0) { it + 1 }\n}\n```\n\n## LICENSE\nThis crate is licensed under the terms of the MIT License.\n\nSee [LICENSE](LICENSE) to see the full text.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquebin31%2Fpreferences-helper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fquebin31%2Fpreferences-helper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquebin31%2Fpreferences-helper/lists"}