Skip to content

Commit

Permalink
解决activity没有任何注解修饰的时候,不设置状态栏的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
catch-pig committed Oct 20, 2019
1 parent 8023041 commit 6fb187d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ kapt "com.google.dagger:dagger-android-processor:2.23.2"
### 1. Application
* 继承BaseApplication
* 如果不继承BaseApplication,请在onCreate中添加如下代码:
> registerActivityLifecycleCallbacks(ActivityManagerLifeCycleCallbacksImpl())
> ***registerActivityLifecycleCallbacks(ActivityManagerLifeCycleCallbacksImpl())***
### 2. 在需要使用状态栏、标题栏、加载动画的主题中配置全局参数:

|属性|类型|必须|默认|说明|
Expand Down
74 changes: 41 additions & 33 deletions compiler/src/main/java/com/catchpig/compiler/KotlinMvpProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,38 +61,36 @@ class KotlinMvpProcessor : AbstractProcessor() {
val elements = allElements.filter {
if (it is TypeElement) {
if (it.getAnnotation(Title::class.java) != null
|| it.getAnnotation(StatusBar::class.java) != null) {
|| it.getAnnotation(StatusBar::class.java) != null
|| superClassIsBaseActivity(it)) {
return@filter true
}
}
return@filter false
}.map {
it as TypeElement
}
elements.forEach {
if (it is TypeElement) {
val title = it.getAnnotation(Title::class.java)
val statusBar = it.getAnnotation(StatusBar::class.java)
val title = it.getAnnotation(Title::class.java)
val statusBar = it.getAnnotation(StatusBar::class.java)

val className = it.simpleName.toString()
val fullPackageName = elementUtils.getPackageOf(it).qualifiedName.toString()
val typeSpec = TypeSpec
.classBuilder(className + "_MvpCompiler")
.addModifiers(KModifier.FINAL, KModifier.PUBLIC)
.addSuperinterface(CLASS_NAME_MVP_COMPILER)
.addProperty(initTitleProperty(title))
.addProperty(initStatusBarProperty(statusBar))
.addFunction(injectFun(className))
.addFunction(initTitleMenuOnClick(it, title))
.build()
FileSpec
.builder(fullPackageName, typeSpec.name!!)
.addType(typeSpec)
.addImport("com.catchpig.kotlin_mvp", "R")
.build()
.writeTo(filer)
} else {
error("Title注解必须在类上面")
return false
}
val className = it.simpleName.toString()
val fullPackageName = elementUtils.getPackageOf(it).qualifiedName.toString()
val typeSpec = TypeSpec
.classBuilder(className + "_MvpCompiler")
.addModifiers(KModifier.FINAL, KModifier.PUBLIC)
.addSuperinterface(CLASS_NAME_MVP_COMPILER)
.addProperty(initTitleProperty(title,className))
.addProperty(initStatusBarProperty(statusBar,className))
.addFunction(injectFun(className))
.addFunction(initTitleMenuOnClick(it, title))
.build()
FileSpec
.builder(fullPackageName, typeSpec.name!!)
.addType(typeSpec)
.addImport("com.catchpig.kotlin_mvp", "R")
.build()
.writeTo(filer)
}
return true
}
Expand Down Expand Up @@ -130,7 +128,7 @@ class KotlinMvpProcessor : AbstractProcessor() {
val paramType = parameters[0].asType().toString()
if (paramType == TYPE_VIEW.typeName) {
builder = builder.addStatement(" activity.${simpleName}(it)")
}else{
} else {
error("OnClickFirstText注解修饰的参数类型只能为View")
}
}
Expand Down Expand Up @@ -163,7 +161,7 @@ class KotlinMvpProcessor : AbstractProcessor() {
val paramType = parameters[0].asType().toString()
if (paramType == TYPE_VIEW.typeName) {
builder = builder.addStatement(" activity.${simpleName}(it)")
}else{
} else {
error("OnClickFirstDrawable注解修饰的参数类型只能为View")
}
}
Expand Down Expand Up @@ -196,7 +194,7 @@ class KotlinMvpProcessor : AbstractProcessor() {
val paramType = parameters[0].asType().toString()
if (paramType == TYPE_VIEW.typeName) {
builder = builder.addStatement(" activity.${simpleName}(it)")
}else{
} else {
error("OnClickSecondText注解修饰的参数类型只能为View")
}
}
Expand Down Expand Up @@ -229,7 +227,7 @@ class KotlinMvpProcessor : AbstractProcessor() {
val paramType = parameters[0].asType().toString()
if (paramType == TYPE_VIEW.typeName) {
builder = builder.addStatement(" activity.${simpleName}(it)")
}else{
} else {
error("OnClickSecondDrawable注解修饰的参数类型只能为View")
}
}
Expand Down Expand Up @@ -269,12 +267,12 @@ class KotlinMvpProcessor : AbstractProcessor() {
/**
* 添加StatusBar属性
*/
private fun initStatusBarProperty(statusBar: StatusBar?): PropertySpec {
private fun initStatusBarProperty(statusBar: StatusBar?,className: String): PropertySpec {
var builder = PropertySpec
.builder("statusBar", CLASS_NAME_STATUS_BAR_PARAM.copy(nullable = true))
.addModifiers(KModifier.PRIVATE)
return if (null == statusBar) {
warning("StatusBar注解没有使用")
warning("$className:StatusBar注解没有使用")
builder
.initializer("null")
.build()
Expand All @@ -288,12 +286,12 @@ class KotlinMvpProcessor : AbstractProcessor() {
/**
* 添加title属性
*/
private fun initTitleProperty(title: Title?): PropertySpec {
private fun initTitleProperty(title: Title?,className: String): PropertySpec {
var builder = PropertySpec
.builder("title", CLASS_NAME_TITLE_PARAM.copy(nullable = true))
.addModifiers(KModifier.PRIVATE)
return if (null == title) {
warning("Title注解没有使用")
warning("$className:Title注解没有使用")
builder
.initializer("null")
.build()
Expand All @@ -304,6 +302,16 @@ class KotlinMvpProcessor : AbstractProcessor() {
}
}

/**
* 判断父类是否是BaseActivity
*/
private fun superClassIsBaseActivity(typeElement: TypeElement):Boolean{
val className = typeElement.superclass.toString()
if (className.contains("com.catchpig.mvp.base.activity")) {
return true
}
return false
}

private fun warning(msg: String) {
messager.printMessage(Diagnostic.Kind.WARNING, msg)
Expand Down

0 comments on commit 6fb187d

Please sign in to comment.