EasyDataStore: Simplifying Data Storage in Your Android App


 EasyDataStore is a helpful library for managing data storage in your Android application. As you know, SharedPreferences, the traditional method for storing app data, is now deprecated. DataStore is the recommended alternative, but it can be prone to errors during implementation.

EasyDataStore comes to the rescue, offering a streamlined approach to data storage within DataStore. With this library, you can:

  • Effortlessly Store and Retrieve Data: Say goodbye to complex code. EasyDataStore provides simple functions to store and retrieve various data types like integers, strings, and booleans.
  • Avoid Errors: The library handles potential issues that might arise during DataStore implementation, ensuring a smoother development experience.

Getting Started with EasyDataStore

Here’s a breakdown of how to use EasyDataStore in your project:

Dependency

1.) First need to add a data store in your app

//DataStore
implementation "androidx.datastore:datastore-preferences:1.1.0-alpha04"
implementation "androidx.datastore:datastore-core:1.1.0-alpha04"

//Coroutines and LifeCycle Libraries
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"

2.) Second add this dependency in your project for data store setup

//Data Store  Setup
//Step 1. Add the JitPack repository to your build file
//Add it in your root build.gradle at the end of repositories:

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

//Step 2. Add the dependency
dependencies {
implementation 'com.github.rajputmukesh748:EasyDataStore:2.0.0'
}

Implementation

1.) Initialize DataStore in the Application class

class AppController : Application() {

override fun onCreate() {
super.onCreate()

CallDataStore.build(
context = applicationContext,
dataBaseName = "DemoAppData"
)
}
}

2.) Add this class to your manifest file

<application
android:name=".AppController"
....
>

.....
</application>

3.) Create a Preferences key for storing data in a particular key. Each data type key is available in this dependency. You just need to first enter which data type key is used to store data. Then call the getDataPreferenceKey() function with pass unique key in the string.

val INT_KEY = Int.getDataPreferenceKey("loginData")
val DOUBLE_KEY = Double.getDataPreferenceKey("loginData")
val STRING_KEY = String.getDataPreferenceKey("loginData")
val BOOLEAN_KEY = Boolean.getDataPreferenceKey("loginData")
val FLOAT_KEY = Float.getDataPreferenceKey("loginData")
val LONG_KEY = Long.getDataPreferenceKey("loginData")

4.) For storing data in Data Store

CallDataStore.storeData(key = STRING_KEY, storeData = storeData)

5.) Get stored data with key

CallDataStore.getPreferenceData(STRING_KEY){
Log.e("skdnaskndkasdasd", "${it}")
}

6.) Clear unique key data from an app

CallDataStore.clearKeyData(STRING_KEY)

7.) Clear all preference data from the app. They will clear every key data.

CallDataStore.clearAllData()

Conclusion:

EasyDataStore simplifies data storage within DataStore, eliminating the complexities and potential errors you might encounter with raw DataStore implementation. With its user-friendly approach, EasyDataStore helps you manage your app’s data efficiently.

Comments