Skip to content

Commit

Permalink
Optimize exception message in fragments #37
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanCaiCoding committed Nov 19, 2021
1 parent 7c721b1 commit 1aac8df
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding

/**
* @author Dylan Cai
*/

inline fun <reified VB : ViewBinding> BindingViewHolder(parent: ViewGroup) =
BindingViewHolder(inflateBinding<VB>(parent))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty


/**
* @author Dylan Cai
*/

inline fun <reified VB : ViewBinding> ComponentActivity.binding() = lazy {
inflateBinding<VB>(layoutInflater).also {
setContentView(it.root)
Expand Down Expand Up @@ -109,8 +105,12 @@ class FragmentBindingDelegate<VB : ViewBinding>(private val block: () -> VB) : R

override fun getValue(thisRef: Fragment, property: KProperty<*>): VB {
if (binding == null) {
binding = block().also {
if (it is ViewDataBinding) it.lifecycleOwner = thisRef.viewLifecycleOwner
binding = try {
block().also {
if (it is ViewDataBinding) it.lifecycleOwner = thisRef.viewLifecycleOwner
}
} catch (e: IllegalStateException) {
throw IllegalStateException("The binding property has been destroyed.")
}
thisRef.doOnDestroyView {
if (thisRef is BindingLifecycleOwner) thisRef.onDestroyViewBinding(binding!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding

/**
* @author Dylan Cai
*/

class BindingViewHolder<VB : ViewBinding>(val binding: VB) : RecyclerView.ViewHolder(binding.root) {
constructor(parent: ViewGroup, inflate: (LayoutInflater, ViewGroup, Boolean) -> VB) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ import com.google.android.material.tabs.TabLayout
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

/**
* @author Dylan Cai
*/

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

override fun getValue(thisRef: Fragment, property: KProperty<*>): VB {
if (binding == null) {
binding = bind(thisRef.requireView()).also {
if (it is ViewDataBinding) it.lifecycleOwner = thisRef.viewLifecycleOwner
binding = try {
bind(thisRef.requireView()).also {
if (it is ViewDataBinding) it.lifecycleOwner = thisRef.viewLifecycleOwner
}
} catch (e: IllegalStateException) {
throw IllegalStateException("The binding property has been destroyed.")
}
thisRef.doOnDestroyView {
if (thisRef is BindingLifecycleOwner) thisRef.onDestroyViewBinding(binding!!)
Expand Down

0 comments on commit 1aac8df

Please sign in to comment.