Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View hluhovskyi's full-sized avatar

Artem Hluhovskyi hluhovskyi

  • Snapchat
  • Odessa
View GitHub Profile
class OwnFragment : Fragment() {
val errorMessage by string(R.string.error_message)
}
fun View.string(@StringRes id: Int): ReadOnlyProperty<View, String>
= StringDelegate(contextProvider, id)
fun Activity.string(@StringRes id: Int): ReadOnlyProperty<Activity, String>
= StringDelegate(contextProvider, id)
fun Fragment.string(@StringRes id: Int): ReadOnlyProperty<Fragment, String>
= StringDelegate(contextProvider, id)
private class StringDelegate : ReadOnlyProperty<Any, Stirng>
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
}
val View.contextProvider: () -> Context
get() = this::getContext
val Activity.contextProvider: () -> Context
get() = { this }
val Fragment.contextProvider: () -> Context
get() = this::getContext
class OwnFragment : Fragment(), ContextProvider {
val errorMessage by string(R.string.error_message)
overrider fun provideContext() = context
}
fun ContextProvider.string(@StringRes stringRes: Int): ReadOnlyProperty<ContextProvider, Stirng>
= StringDelegate(stringRes)
class StringDelegate : ReadOnlyPropery<ContextProvider, String> {
private val stringRes: Int
private var string: String? = null
constructor(@StringRes stringRes: Int) {
this.stringRes = stringRes
}
override fun getValue(ref: ContextProvider, property: KProperty<*>): String {
interface ContextProvdier {
fun provideContext(): Context
}
class OwnActivity : AppCompatActivity(), ContextProvider {
fun provideContext() = this
}
class OwnFragment : Fragment(), ContextProvider {
fun provideContext() = context
private val errorMessage: TextView by view(R.id.errorMessage)
fun <T : View> Activity.view(IdRes id: Int): ReadOnlyProperty<Activity, T>
= ViewFinderDelegate(id)