@@ -2,7 +2,6 @@ package com.omegar.mvp_processor
22
33import com.google.devtools.ksp.KspExperimental
44import com.google.devtools.ksp.getAllSuperTypes
5- import com.google.devtools.ksp.getAnnotationsByType
65import com.google.devtools.ksp.getClassDeclarationByName
76import com.google.devtools.ksp.processing.CodeGenerator
87import com.google.devtools.ksp.processing.KSPLogger
@@ -18,7 +17,7 @@ import com.google.devtools.ksp.symbol.KSTypeReference
1817import com.google.devtools.ksp.symbol.KSValueParameter
1918import com.google.devtools.ksp.symbol.KSVisitorVoid
2019import com.google.devtools.ksp.validate
21- import com.omega_r.base.annotations.AutoPresenterLauncher
20+ import com.omegar.mvp.MvpPresenter
2221import com.omegar.mvp.MvpView
2322import com.squareup.kotlinpoet.ClassName
2423import com.squareup.kotlinpoet.CodeBlock
@@ -51,7 +50,7 @@ class MvpProcessor(
5150 companion object {
5251
5352 private val PRESENTER_TYPE = ClassName (" com.omegar.mvp.presenter" , " PresenterType" )
54- private val MVP_PRESENTER_FACTORY = ClassName (" com.omega_r.base.mvp.factory" , " MvpPresenterFactory " )
53+ private val MVP_PRESENTER_FACTORY = ClassName (" com.omega_r.base.mvp.factory" , " MvpScreenFactory " )
5554 private val BUNDLE = ClassName (" android.os" , " Bundle" )
5655 private val PARCELABLE = ClassName (" android.os" , " Parcelable" )
5756 private val SERIALIZABLE = ClassName (" java.io" , " Serializable" )
@@ -73,7 +72,7 @@ class MvpProcessor(
7372 private val delegateLauncherMap = mapOf (
7473 DelegateType .ACTIVITY to ACTIVITY_LAUNCHER_NAME ,
7574 DelegateType .FRAGMENT to FRAGMENT_LAUNCHER_NAME ,
76- DelegateType .DIALOG_FRAGMENT to DIALOG_FRAGMENT_NAME ,
75+ DelegateType .DIALOG_FRAGMENT to DIALOG_FRAGMENT_LAUNCHER_NAME ,
7776 )
7877
7978 private val mvpDelegateLauncherMap = mapOf (
@@ -83,17 +82,25 @@ class MvpProcessor(
8382 )
8483 }
8584
85+ private var isProcessed = false
8686
8787 override fun process (resolver : Resolver ): List <KSAnnotated > {
88+ if (isProcessed) {
89+ return emptyList()
90+ }
91+ isProcessed = true
8892 val serializableType = resolver.getClassDeclarationByName(SERIALIZABLE .canonicalName)!! .asType(emptyList())
8993 val parcelableType = resolver.getClassDeclarationByName(PARCELABLE .canonicalName)!! .asType(emptyList())
9094 val activityType = resolver.getClassDeclarationByName(ACTIVITY_NAME .canonicalName)!! .asType(emptyList())
9195 val fragmentType = resolver.getClassDeclarationByName(FRAGMENT_NAME .canonicalName)!! .asType(emptyList())
9296 val dialogFragmentType = resolver.getClassDeclarationByName(DIALOG_FRAGMENT_NAME .canonicalName)!! .asType(emptyList())
9397 val mvpView = resolver.getClassDeclarationByName(MvpView ::class .qualifiedName!! )!! .asStarProjectedType()
98+ val mvpPresenter = resolver.getClassDeclarationByName(MvpPresenter ::class .qualifiedName!! )!! .asStarProjectedType()
9499
95- return resolver.getSymbolsWithAnnotation(AutoPresenterLauncher ::class .qualifiedName!! )
100+ return resolver.getAllFiles()
101+ .flatMap { it.declarations }
96102 .filterIsInstance<KSClassDeclaration >()
103+ .filter { mvpPresenter.isAssignableFrom(it.asStarProjectedType()) }
97104 .filter {
98105 if (it.validate()) {
99106 it.accept(
@@ -103,7 +110,8 @@ class MvpProcessor(
103110 activityType = activityType,
104111 fragmentType = fragmentType,
105112 dialogFragmentType = dialogFragmentType,
106- mvpView = mvpView
113+ mvpView = mvpView,
114+ resolver = resolver
107115 ),
108116 data = Unit
109117 )
@@ -120,28 +128,38 @@ class MvpProcessor(
120128 private val fragmentType : KSType ,
121129 private val dialogFragmentType : KSType ,
122130 private val mvpView : KSType ,
131+ private val resolver : Resolver ,
123132
124133 ) : KSVisitorVoid() {
125134
126135 @OptIn(KspExperimental ::class )
127136 override fun visitClassDeclaration (classDeclaration : KSClassDeclaration , data : Unit ) {
128- val factoryName = classDeclaration.simpleName.asString() + " Factory "
137+ val factoryName = classDeclaration.simpleName.asString().replace( " Presenter " , " " ) + " ScreenFactory "
129138
130- val annotation = classDeclaration.getAnnotationsByType(AutoPresenterLauncher ::class ).first()
131- val ksAnnotation = classDeclaration.annotations.first {
132- it.annotationType.resolve().declaration.qualifiedName?.asString() == AutoPresenterLauncher ::class .qualifiedName
133- }
134- val targetClass = ksAnnotation.arguments.first { it.name?.asString() == " delegatedClass" }.value as List <KSType >
139+ val (_, view) = classDeclaration.getSuperPresenterAndView()
135140
136- val presenterType = if (annotation.localPresenterType) " LOCAL" else " GLOBAL"
141+ val targetClass = resolver.getAllFiles()
142+ .flatMap { it.declarations }
143+ .filterIsInstance<KSClassDeclaration >()
144+ .map { it.asStarProjectedType() }
145+ .filter {
146+ (it.isActivity() || it.isFragment() || it.isDialogFragment()) && (view.isAssignableFrom(it))
147+ }
148+ .toList()
149+
150+ val presenterType = " LOCAL"
137151
138- val (_, view) = classDeclaration.getSuperPresenterAndView()
139152 val viewStateName = view.toClassName().simpleName.replace(" View" , " MvpViewState" )
140153
141154 val presenterClassName = classDeclaration.toClassName()
142155
143156 val typeSpec = TypeSpec .objectBuilder(factoryName)
144- .addOriginatingKSFile(classDeclaration.containingFile!! )
157+ .apply {
158+ addOriginatingKSFile(classDeclaration.containingFile!! )
159+ targetClass.forEach {
160+ addOriginatingKSFile(it.declaration.containingFile!! )
161+ }
162+ }
145163 .superclass(MVP_PRESENTER_FACTORY .parameterizedBy(presenterClassName))
146164 .addSuperclassConstructorParameter(" %T.$presenterType , %T::class" , PRESENTER_TYPE , presenterClassName)
147165 .addInitializerBlock(CodeBlock .of( " $viewStateName .Companion" ))
@@ -330,8 +348,8 @@ class MvpProcessor(
330348
331349 private fun KSType.getDelegateType () = when {
332350 isActivity() -> DelegateType .ACTIVITY
333- isFragment() -> DelegateType .FRAGMENT
334351 isDialogFragment() -> DelegateType .DIALOG_FRAGMENT
352+ isFragment() -> DelegateType .FRAGMENT
335353 else -> throw IllegalArgumentException (" Unknown type $this " )
336354 }
337355
0 commit comments