Skip to content

Instantly share code, notes, and snippets.

@hluhovskyi
Last active September 11, 2017 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hluhovskyi/e2acce4e73f4cbf22481c96882e9e3ba to your computer and use it in GitHub Desktop.
Save hluhovskyi/e2acce4e73f4cbf22481c96882e9e3ba to your computer and use it in GitHub Desktop.
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