Skip to content

SharedPreference Library to save all types including your custom types and observe them if need be.

Notifications You must be signed in to change notification settings

EhmaUgbogo/EasyPref

Repository files navigation

GitHub release (latest by date) GitHub top language GitHub all releases Twitter Follow

EasyPref

Observable EasyPrefLib

Description:

A SharedPreference Library that can be used to store all types including your custom classes and observe them too if needed.


Features:

  • Easily store primitive types
  • Easily store custom classes
  • Easily observe primitive or custom classes

Setup.

To get a Git project into your build:

  1. Add the JitPack repository to you root build.gradle at the end of repositories for Android Studio before Artic Fox:
allprojects {
    repositories {
	...
	maven { url 'https://jitpack.io' }
    }
}

For Android Studio Artic Fox and later add JitPack repository to you root settings.gradle

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  1. Add the dependency
dependencies {
    implementation 'com.github.EhmaUgbogo:EasyPref:1.0.2'
}

Usage

  1. Initialize EasyPref in your application class or MainActivity
  EasyPref.initialize(this) // If setup in your application class
  EasyPref.initialize(application) // If setup in MainActivity
  1. Use as needed
  EasyPref.hasKey(key)
  
  EasyPref.deleteKey(key)

  // Store primitive types

  EasyPref.putBoolean(key, boolValue)
  EasyPref.getBoolean(key)  // ... Same for Strings, Int, etc 
  
  
  // Store custom types
  
  EasyPref.putObject(OBJ_KEY, yourData)
  EasyPref.getObject(OBJ_KEY, YourData::class.java)
  
  1. Observe as needed
  
  EasyPref.observeBoolean(key).observe(this@MainActivity) {
       // your code here
  }
  
  // observe custom types - returns (PrefObjectHolder<T>)
  
  EasyPref.observeObject(OBJ_KEY, YourData::class.java).observe(viewLifecycleOwner) {
      it.value?.let { yourData -> showSnackBar("$yourData Observed") }
  }
 
  

Licence

MIT Licence

Copyright (c) [2021] Ehma Ugbogo

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Author Info

@EhmaUgbogo

Back To The Top

About

SharedPreference Library to save all types including your custom types and observe them if need be.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages