Skip to content

Instantly share code, notes, and snippets.

@AlonsoFloo
Created July 13, 2019 13:20
Show Gist options
  • Save AlonsoFloo/424a7c017df1d41d856c863f5730c6d5 to your computer and use it in GitHub Desktop.
Save AlonsoFloo/424a7c017df1d41d856c863f5730c6d5 to your computer and use it in GitHub Desktop.
Koltin property delegate tools for Android SharedChamber --> https://github.com/afiqiqmal/SharedChamber
class SharedChamberPropertyDelegate<T>(
private val sharedChamber: SharedChamber<Any>,
private val defaultValue: T,
private val getter: SharedChamber<Any>.(String, T) -> T,
private val setter: SharedChamber<Any>.(String, T) -> Unit
) : ReadWriteProperty<Any, T> {
override fun getValue(thisRef: Any, property: KProperty<*>): T =
sharedChamber.getter(property.name, defaultValue)
override fun setValue(thisRef: Any, property: KProperty<*>, value: T) {
sharedChamber.setter(property.name, value)
}
}
fun SharedChamber<Any>.booleanDelegate(defaultValue: Boolean = false): ReadWriteProperty<Any, Boolean> =
SharedChamberPropertyDelegate(
sharedChamber = this,
defaultValue = defaultValue,
getter = SharedChamber<Any>::getBoolean,
setter = SharedChamber<Any>::put
)
fun SharedChamber<Any>.intDelegate(defaultValue: Int = 0): ReadWriteProperty<Any, Int> =
SharedChamberPropertyDelegate(
sharedChamber = this,
defaultValue = defaultValue,
getter = SharedChamber<Any>::getInt,
setter = SharedChamber<Any>::put
)
fun SharedChamber<Any>.longDelegate(defaultValue: Long = 0L): ReadWriteProperty<Any, Long> =
SharedChamberPropertyDelegate(
sharedChamber = this,
defaultValue = defaultValue,
getter = SharedChamber<Any>::getLong,
setter = SharedChamber<Any>::put
)
fun SharedChamber<Any>.floatDelegate(defaultValue: Float = 0f): ReadWriteProperty<Any, Float> =
SharedChamberPropertyDelegate(
sharedChamber = this,
defaultValue = defaultValue,
getter = SharedChamber<Any>::getFloat,
setter = SharedChamber<Any>::put
)
fun SharedChamber<Any>.stringDelegate(defaultValue: String? = null): ReadWriteProperty<Any, String?> =
SharedChamberPropertyDelegate(
sharedChamber = this,
defaultValue = defaultValue,
getter = SharedChamber<Any>::getString,
setter = SharedChamber<Any>::put
)
fun SharedChamber<Any>.dateDelegate(defaultValue: Date? = null): ReadWriteProperty<Any, Date?> =
SharedChamberPropertyDelegate(
sharedChamber = this,
defaultValue = defaultValue,
getter = SharedChamber<Any>::getDate,
setter = SharedChamber<Any>::put
)
fun SharedChamber<Any>.getDate(key: String, defaultValue: Date? = null): Date? =
getLong(key, -1).let {
if (it >= 0) Date(it) else defaultValue
}
fun SharedChamber<Any>.put(key: String, value: Date? = null) =
if (value != null) {
put(key, value.time)
} else {
remove(key)
}
class SharedPreferenceManager(
val context: Context
) {
private val sharedPreferences by lazy {
PreferenceManager.getDefaultSharedPreferences(context)
}
var one by sharedChamber.stringDelegate()
var two by sharedPreferences.dateDelegate()
var three by sharedChamber.stringDelegate()
var four by sharedChamber.booleanDelegate(false)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment