Skip to content

Commit

Permalink
Separate viewBinding method
Browse files Browse the repository at this point in the history
  • Loading branch information
wada811 committed Apr 22, 2021
1 parent 7047836 commit de54303
Showing 1 changed file with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,27 @@ import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
import androidx.viewbinding.ViewBinding

inline fun <reified T : ViewBinding> FragmentActivity.viewBinding(
crossinline bind: (View) -> T = {
T::class.java.getMethod("bind", View::class.java).invoke(null, it) as T
inline fun <reified T : ViewBinding> FragmentActivity.viewBinding(): Lazy<T> {
return lazy(LazyThreadSafetyMode.NONE) {
val bind: (View) -> T = {
T::class.java.getMethod("bind", View::class.java).invoke(null, it) as T
}
val getContentView: FragmentActivity.() -> View = {
checkNotNull(findViewById<ViewGroup>(android.R.id.content).getChildAt(0)) {
"Call setContentView or Use Activity's secondary constructor passing layout res id."
}
}
bind(getContentView())
}
): Lazy<T> = lazy(LazyThreadSafetyMode.NONE) {
val getContentView: FragmentActivity.() -> View = {
checkNotNull(findViewById<ViewGroup>(android.R.id.content).getChildAt(0)) {
"Call setContentView or Use Activity's secondary constructor passing layout res id."
}

fun <T : ViewBinding> FragmentActivity.viewBinding(bind: (View) -> T): Lazy<T> {
return lazy(LazyThreadSafetyMode.NONE) {
val getContentView: FragmentActivity.() -> View = {
checkNotNull(findViewById<ViewGroup>(android.R.id.content).getChildAt(0)) {
"Call setContentView or Use Activity's secondary constructor passing layout res id."
}
}
bind(getContentView())
}
bind(getContentView())
}

0 comments on commit de54303

Please sign in to comment.