Skip to content

Commit

Permalink
Merge pull request #8 from skydoves/feature/method-prefix
Browse files Browse the repository at this point in the history
Find java beans related function name for mapping bindable resource
skydoves authored Apr 8, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 1ac1470 + 3e6f522 commit cb4a4b2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class BindingInitProvider : ContentProvider() {
if (fieldSize <= MIN_FIELD_SIZE) {
Log.i(TAG, "BindingManager initialization successful, but there is no `@Bindable` field.")
} else {
Log.i(TAG, "BindingManager initialization successful.")
Log.i(TAG, "BindingManager initialization successful. Size: $fieldSize")
}
return false
}
Expand Down
13 changes: 9 additions & 4 deletions bindables/src/main/java/com/skydoves/bindables/BindingManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ object BindingManager {
/** Java Bean conventions for presenting a getter. */
private const val JAVA_BEANS_GETTER: String = "get"

/** Java Bean conventions for presenting a setter. */
private const val JAVA_BEANS_SETTER: String = "set"

/**
* Binds the `BR` class into the [BindingManager].
* This method only needs to be called once in the application.
Expand Down Expand Up @@ -89,10 +92,12 @@ object BindingManager {
}
?: throw IllegalArgumentException("KFunction: ${function.name} must be annotated with the `@Bindable` annotation.")
val functionName = bindingFunction.name.decapitalize(Locale.ENGLISH)
val bindingFunctionName = functionName
.takeIf { it.startsWith(JAVA_BEANS_GETTER) }
?.replaceFirst(JAVA_BEANS_GETTER, String())
?.decapitalize(Locale.ENGLISH) ?: functionName
val bindingFunctionName = when {
functionName.startsWith(JAVA_BEANS_GETTER) -> functionName.replaceFirst(JAVA_BEANS_GETTER, String())
functionName.startsWith(JAVA_BEANS_SETTER) -> functionName.replaceFirst(JAVA_BEANS_SETTER, String())
functionName.startsWith(JAVA_BEANS_BOOLEAN) -> functionName.replaceFirst(JAVA_BEANS_BOOLEAN, String())
else -> throw IllegalArgumentException("@Bindable associated with method must follow JavaBeans convention $functionName")
}.decapitalize(Locale.ENGLISH)
return bindingFieldsMap[bindingFunctionName] ?: BR._all
}
}

0 comments on commit cb4a4b2

Please sign in to comment.