Skip to content

Commit

Permalink
feat: add some helpers for view bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
FunkyMuse committed Apr 25, 2022
1 parent c5722a0 commit f0ec976
Showing 1 changed file with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.children
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding


inline fun <T : ViewBinding> Activity.viewBinding(crossinline bindingInflater: (LayoutInflater) -> T) =
lazy(LazyThreadSafetyMode.NONE) {
bindingInflater.invoke(layoutInflater)
}
lazy(LazyThreadSafetyMode.NONE) {
bindingInflater.invoke(layoutInflater)
}

fun <T : ViewBinding> Fragment.viewBinding(viewBindingFactory: (View) -> T, disposeRecyclerViewsAutomatically: Boolean = true) =
FragmentViewBindingDelegate(this, viewBindingFactory, disposeRecyclerViewsAutomatically)
FragmentViewBindingDelegate(this, viewBindingFactory, disposeRecyclerViewsAutomatically)


fun <T : ViewBinding> globalViewBinding(viewBindingFactory: (View) -> T) =
GlobalViewBindingDelegate(viewBindingFactory)
GlobalViewBindingDelegate(viewBindingFactory)

internal fun ensureMainThread() {
if (Looper.myLooper() != Looper.getMainLooper()) {
Expand All @@ -39,4 +41,16 @@ fun ViewBinding?.disposeRecyclers() {
it.adapter = null
}
}
}

fun ViewBinding.gone() {
root.isVisible = false
}

fun ViewBinding.show() {
root.isVisible = true
}

fun ViewBinding.invisible() {
root.isInvisible = true
}

0 comments on commit f0ec976

Please sign in to comment.