Skip to content

Commit 2665b21

Browse files
Optimize FragmentInflateBindingProperty
1 parent ff01f53 commit 2665b21

File tree

2 files changed

+11
-15
lines changed
  • viewbinding-ktx/src/main/java/com/dylanc/viewbinding
  • viewbinding-nonreflection-ktx/src/main/java/com/dylanc/viewbinding/nonreflection

2 files changed

+11
-15
lines changed

viewbinding-ktx/src/main/java/com/dylanc/viewbinding/Fragment.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package com.dylanc.viewbinding
2020

21+
import android.os.Handler
22+
import android.os.Looper
2123
import android.view.LayoutInflater
2224
import androidx.databinding.ViewDataBinding
2325
import androidx.fragment.app.Fragment
@@ -27,6 +29,8 @@ import androidx.viewbinding.ViewBinding
2729
import kotlin.properties.ReadOnlyProperty
2830
import kotlin.reflect.KProperty
2931

32+
enum class Method { BIND, INFLATE }
33+
3034
inline fun <reified VB : ViewBinding> Fragment.binding() =
3135
FragmentBindingProperty(VB::class.java)
3236

@@ -47,29 +51,23 @@ class FragmentBindingProperty<VB : ViewBinding>(private val clazz: Class<VB>) :
4751

4852
class FragmentInflateBindingProperty<VB : ViewBinding>(private val clazz: Class<VB>) : ReadOnlyProperty<Fragment, VB> {
4953
private var binding: VB? = null
54+
private val handler by lazy { Handler(Looper.getMainLooper()) }
5055

51-
@Suppress("UNCHECKED_CAST")
5256
override fun getValue(thisRef: Fragment, property: KProperty<*>): VB {
5357
if (binding == null) {
5458
try {
59+
@Suppress("UNCHECKED_CAST")
5560
binding = (clazz.getMethod("inflate", LayoutInflater::class.java).invoke(null, thisRef.layoutInflater) as VB)
5661
.also { binding -> if (binding is ViewDataBinding) binding.lifecycleOwner = thisRef.viewLifecycleOwner }
5762
} catch (e: IllegalStateException) {
5863
throw IllegalStateException("The property of ${property.name} has been destroyed.")
5964
}
6065
thisRef.viewLifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver {
6166
override fun onDestroy(owner: LifecycleOwner) {
62-
if (thisRef is BindingLifecycleOwner) thisRef.onDestroyViewBinding(binding!!)
63-
binding = null
67+
handler.post { binding = null }
6468
}
6569
})
6670
}
6771
return binding!!
6872
}
6973
}
70-
71-
enum class Method { BIND, INFLATE }
72-
73-
interface BindingLifecycleOwner {
74-
fun onDestroyViewBinding(destroyingBinding: ViewBinding)
75-
}

viewbinding-nonreflection-ktx/src/main/java/com/dylanc/viewbinding/nonreflection/Fragment.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package com.dylanc.viewbinding.nonreflection
2020

21+
import android.os.Handler
22+
import android.os.Looper
2123
import android.view.LayoutInflater
2224
import android.view.View
2325
import androidx.databinding.ViewDataBinding
@@ -47,6 +49,7 @@ class FragmentBindingDelegate<VB : ViewBinding>(private val bind: (View) -> VB)
4749

4850
class FragmentInflateBindingDelegate<VB : ViewBinding>(private val inflate: (LayoutInflater) -> VB) : ReadOnlyProperty<Fragment, VB> {
4951
private var binding: VB? = null
52+
private val handler by lazy { Handler(Looper.getMainLooper()) }
5053

5154
override fun getValue(thisRef: Fragment, property: KProperty<*>): VB {
5255
if (binding == null) {
@@ -59,15 +62,10 @@ class FragmentInflateBindingDelegate<VB : ViewBinding>(private val inflate: (Lay
5962
}
6063
thisRef.viewLifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver {
6164
override fun onDestroy(owner: LifecycleOwner) {
62-
if (thisRef is BindingLifecycleOwner) thisRef.onDestroyViewBinding(binding!!)
63-
binding = null
65+
handler.post { binding = null }
6466
}
6567
})
6668
}
6769
return binding!!
6870
}
6971
}
70-
71-
interface BindingLifecycleOwner {
72-
fun onDestroyViewBinding(destroyingBinding: ViewBinding)
73-
}

0 commit comments

Comments
 (0)