RxPreferences
An RxJava wrapper for Android's SharedPreferences
Install / Use
/learn @drewhamilton/RxPreferencesREADME
RxPreferences
An RxJava wrapper for Android's SharedPreferences.
Download
RxPreferences is available in Maven Central.
// RxPreferences:
implementation "dev.drewhamilton.rxpreferences:rxpreferences:$version"
// With Kotlin extensions:
implementation "dev.drewhamilton.rxpreferences:rxpreferences-ktx:$version"
// Dagger component:
implementation "dev.drewhamilton.rxpreferences:rxpreferences-dagger:$version"
Usage
RxPreferences
Get the current value of any preference:
rxPreferences.getIntOnce("Language count", 0)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(count -> displayCount(count));
Observe the initial value plus any changes to a preference:
rxPreferences.getStringStream("Favorite language", "None")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(name -> displayBestFriend(name));
Edit preferences and monitor the completion of committing those changes:
rxPreferences.edit()
.putInt("Language count", 1)
.putString("Favorite language", "Java")
.commit()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> showSavedIndicator(), error -> displayError(error));
Kotlin extensions
With Kotlin extensions, use the Editor as a receiver:
rxPreferences.edit {
putInt("Language count", 2)
putString("Favorite language", "Kotlin")
}.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ showSavedIndicator() }, { error -> displayError(error) })
Dagger component
Easily provide and access RxPreferences instances using RxPreferencesComponent:
@Module
object PersistenceModule {
private const val sharedPreferencesName = "example.SharedPreferences"
private val ExampleApplication.sharedPreferences
get() = getSharedPreferences(sharedPreferencesName, Context.MODE_PRIVATE)
@JvmStatic
@Provides
fun preferencesComponent(application: ExampleApplication) =
RxPreferencesComponent.create(application.sharedPreferences)
@JvmStatic
@Provides
@Reusable
fun preferences(preferencesComponent: RxPreferencesComponent) =
preferencesComponent.rxPreferences()
}
License
Copyright 2018 Drew Hamilton
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Related Skills
node-connect
347.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
