fix(xml-inflater): 'layout' attribute in <include> tags is not preserved while inflating layouts (closes #1214)

This commit is contained in:
Akash Yadav
2023-08-22 21:20:07 +05:30
parent 1d47ff6972
commit 0f7fc23084
6 changed files with 21 additions and 10 deletions
@@ -33,6 +33,6 @@ internal class AttrAddedAction(view: com.itsaky.androidide.inflater.IView, attr:
}
override fun redo() {
view.addAttribute(attr, true)
view.addAttribute(attr, update = true)
}
}
@@ -52,8 +52,11 @@ interface IView {
* Add and apply the given attribute to this view.
*
* @param attribute The attribute to apply.
* @param apply Whether the attribute should be applied to the attribute. If this `false` then the
* attribute will be simply added to the attributes list.
* @param update Whether the attribute's value should be updated if the attribute is already applied to this view.
*/
fun addAttribute(attribute: IAttribute, update: Boolean = false)
fun addAttribute(attribute: IAttribute, apply: Boolean = true, update: Boolean = false)
/**
* Remove the given attribute and update the view accordingly.
@@ -185,7 +188,7 @@ interface IView {
override fun onAttributeAdded(view: IView, attribute: IAttribute) {}
override fun onAttributeRemoved(view: IView, attribute: IAttribute) {}
override fun onAttributeUpdated(view: IView, attribute: IAttribute,
oldValue: String
oldValue: String
) {
}
}
@@ -28,7 +28,7 @@ import com.itsaky.androidide.inflater.IView.AttributeChangeListener
*/
class ImmutableViewImpl(private val src: ViewImpl) : IView by src {
override fun addAttribute(attribute: IAttribute, update: Boolean) {
override fun addAttribute(attribute: IAttribute, apply: Boolean, update: Boolean) {
throw UnsupportedOperationException("Immutable!")
}
@@ -217,7 +217,7 @@ open class LayoutInflaterImpl : ILayoutInflater {
view: ViewImpl,
parent: IViewGroup,
attachToParent: Boolean = true,
checkAttr: (XmlAttribute) -> Boolean = { true }
shouldApplyAttr: (XmlAttribute) -> Boolean = { true }
) {
val parentView = parent.view as ViewGroup
val adapter =
@@ -233,16 +233,13 @@ open class LayoutInflaterImpl : ILayoutInflater {
if (element.attributeCount > 0) {
for (xmlAttribute in element.attributeList) {
if (!checkAttr(xmlAttribute)) {
continue
}
val namespace =
if (xmlAttribute.namespaceUri.isNullOrBlank()) null
else view.findNamespaceByUri(xmlAttribute.namespaceUri)
val attr = onCreateAttribute(view, namespace, xmlAttribute.name, xmlAttribute.value)
view.addAttribute(attribute = attr, update = true)
view.addAttribute(attribute = attr, apply = shouldApplyAttr(xmlAttribute), update = true)
inflationEventListener?.onEvent(OnApplyAttributeEvent(view, attr))
}
}
@@ -56,7 +56,7 @@ constructor(
override val attributes: List<IAttribute>
get() = this._attributes
override fun addAttribute(attribute: IAttribute, update: Boolean) {
override fun addAttribute(attribute: IAttribute, apply: Boolean, update: Boolean) {
if (hasAttribute(attribute)) {
if (!update) {
return
@@ -64,6 +64,12 @@ constructor(
updateAttribute(attribute)
} else {
this._attributes.add(attribute)
if (!apply) {
// attribute should not be applied
return
}
applyAttribute(attribute)
notifyAttrAdded(attribute)
}
@@ -38,6 +38,7 @@ import com.itsaky.androidide.inflater.IAttribute
import com.itsaky.androidide.inflater.INamespace
import com.itsaky.androidide.inflater.IView
import com.itsaky.androidide.inflater.IViewAdapter
import com.itsaky.androidide.inflater.internal.IncludeView
import com.itsaky.androidide.resources.R
import com.itsaky.androidide.inflater.models.UiWidget
import com.itsaky.androidide.inflater.utils.newAttribute
@@ -143,6 +144,10 @@ open class ViewAdapter<T : View> : IViewAdapter<T>() {
}
override fun applyBasic(view: IView) {
if (view is IncludeView) {
return
}
view.addAttribute(newAttribute(view = view, name = "layout_height", value = "wrap_content"))
view.addAttribute(newAttribute(view = view, name = "layout_width", value = "wrap_content"))
}