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

Commit ea79f81

Browse files
committed
fix(xml-inflater): 'layout' attribute in <include> tags is not preserved while inflating layouts (closes #1214)
1 parent 9b5cb33 commit ea79f81

File tree

7 files changed

+23
-12
lines changed

7 files changed

+23
-12
lines changed

.idea/compiler.xml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

uidesigner/src/main/java/com/itsaky/androidide/uidesigner/undo/AttrAddedAction.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ internal class AttrAddedAction(view: com.itsaky.androidide.inflater.IView, attr:
3333
}
3434

3535
override fun redo() {
36-
view.addAttribute(attr, true)
36+
view.addAttribute(attr, update = true)
3737
}
3838
}

xml-inflater/src/main/java/com/itsaky/androidide/inflater/IView.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ interface IView {
5252
* Add and apply the given attribute to this view.
5353
*
5454
* @param attribute The attribute to apply.
55+
* @param apply Whether the attribute should be applied to the attribute. If this `false` then the
56+
* attribute will be simply added to the attributes list.
57+
* @param update Whether the attribute's value should be updated if the attribute is already applied to this view.
5558
*/
56-
fun addAttribute(attribute: IAttribute, update: Boolean = false)
59+
fun addAttribute(attribute: IAttribute, apply: Boolean = true, update: Boolean = false)
5760

5861
/**
5962
* Remove the given attribute and update the view accordingly.
@@ -185,7 +188,7 @@ interface IView {
185188
override fun onAttributeAdded(view: IView, attribute: IAttribute) {}
186189
override fun onAttributeRemoved(view: IView, attribute: IAttribute) {}
187190
override fun onAttributeUpdated(view: IView, attribute: IAttribute,
188-
oldValue: String
191+
oldValue: String
189192
) {
190193
}
191194
}

xml-inflater/src/main/java/com/itsaky/androidide/inflater/internal/ImmutableViewImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import com.itsaky.androidide.inflater.IView.AttributeChangeListener
2828
*/
2929
class ImmutableViewImpl(private val src: ViewImpl) : IView by src {
3030

31-
override fun addAttribute(attribute: IAttribute, update: Boolean) {
31+
override fun addAttribute(attribute: IAttribute, apply: Boolean, update: Boolean) {
3232
throw UnsupportedOperationException("Immutable!")
3333
}
3434

xml-inflater/src/main/java/com/itsaky/androidide/inflater/internal/LayoutInflaterImpl.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ open class LayoutInflaterImpl : ILayoutInflater {
217217
view: ViewImpl,
218218
parent: IViewGroup,
219219
attachToParent: Boolean = true,
220-
checkAttr: (XmlAttribute) -> Boolean = { true }
220+
shouldApplyAttr: (XmlAttribute) -> Boolean = { true }
221221
) {
222222
val parentView = parent.view as ViewGroup
223223
val adapter =
@@ -233,16 +233,13 @@ open class LayoutInflaterImpl : ILayoutInflater {
233233

234234
if (element.attributeCount > 0) {
235235
for (xmlAttribute in element.attributeList) {
236-
if (!checkAttr(xmlAttribute)) {
237-
continue
238-
}
239236

240237
val namespace =
241238
if (xmlAttribute.namespaceUri.isNullOrBlank()) null
242239
else view.findNamespaceByUri(xmlAttribute.namespaceUri)
243240

244241
val attr = onCreateAttribute(view, namespace, xmlAttribute.name, xmlAttribute.value)
245-
view.addAttribute(attribute = attr, update = true)
242+
view.addAttribute(attribute = attr, apply = shouldApplyAttr(xmlAttribute), update = true)
246243
inflationEventListener?.onEvent(OnApplyAttributeEvent(view, attr))
247244
}
248245
}

xml-inflater/src/main/java/com/itsaky/androidide/inflater/internal/ViewImpl.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,20 @@ constructor(
5656
override val attributes: List<IAttribute>
5757
get() = this._attributes
5858

59-
override fun addAttribute(attribute: IAttribute, update: Boolean) {
59+
override fun addAttribute(attribute: IAttribute, apply: Boolean, update: Boolean) {
6060
if (hasAttribute(attribute)) {
6161
if (!update) {
6262
return
6363
}
6464
updateAttribute(attribute)
6565
} else {
6666
this._attributes.add(attribute)
67+
68+
if (!apply) {
69+
// attribute should not be applied
70+
return
71+
}
72+
6773
applyAttribute(attribute)
6874
notifyAttrAdded(attribute)
6975
}

xml-inflater/src/main/java/com/itsaky/androidide/inflater/internal/adapters/ViewAdapter.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import com.itsaky.androidide.inflater.IAttribute
3838
import com.itsaky.androidide.inflater.INamespace
3939
import com.itsaky.androidide.inflater.IView
4040
import com.itsaky.androidide.inflater.IViewAdapter
41+
import com.itsaky.androidide.inflater.internal.IncludeView
4142
import com.itsaky.androidide.resources.R
4243
import com.itsaky.androidide.inflater.models.UiWidget
4344
import com.itsaky.androidide.inflater.utils.newAttribute
@@ -143,6 +144,10 @@ open class ViewAdapter<T : View> : IViewAdapter<T>() {
143144
}
144145

145146
override fun applyBasic(view: IView) {
147+
if (view is IncludeView) {
148+
return
149+
}
150+
146151
view.addAttribute(newAttribute(view = view, name = "layout_height", value = "wrap_content"))
147152
view.addAttribute(newAttribute(view = view, name = "layout_width", value = "wrap_content"))
148153
}

0 commit comments

Comments
 (0)