{"id":16895106,"url":"https://github.com/afollestad/rxkprefs","last_synced_at":"2025-07-14T07:32:58.916Z","repository":{"id":54783968,"uuid":"147118926","full_name":"afollestad/rxkprefs","owner":"afollestad","description":"🛠 A small Kotlin library to make shared preferences easy + RxJava and Coroutines support","archived":false,"fork":false,"pushed_at":"2021-02-21T05:05:49.000Z","size":224,"stargazers_count":277,"open_issues_count":3,"forks_count":16,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-05-20T04:02:58.887Z","etag":null,"topics":["android","kotlin","reactive","rx","rxjava","rxkotlin","settings","sharedpreferences","storage"],"latest_commit_sha":null,"homepage":"https://af.codes","language":"Kotlin","has_issues":false,"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/afollestad.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"afollestad","ko_fi":"afollestad"}},"created_at":"2018-09-02T20:22:17.000Z","updated_at":"2025-01-07T03:28:13.000Z","dependencies_parsed_at":"2022-08-14T02:51:24.582Z","dependency_job_id":null,"html_url":"https://github.com/afollestad/rxkprefs","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"purl":"pkg:github/afollestad/rxkprefs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afollestad%2Frxkprefs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afollestad%2Frxkprefs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afollestad%2Frxkprefs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afollestad%2Frxkprefs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/afollestad","download_url":"https://codeload.github.com/afollestad/rxkprefs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/afollestad%2Frxkprefs/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265255355,"owners_count":23735242,"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","kotlin","reactive","rx","rxjava","rxkotlin","settings","sharedpreferences","storage"],"created_at":"2024-10-13T17:21:18.311Z","updated_at":"2025-07-14T07:32:58.882Z","avatar_url":"https://github.com/afollestad.png","language":"Kotlin","funding_links":["https://github.com/sponsors/afollestad","https://ko-fi.com/afollestad"],"categories":[],"sub_categories":[],"readme":"# RxkPrefs\n\nThis library provides reactive shared preferences interaction with very little code. It is \ndesigned specifically to be used with Kotlin.\n\n\u003cimg src=\"https://raw.githubusercontent.com/afollestad/rxkprefs/master/showcase2.png\" width=\"500\" /\u003e\n\nInspiration has been taken from other libraries, but it was written from the ground up on its own.\n\n[![Android CI](https://github.com/afollestad/rxkprefs/workflows/Android%20CI/badge.svg)](https://github.com/afollestad/rxkprefs/actions?query=workflow%3A%22Android+CI%22)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/4b70e396d3c549d28bbb6373885200a0)](https://www.codacy.com/app/drummeraidan_50/rxkprefs?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=afollestad/rxkprefs\u0026amp;utm_campaign=Badge_Grade)\n[![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg?style=flat)](https://www.apache.org/licenses/LICENSE-2.0.html)\n\n---\n\n# Gradle Dependency\n\n[ ![Core](https://img.shields.io/maven-central/v/com.afollestad.rxkprefs/core?style=flat\u0026label=Core) ](https://repo1.maven.org/maven2/com/afollestad/rxkprefs)\n\nAdd this to your module's `build.gradle` file:\n\n```gradle\ndependencies {\n\n  implementation \"com.afollestad.rxkprefs:core:2.0.3\"\n}\n```\n\n---\n\n# Getting Started\n\nThe core of the library is the `RxkPrefs` interface. You can retrieve an instance of this interface \n with the `rxkPrefs` method, which takes 3 parameters. One of these parameters is optional \n (the shared preferences mode).\n\n```kotlin\n// Parameter is your Context, like an Activity, uses PreferenceManager#getDefaultSharedPreferences\nval myPrefs = rxkPrefs(this)\n\n// First parameter is your Context, like an Activity, the second is a key.\nval myPrefs = rxkPrefs(this, \"my_prefs\")\n\n// The optional third parameter is a mode, it defaults to MODE_PRIVATE above.\n// This is like using Context.getSharedPreferences(\"my_prefs\", MODE_PRIVATE)\nval myPrefs = rxkPrefs(this, \"my_prefs\", MODE_PRIVATE)\n```\n\n### Retrieving a Preference\n\nWith a `RxkPrefs` instance, you can retrieve preferences. By that, I do not mean the raw \nvalue of the preference, but an instance of the `Pref` interface which provides more functionality.\n\n```kotlin\nval myPrefs: RxkPrefs = // ...\n\n// Getting a string preference is as simple as this:\nval myString: Pref\u003cString\u003e = myPrefs.string(\"my_string\", \"default_value\")\n\n// You could omit the second parameter to use the default, default value (empty string)\nval myString: Pref\u003cString\u003e = myPrefs.string(\"my_string\")\n```\n\n### Interacting with a Preference\n\nOnce you have a reference to a preference, there are a few things \nyou can do with them.\n\n```kotlin\nval myPref: Pref\u003cInt\u003e = // ...\n\n// The key of the preference - first parameter passed in prefs.integer(...) or any other pref getter\n// This is always a String.\nval key: String = myPref.key()\n\n// The default value of the preference - second parameter passed in prefs.integer(...) or any other pref getter...\n// Or the primitive default, such as an empty string, 0, or false.\nval defaultValue: Int = myPref.defaultValue()\n\n// The current value of the preference, or the default value if none.\nval currentValue: Int = myPref.get()\n\n// Changes the value of the preference.\nmyPref.set(1024)\n\n// True if a value has been set, otherwise false.\nval isSet: Boolean = myPref.isSet()\n\n// Deletes any existing value for the preference.\nmyPref.delete()\n\n// These are used by the RxJava and coroutines extensions, but you may find them useful.\nmyPref.addOnChangedListener { }\nmyPref.addOnDestroyedListener { }\n\n// Destroys the instance, clearing listeners and anything that could leak memory.\nmyPref.destroy()\n```\n\n---\n\n# Coroutines Extension\n\n### Gradle Dependency\n\n[ ![Coroutines](https://img.shields.io/maven-central/v/com.afollestad.rxkprefs/coroutines?style=flat\u0026label=Coroutines) ](https://repo1.maven.org/maven2/com/afollestad/rxkprefs/coroutines)\n\nAdd this to your module's `build.gradle` file:\n\n```gradle\ndependencies {\n    \n  implementation \"com.afollestad.rxkprefs:coroutines:2.0.3\"\n}\n```\n\n### As a Flow\n\nYou can receive changes to a preference in real-time using a coroutines `Flow`, specifically a hot \nflow.\n\n```kotlin\nval myPref: Pref\u003cBoolean\u003e = // ...\nval flow: Flow\u003cBoolean\u003e = myPref.asFlow()\n\n// One way...\nscope.launch {\n  flow.collect { println(it) }\n}\n\n// Another way...\nflow\n  .onEach { println(it) }\n  .launchIn(scope)\n\n```\n\n---\n\n# RxJava Extension\n\n### Gradle Dependency\n\n[ ![RxJava](https://img.shields.io/maven-central/v/com.afollestad.rxkprefs/rxjava?style=flat\u0026label=RxJava) ](https://repo1.maven.org/maven2/com/afollestad/rxkprefs/rxjava)\n\nAdd this to your module's `build.gradle` file:\n\n```gradle\ndependencies {\n    \n  implementation \"com.afollestad.rxkprefs:rxjava:2.0.3\"\n}\n```\n\n### As an Observable\n\nYou can receive changes to a preference in real-time using an RxJava \nObservable.\n\n```kotlin\nval myPref: Pref\u003cLong\u003e = // ...\nval obs: Observable\u003cLong\u003e = myPref.observe()\n\nval disposable = obs.subscribe { newValue -\u003e\n  // use new value\n}\n// when you no longer want to receive values\nsub.dispose()\n```\n\nFurther usage of this is more of an RxJava issue and less specific to \nthis library. You should have a basic understanding of what you can do \nwith RxJava and what its use cases are.\n\n### As a Consumer\n\n`Pref` can act as an RxJava `Consumer`. You can use this to save preference values \nfrom the emissions of an Observable.\n\nSay you're using [RxBinding](https://github.com/JakeWharton/RxBinding) \nto bind Android views to Observables that emit when their value changes, \nsuch as a CheckBox:\n\n```kotlin\nval myPref: Pref\u003cBoolean\u003e = // ...\n\nRxCompoundButton.checks(yourCheckboxView)\n  .subscribe(myPref.asConsumer())\n``` \n\nWhenever the checkbox is checked or unchecked, the underlying \nboolean shared preference is set to true or false automatically.\n\nBasically, it works like this:\n\n```kotlin\nval myObs: Observable\u003cString\u003e = // ...\nval myConsumer: Consumer\u003cString\u003e = // ...which can be from an instance of Pref\n\nmyObs.subscribe(myConsumer)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fafollestad%2Frxkprefs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fafollestad%2Frxkprefs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fafollestad%2Frxkprefs/lists"}