Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanCaiCoding committed Mar 29, 2021
1 parent 64ed3ca commit 98a421f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import androidx.viewbinding.ViewBinding
* @author Dylan Cai
*/

class BindingViewHolder<VB : ViewBinding>(val binding: VB) : RecyclerView.ViewHolder(binding.root) {
open class BindingViewHolder<VB : ViewBinding>(val binding: VB) : RecyclerView.ViewHolder(binding.root) {

constructor(block: (LayoutInflater, ViewGroup, Boolean) -> VB, parent: ViewGroup) :
this(block(LayoutInflater.from(parent.context), parent, false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,23 @@ class FragmentBindingDelegate<VB : ViewBinding>(
private val bind: (View) -> VB
) : ReadOnlyProperty<Fragment, VB> {

private var lifecycleObserver: LifecycleObserver? = null
private var observedLifecycle = false
private var binding: VB? = null

@Suppress("UNCHECKED_CAST")
override fun getValue(thisRef: Fragment, property: KProperty<*>): VB {
if (lifecycleObserver == null) {
lifecycleObserver = object : LifecycleObserver {
if (!observedLifecycle) {
thisRef.viewLifecycleOwner.lifecycle.addObserver(object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroyView() {
binding = null
}
}.also {
thisRef.viewLifecycleOwner.lifecycle.addObserver(it)
}
})
observedLifecycle = true
}
if (binding == null) {
binding = bind(thisRef.requireView())
}
return binding!!
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import androidx.viewbinding.ViewBinding
inline fun <reified VB : ViewBinding> BindingViewHolder(parent: ViewGroup) =
BindingViewHolder(inflateBinding<VB>(parent))

class BindingViewHolder<VB : ViewBinding>(val binding: VB) : RecyclerView.ViewHolder(binding.root) {
open class BindingViewHolder<VB : ViewBinding>(val binding: VB) : RecyclerView.ViewHolder(binding.root) {

constructor(block: (LayoutInflater, ViewGroup, Boolean) -> VB, parent: ViewGroup) :
this(block(LayoutInflater.from(parent.context), parent, false))
Expand Down
15 changes: 7 additions & 8 deletions viewbinding/src/main/java/com/dylanc/viewbinding/ViewBinding.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,24 @@ class FragmentBindingDelegate<VB : ViewBinding>(
private val clazz: Class<VB>
) : ReadOnlyProperty<Fragment, VB> {

private var lifecycleObserver: LifecycleObserver? = null
private var observedLifecycle = false
private var binding: VB? = null

@Suppress("UNCHECKED_CAST")
override fun getValue(thisRef: Fragment, property: KProperty<*>): VB {
if (lifecycleObserver == null) {
lifecycleObserver = object : LifecycleObserver {
if (!observedLifecycle) {
thisRef.viewLifecycleOwner.lifecycle.addObserver(object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroyView() {
binding = null
}
}.also {
thisRef.viewLifecycleOwner.lifecycle.addObserver(it)
}
})
observedLifecycle = true
}
if (binding == null) {
binding = clazz.getMethod("bind", View::class.java)
.invoke(null, thisRef.requireView()) as VB
}
return binding!!
}
}
}

0 comments on commit 98a421f

Please sign in to comment.