Last active
September 11, 2017 15:49
-
-
Save hluhovskyi/e2acce4e73f4cbf22481c96882e9e3ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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