Skip to content

Instantly share code, notes, and snippets.

@ElianFabian
Last active May 12, 2024 23:21
Show Gist options
  • Save ElianFabian/38b4a0225adc2066dd28df970f40c5c9 to your computer and use it in GitHub Desktop.
Save ElianFabian/38b4a0225adc2066dd28df970f40c5c9 to your computer and use it in GitHub Desktop.
import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.viewbinding.ViewBinding
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
// From: https://gist.github.com/gmk57/aefa53e9736d4d4fb2284596fb62710d
// To avoid autocompletion with a lambda when using typing "by viewBinding"
private typealias BindView<T> = kotlin.reflect.KFunction1<View, T>
private typealias InflateView<T> = kotlin.reflect.KFunction1<LayoutInflater, T>
private typealias ViewGroupInflateView<T> = kotlin.reflect.KFunction3<LayoutInflater, ViewGroup, Boolean, T>
/** Activity binding delegate, may be used since onCreate up to onDestroy (inclusive) */
fun <T : ViewBinding> Activity.viewBinding(
bindingInflater: InflateView<T>,
): Lazy<T> {
return lazy(LazyThreadSafetyMode.NONE) {
bindingInflater(layoutInflater)
}
}
/** Fragment binding delegate, may be used since onViewCreated up to onDestroyView (inclusive) */
fun <T : ViewBinding> Fragment.viewBinding(
bindingBinder: BindView<T>,
): ReadOnlyProperty<Fragment, T> {
return FragmentViewBindingDelegate(
fragment = this,
viewBindingFactory = bindingBinder,
)
}
class FragmentViewBindingDelegate<T : ViewBinding>(
private val fragment: Fragment,
private val viewBindingFactory: BindView<T>,
) : ReadOnlyProperty<Fragment, T> {
private var binding: T? = null
init {
fragment.lifecycle.addObserver(object : DefaultLifecycleObserver {
val viewLifecycleOwnerLiveDataObserver =
Observer<LifecycleOwner?> { lifecycleOwner ->
val viewLifecycleOwner = lifecycleOwner ?: return@Observer
viewLifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver {
override fun onDestroy(owner: LifecycleOwner) {
binding = null
}
})
}
override fun onCreate(owner: LifecycleOwner) {
fragment.viewLifecycleOwnerLiveData.observeForever(viewLifecycleOwnerLiveDataObserver)
}
override fun onDestroy(owner: LifecycleOwner) {
fragment.viewLifecycleOwnerLiveData.removeObserver(viewLifecycleOwnerLiveDataObserver)
}
})
}
override fun getValue(thisRef: Fragment, property: KProperty<*>): T {
val binding = binding
if (binding != null) {
return binding
}
val lifecycle = fragment.viewLifecycleOwner.lifecycle
if (!lifecycle.currentState.isAtLeast(Lifecycle.State.INITIALIZED)) {
throw IllegalStateException("Should not attempt to get bindings when Fragment views are destroyed")
}
return viewBindingFactory(thisRef.requireView()).also {
this.binding = it
}
}
}
/** Not really a delegate, just a small helper for RecyclerView.ViewHolders */
fun <T : ViewBinding> ViewGroup.viewBinding(bindingInflater: ViewGroupInflateView<T>) =
bindingInflater(LayoutInflater.from(context), this, false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment