Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
fix(xml-inflater): 'layout' attribute in <include> tags is not preser…
Browse files Browse the repository at this point in the history
…ved while inflating layouts (closes #1214)
  • Loading branch information
itsaky committed Aug 27, 2023
1 parent 4545f3c commit a3a4207
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,20 @@ 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
}
updateAttribute(attribute)
} else {
this._attributes.add(attribute)

if (!apply) {
// attribute should not be applied
return
}

applyAttribute(attribute)
notifyAttrAdded(attribute)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))
}
Expand Down

0 comments on commit a3a4207

Please sign in to comment.