Skip to content

Commit 1aac8df

Browse files
Optimize exception message in fragments #37
1 parent 7c721b1 commit 1aac8df

File tree

4 files changed

+12
-17
lines changed

4 files changed

+12
-17
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ import androidx.recyclerview.widget.ListAdapter
2626
import androidx.recyclerview.widget.RecyclerView
2727
import androidx.viewbinding.ViewBinding
2828

29-
/**
30-
* @author Dylan Cai
31-
*/
3229

3330
inline fun <reified VB : ViewBinding> BindingViewHolder(parent: ViewGroup) =
3431
BindingViewHolder(inflateBinding<VB>(parent))

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ import kotlin.properties.ReadOnlyProperty
3535
import kotlin.reflect.KProperty
3636

3737

38-
/**
39-
* @author Dylan Cai
40-
*/
41-
4238
inline fun <reified VB : ViewBinding> ComponentActivity.binding() = lazy {
4339
inflateBinding<VB>(layoutInflater).also {
4440
setContentView(it.root)
@@ -109,8 +105,12 @@ class FragmentBindingDelegate<VB : ViewBinding>(private val block: () -> VB) : R
109105

110106
override fun getValue(thisRef: Fragment, property: KProperty<*>): VB {
111107
if (binding == null) {
112-
binding = block().also {
113-
if (it is ViewDataBinding) it.lifecycleOwner = thisRef.viewLifecycleOwner
108+
binding = try {
109+
block().also {
110+
if (it is ViewDataBinding) it.lifecycleOwner = thisRef.viewLifecycleOwner
111+
}
112+
} catch (e: IllegalStateException) {
113+
throw IllegalStateException("The binding property has been destroyed.")
114114
}
115115
thisRef.doOnDestroyView {
116116
if (thisRef is BindingLifecycleOwner) thisRef.onDestroyViewBinding(binding!!)

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ import androidx.recyclerview.widget.ListAdapter
2626
import androidx.recyclerview.widget.RecyclerView
2727
import androidx.viewbinding.ViewBinding
2828

29-
/**
30-
* @author Dylan Cai
31-
*/
3229

3330
class BindingViewHolder<VB : ViewBinding>(val binding: VB) : RecyclerView.ViewHolder(binding.root) {
3431
constructor(parent: ViewGroup, inflate: (LayoutInflater, ViewGroup, Boolean) -> VB) :

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ import com.google.android.material.tabs.TabLayout
3434
import kotlin.properties.ReadOnlyProperty
3535
import kotlin.reflect.KProperty
3636

37-
/**
38-
* @author Dylan Cai
39-
*/
4037

4138
fun <VB : ViewBinding> ComponentActivity.binding(inflate: (LayoutInflater) -> VB) = lazy {
4239
inflate(layoutInflater).also {
@@ -91,8 +88,12 @@ class FragmentBindingDelegate<VB : ViewBinding>(private val bind: (View) -> VB)
9188

9289
override fun getValue(thisRef: Fragment, property: KProperty<*>): VB {
9390
if (binding == null) {
94-
binding = bind(thisRef.requireView()).also {
95-
if (it is ViewDataBinding) it.lifecycleOwner = thisRef.viewLifecycleOwner
91+
binding = try {
92+
bind(thisRef.requireView()).also {
93+
if (it is ViewDataBinding) it.lifecycleOwner = thisRef.viewLifecycleOwner
94+
}
95+
} catch (e: IllegalStateException) {
96+
throw IllegalStateException("The binding property has been destroyed.")
9697
}
9798
thisRef.doOnDestroyView {
9899
if (thisRef is BindingLifecycleOwner) thisRef.onDestroyViewBinding(binding!!)

0 commit comments

Comments
 (0)