Skip to content

Commit

Permalink
Issue #11 : Add support for FrameLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryOkafor authored Oct 2, 2020
1 parent 5ab09a0 commit bf61510
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2020 Sebastian Kaspari
*
* 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 recompose.ast.viewgroup

import recompose.ast.ViewGroupNode
import recompose.ast.attributes.ViewAttributes
import recompose.ast.attributes.ViewGroupAttributes
import recompose.visitor.Visitor

/**
* Data classs holding value of a parsed `FrameLayout`
*
* https://developer.android.com/reference/android/widget/FrameLayout
*/
data class FrameLayoutNode(
override val viewGroup: ViewGroupAttributes,
override val view: ViewAttributes,
) : ViewGroupNode {
override fun accept(visitor: Visitor) = visitor.visitFrameLayout(this)
}
2 changes: 2 additions & 0 deletions recompose-ast/src/main/kotlin/recompose/visitor/Visitor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import recompose.ast.view.TextViewNode
import recompose.ast.view.ViewNode
import recompose.ast.viewgroup.CardViewNode
import recompose.ast.viewgroup.ConstraintLayoutNode
import recompose.ast.viewgroup.FrameLayoutNode
import recompose.ast.viewgroup.LinearLayoutNode
import recompose.ast.viewgroup.UnknownNode

Expand All @@ -41,6 +42,7 @@ interface Visitor {
fun visitCardView(node: CardViewNode)
fun visitImageView(node: ImageViewNode)
fun visitLinearLayout(node: LinearLayoutNode)
fun visitFrameLayout(node: FrameLayoutNode)
fun visitCheckBox(node: CheckBoxNode)
fun visitConstraintLayout(node: ConstraintLayoutNode)
fun visitUnknown(node: UnknownNode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import recompose.ast.view.TextViewNode
import recompose.ast.view.ViewNode
import recompose.ast.viewgroup.CardViewNode
import recompose.ast.viewgroup.ConstraintLayoutNode
import recompose.ast.viewgroup.FrameLayoutNode
import recompose.ast.viewgroup.LinearLayoutNode
import recompose.ast.viewgroup.UnknownNode
import recompose.composer.ext.findChains
Expand Down Expand Up @@ -172,6 +173,13 @@ internal class ComposingVisitor : Visitor {
}
}

override fun visitFrameLayout(node: FrameLayoutNode) {
val rowModifier = ModifierBuilder(node)
writer.writeCall(name = "Box", parameters = listOf(rowModifier.toCallParameter())) {
node.viewGroup.children.forEach { it.accept(this@ComposingVisitor) }
}
}

override fun visitConstraintLayout(node: ConstraintLayoutNode) {
val modifier = ModifierBuilder(node)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import recompose.ast.values.InputType
import recompose.ast.values.Size

/**
* Sealed class for types of parameter values that a passed to a `Composable`.
* Sealed class for types of parameter values that are passed to a `Composable`.
*/
internal sealed class ParameterValue {
class RawValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,19 @@ class ComposerTest {
""".trimIndent()
)
}

@Test
fun `FrameLayout with TextView and Button`() {
assertComposing(
fileName = "framelayout-textview-button.xml",
"""
Box(modifier = Modifier.fillMaxWidth().fillMaxHeight().padding(16.dp)) {
Text(text = "Center", fontSize = 20.sp, modifier = Modifier.fillMaxWidth().fillMaxHeight())
Button(onClick = {}, modifier = Modifier.fillMaxWidth()) {
Text(text = "Button", textAlign = TextAlign.Center)
}
}
""".trimIndent()
)
}
}
2 changes: 2 additions & 0 deletions recompose-parser/src/main/kotlin/recompose/parser/xml/View.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import recompose.parser.xml.view.imageView
import recompose.parser.xml.view.textView
import recompose.parser.xml.view.view
import recompose.parser.xml.viewgroup.constraintLayout
import recompose.parser.xml.viewgroup.frameLayout
import recompose.parser.xml.viewgroup.linearLayout
import recompose.parser.xml.viewgroup.unknown

Expand All @@ -46,6 +47,7 @@ internal fun XmlPullParser.node(): Node {
return when (name) {
// ViewGroupNode
"LinearLayout" -> linearLayout()
"FrameLayout" -> frameLayout()

// ViewNode
"View" -> view()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2020 Sebastian Kaspari
*
* 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 recompose.parser.xml.viewgroup

import org.xmlpull.v1.XmlPullParser
import recompose.ast.viewgroup.FrameLayoutNode
import recompose.parser.xml.viewAttributes
import recompose.parser.xml.viewGroupAttributes

/**
* Parses a `<FrameLayout> element.
*
* https://developer.android.com/reference/android/widget/FrameLayout
*/
fun XmlPullParser.frameLayout(): FrameLayoutNode {
return FrameLayoutNode(
view = viewAttributes(),
viewGroup = viewGroupAttributes()
)
}
39 changes: 39 additions & 0 deletions recompose-parser/src/test/kotlin/recompose/parser/ParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import recompose.ast.view.TextViewNode
import recompose.ast.view.ViewNode
import recompose.ast.viewgroup.CardViewNode
import recompose.ast.viewgroup.ConstraintLayoutNode
import recompose.ast.viewgroup.FrameLayoutNode
import recompose.ast.viewgroup.LinearLayoutNode
import recompose.test.utils.assertAST

Expand Down Expand Up @@ -418,4 +419,42 @@ class ParserTest {
)
)
}

@Test
fun `FrameLayout with TextView and Button`() {
assertAST(
fileName = "framelayout-textview-button.xml",
Layout(
listOf(
FrameLayoutNode(
view = ViewAttributes(
width = LayoutSize.MatchParent,
height = LayoutSize.MatchParent,
padding = Padding(all = Size.Dp(16))
),
viewGroup = ViewGroupAttributes(
children = listOf(
TextViewNode(
view = ViewAttributes(
width = LayoutSize.MatchParent,
height = LayoutSize.MatchParent
),
text = "Center",
textSize = Size.Sp(20)
),
ButtonNode(
view = ViewAttributes(
width = LayoutSize.MatchParent,
height = LayoutSize.WrapContent
),
text = "Button"
)
)

)
)
)
)
)
}
}
20 changes: 20 additions & 0 deletions recompose-test/src/main/resources/framelayout-textview-button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Center"
android:textSize="20sp" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:gravity="bottom|center"
android:text="Button"
android:textSize="20sp" />
</FrameLayout>

0 comments on commit bf61510

Please sign in to comment.