# rx-preferences **Repository Path**: HarmonyOS-tpc/rx-preferences ## Basic Information - **Project Name**: rx-preferences - **Description**: Reactive SharedPreferences for OpenHarmony. - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 5 - **Forks**: 1 - **Created**: 2021-04-01 - **Last Updated**: 2023-04-17 ## Categories & Tags **Categories**: harmonyos-toolkit **Tags**: None ## README ## RxPreferences Reactive `SharedPreferences` for OpenHarmony. ## Usage Solution 1: local har package integration 1.Add the har package to the lib folder. 2.Add the following code to gradle of the entry: ```gradle implementation fileTree(dir: 'libs', include: ['.jar', '.har']) ``` Solution 2: ```gradle allprojects{ repositories{ mavenCentral() } } implementation 'io.openharmony.tpc.thirdlib:rx-preferences:1.0.3' ``` ## entry运行要求 通过DevEco studio,并下载OpenHarmonySDK 将项目中的build.gradle文件中dependencies→classpath版本改为对应的版本(即你的IDE新建项目中所用的版本) ## How to use Create an `RxSharedPreferences` instance which wraps a `SharedPreferences`: ```java Preferences preferences = new DatabaseHelper(this).getPreferences("rx_preferences"); RxSharedPreferences rxPreferences = RxSharedPreferences.create(preferences); ``` *Hint: Keep a strong reference on your `RxSharedPreferences` instance for as long as you want to observe them to prevent listeners from being GCed.* Create individual `Preference` objects: ```java Preference username = rxPreferences.getString("username"); Preference showWhatsNew = rxPreferences.getBoolean("show-whats-new", true); ``` Observe changes to individual preferences: ```java username.asObservable().subscribe(new Action1() { @Override public void call(String username) { Log.d(TAG, "Username: " + username); } } ``` Subscribe preferences to streams to store values: ```java RxAbsButton.checkedStateChanges(showWhatsNewView) .subscribe(showWhatsNew.asConsumer()); ``` *(Note: `RxAbsButton` is from [RxBinding][1])* ## License Copyright 2014 Prateek Srivastava 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. [1]: https://gitee.com/openharmony-tpc/RxBinding [2]: http://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=com.f2prateek.rx.preferences&a=rx-preferences&v=LATEST