Skip to content

Commit b34ef95

Browse files
committed
修复ViewStub不能正常使用的问题,优化生成出来的代码格式
1 parent a388449 commit b34ef95

File tree

4 files changed

+66
-27
lines changed

4 files changed

+66
-27
lines changed

FastInflatePlugin/src/main/java/com/dreamgyf/android/plugin/fastinflate/plugin/FastInflateLayoutGenerator.kt

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.dreamgyf.android.plugin.fastinflate.plugin
22

33
import com.squareup.kotlinpoet.*
4+
import com.squareup.kotlinpoet.jvm.throws
45
import groovy.util.Node
56
import groovy.xml.XmlParser
67
import java.io.File
@@ -96,38 +97,48 @@ object FastInflateLayoutGenerator {
9697
)
9798
.addStatement("val res = context.resources")
9899
.addStatement("val parser = res.getLayout(resource)")
99-
.addStatement("try {")
100+
.beginControlFlow("try")
100101
.addStatement("val attrs = %T.asAttributeSet(parser)", xmlClz)
101102
.addStatement("var result: %T = root", viewClz.copy(nullable = true))
102103
.addStatement("%T.advanceToRootNode(parser)", helperClz)
103104

104105
val rootNodeName = root.name().toString()
105106
if (rootNodeName == TAG_MERGE) {
106107
inflateFunBuilder
108+
.beginControlFlow("if (root == null || !attachToRoot)")
107109
.addStatement(
108-
"if (root == null || !attachToRoot) { throw %T(\"%L\") }",
110+
"throw %T(%S)",
109111
fastInflateExceptionClz,
110-
"<merge·/>·can·be·used·only·with·a·valid·ViewGroup·root·and·attachToRoot=true"
112+
"<merge /> can be used only with a valid ViewGroup root and attachToRoot=true"
111113
)
114+
.endControlFlow()
112115
.addStatement("inflateChildren(parser, root, context, attrs)")
113116
} else {
114117
inflateFunBuilder.addStatement(
115118
"val temp = %L(root, context, attrs, false)",
116119
genCreateViewFunc(root)
117120
)
118121
.addStatement("var params: %T.LayoutParams? = null", viewGroupClz)
119-
.addStatement("if (root != null) {")
122+
.beginControlFlow("if (root != null)")
120123
.addStatement("params = root.generateLayoutParams(attrs)")
121-
.addStatement("if (!attachToRoot) { temp.layoutParams = params }")
122-
.addStatement("}")
124+
.beginControlFlow("if (!attachToRoot)")
125+
.addStatement("temp.layoutParams = params")
126+
.endControlFlow()
127+
.endControlFlow()
123128
.addStatement("inflateChildren(parser, temp, context, attrs)")
124-
.addStatement("if (root != null && attachToRoot) { root.addView(temp, params) }")
125-
.addStatement("if (root == null || !attachToRoot) { result = temp }")
129+
.beginControlFlow("if (root != null && attachToRoot)")
130+
.addStatement("root.addView(temp, params)")
131+
.endControlFlow()
132+
.beginControlFlow("if (root == null || !attachToRoot)")
133+
.addStatement("result = temp")
134+
.endControlFlow()
126135
}
127136

128137
inflateFunBuilder
129138
.addStatement("return result!!")
130-
.addStatement("} finally { parser.close() }")
139+
.nextControlFlow("finally")
140+
.addStatement("parser.close()")
141+
.endControlFlow()
131142

132143
return inflateFunBuilder.build()
133144
}
@@ -200,8 +211,9 @@ object FastInflateLayoutGenerator {
200211
.addStatement("%T.consumeChildElements(parser)", helperClz)
201212
} else if (nodeName == TAG_MERGE) {
202213
funSpecBuilder.addStatement(
203-
"throw %T(\"<merge /> must be the root element\")",
204-
fastInflateExceptionClz
214+
"throw %T(%S)",
215+
fastInflateExceptionClz,
216+
"<merge /> must be the root element"
205217
)
206218
} else {
207219
funSpecBuilder.addStatement(
@@ -265,39 +277,42 @@ object FastInflateLayoutGenerator {
265277
.addParameter("ignoreThemeAttr", Boolean::class.java)
266278
.returns(viewClz)
267279
.addStatement("var ctx = context")
268-
.addStatement("if (!ignoreThemeAttr) {")
280+
.beginControlFlow("if (!ignoreThemeAttr)")
269281
.addStatement("val ta = context.obtainStyledAttributes(attrs, ATTRS_THEME)")
270282
.addStatement("val themeResId = ta.getResourceId(0, 0)")
271-
.addStatement(
272-
"if (themeResId != 0) { ctx = %T(context, themeResId) }",
273-
contextThemeWrapperClz
274-
)
275-
.addStatement("ta.recycle() }")
276-
283+
.beginControlFlow("if (themeResId != 0)")
284+
.addStatement("ctx = %T(context, themeResId)", contextThemeWrapperClz)
285+
.endControlFlow()
286+
.addStatement("ta.recycle()")
287+
.endControlFlow()
277288
if (nodeName == TAG_1995) {
278289
funSpecBuilder.addStatement("return %T.newBlinkLayout(ctx, attrs)", helperClz)
279290
} else {
280291
funSpecBuilder
281292
.addStatement("var view: %T? = null", viewClz)
282-
.addStatement("if (layoutInflater.factory2 != null) {")
293+
.beginControlFlow("if (layoutInflater.factory2 != null)")
283294
.addStatement(
284-
"view = layoutInflater.factory2.onCreateView(parent, \"%L\", ctx, attrs)",
295+
"view = layoutInflater.factory2.onCreateView(parent, %S, ctx, attrs)",
285296
className
286297
)
287-
.addStatement("} else if (layoutInflater.factory != null) {")
298+
.nextControlFlow("else if (layoutInflater.factory != null)")
288299
.addStatement(
289-
"view = layoutInflater.factory.onCreateView(\"%L\", ctx, attrs)",
300+
"view = layoutInflater.factory.onCreateView(%S, ctx, attrs)",
290301
className
291302
)
292-
.addStatement("}")
303+
.endControlFlow()
304+
.beginControlFlow("if (view == null)")
293305
.addStatement(
294-
"if (view == null) { view = privateFactory?.onCreateView(\"%L\", ctx, attrs) }",
306+
"view = privateFactory?.onCreateView(%S, ctx, attrs)",
295307
className
296308
)
297-
.addStatement("if (view == null) { view = $fixedClassName(ctx, attrs) }")
309+
.endControlFlow()
310+
.beginControlFlow("if (view == null)")
311+
.addStatement("view = $fixedClassName(ctx, attrs)")
312+
.endControlFlow()
298313
.apply {
299-
if (className == "android.view.ViewStub") {
300-
addStatement("view.layoutInflater = LayoutInflater.from(context)")
314+
if (fixedClassName == "android.view.ViewStub") {
315+
addStatement("(view as? android.view.ViewStub?)?.layoutInflater = layoutInflater.cloneInContext(context)")
301316
}
302317
}
303318
.addStatement("return view")

Sample/src/main/java/com/dreamgyf/android/plugin/fastinflate/sample/MainActivity.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.os.Bundle
55
import android.util.Log
66
import android.view.LayoutInflater
77
import android.view.View
8+
import android.view.ViewStub
89
import com.dreamgyf.android.plugin.fastinflate.FastInflate
910

1011
class MainActivity : AppCompatActivity() {
@@ -41,5 +42,8 @@ class MainActivity : AppCompatActivity() {
4142
val tagValue = root.getTag(R.id.tag)
4243

4344
assert(tagValue == "#ff000000")
45+
46+
val viewStub = findViewById<ViewStub>(R.id.view_stub)
47+
viewStub.inflate()
4448
}
4549
}

Sample/src/main/res/layout/activity_main.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,12 @@
6060
android:id="@+id/layout_include_merge"
6161
layout="@layout/layout_include_merge" />
6262

63+
<ViewStub
64+
android:id="@+id/view_stub"
65+
android:layout_width="wrap_content"
66+
android:layout_height="wrap_content"
67+
android:layout="@layout/layout_view_stub"
68+
app:layout_constraintRight_toRightOf="parent"
69+
app:layout_constraintTop_toTopOf="parent" />
70+
6371
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="wrap_content"
4+
android:layout_height="wrap_content">
5+
6+
<TextView
7+
android:layout_width="wrap_content"
8+
android:layout_height="wrap_content"
9+
android:layout_gravity="center"
10+
android:text="This is view stub layout" />
11+
12+
</FrameLayout>

0 commit comments

Comments
 (0)