Skip to content

Commit c8a0fbc

Browse files
Optimize the tag of ViewBinding
1 parent 47a9e1f commit c8a0fbc

File tree

8 files changed

+56
-18
lines changed

8 files changed

+56
-18
lines changed

viewbinding-base/src/main/java/com/dylanc/viewbinding/base/ViewBindingUtil.kt

+20-14
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ object ViewBindingUtil {
3434
fun <VB : ViewBinding> inflateWithGeneric(genericOwner: Any, layoutInflater: LayoutInflater): VB =
3535
withGenericBindingClass<VB>(genericOwner) { clazz ->
3636
clazz.getMethod("inflate", LayoutInflater::class.java).invoke(null, layoutInflater) as VB
37-
}.also { binding ->
38-
if (genericOwner is ComponentActivity && binding is ViewDataBinding) {
39-
binding.lifecycleOwner = genericOwner
40-
}
41-
}
37+
}.withLifecycleOwner(genericOwner)
4238

4339
@JvmStatic
4440
fun <VB : ViewBinding> inflateWithGeneric(genericOwner: Any, parent: ViewGroup): VB =
@@ -49,21 +45,31 @@ object ViewBindingUtil {
4945
withGenericBindingClass<VB>(genericOwner) { clazz ->
5046
clazz.getMethod("inflate", LayoutInflater::class.java, ViewGroup::class.java, Boolean::class.java)
5147
.invoke(null, layoutInflater, parent, attachToParent) as VB
52-
}.also { binding ->
53-
if (genericOwner is Fragment && binding is ViewDataBinding) {
54-
binding.lifecycleOwner = genericOwner.viewLifecycleOwner
55-
}
56-
}
48+
}.withLifecycleOwner(genericOwner)
5749

5850
@JvmStatic
51+
@Suppress("DEPRECATION")
52+
fun <VB : ViewBinding> getBindingWithGeneric(genericOwner: Any, view: View): VB =
53+
view.getTag(R.id.tag_view_binding) as? VB ?: bindWithGeneric<VB>(genericOwner, view)
54+
.also { view.setTag(R.id.tag_view_binding, it) }
55+
56+
@JvmStatic
57+
@Deprecated(
58+
"Use ViewBindingUtil.getBindingWithGeneric<VB>(genericOwner, view) instead.",
59+
ReplaceWith("ViewBindingUtil.getBindingWithGeneric<VB>(genericOwner, view)")
60+
)
5961
fun <VB : ViewBinding> bindWithGeneric(genericOwner: Any, view: View): VB =
6062
withGenericBindingClass<VB>(genericOwner) { clazz ->
6163
clazz.getMethod("bind", View::class.java).invoke(null, view) as VB
62-
}.also { binding ->
63-
if (genericOwner is Fragment && binding is ViewDataBinding) {
64-
binding.lifecycleOwner = genericOwner.viewLifecycleOwner
65-
}
64+
}.withLifecycleOwner(genericOwner)
65+
66+
private fun <VB : ViewBinding> VB.withLifecycleOwner(genericOwner: Any) = apply {
67+
if (this is ViewDataBinding && genericOwner is ComponentActivity) {
68+
lifecycleOwner = genericOwner
69+
} else if (this is ViewDataBinding && genericOwner is Fragment) {
70+
lifecycleOwner = genericOwner.viewLifecycleOwner
6671
}
72+
}
6773

6874
private fun <VB : ViewBinding> withGenericBindingClass(genericOwner: Any, block: (Class<VB>) -> VB): VB {
6975
var genericSuperclass = genericOwner.javaClass.genericSuperclass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<item name="tag_view_binding" type="id" />
4+
</resources>

viewbinding-brvah/src/main/java/com/dylanc/viewbinding/brvah/BaseViewHolderUtil.kt

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright (c) 2020. Dylan Cai
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
@file:Suppress("unused")
218

319
package com.dylanc.viewbinding.brvah
@@ -8,4 +24,4 @@ import com.chad.library.adapter.base.viewholder.BaseViewHolder
824

925
@Suppress("UNCHECKED_CAST")
1026
fun <VB : ViewBinding> BaseViewHolder.getBinding(bind: (View) -> VB): VB =
11-
itemView.getTag(Int.MIN_VALUE) as? VB ?: bind(itemView).also { itemView.setTag(Int.MIN_VALUE, it) }
27+
itemView.getTag(R.id.tag_view_binding) as? VB ?: bind(itemView).also { itemView.setTag(R.id.tag_view_binding, it) }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<item name="tag_view_binding" type="id" />
4+
</resources>

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ inline fun <reified VB : ViewBinding> View.getBinding() = getBinding(VB::class.j
2323

2424
@Suppress("UNCHECKED_CAST")
2525
fun <VB : ViewBinding> View.getBinding(clazz: Class<VB>) =
26-
getTag(Int.MIN_VALUE) as? VB ?: (clazz.getMethod("bind", View::class.java)
26+
getTag(R.id.tag_view_binding) as? VB ?: (clazz.getMethod("bind", View::class.java)
2727
.invoke(null, this) as VB)
28-
.also { setTag(Int.MIN_VALUE, it) }
28+
.also { setTag(R.id.tag_view_binding, it) }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<item name="tag_view_binding" type="id" />
4+
</resources>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ import androidx.viewbinding.ViewBinding
2525

2626
@Suppress("UNCHECKED_CAST")
2727
fun <VB : ViewBinding> View.getBinding(bind: (View) -> VB): VB =
28-
getTag(Int.MIN_VALUE) as? VB ?: bind(this).also { setTag(Int.MIN_VALUE, it) }
28+
getTag(R.id.tag_view_binding) as? VB ?: bind(this).also { setTag(R.id.tag_view_binding, it) }
2929

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<item name="tag_view_binding" type="id" />
4+
</resources>

0 commit comments

Comments
 (0)