Skip to content

Instantly share code, notes, and snippets.

@hluhovskyi hluhovskyi/medium_15.kt
Last active Sep 11, 2017

Embed
What would you like to do?
class StringDelegate : ReadOnlyPropery<Any, String> {
private val stringRes: Int
private val contextProvider: () -> Context
private var string: String? = null
constructor(contextProvider: () -> Context, @StringRes stringRes: Int) {
this.contextProvider = contextProvider
this.stringRes = stringRes
}
override fun getValue(ref: Any, property: KProperty<*>): String {
if (string == null) {
string = contextProvider().getString(stringRes)
}
return string ?: throw IllegalStateException()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.