DataBinderWriter.kt revision ed6428586a939e00d9e66314d5cf1056ad48767e
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("private final java.util.HashMap<String, Integer> mLayoutIds;") 27 nl("") 28 tab("public $className() {") { 29 tab("mLayoutIds = new java.util.HashMap<String, Integer>();") 30 layoutBinders.forEach { 31 tab("mLayoutIds.put(\"${it.getTag()}_0\", ${it.getModulePackage()}.R.layout.${it.getLayoutname()});") 32 } 33 } 34 tab("}") 35 nl("") 36 tab("public android.databinding.ViewDataBinding getDataBinder(android.view.View view, int layoutId) {") { 37 tab("switch(layoutId) {") { 38 layoutBinders.groupBy{it.getLayoutname()}.forEach { 39 val firstVal = it.value.get(0) 40 tab("case ${firstVal.getModulePackage()}.R.layout.${firstVal.getLayoutname()}:") { 41 if (it.value.size() == 1) { 42 if (firstVal.isMerge()) { 43 tab("return new ${firstVal.getPackage()}.${firstVal.getImplementationName()}(new android.view.View[]{view});") 44 } else { 45 tab("return ${firstVal.getPackage()}.${firstVal.getImplementationName()}.bind(view);") 46 } 47 } else { 48 // we should check the tag to decide which layout we need to inflate 49 tab("{") { 50 tab("final Object tag = view.getTag();") 51 tab("if(tag == null) throw new java.lang.RuntimeException(\"view must have a tag\");") 52 it.value.forEach { 53 tab("if (\"${it.getTag()}_0\".equals(tag)) {") { 54 if (it.isMerge()) { 55 tab("return new ${it.getPackage()}.${it.getImplementationName()}(new android.view.View[]{view});") 56 } else { 57 tab("return new ${it.getPackage()}.${it.getImplementationName()}(view);") 58 } 59 } tab("}") 60 } 61 }tab("}") 62 } 63 64 } 65 } 66 } 67 tab("}") 68 tab("return null;") 69 } 70 tab("}") 71 72 tab("android.databinding.ViewDataBinding getDataBinder(android.view.View[] views, int layoutId) {") { 73 tab("switch(layoutId) {") { 74 layoutBinders.filter{it.isMerge()}.groupBy{it.getLayoutname()}.forEach { 75 val firstVal = it.value.get(0) 76 tab("case ${firstVal.getModulePackage()}.R.layout.${firstVal.getLayoutname()}:") { 77 if (it.value.size() == 1) { 78 tab("return new ${firstVal.getPackage()}.${firstVal.getImplementationName()}(views);") 79 } else { 80 // we should check the tag to decide which layout we need to inflate 81 tab("{") { 82 tab("final Object tag = views[0].getTag();") 83 tab("if(tag == null) throw new java.lang.RuntimeException(\"view must have a tag\");") 84 it.value.forEach { 85 tab("if (\"${it.getTag()}_0\".equals(tag)) {") { 86 tab("return new ${it.getPackage()}.${it.getImplementationName()}(views);") 87 } tab("}") 88 } 89 }tab("}") 90 } 91 } 92 } 93 } 94 tab("}") 95 tab("return null;") 96 } 97 tab("}") 98 99 tab("int getLayoutId(String tag) {") { 100 tab("Integer id = mLayoutIds.get(tag);") 101 tab("if (id == null) {") { 102 tab("return 0;") 103 } 104 tab("}") 105 tab("return id;") 106 } 107 tab("}") 108 109 tab("public int getId(String key) {") { 110 tab("return BR.getId(key);") 111 } 112 tab("}") 113 } 114 nl("}") 115 }.generate() 116}