Archived
49 lines
1.7 KiB
Kotlin
49 lines
1.7 KiB
Kotlin
package com.denizk0461.studip.data
|
|
|
|
import android.view.View
|
|
import androidx.fragment.app.Fragment
|
|
import androidx.lifecycle.DefaultLifecycleObserver
|
|
import androidx.lifecycle.Lifecycle
|
|
import androidx.lifecycle.LifecycleOwner
|
|
import androidx.viewbinding.ViewBinding
|
|
import kotlin.properties.ReadOnlyProperty
|
|
import kotlin.reflect.KProperty
|
|
|
|
// written by Zhuinden (props to them!)
|
|
class FragmentViewBindingDelegate<T : ViewBinding>(
|
|
val fragment: Fragment,
|
|
val viewBindingFactory: (View) -> T
|
|
) : ReadOnlyProperty<Fragment, T> {
|
|
private var _binding: T? = null
|
|
|
|
init {
|
|
fragment.lifecycle.addObserver(object : DefaultLifecycleObserver {
|
|
override fun onCreate(owner: LifecycleOwner) {
|
|
fragment.viewLifecycleOwnerLiveData.observe(fragment) { viewLifecycleOwner ->
|
|
viewLifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver {
|
|
override fun onDestroy(owner: LifecycleOwner) {
|
|
_binding = null
|
|
}
|
|
})
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
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 { _binding = it }
|
|
}
|
|
}
|
|
|
|
fun <T : ViewBinding> Fragment.viewBinding(viewBindingFactory: (View) -> T) =
|
|
FragmentViewBindingDelegate(this, viewBindingFactory) |