DPrefs is an Android library which takes care of all your key value shared preferences needs. This library handles everything for you and also encrypts your saved preferences thereby ensuring that your preferences data are securely saved and prevents information theft.
You can save all kinds of values such as:
- String
- Int
- Float
- Boolean
- Long
- Double and
- Custom object
- Import the Library
Import the library to your project by adding the library into your app level build.gradle file as shown below:
dependencies {
implementation "com.github.D-Soft-Tech:DPrefs:$version"
}
Because the library is hosted on jitpack, You also need to add jitpack
to your project if you haven't added it before:
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
- Initialize the library
Initialize the library in the application class, this will make the library have an application wide scope and make the preferences you saved with it to be available throughout the application. This is the recommended way
- Kotlin:
class MyApplicationClass : Application() {
override fun onCreate() {
super.onCreate()
DPrefs.initializeDPrefs(applicationContext)
}
}
- Java:
public class MyApplicationClass extends Application {
@Override
public void onCreate() {
super.onCreate();
DPrefs.INSTANCE.initializeDPrefs(getApplicationContext());
}
}
If for any reason you want the preferences to only have an activity scope, then instead initialize the library in the onCreate()
callback function of the activity as shown below:
- Kotlin:
class MyActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
DPrefs.initializeDPrefs(this)
}
}
- Java:
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DPrefs.INSTANCE.initializeDPrefs(this);
}
}
That is all you need to do in other to set up the library
- To save a value
To save any value of any type you have to callDPrefs
which is the instance of the library exposed to you by the library itself and call the methodput\<DataTypeOfValue\>(key, value)
wherekey
is a string andvalue
is what you want to save and is of type'DataTypeOfValue'
** Hence the Nomenclature the library follows is like so: **
DPrefs::put(key, value)
Note: Thekey
must always be aString
For example:
- to save a string of value
'String value to save'
using the key'STRING_VALUE_KEY'
- Kotlin:
val key = "STRING_VALUE_KEY"
val value = "String value to save"
DPrefs.putString(key, value)
- Java:
String key = "STRING_VALUE_KEY";
String value = "String value to save";
DPrefs.INSTANCE.putString(key, value);
- to save an int value
12345
using the key'INT_VALUE_KEY'
- Kotlin:
val key = "INT_VALUE_KEY"
val value = 12345
DPrefs.putInt(key, value)
- Java:
String key = "INT_VALUE_KEY";
int value = 12345;
DPrefs.INSTANCE.putInt(key, value);
The above two examples show the convention that saving a preference using the DPrefs
library follows. The only exception to this is putting an object and this is as shown below:
- to save an object of type UserInfo shown in the json payload below:
{
"firstName": "Adebayo",
"lastName": "Oloyede",
"age": 31,
"address": "Oyo state, Nigeria",
"latitude": 3.3456987,
"longitude": 7.432765,
"isLoggedIn": true
}
The UserInfo
data class can be modelled from the above json
payload. like so:
The
UserInfo
data class can be modelled from the abovejson
payload. like so:
// UserInfo Model
data class UserInfo(
val firstName: String,
val lastName: String,
val age: Int,
val address: String,
val latitude: Double,
val longitude: Double,
val isLoggedIn: Boolean
)
- Sample Usage
Kotlin
// Instantiate an object of UserInfo
val userA = UserInfo("Adebayo", "Oloyede", 31, "Oyo state, Nigeria", 3.3456987, 7.432765, true)
// save userA as preference
val objectKey = "OBJECT_VALUE_KEY"
DPrefs.putObject(objectKey, userA)
- Sample Usage
Java
// Instantiate an object of UserInfo
UserInfo userA = new UserInfo("Adebayo", "Oloyede", 31, "Oyo state, Nigeria", 3.3456987, 7.432765, true);
// save userA as preference
String objectKey = "OBJECT_VALUE_KEY";
DPrefs.INSTANCE.putObject(objectKey, userA);
- Fetching a preference value
To get a particular preference value from the library, you only need to supply the
key
with which you saved the preference value and adefault value
. This will return the saved value or thedefault value
provided. In the event that the no value exists for thekey
provided and nodefault value
is supplied, adefault value
provided by the library is returned.
For example:
-
To get a
String
- Kotlin:
val key = "STRING_VALUE_KEY" val defaultValue = "NO_VALUE" val savedString = DPrefs.getString(key, defaultValue)
- Java:
String key = "STRING_VALUE_KEY"; String defaultValue = "NO_VALUE"; String savedString = DPrefs.INSTANCE.getString(key, defaultValue);
-
To get a
Int
- Kotlin:
val key = "INT_VALUE_KEY" val defaultValue = 10 val savedInt = DPrefs.getInt(key, defaultValue)
- Java:
String key = "INT_VALUE_KEY"; int defaultValue = 10; int savedInt = DPrefs.INSTANCE.getInt(key, defaultValue);
The above two examples show the convention for getting a preference using the DPrefs
library. The next item shows how to fetch an object:
-
to get an object of type
UserInfo
saved above- Kotlin:
val key = "OBJECT_VALUE_KEY" val savedObject = DPrefs.getObject(key, UserInfo::class)
- Java:
String key = "OBJECT_VALUE_KEY"; UserInfo savedObject = DPrefs.INSTANCE.getObject(key, UserInfo.class);
Kindly check the
DPrefsDefaultValue
section for the full list of the default value that can be returned by the library.
- Removing a preference value
To remove a saved preference value, you only need to pass the key. The example below how to remove a string value that was saved using the
key
'STRING_VALUE_KEY'
. The same approach can be followed for int, boolean, object etc.
Sample code:
- Kotlin:
val key = "STRING_VALUE_KEY"
DPrefs.removePref(key)
- Java:
String key = "STRING_VALUE_KEY";
DPrefs.INSTANCE.removePref(key);
- To check whether a
key
already exist CalldoesKeyExists
and pass it thekey
. This will return aBoolean
that tells whether thekey
exist or not. See example below:
- Kotlin:
val key = "STRING_VALUE_KEY"
val keyExist = DPrefs.doesKeyExists(key)
if(keyExist) println("True") else println("False")
- Java:
String key = "STRING_VALUE_KEY";
boolean keyExist = DPrefs.INSTANCE.doesKeyExists(key);
if(keyExist) {
System.out.println("True");
} else {
System.out.println("False");
}
- Clearing all the Preferences
To remove all the preference that has been saved all at once, call
clearAllPrefs
on the instance of the library as shown in the sample code below:
Sample code for clearing the preferences:
- Kotlin:
DPrefs.clearAllPrefs()
- Java:
DPrefs.INSTANCE.clearAllPrefs();
There are two exceptions that can be thrown by the library which are:
DPrefsNotInitializedException
This is thrown when you try to use the library without first initializing it, to resolve this, go to your application class to initialize the library as shown above.
DPrefsKeyAlreadyExistsException
This is thrown when you try save a preference value with a key that already exist. To resolve this, just use another set of character strings.
This is an enum which represent the default value that the library returns in the case that no value is found and no defaultValue
is set. There are various instances of it as shown in the list below:
-
- DPrefsDefaultValue.DEFAULT_VALUE_STRING
This is a default
String
that can be returned by the library and it's value is an empty string""
-
- DPrefsDefaultValue.DEFAULT_VALUE_INT
This is a default
Int
that can be returned by the library and it's value is-1111111
-
- DPrefsDefaultValue.DEFAULT_VALUE_BOOLEAN
This is a default
Boolean
that can be returned by the library and it's value isfalse
-
- DPrefsDefaultValue.DEFAULT_VALUE_FLOAT
This is a default
float
that can be returned by the library and it's value is-0.1000001F
-
- DPrefsDefaultValue.DEFAULT_VALUE_LONG
This is a default
Long
that can be returned by the library and it's value is-1111111L
-
- DPrefsDefaultValue.DEFAULT_VALUE_DOUBLE
This is a default
Double
that can be returned by the library and it's value is-0.1000001