Kref
Android SharedPreference management library made for kotlin
Download
Use Gradle:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.dudgns0507:Kref:1.0.1'
}
How do I use Kref?
Initialize KrefManager:
class App: Application() {
override fun onCreate() {
super.onCreate()
KrefManager.init(this)
// KrefManager.init(this, "SharedPreference Name")
// KrefManager.init(this, "SharedPreference Name", Context.MODE_PRIVATE)
// Clear SharedPreference
// KrefManager.instance.clear()
}
}
Save data to SharedPreference (Example):
class PrefManager {
companion object {
val instance = PrefManager()
}
var kString: String by StringPref(default = "")
}
// getValue like this
PrefManager.instance.kString
// setValue like this
PrefManager.instance.kString = "Kref"
Support Types:
<div class="highlight highlight-source-java position-relative" data-snippet-clipboard-copy-content="// default is default value when SharedPreference key-value is empty
// name is SharedPreference key (if name is blank default is "{variable name}_Kref")
var kref: String by StringPref(default = "", name = "")
var kref: String? by StringNullablePref()
var kref: Boolean by BooleanPref(default = false)
var kref: Int by IntPref(default = 0)
var kref: Long by LongPref(default = 0L)
var kref: Float by FloatPref(default = 0F)
var kref: List by ListPref(default = arrayListOf()))
data class Test(
var a: String
)
var kref: Test by ObjectPref(Test::class, default = Test(a = ""))
“>
// default is default value when SharedPreference key-value is empty // name is SharedPreference key (if name is blank default is "{variable name}_Kref") var kref: String by StringPref(default = "", name = "") var kref: String? by StringNullablePref() var kref: Boolean by BooleanPref(default = false) var kref: Int by IntPref(default = 0) var kref: Long by LongPref(default = 0L) var kref: Float by FloatPref(default = 0F) var kref: List<Any> by ListPref(default = arrayListOf())) data class Test( var a: String ) var kref: Test by ObjectPref(Test::class, default = Test(a = ""))