Bulldog
Search…
How to use it
Android library to simplify reading and writing to SharedPreferences, never write code like this anymore prefs.edit().putString("someKey","someString").apply()
Just create a model with wanted properties and default values like the this:
1
@Bulldog(name = "UserSettings")
2
object UserModel {
3
const val id: Int = 20 // Default value
4
const val email: String = "[email protected]"
5
const val likes: Long = 20L
6
const val isPremium: Boolean = false
7
const val minutesLeft: Float = 24.5F
8
}
Copied!
Init Bulldog context in your application class.
bullDogCtx = applicationContext
Bulldog will generate a class from this specification with the name UserSettings. Use it like a normal object to access values, under the hood it uses Kotlin delegated properties to read and writes values to SharedPreferences.
1
UserSettings().apply {
2
id = 2
3
4
}
5
Log.d("PREFS", UserSettings().id)
6
Log.d("PREFS", UserSettings().toString())
Copied!
If no name is specified in @Bulldog annotation, the generated class will have the name of the specification object prefixed with Bulldog

Read values

Just access object property like a normal object
1
Log.d("UserId", UserSettings().id)
Copied!

Write values

1
UserSettings().apply{
2
id = 4
3
Log.d("UserId", id)
4
}
Copied!

Clear values

Bulldog generates a clear method for each entry
1
UserSettings().apply{
2
clearId()
3
Log.d("UserId", id) // Will return the default value
4
}
Copied!
Bulldog also generates a toString() human readable implementation
1
Log.d("PREFS", UserSettings().toString())
2
3
// Ouput
4
// UserSettings id=20, [email protected], likes=20, isPremium=false minutesLeft=24.5
Copied!
Last modified 3yr ago