Skip to content

Commit

Permalink
Use property's name for key of view's tag
Browse files Browse the repository at this point in the history
  • Loading branch information
wada811 committed Aug 24, 2023
1 parent 333d7dd commit e352567
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import com.wada811.viewbindingktx.viewBinding
import com.wada811.viewbindingktx.withBinding
import kotlin.properties.ReadOnlyProperty

@Deprecated("Import com.wada811.viewbindingktx", ReplaceWith("viewBinding()", "com.wada811.viewbindingktx.viewBinding"))
inline fun <reified T : ViewBinding> Fragment.viewBinding(): ReadOnlyProperty<Fragment, T> = this.viewBinding()

@Deprecated("Import com.wada811.viewbindingktx", ReplaceWith("viewBinding(bind)", "com.wada811.viewbindingktx.viewBinding"))
fun <T : ViewBinding> Fragment.viewBinding(bind: (View) -> T): ReadOnlyProperty<Fragment, T> = this.viewBinding(bind)

inline fun <reified T : ViewBinding> Fragment.withBinding(noinline withBinding: (binding: T) -> Unit) = this.withBinding(withBinding)
@Deprecated("Import com.wada811.viewbindingktx", ReplaceWith("viewBinding(block)", "com.wada811.viewbindingktx.viewBinding"))
inline fun <reified T : ViewBinding> Fragment.withBinding(noinline block: (binding: T) -> Unit) = this.withBinding(block)

fun <T : ViewBinding> Fragment.withBinding(bind: (View) -> T, withBinding: (binding: T) -> Unit) = this.withBinding(bind, withBinding)
@Deprecated("Import com.wada811.viewbindingktx", ReplaceWith("viewBinding(bind, block)", "com.wada811.viewbindingktx.viewBinding"))
fun <T : ViewBinding> Fragment.withBinding(bind: (View) -> T, block: (binding: T) -> Unit) = this.withBinding(bind, block)
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ fun <T : ViewBinding> Fragment.viewBinding(bind: (View) -> T): ReadOnlyProperty<
return object : ReadOnlyProperty<Fragment, T> {
@Suppress("UNCHECKED_CAST")
override fun getValue(thisRef: Fragment, property: KProperty<*>): T {
(requireView().getTag(R.id.view_binding_tag) as? T)?.let { return it }
(requireView().getTag(property.key) as? T)?.let { return it }
return bind(requireView()).also {
requireView().setTag(R.id.view_binding_tag, it)
requireView().setTag(property.key, it)
}
}
}
}

private val KProperty<*>.key: Int
get() = name.hashCode()


inline fun <reified T : ViewBinding> Fragment.withBinding(noinline withBinding: (binding: T) -> Unit) {
withBinding({
T::class.java.getMethod("bind", View::class.java).invoke(null, it) as T
Expand Down
4 changes: 0 additions & 4 deletions ViewBinding-ktx/src/main/res/values/ids.xml

This file was deleted.

5 changes: 0 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,3 @@ android.databinding.incremental=true
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
#android.enableJetifier=true
# Cacheable unit tests
# https://developer.android.com/studio/releases/gradle-plugin#cacheable_tests
android.testConfig.useRelativePath=true

0 comments on commit e352567

Please sign in to comment.