DataBinderWriter.kt revision 125f08f32dbe73b457768aeca4accfbc44dd44b6
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, val layoutBinders : List<LayoutBinder> ) {
19    fun write() =
20            kcode("") {
21                nl("package $pkg;")
22                nl("import $projectPackage.BR;")
23                nl("class $className {") {
24                    tab("public android.databinding.ViewDataBinding getDataBinder(android.view.View view, int layoutId) {") {
25                        tab("switch(layoutId) {") {
26                            layoutBinders.groupBy{it.getLayoutname()}.forEach {
27                                tab("case ${it.value.first!!.getModulePackage()}.R.layout.${it.value.first!!.getLayoutname()}:") {
28                                    if (it.value.size() == 1) {
29                                        tab("return ${it.value.first!!.getPackage()}.${it.value.first!!.getImplementationName()}.bind(view);")
30                                    } else {
31                                        // we should check the tag to decide which layout we need to inflate
32                                        tab("{") {
33                                            tab("final String tag = (String)view.getTag();")
34                                            tab("if(tag == null) throw new java.lang.RuntimeException(\"view must have a tag\");")
35                                            it.value.forEach {
36                                                // TODO don't use strings. not necessary
37                                                tab("if (tag.equals(String.valueOf(${it.getId()}))) {") {
38                                                    tab("return new ${it.getPackage()}.${it.getImplementationName()}(view);")
39                                                } tab("}")
40                                            }
41                                        }tab("}")
42                                    }
43
44                                }
45                            }
46                        }
47                        tab("}")
48                        tab("return null;")
49                    }
50                    tab("}")
51
52                    tab("public int getId(String key) {") {
53                        tab("return BR.getId(key);")
54                    } tab("}")
55                }
56                nl("}")
57            }.generate()
58}