Kotlin Preferences Extensions

Additional

Language
Kotlin
Version
N/A
Created
Jul 4, 2017
Updated
Sep 11, 2017
Owner
Mohammad Mirrajabi (mirrajabi)
Contributor
Mohammad Mirrajabi (mirrajabi)
1
Activity
Badge
Generate
Download
Source code

Advertisement

kotlin-preferences-extensions

A set of Rx Extensions to get/set values of SharedPreferences in a super simple way

Usage

First add jitpack to your projects build.gradle file

repositories {
    maven { url 'https://jitpack.io' }
}

Then add the dependency in modules build.gradle file

dependencies {
    compile 'com.github.mirrajabi:kotlin-preferences-extensions:1.0.1'
}

Now initialize it in your application or activity etc...

override fun onCreate() {
        super.onCreate()
        KotlinPreferences.init(this)
        // Or with your custom preference name
        KotlinPreferences.init(this, "custom_pref")
    }

Then use these extensions on the key strings like below samples

Sample

companion object {
    val keyForString = "key_some_string"
}

keyForString.putString("Kotlin extensions")
            .subscribe()
            
"another_key".putBoolean(true)
             .subscribe({
                Log.v(packageName, "another key has been saved with $it value")
             })

keyForString.getString("No records for this key")
            .subscribe({ textView.text = it })
            
"another_key".getBoolean()
             .subscribe({
                Log.v(packageName, "Saved boolean value is $it")
             })