DataBinderWriter.kt revision 96e1c821dd446d1ed78f8ae61131550588f60a24
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *      http://www.apache.org/licenses/LICENSE-2.0
7 * Unless required by applicable law or agreed to in writing, software
8 * distributed under the License is distributed on an "AS IS" BASIS,
9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 * See the License for the specific language governing permissions and
11 * limitations under the License.
12 */
13
14package android.databinding.tool.writer
15
16import android.databinding.tool.LayoutBinder
17
18class DataBinderWriter(val pkg: String, val projectPackage: String, val className: String,
19        val layoutBinders : List<LayoutBinder>, val minSdk : kotlin.Int) {
20    fun write() = kcode("") {
21        nl("package $pkg;")
22        nl("import $projectPackage.BR;")
23        nl("class $className {") {
24            tab("final static int TARGET_MIN_SDK = ${minSdk};")
25            nl("")
26            tab("public android.databinding.ViewDataBinding getDataBinder(android.view.View view, int layoutId) {") {
27                tab("switch(layoutId) {") {
28                    layoutBinders.groupBy{it.getLayoutname()}.forEach {
29                        val firstVal = it.value.get(0)
30                        tab("case ${firstVal.getModulePackage()}.R.layout.${firstVal.getLayoutname()}:") {
31                            if (it.value.size() == 1) {
32                                if (firstVal.isMerge()) {
33                                    tab("return new ${firstVal.getPackage()}.${firstVal.getImplementationName()}(new android.view.View[]{view});")
34                                } else {
35                                    tab("return ${firstVal.getPackage()}.${firstVal.getImplementationName()}.bind(view);")
36                                }
37                            } else {
38                                // we should check the tag to decide which layout we need to inflate
39                                tab("{") {
40                                    tab("final Object tag = view.getTag();")
41                                    tab("if(tag == null) throw new java.lang.RuntimeException(\"view must have a tag\");")
42                                    it.value.forEach {
43                                        tab("if (\"${it.getTag()}_0\".equals(tag)) {") {
44                                            if (it.isMerge()) {
45                                                tab("return new ${it.getPackage()}.${it.getImplementationName()}(new android.view.View[]{view});")
46                                            } else {
47                                                tab("return new ${it.getPackage()}.${it.getImplementationName()}(view);")
48                                            }
49                                        } tab("}")
50                                    }
51                                }tab("}")
52                            }
53
54                        }
55                    }
56                }
57                tab("}")
58                tab("return null;")
59            }
60            tab("}")
61
62            tab("android.databinding.ViewDataBinding getDataBinder(android.view.View[] views, int layoutId) {") {
63                tab("switch(layoutId) {") {
64                    layoutBinders.filter{it.isMerge()}.groupBy{it.getLayoutname()}.forEach {
65                        val firstVal = it.value.get(0)
66                        tab("case ${firstVal.getModulePackage()}.R.layout.${firstVal.getLayoutname()}:") {
67                            if (it.value.size() == 1) {
68                                tab("return new ${firstVal.getPackage()}.${firstVal.getImplementationName()}(views);")
69                            } else {
70                                // we should check the tag to decide which layout we need to inflate
71                                tab("{") {
72                                    tab("final Object tag = views[0].getTag();")
73                                    tab("if(tag == null) throw new java.lang.RuntimeException(\"view must have a tag\");")
74                                    it.value.forEach {
75                                        tab("if (\"${it.getTag()}_0\".equals(tag)) {") {
76                                            tab("return new ${it.getPackage()}.${it.getImplementationName()}(views);")
77                                        } tab("}")
78                                    }
79                                }tab("}")
80                            }
81                        }
82                    }
83                }
84                tab("}")
85                tab("return null;")
86            }
87            tab("}")
88
89            tab("public int getId(String key) {") {
90                tab("return BR.getId(key);")
91            } tab("}")
92        }
93        nl("}")
94    }.generate()
95}