Created
May 25, 2021 14:08
-
-
Save FelixZY/bea72a61d9a2cd5d1fc84c14de680cf9 to your computer and use it in GitHub Desktop.
Lazy ViewBinding delegate designed to imitate `androidx.fragment.app.viewModels`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2021 Felix Zedén Yverås | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated | |
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the | |
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit | |
persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the | |
Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | |
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR | |
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
package se.fzy.snippets | |
import android.view.View | |
import androidx.annotation.IdRes | |
import androidx.core.view.ViewCompat | |
import androidx.fragment.app.Fragment | |
import androidx.lifecycle.Lifecycle | |
import androidx.lifecycle.LifecycleObserver | |
import androidx.lifecycle.OnLifecycleEvent | |
import androidx.viewbinding.ViewBinding | |
class ViewBindingLazy<VB : ViewBinding>( | |
private val viewBindingClass: Class<VB>, | |
private val viewLifecycleProvider: () -> Lifecycle, | |
private val rootViewProvider: () -> View, | |
) : Lazy<VB>, LifecycleObserver { | |
private var cached: VB? = null | |
override val value: VB | |
get() { | |
if (cached == null) { | |
viewLifecycleProvider().addObserver(this) | |
// Use java reflection to avoid a dependency on kotlin-reflect.jar | |
@Suppress("UNCHECKED_CAST") | |
cached = viewBindingClass | |
.getMethod("bind", View::class.java) | |
.invoke(null, rootViewProvider()) as VB | |
} | |
return cached!! | |
} | |
override fun isInitialized(): Boolean = cached != null | |
@Suppress("unused") | |
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) | |
fun onDestroy() { | |
cached = null | |
} | |
} | |
/** | |
* Convenience method for retrieving view bindings designed to imitate | |
* [androidx.fragment.app.viewModels]. | |
* | |
* **Basic usage:** | |
* ```kotlin | |
* private lateinit var views: MyFragmentBindings by viewBindings() | |
* ``` | |
* | |
* **See also:** | |
* [https://developer.android.com/topic/libraries/view-binding](https://developer.android.com/topic/libraries/view-binding) | |
* | |
* @param rootViewProvider function providing the view on which to call `VB.bind(view)` | |
*/ | |
inline fun <reified VB : ViewBinding> Fragment.viewBindings( | |
noinline rootViewProvider: () -> View = { requireView() }, | |
): Lazy<VB> = ViewBindingLazy( | |
VB::class.java, | |
{ viewLifecycleOwner.lifecycle }, | |
rootViewProvider, | |
) | |
/** | |
* @see Fragment.viewBindings | |
*/ | |
inline fun <reified VB : ViewBinding> Fragment.viewBindings( | |
@IdRes rootView: Int, | |
): Lazy<VB> = ViewBindingLazy( | |
VB::class.java, | |
{ viewLifecycleOwner.lifecycle }, | |
{ ViewCompat.requireViewById(requireView(), rootView) } | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment