Skip to content

Commit efdf8a3

Browse files
committed
Refactoring
1 parent be018de commit efdf8a3

File tree

6 files changed

+34
-12
lines changed

6 files changed

+34
-12
lines changed

moxy-androidx-sample/src/main/kotlin/example/com/moxy_androidx_sample/contract/Contract.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface Contract {
1010

1111
var value: String
1212

13-
@StateStrategyType(ADD_TO_END_SINGLE)
13+
@StateStrategyType(ADD_TO_END_SINGLE, singleInstance = true)
1414
fun printLog(msg: D?, log: String?)
1515

1616
}

moxy-compiler/src/main/java/com/omegar/mvp/compiler/Util.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ public static String getAnnotationValueAsString(AnnotationMirror annotationMirro
187187
}
188188
}
189189

190+
public static boolean getAnnotationValueAsBoolean(AnnotationMirror annotationMirror, String key) {
191+
AnnotationValue av = getAnnotationValue(annotationMirror, key);
192+
193+
if (av != null) {
194+
return Boolean.parseBoolean(av.getValue().toString());
195+
} else {
196+
return false;
197+
}
198+
}
199+
200+
190201
public static AnnotationValue getAnnotationValue(AnnotationMirror annotationMirror, String key) {
191202
if (annotationMirror == null) return null;
192203

moxy-compiler/src/main/java/com/omegar/mvp/compiler/entity/CommandViewMethod.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,19 @@ public class CommandViewMethod extends ViewMethod {
2121
private final String mUniqueSuffix;
2222
private final TypeElement mStrategy;
2323
private final String mTag;
24+
private final boolean mSingleInstance;
2425

2526
public CommandViewMethod(Types types, DeclaredType targetInterfaceElement,
2627
ExecutableElement methodElement,
2728
TypeElement strategy,
2829
String tag,
29-
String uniqueSuffix) {
30+
String uniqueSuffix,
31+
boolean singleInstance) {
3032
super(types, targetInterfaceElement, methodElement);
3133
mStrategy = strategy;
3234
mTag = tag;
3335
mUniqueSuffix = uniqueSuffix;
36+
mSingleInstance = singleInstance;
3437
}
3538

3639
public TypeElement getStrategy() {
@@ -49,6 +52,10 @@ public String getCommandClassName() {
4952
return Util.capitalizeString(getName()) + mUniqueSuffix + "Command";
5053
}
5154

55+
public boolean isSingleInstance() {
56+
return mSingleInstance;
57+
}
58+
5259
@Override
5360
public boolean equals(Object o) {
5461
if (this == o) return true;
@@ -57,11 +64,12 @@ public boolean equals(Object o) {
5764
CommandViewMethod that = (CommandViewMethod) o;
5865
return Objects.equals(mUniqueSuffix, that.mUniqueSuffix) &&
5966
Objects.equals(mStrategy, that.mStrategy) &&
60-
Objects.equals(mTag, that.mTag);
67+
Objects.equals(mTag, that.mTag) &&
68+
mSingleInstance == that.mSingleInstance;
6169
}
6270

6371
@Override
6472
public int hashCode() {
65-
return Objects.hash(super.hashCode(), mUniqueSuffix, mStrategy, mTag);
73+
return Objects.hash(super.hashCode(), mUniqueSuffix, mStrategy, mTag, mSingleInstance);
6674
}
6775
}

moxy-compiler/src/main/java/com/omegar/mvp/compiler/viewstate/ElementToViewInterfaceInfoProcessor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,16 @@ private List<ViewMethod> getMethods(TypeElement typeElement) {
132132
// Allow methods be with same names
133133
String uniqueSuffix = getUniqueSuffix(methodsCounter, methodElement);
134134

135+
boolean singleInstance = Util.getAnnotationValueAsBoolean(annotation, "singleInstance");
136+
135137
return new CommandViewMethod(mTypes,
136138
(DeclaredType) typeElement.asType(),
137139
methodElement,
138140
strategyClass,
139141
methodTag,
140-
uniqueSuffix);
142+
uniqueSuffix,
143+
singleInstance
144+
);
141145

142146
});
143147

moxy-compiler/src/main/java/com/omegar/mvp/compiler/viewstate/ViewInterfaceInfoToViewStateJavaFileProcessor.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public final class ViewInterfaceInfoToViewStateJavaFileProcessor extends JavaFil
5252
private static final TypeVariableName GENERIC_TYPE_VARIABLE_NAME = TypeVariableName.get(VIEW);
5353
private static final ClassName MVP_VIEW_STATE_CLASS_NAME = ClassName.get(MvpViewState.class);
5454
private static final ClassName VIEW_COMMAND_CLASS_NAME = ClassName.get(ViewCommand.class);
55-
private static final String STRATEGY_ADD_TO_END_SINGLE = AddToEndSingleStrategy.class.getSimpleName();
5655
private static final ParameterizedTypeName VIEW_COMMAND_TYPE_NAME
5756
= ParameterizedTypeName.get(VIEW_COMMAND_CLASS_NAME, GENERIC_TYPE_VARIABLE_NAME);
5857
private static final ParameterizedTypeName MVP_VIEW_STATE_TYPE_NAME
@@ -171,19 +170,18 @@ private TypeVariableName[] generateSuperClassTypeVariables(ViewInterfaceInfo vie
171170

172171
private TypeSpec generateCommandClass(CommandViewMethod method, List<TypeVariableName> variableNames) {
173172
MethodSpec applyMethod = MethodSpec.methodBuilder("apply")
174-
.addAnnotation(Override.class)
175-
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
173+
.addModifiers(Modifier.PUBLIC)
176174
.addParameter(GENERIC_TYPE_VARIABLE_NAME, "mvpView")
177175
.addExceptions(method.getExceptions())
178176
.addStatement("mvpView.$L($L)", method.getName(), method.getArgumentsString())
179177
.build();
180178

181179
MethodSpec.Builder updateMethodBuilder = null;
182180

183-
if (STRATEGY_ADD_TO_END_SINGLE.equals(method.getStrategy().getSimpleName().toString())) {
181+
if (method.isSingleInstance()) {
184182

185183
updateMethodBuilder = MethodSpec.methodBuilder("update")
186-
.addModifiers(Modifier.PRIVATE, Modifier.FINAL)
184+
.addModifiers(Modifier.PRIVATE)
187185
.addParameters(method.getParameterSpecs());
188186

189187
for (ParameterSpec typeVariable : method.getParameterSpecs()) {
@@ -218,7 +216,7 @@ private MethodSpec generateVoidMethod(DeclaredType enclosingType, CommandViewMet
218216
String commandClassName = method.getCommandClassName();
219217

220218
MethodSpec.Builder builder = MethodSpec.overriding(method.getElement(), enclosingType, mTypes);
221-
if (STRATEGY_ADD_TO_END_SINGLE.equals(method.getStrategy().getSimpleName().toString())) {
219+
if (method.isSingleInstance()) {
222220
builder.addStatement("$1L command = findCommand($1L.class)", commandClassName)
223221
.beginControlFlow("if (command == null)")
224222
.addStatement("apply(new $1N($2L))", commandClass, method.getArgumentsString())
@@ -301,7 +299,6 @@ private MethodSpec generateToStringMethodSpec(CommandViewMethod method) {
301299
}
302300

303301
return MethodSpec.methodBuilder("toString")
304-
.addAnnotation(Override.class)
305302
.addModifiers(Modifier.PUBLIC)
306303
.returns(String.class)
307304
.addStatement(statement.toString())

moxy/src/main/java/com/omegar/mvp/viewstate/strategy/StateStrategyType.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@
2222

2323
String tag() default "";
2424

25+
boolean singleInstance() default false;
26+
2527
}

0 commit comments

Comments
 (0)