Skip to content

Commit

Permalink
Add support for generating on focus change listener
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Jun 26, 2022
1 parent 384bc3d commit 518138d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
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)
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.amrdeveloper.easyadapter.compiler.generator

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.asTypeName

object GeneratorConstants {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ class AdapterKspParser(private val logger: KSPLogger) {
ListenerType.OnHover -> HoverListenerData(modelName, it.viewId)
ListenerType.OnCheckedChange -> CheckedListenerData(modelName, it.viewId)
ListenerType.OnTextChange -> TextChangedListenerData(modelName, it.viewId)
ListenerType.OnFocusChange -> FocusChangeListenerData(modelName, it.viewId)
}
val isUnique = listeners.add(listener)
if (isUnique.not()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ class AdapterParser(private val elementUtils: Elements, private val logger: Easy
ListenerType.OnHover -> HoverListenerData(modelName, listener.viewId)
ListenerType.OnCheckedChange -> CheckedListenerData(modelName, listener.viewId)
ListenerType.OnTextChange -> TextChangedListenerData(modelName, listener.viewId)
ListenerType.OnFocusChange -> FocusChangeListenerData(modelName, listener.viewId)
}
val isUnique = listeners.add(listenerData)
if (isUnique.not()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ enum class ListenerType(val shortName: String) {
* Used to provide TextWatcher
*/
OnTextChange("TextChange"),

/**
* Used to provide View.SetOnFocusChangeListener
*/
OnFocusChange("FocusChange")
}

0 comments on commit 518138d

Please sign in to comment.