{
+ return lazy(LazyThreadSafetyMode.NONE) {
+ bindingInflater.invoke(layoutInflater)
+ }
+}
+
+/**
+ * #### Description
+ *
+ * Helps manage the [ViewBinding] for a [Fragment] so that it is properly destroyed and not leaked.
+ *
+ * #### Notes
+ *
+ * - Attaches a lifecycle observer to the receiving fragment
+ * - Requires the fragment to set the binding value
+ * - Removes the binding when the Fragment's onDestroy is triggered
+ * - Throws and error when binding is accessed before onCreateView or after onDestroy
+ *
+ * #### Example Usage
+ *
+ *
+ * private var binding: FragmentMainBinding by paste()
+ *
+ * override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
+ * binding = FragmentMainBinding.inflate(inflater, container, false)
+ * return binding.root
+ * }
+ *
+ *
+ * @receiver [Fragment]
+ * @return ReadWriteProperty
+ * @throws [error] When binding is accessed before onCreateView or after onDestroy
+ *
+ * @author Patches Klinefelter
+ * @since 07/2021
+ */
+fun Fragment.paste(): ReadWriteProperty {
+ return object : ReadWriteProperty, LifecycleObserver {
+
+ private var binding: VIEW_BINDING? = null
+
+ init {
+ this@paste
+ .viewLifecycleOwnerLiveData
+ .observe(
+ this@paste,
+ { owner: LifecycleOwner? ->
+ owner?.lifecycle?.addObserver(this)
+ }
+ )
+ }
+
+ @Suppress("unused")
+ @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
+ fun onDestroy() {
+ binding = null
+ }
+
+ override fun getValue(thisRef: Fragment, property: KProperty<*>): VIEW_BINDING {
+ return binding ?: error("Called before onCreateView or after onDestroy.")
+ }
+
+ override fun setValue(thisRef: Fragment, property: KProperty<*>, value: VIEW_BINDING) {
+ binding = value
+ }
+ }
+}
diff --git a/viewglu/src/test/java/com/isupatches/android/viewglu/ExampleUnitTest.kt b/viewglu/src/test/java/com/isupatches/android/viewglu/ExampleUnitTest.kt
new file mode 100644
index 0000000..721a03f
--- /dev/null
+++ b/viewglu/src/test/java/com/isupatches/android/viewglu/ExampleUnitTest.kt
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2021 Patches Klinefelter
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.isupatches.android.viewglu
+
+import org.junit.Assert.assertEquals
+import org.junit.Test
+
+/**
+ * Example local unit test, which will execute on the development machine (host).
+ *
+ * See [testing documentation](http://d.android.com/tools/testing).
+ */
+internal class ExampleUnitTest {
+
+ @Test
+ fun addition_isCorrect() {
+ assertEquals(4, (2 + 2))
+ }
+}