How it works
Bulldog uses annotation processing to generate classes at compile time from object specifications. Also it leverages Kotlin delegated properties feature to read and write to Android SharedPreferences.
The following example:
@Bulldog(name = "UserSettings")
object UserModel {
const val id: Int = 20
const val email: String = "[email protected]"
const val likes: Long = 20L
const val isPremium: Boolean = false
const val minutesLeft: Float = 24.5F
Generates a class like the following:
class UserSettings {
private val prefs: SharedPreferences =
bullDogCtx.getSharedPreferences(javaClass.simpleName, MODE_PRIVATE)
var id: Int by bindPreference(prefs, 20, "id")
var email: String by bindPreference(prefs, "[email protected]", "email")
var likes: Long by bindPreference(prefs, 20, "likes")
var isPremium: Boolean by bindPreference(prefs, false, "isPremium")
var minutesLeft: Float by bindPreference(prefs, 24.5F, "minutesLeft")
fun clearId() = prefs.edit().remove("id").apply()
fun clearEmail() = prefs.edit().remove("email").apply()
fun clearLikes() = prefs.edit().remove("likes").apply()
fun clearIsPremium() = prefs.edit().remove("isPremium").apply()
fun clearMinutesLeft() = prefs.edit().remove("minutesLeft").apply()
fun clearAll() {
override fun toString(): String = "UserSettings id=$id, email=$email, likes=$likes, isPremium=$isPremium, minutesLeft=$minutesLeft,"
We get a clearXXX method for each entry and also a human readable toString() implementation.
Last modified 3yr ago
Copy link