| package com.airbnb.lottie.snapshots.utils |
| |
| import android.app.Activity |
| import android.view.LayoutInflater |
| import androidx.viewbinding.ViewBinding |
| import kotlin.properties.ReadOnlyProperty |
| import kotlin.reflect.KProperty |
| |
| /** |
| * Create bindings for a view similar to bindView. |
| * |
| * To use, just call: |
| * private val binding: HomeWorkoutDetailsActivityBinding by viewBinding() |
| * with your binding class and access it as you normally would. |
| */ |
| inline fun <reified T : ViewBinding> Activity.viewBinding() = ActivityViewBindingDelegate(T::class.java, this) |
| |
| class ActivityViewBindingDelegate<T : ViewBinding>( |
| private val bindingClass: Class<T>, |
| val activity: Activity |
| ) : ReadOnlyProperty<Activity, T> { |
| private var binding: T? = null |
| |
| override fun getValue(thisRef: Activity, property: KProperty<*>): T { |
| binding?.let { return it } |
| |
| val inflateMethod = bindingClass.getMethod("inflate", LayoutInflater::class.java) |
| @Suppress("UNCHECKED_CAST") |
| binding = inflateMethod.invoke(null, thisRef.layoutInflater) as T |
| thisRef.setContentView(binding!!.root) |
| return binding!! |
| } |
| } |