Skip to content

Commit

Permalink
render release on destroy view
Browse files Browse the repository at this point in the history
  • Loading branch information
Lunkov_A@utkonos.ru authored and Lunkov_A@utkonos.ru committed Jun 17, 2020
1 parent 8cd431a commit 64a10ea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.impression.ui_generator_base

import android.view.View
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModelProvider
Expand Down Expand Up @@ -52,7 +53,6 @@ interface Component<C, VM : ComponentViewModel> {
}
}

fun render(immediately: Boolean = true) {
renderer.render(scheme.getBindingClass?.invoke(this as C, viewModel), immediately)
}
fun render(attachToContainer: Boolean = true): ViewDataBinding? =
renderer.render(scheme.getBindingClass?.invoke(this as C, viewModel), attachToContainer)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,34 @@ import kotlin.reflect.KClass

class Renderer(private val component: Component<*, *>) {

var currentBinding: ViewDataBinding? = null
private var currentBinding: ViewDataBinding? = null

var currentBindingClass: KClass<out ViewDataBinding>? = null
private var currentBindingClass: KClass<out ViewDataBinding>? = null

fun render(newBindingClass: KClass<out ViewDataBinding>?, immediately: Boolean) {
fun render(
newBindingClass: KClass<out ViewDataBinding>?,
attachToContainer: Boolean
): ViewDataBinding? {
currentBinding?.let {
if (newBindingClass != null && newBindingClass == currentBindingClass) {
it.setViewModel(component.viewModel)
if (immediately) it.executePendingBindings()
return
return it
}
it.unbind()
(component.container as? ViewGroup)?.removeAllViews()
}
currentBinding = newBindingClass?.inflate(
component.container as? ViewGroup
?: throw UnsupportedOperationException("Component must be ViewGroup"),
component.viewModel,
component.boundLifecycleOwner,
immediately
)?.apply { if (immediately) executePendingBindings() }
attachToContainer
)
currentBindingClass = newBindingClass
return currentBinding
}

fun release() {
currentBinding = null
currentBindingClass = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ class FragmentComponentClassBuilder(
}

override fun TypeSpec.Builder.addRestMembers() {
bindableProperties.forEach { addProperty(buildBindableProperty(it)) }
addFunction(buildOnCreateViewFunction())
addFunction(buildOnActivityCreatedFunction())
bindableProperties.forEach { addProperty(buildBindableProperty(it)) }
addFunction(buildOnDestroyViewFunction())
}

private fun buildBindableProperty(bindableProperty: BindableProperty) = with(
Expand All @@ -109,8 +110,7 @@ class FragmentComponentClassBuilder(
addCode(
"""
this.container = container
render(false)
return renderer.currentBinding?.root
return render(false)?.root
""".trimIndent()
)
build()
Expand All @@ -128,4 +128,15 @@ class FragmentComponentClassBuilder(
addCode("startObservations()")
build()
}

private fun buildOnDestroyViewFunction() = with(FunSpec.builder("onDestroyView")) {
addModifiers(KModifier.OVERRIDE)
addCode(
"""
super.onDestroyView()
renderer.release()
""".trimIndent()
)
build()
}
}

0 comments on commit 64a10ea

Please sign in to comment.