[Android Library] A SharedPreferences helper library to save and fetch the values easily.

Overview

Preference Helper

A SharedPreferences helper library to save and fetch the values easily.

Featured in

AndroidDev Digest

Use in your project

Add this to your module's build.gradle

repositories {
    jcenter()
}

dependencies {
    implementation 'com.naveentp:preferencehelper:0.0.1'
}

Usage Example

  • Initialise the SharedPreferences

    • Default SharedPreferences

      PreferenceHelper.defaultPref(this)

      or Lazy loading

      private val sharedPref: SharedPreferences by lazy {
          PreferenceHelper.customPref(this, "Sample")
      }
    • Custom SharedPreferences

      PreferenceHelper.customPref(this, "YOUR_SHARED_PREFERENCES_NAME")
  • Save / Fetch values from SharedPreferences

    • Using operator overloading method

      • Save

        // To save the value to SharedPreferences
        sharedPref["name"] = "Naveen" 
        
      • Fetch

        // To fetch the value from SharedPreferences
        val userName = sharedPref["name", DEFAULT_VALUE] 
        
    • Using Delegation properties method

      • Initialisation

        // Create userName variable that corresponds to sharedPreference "name" key
        var name by PreferenceDelegate(sharedPref, "name", DEFAULT_VALUE)
        
      • Save

        //Saves value "Naveen" to sharedPreference with key "name"
        name = "Naveen"
      • Fetch

        // To fetch the value "Naveen" from SharedPreferences
        val userName = name
        
    • Adding Listeners

      • Individual

        var userAge by PreferenceDelegate(sharedPref, "name", 0, object : PreferenceObserver {
            override fun <T> onChangePreferenceValue(key: String, value: T) {
                print("$key: $value")
            }
        })
      • Global (Activity level)

        class MainActivity : AppCompatActivity(), PreferenceObserver {
            
            //Lazy initialisation of SharedPreferences
            private val sharedPref: SharedPreferences by lazy {
                PreferenceHelper.customPref(this, "Sample")
            }
            
            override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
                setContentView(R.layout.activity_main)
                
                //Pass activity reference handle callbacks all at one place.
                var userAge by PreferenceDelegate(sharedPref, "name", 0, this)
            }
            
            
            // Global listener which listens to all the PreferenceDelegates
            override fun <T> onChangePreferenceValue(key: String, value: T) {
                print("$key: $value")
            }
        }

    ** Refer MainActivity in Sample project for more samples

License

Licensed under Apache Licence, Version 2.0. Check the full licence here

You might also like...
A Kotlin Multiplatform and Compose template that allows you to easily set up your project targeting: Android, Desktop, and Web

A Kotlin Multiplatform and Compose template that allows you to easily set up your project targeting: Android, Desktop, and Web

Events Calendar is a user-friendly library that helps you achieve a cool Calendar UI with events mapping. You can customise every pixel of the calendar as per your wish and still achieve in implementing all the functionalities of the native android calendar in addition with adding dots to the calendar which represents the presence of an event on the respective dates. It can be done easily, you are just a few steps away from implementing your own badass looking Calendar for your very own project! This library is a set of simple wrapper classes that are aimed to help you easily access android device information.
This library is a set of simple wrapper classes that are aimed to help you easily access android device information.

SysInfo Simple, single class wrapper to get device information from an android device. This library provides an easy way to access all the device info

Kotlin and Ktor app, which can easily be deployed to Heroku

[ 🚧 Work in progress 👷‍♀️ ⛏ 👷 🔧️ 👷 🔧 🚧 ] Shoppe Kotlin Multiplatform App Kotlin and Ktor app, which can easily be deployed to Heroku. This appl

A react-like kotlin library to create an inventory ui easily in paper plugins
A react-like kotlin library to create an inventory ui easily in paper plugins

A react-like kotlin library to create an inventory ui easily in paper plugins

A Gradle plugin to easily publish library components to Maven.
A Gradle plugin to easily publish library components to Maven.

Component Publisher A Gradle plugin to easily publish components based on maven-publish. You can find the latest released plugin on Gradle Plugin Port

With MaterialTimelineView you can easily create a material looking timeline.
With MaterialTimelineView you can easily create a material looking timeline.

MaterialTimelineView With MaterialTimelineView you can easily create a material looking timeline. Setup The library is pushed to jCenter() as an AAR,

Quick route to developer options page easily
Quick route to developer options page easily

QuickRoute Using Quick Settings Tile to navigate to Developer options page without click a lot of buttons. Preview Install Can install from Google Pla

Arrow Endpoint offers a composable Endpoint datatype, that allows us easily define an Endpoint from which we can derive clients, servers & documentation.

Arrow Endpoint Arrow Endpoint offers a composable Endpoint datatype, that allows us easily define an Endpoint from which we can derive clients, server

Releases(v0.0.1)
Owner
Naveen T P
An Android developer.
Naveen T P
The most complete and powerful data-binding library and persistence infra for Kotlin 1.3, Android & Splitties Views DSL, JavaFX & TornadoFX, JSON, JDBC & SQLite, SharedPreferences.

Lychee (ex. reactive-properties) Lychee is a library to rule all the data. ToC Approach to declaring data Properties Other data-binding libraries Prop

Mike 112 Dec 9, 2022
A Kotlin library for reactive and boilerplate-free SharedPreferences in Android

KPreferences A Kotlin library for reactive and boilerplate-free Shared Preferences in Android. With KPreferences you can use Kotlin's marvelous delega

Mohamad Amin Mohamadi 19 Dec 16, 2020
Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties

Easy lightweight SharedPreferences library for Android in Kotlin using delegated properties Idea Delegated properties in Kotlin allow you to execute a

null 25 Dec 27, 2022
Android Library to make SharedPreferences usage easier.

KotlinPreferences Kotlin Android Library, that makes preference usage simple and fun. KotlinPreferences now have a brother. With KotlinPreferences, yo

Marcin Moskała 50 Nov 6, 2022
Kotpref - Android SharedPreferences delegation library for Kotlin

Kotpref Android SharedPreference delegation for Kotlin. Install repositories { mavenCentral() } dependencies { // core implementation 'co

Takao Chiba 684 Dec 22, 2022
Adapter library for SharedPreferences

EasyPrefs Adapter library for SharedPreferences which reduces boilerplate needed to store simple data, but open enough to not interfere with your own

Kacper Wojciechowski 6 Nov 23, 2021
Android app to fetch closed pull request of any public repo

Pullr Android app to fetch closed pull request of any public repo ?? Features Co

Sonu Sourav 0 Dec 26, 2021
Fetch random dog images

RandomDogImages Show Random Dog Images Demo application built in 100% Kotlin and implementing some Jetpack Components. This app was developed for as a

ValentineRutto 5 Dec 7, 2022
AppCode helper for Kotlin/Native and Xcode

Kotlin Xcode compatibility Gradle plugin The plugin is used by AppCode to set up Kotlin/Native project along with Xcode Sources A multi-build sample w

Kotlin 21 Oct 23, 2022
TensorFlow Lite Helper for Android to help getting started with TesnorFlow.

TensorFlow Lite Helper for Android This library helps with getting started with TensorFlow Lite on Android. Inspired by TensorFlow Lite Android image

Ahmed Gamal 26 Nov 19, 2022