├── .circleci └── config.yml ├── .gitignore ├── .idea ├── .gitignore ├── codeStyles │ ├── Project.xml │ └── codeStyleConfig.xml ├── compiler.xml ├── dictionaries │ └── jintinlin.xml ├── gradle.xml ├── inspectionProfiles │ └── Project_Default.xml ├── jarRepositories.xml ├── misc.xml └── vcs.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── jintin │ │ └── preferencesextension │ │ └── app │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── jintin │ │ │ └── preferencesextension │ │ │ └── app │ │ │ ├── MainActivity.kt │ │ │ └── MainViewModel.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── values-night │ │ └── themes.xml │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── themes.xml │ └── test │ └── java │ └── com │ └── jintin │ └── preferencesextension │ └── app │ └── ExampleUnitTest.kt ├── build.gradle ├── flow ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── jintin │ │ └── preferencesextension │ │ └── flow │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ └── java │ │ └── com │ │ └── jintin │ │ └── preferencesextension │ │ └── flow │ │ └── PreferenceFlow.kt │ └── test │ └── java │ └── com │ └── jintin │ └── preferencesextension │ └── flow │ └── ExampleUnitTest.kt ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── preferences ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── jintin │ │ └── preferencesextension │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ └── java │ │ └── com │ │ └── jintin │ │ └── preferencesextension │ │ ├── PreferenceLiveData.kt │ │ └── PreferencesExtension.kt │ └── test │ └── java │ └── com │ └── jintin │ └── preferencesextension │ └── ExampleUnitTest.kt ├── rxjava ├── .gitignore ├── build.gradle ├── consumer-rules.pro ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── jintin │ │ └── preferencesextension │ │ └── rxjava3 │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ └── java │ │ └── com │ │ └── jintin │ │ └── preferencesextension │ │ └── rxjava3 │ │ └── PreferenceObservable.kt │ └── test │ └── java │ └── com │ └── jintin │ └── preferencesextension │ └── rxjava3 │ └── ExampleUnitTest.kt └── settings.gradle /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | 3 | orbs: 4 | android: circleci/android@0.2.1 5 | 6 | jobs: 7 | build: 8 | executor: android/android 9 | 10 | steps: 11 | - checkout 12 | - run: 13 | command: ./gradlew build 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | local.properties 16 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 20 | 22 | 23 | 135 | 136 | 138 | 139 | -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/dictionaries/jintinlin.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | jintin 5 | preferencesextension 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 24 | 25 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | 29 | 30 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Jintin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PreferencesExtension 2 | 3 | [![CircleCI](https://circleci.com/gh/Jintin/PreferencesExtension.svg?style=shield)](https://circleci.com/gh/Jintin/PreferencesExtension) 4 | [![](https://jitpack.io/v/Jintin/PreferencesExtension.svg)](https://jitpack.io/#Jintin/PreferencesExtension) 5 | 6 | Extension for Android `SharePreferences`, provide easier usage and LiveData style of change listener. 7 | 8 | ## Install 9 | 10 | Add [Jitpack](https://jitpack.io/) repository to your root `build.grable`: 11 | ```groovy 12 | allprojects { 13 | repositories { 14 | ... 15 | maven { url 'https://jitpack.io' } 16 | } 17 | } 18 | ``` 19 | 20 | Then add dependency in your module `build.gradle`: 21 | ```groovy 22 | dependencies { 23 | // Base module and LiveData usage 24 | implementation 'com.github.Jintin.PreferencesExtension:preferences:1.3.0' 25 | // RxJava3 usage 26 | implementation 'com.github.Jintin.PreferencesExtension:rxjava:1.3.0' 27 | // Flow usage 28 | implementation 'com.github.Jintin.PreferencesExtension:flow:1.3.0' 29 | 30 | } 31 | ``` 32 | 33 | ## Usage 34 | 35 | 1. Provide reified type usage: 36 | ```kotlin 37 | val value = preference.get(MY_KEY) 38 | // or 39 | val value: String = preference.get(MY_KEY) 40 | ``` 41 | 42 | 2. `LiveData` style to get preference modify event: 43 | ```kotlin 44 | val liveData = preference.liveData(MY_KEY) 45 | 46 | liveData.observe(this) { 47 | // get update here 48 | } 49 | ``` 50 | 51 | 3. `RxJava3` style to get preference modify event: 52 | ```kotlin 53 | val observable = preference.observable(MY_KEY) 54 | 55 | observable.subscribe { 56 | // get update here 57 | } 58 | ``` 59 | 60 | 4. `Flow` style to get preference modify event: 61 | ```kotlin 62 | val flow = preference.flow(MY_KEY) 63 | 64 | flow.collect { 65 | // get update here 66 | } 67 | ``` 68 | 69 | You can go to `./app` module to see the full example and get more information. 70 | 71 | ## Contributing 72 | Bug reports and pull requests are welcome on GitHub at [https://github.com/Jintin/PreferencesExtension](https://github.com/Jintin/PreferencesExtension). 73 | 74 | ## License 75 | The package is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). 76 | 77 | [![Buy Me A Coffee](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/jintin) 78 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'kotlin-android' 4 | } 5 | 6 | android { 7 | compileSdkVersion 30 8 | buildToolsVersion "30.0.2" 9 | 10 | defaultConfig { 11 | applicationId "com.jintin.preferencesextension.app" 12 | minSdkVersion 16 13 | targetSdkVersion 30 14 | versionCode 1 15 | versionName "1.0" 16 | 17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 18 | } 19 | buildFeatures { 20 | viewBinding true 21 | } 22 | buildTypes { 23 | release { 24 | minifyEnabled false 25 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 26 | } 27 | } 28 | compileOptions { 29 | sourceCompatibility JavaVersion.VERSION_1_8 30 | targetCompatibility JavaVersion.VERSION_1_8 31 | } 32 | kotlinOptions { 33 | jvmTarget = '1.8' 34 | } 35 | } 36 | 37 | dependencies { 38 | implementation "androidx.preference:preference-ktx:1.1.1" 39 | implementation 'io.reactivex.rxjava3:rxjava:3.0.7' 40 | // implementation 'com.github.Jintin.PreferencesExtension:preferences:master-SNAPSHOT' 41 | // implementation 'com.github.Jintin.PreferencesExtension:rxjava:master-SNAPSHOT' 42 | // implementation 'com.github.Jintin.PreferencesExtension:flow:master-SNAPSHOT' 43 | implementation project(":preferences") 44 | implementation project(':rxjava') 45 | implementation project(':flow') 46 | 47 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 48 | implementation 'androidx.core:core-ktx:1.3.2' 49 | implementation 'androidx.appcompat:appcompat:1.2.0' 50 | implementation 'com.google.android.material:material:1.2.1' 51 | implementation 'androidx.constraintlayout:constraintlayout:2.0.4' 52 | testImplementation 'junit:junit:4.13.1' 53 | androidTestImplementation 'androidx.test.ext:junit:1.1.2' 54 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' 55 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/androidTest/java/com/jintin/preferencesextension/app/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.jintin.preferencesextension.app 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.jintin.preferencesextension.app", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/jintin/preferencesextension/app/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.jintin.preferencesextension.app 2 | 3 | import android.content.Intent 4 | import android.os.Bundle 5 | import androidx.activity.viewModels 6 | import androidx.appcompat.app.AppCompatActivity 7 | import com.jintin.preferencesextension.app.databinding.ActivityMainBinding 8 | 9 | class MainActivity : AppCompatActivity() { 10 | 11 | private val viewModel by viewModels() 12 | 13 | override fun onCreate(savedInstanceState: Bundle?) { 14 | super.onCreate(savedInstanceState) 15 | 16 | ActivityMainBinding.inflate(layoutInflater).let { binding -> 17 | setContentView(binding.root) 18 | 19 | binding.submit.setOnClickListener { 20 | viewModel.setPreference(binding.newValue.text.toString()) 21 | } 22 | binding.nextPage.setOnClickListener { 23 | startActivity(Intent(this, MainActivity::class.java)) 24 | } 25 | viewModel.preferenceLiveData.observe(this) { 26 | println("get update from liveData : $it") 27 | binding.display.text = getString(R.string.display_text, it) 28 | } 29 | } 30 | 31 | } 32 | } -------------------------------------------------------------------------------- /app/src/main/java/com/jintin/preferencesextension/app/MainViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.jintin.preferencesextension.app 2 | 3 | import android.app.Application 4 | import androidx.core.content.edit 5 | import androidx.lifecycle.AndroidViewModel 6 | import androidx.lifecycle.viewModelScope 7 | import androidx.preference.PreferenceManager 8 | import com.jintin.preferencesextension.flow.flow 9 | import com.jintin.preferencesextension.liveData 10 | import com.jintin.preferencesextension.rxjava3.observable 11 | import io.reactivex.rxjava3.disposables.Disposable 12 | import kotlinx.coroutines.flow.collect 13 | import kotlinx.coroutines.launch 14 | 15 | class MainViewModel(application: Application) : AndroidViewModel(application) { 16 | private val preference = PreferenceManager.getDefaultSharedPreferences(application) 17 | private val disposable: Disposable 18 | 19 | val preferenceLiveData = preference.liveData(MY_KEY) 20 | 21 | init { 22 | disposable = triggerObservable() 23 | viewModelScope.launch { 24 | triggerFlow() 25 | } 26 | } 27 | 28 | private fun triggerObservable(): Disposable = 29 | preference.observable(MY_KEY).subscribe { 30 | println("get update from observable : $it") 31 | } 32 | 33 | private suspend fun triggerFlow() { 34 | preference.flow(MY_KEY).collect { 35 | println("get update from flow : $it") 36 | } 37 | } 38 | 39 | override fun onCleared() { 40 | super.onCleared() 41 | disposable.dispose() 42 | } 43 | 44 | fun setPreference(value: String) { 45 | preference.edit { 46 | putString(MY_KEY, value) 47 | } 48 | } 49 | 50 | companion object { 51 | const val MY_KEY = "MY_KEY" 52 | } 53 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 21 | 22 | 34 | 35 |