parent
5801607f4c
commit
95bf1d8330
@ -0,0 +1,60 @@ |
|||||||
|
@file:Suppress("RedundantVisibilityModifier", "unused") |
||||||
|
|
||||||
|
package io.legado.app.utils.viewbindingdelegate |
||||||
|
|
||||||
|
import android.view.View |
||||||
|
import androidx.annotation.IdRes |
||||||
|
import androidx.core.app.ComponentActivity |
||||||
|
import androidx.viewbinding.ViewBinding |
||||||
|
|
||||||
|
private class ActivityViewBindingProperty<A : ComponentActivity, T : ViewBinding>( |
||||||
|
viewBinder: (A) -> T |
||||||
|
) : ViewBindingProperty<A, T>(viewBinder) { |
||||||
|
|
||||||
|
override fun getLifecycleOwner(thisRef: A) = thisRef |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Create new [ViewBinding] associated with the [Activity][ComponentActivity] and allow customize how |
||||||
|
* a [View] will be bounded to the view binding. |
||||||
|
*/ |
||||||
|
@JvmName("viewBindingActivity") |
||||||
|
public fun <A : ComponentActivity, T : ViewBinding> ComponentActivity.viewBinding( |
||||||
|
viewBinder: (A) -> T |
||||||
|
): ViewBindingProperty<A, T> { |
||||||
|
return ActivityViewBindingProperty(viewBinder) |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Create new [ViewBinding] associated with the [Activity][ComponentActivity] and allow customize how |
||||||
|
* a [View] will be bounded to the view binding. |
||||||
|
*/ |
||||||
|
@JvmName("viewBindingActivity") |
||||||
|
public inline fun <A : ComponentActivity, T : ViewBinding> ComponentActivity.viewBinding( |
||||||
|
crossinline vbFactory: (View) -> T, |
||||||
|
crossinline viewProvider: (A) -> View |
||||||
|
): ViewBindingProperty<A, T> { |
||||||
|
return viewBinding { activity: A -> vbFactory(viewProvider(activity)) } |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Create new [ViewBinding] associated with the [Activity][this] and allow customize how |
||||||
|
* a [View] will be bounded to the view binding. |
||||||
|
* |
||||||
|
* @param vbFactory Function that create new instance of [ViewBinding]. `MyViewBinding::bind` can be used |
||||||
|
* @param viewBindingRootId Root view's id that will be used as root for the view binding |
||||||
|
*/ |
||||||
|
@Suppress("unused") |
||||||
|
@JvmName("viewBindingActivity") |
||||||
|
public inline fun <T : ViewBinding> ComponentActivity.viewBinding( |
||||||
|
crossinline vbFactory: (View) -> T, |
||||||
|
@IdRes viewBindingRootId: Int |
||||||
|
): ViewBindingProperty<ComponentActivity, T> { |
||||||
|
return viewBinding { activity: ComponentActivity -> |
||||||
|
vbFactory( |
||||||
|
activity.findViewById( |
||||||
|
viewBindingRootId |
||||||
|
) |
||||||
|
) |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue