Mint — An Android Library to make your key value store beautiful
Is key-value store is almost in every app you have written? Then I think this article you will enjoy.
What is the problem?
once upon a time, I worked on an android project, nothing special, but the app store a lot of data in the shared preferences, and I want to categorize those data such as application settings data, user data, etc.
I notice there boilerplate for every category I should write a class has setter and getter for every data to store and retrieve data from shared preferences.
So how can we improve this?
What if I write a simple interface that describes the fields I want to store, and this is the idea of Mint.
Note: Mint is designed for any key-value store not only for Shared Preferences.
So Let’s dive in
First, you should add Mint to your application
implementation 'io.mhssn:mint:1.0.4'
kapt 'io.mhssn:mint-processor:1.0.4'
You should describe to Mint how to stores and delete your data by implementing Store
Or if you want to store it in shared preferences, mint do the work for you; just add the following dependency to your project
implementation 'io.mhssn:mint-android:1.0.4'
And you should use MintSharedPreferences; MintSharedPreferences is a simple class that implements Store for you.
Then just create an interface for your category
@key annotation is optional. The default will be pref_your_property_name
Finally, let Mint do the rest
val userValues = Mint(store).create(UserValues::class)
val appSettings = Mint(store).create(AppSettings::class)
now, this how you store data
userValues.username = "mhssn"
and this is how you retrieve your data
val username = userValues.username
simple and clean 🙂
what if you want to delete your data
Add @Mint annotation to your interface
@Mint
interface UserValues {
After you build your project, you should get a new interface that has the same name as your interface but ends with Mint
val userValues = Mint(store).create(UserValuesMint::class)
You will notice that every field now has a delete function
Finally, Thanks to you for reading this far; and checkout the GitHub page for Mint goodbye 👋