Separate support and normal properties

This commit is contained in:
Yan Zhulanow
2015-05-07 16:19:29 +03:00
parent 1ae2719d34
commit 7639ab4f41
2 changed files with 19 additions and 15 deletions
@@ -124,7 +124,11 @@ public abstract class AndroidUIXmlProcessor(protected val project: Project) {
private fun renderMainLayoutFile(layoutName: String, resources: List<AndroidResource>): AndroidSyntheticFile {
return renderLayoutFile(layoutName + AndroidConst.LAYOUT_POSTFIX,
escapeAndroidIdentifier(layoutName), resources) { it.mainProperties }
escapeAndroidIdentifier(layoutName), resources) {
val properties = it.mainProperties.toArrayList()
if (supportV4) properties.addAll(it.mainPropertiesForSupportV4)
properties
}
}
private fun renderViewLayoutFile(layoutName: String, resources: List<AndroidResource>): AndroidSyntheticFile {
@@ -143,10 +147,7 @@ public abstract class AndroidUIXmlProcessor(protected val project: Project) {
for (res in resources) {
properties(res).forEach { property ->
// Comment
if (supportV4 || !isFromSupportV4Package(property.first)) {
writeSyntheticProperty(property.first, res, property.second)
}
writeSyntheticProperty(property.first, res, property.second)
}
}
}
@@ -165,7 +166,7 @@ public abstract class AndroidUIXmlProcessor(protected val project: Project) {
private fun KotlinStringWriter.writeSyntheticProperty(receiver: String, widget: AndroidResource, stubCall: String) {
// extract startsWith() to fun
val className = if (isFromSupportV4Package(receiver)) widget.supportClassName() else widget.className
val className = if (isFromSupportV4Package(receiver)) widget.supportClassName else widget.className
val cast = if (widget.className != "View") " as? $className" else ""
val body = arrayListOf("return $stubCall$cast")
writeImmutableExtensionProperty(receiver,
@@ -22,28 +22,29 @@ public data class AndroidModuleInfo(val applicationPackage: String, resDirectori
public abstract class AndroidResource(val id: String) {
public abstract val className: String
public open val supportClassName: String
get() = className
public abstract val mainProperties: List<Pair<String, String>>
public open val mainPropertiesForSupportV4: List<Pair<String, String>> = listOf()
public open val viewProperties: List<Pair<String, String>> = listOf()
public open fun sameClass(other: AndroidResource): Boolean = false
public open fun supportClassName(): String = className
}
public class AndroidWidget(id: String, override val className: String) : AndroidResource(id) {
private companion object {
val MAIN_PROPERTIES = listOf(
AndroidConst.ACTIVITY_FQNAME to "findViewById(0)",
AndroidConst.FRAGMENT_FQNAME to "getView().findViewById(0)",
AndroidConst.SUPPORT_FRAGMENT_FQNAME to "getView().findViewById(0)")
AndroidConst.FRAGMENT_FQNAME to "getView().findViewById(0)")
val MAIN_PROPERTIES_SUPPORT_V4 = listOf(AndroidConst.SUPPORT_FRAGMENT_FQNAME to "getView().findViewById(0)")
val VIEW_PROPERTIES = listOf("android.view.View" to "findViewById(0)")
}
override val mainProperties = MAIN_PROPERTIES
override val mainPropertiesForSupportV4 = MAIN_PROPERTIES_SUPPORT_V4
override val viewProperties = VIEW_PROPERTIES
override fun sameClass(other: AndroidResource) = other is AndroidWidget
@@ -53,16 +54,18 @@ public class AndroidFragment(id: String) : AndroidResource(id) {
private companion object {
val MAIN_PROPERTIES = listOf(
AndroidConst.ACTIVITY_FQNAME to "getFragmentManager().findFragmentById(0)",
AndroidConst.FRAGMENT_FQNAME to "getFragmentManager().findFragmentById(0)",
AndroidConst.FRAGMENT_FQNAME to "getFragmentManager().findFragmentById(0)")
val MAIN_PROPERTIES_SUPPORT_V4 = listOf(
AndroidConst.SUPPORT_FRAGMENT_FQNAME to "getFragmentManager().findFragmentById(0)",
AndroidConst.SUPPORT_FRAGMENT_ACTIVITY_FQNAME to "getSupportFragmentManager().findFragmentById(0)")
}
override val className = AndroidConst.FRAGMENT_FQNAME
override val supportClassName = AndroidConst.SUPPORT_FRAGMENT_FQNAME
override val mainProperties = MAIN_PROPERTIES
override val mainPropertiesForSupportV4 = MAIN_PROPERTIES_SUPPORT_V4
override fun sameClass(other: AndroidResource) = other is AndroidFragment
override fun supportClassName() = AndroidConst.SUPPORT_FRAGMENT_FQNAME
}