Skip to content

Instantly share code, notes, and snippets.

Avatar

Artem Hluhovskyi hluhovskyi

View GitHub Profile
View medium_17.kt
class OwnFragment : Fragment() {
val errorMessage by string(R.string.error_message)
}
View medium_16.kt
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>
View medium_15.kt
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
}
View medium_14.kt
val View.contextProvider: () -> Context
get() = this::getContext
val Activity.contextProvider: () -> Context
get() = { this }
val Fragment.contextProvider: () -> Context
get() = this::getContext
View medium_13.kt
class OwnFragment : Fragment(), ContextProvider {
val errorMessage by string(R.string.error_message)
overrider fun provideContext() = context
}
View medium_12.kt
fun ContextProvider.string(@StringRes stringRes: Int): ReadOnlyProperty<ContextProvider, Stirng>
= StringDelegate(stringRes)
View medium_11.kt
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 {
View medium_10.kt
interface ContextProvdier {
fun provideContext(): Context
}
class OwnActivity : AppCompatActivity(), ContextProvider {
fun provideContext() = this
}
class OwnFragment : Fragment(), ContextProvider {
fun provideContext() = context
View medium_9.kt
private val errorMessage: TextView by view(R.id.errorMessage)
View medium_8.kt
fun <T : View> Activity.view(IdRes id: Int): ReadOnlyProperty<Activity, T>
= ViewFinderDelegate(id)
You can’t perform that action at this time.