-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for generating on focus change listener
- Loading branch information
1 parent
384bc3d
commit 518138d
Showing
5 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
.../main/java/com/amrdeveloper/easyadapter/compiler/data/listener/FocusChangeListenerData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.amrdeveloper.easyadapter.compiler.data.listener | ||
|
||
import com.amrdeveloper.easyadapter.compiler.generator.GeneratorConstants | ||
import com.amrdeveloper.easyadapter.compiler.utils.ViewTable | ||
import com.amrdeveloper.easyadapter.compiler.utils.toCamelCase | ||
import com.amrdeveloper.easyadapter.option.ListenerType | ||
import com.squareup.kotlinpoet.* | ||
|
||
data class FocusChangeListenerData ( | ||
override val modelName: String, | ||
override val viewId: String, | ||
override val viewClassName: ClassName = GeneratorConstants.viewClassName, | ||
override val listenerType: ListenerType = ListenerType.OnFocusChange, | ||
override val defaultListenerFormat: String = "setOnFocusChangeListener{ view, hasFocus -> \n%L}" | ||
) : ListenerData() { | ||
|
||
override fun generateInterfaceDeclarations( | ||
builder: TypeSpec.Builder, | ||
modelClassName: ClassName | ||
) { | ||
val listenerFunctionName = "on${modelName}${viewId.toCamelCase()}FocusChangeListener" | ||
builder.addType( | ||
TypeSpec.funInterfaceBuilder(getListenerInterfaceName()) | ||
.addFunction( | ||
FunSpec.builder(listenerFunctionName) | ||
.addModifiers(KModifier.ABSTRACT) | ||
.addParameter("model", modelClassName) | ||
.addParameter("view", GeneratorConstants.viewClassName) | ||
.addParameter("hasFocus", BOOLEAN) | ||
.returns(BOOLEAN) | ||
.build() | ||
) | ||
.build() | ||
) | ||
} | ||
|
||
override fun generateBinds(builder: FunSpec.Builder, table: ViewTable, rClass: ClassName) { | ||
val listenerFunctionName = "on${modelName}${viewId.toCamelCase()}FocusChangeListener" | ||
val listenerBinding = """ | ||
if (::${getListenerVarName()}.isInitialized) { | ||
${getListenerVarName()}.$listenerFunctionName(item, view, hasFocus) | ||
} | ||
""".trimIndent() | ||
if (viewId == "itemView") { | ||
builder.addStatement("itemView.${defaultListenerFormat}", listenerBinding) | ||
} else { | ||
var variableName = table.resolve(viewId, viewClassName.canonicalName) | ||
if (variableName.isEmpty()) { | ||
variableName = table.define(viewId, viewClassName.canonicalName) | ||
declareViewVariable(builder, variableName, viewClassName, viewId, rClass) | ||
} | ||
|
||
builder.addStatement("$variableName.${defaultListenerFormat}", listenerBinding) | ||
} | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...piler/src/main/java/com/amrdeveloper/easyadapter/compiler/generator/GeneratorConstants.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters