Skip to content

Commit

Permalink
用 callback 重构 Activity reference updater
Browse files Browse the repository at this point in the history
  • Loading branch information
douo committed Mar 4, 2018
1 parent b05b366 commit 9870506
Show file tree
Hide file tree
Showing 18 changed files with 528 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ private MethodSpec buildResultCallback(ResultModel result) throws IOException {
.returns(builderWithParameter)
.addModifiers(Modifier.PUBLIC)
.addParameter(result.getConsumerType(), result.getConsumerName())
.addStatement("getConsumer().$L = $L", result.getConsumerName(), result.getConsumerName())
.addStatement(
"getConsumer().set$LConsumer($L)", result.getCapitalizeName(), result.getConsumerName())
.addStatement("return this")
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package info.dourok.compiler.generator;

import android.content.Intent;
import com.google.common.base.CaseFormat;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.JavaFile;
Expand Down Expand Up @@ -91,6 +92,7 @@ protected TypeSpec generate() {
"return process$L($L,$L)", result.getCapitalizeName(), "activity", "intent");
try {
consumer.addField(buildField(result));
consumer.addMethod(buildSetter(result));
} catch (IOException e) {
// TODO 生成 consumer 接口失败
error("create consumer failed:" + e.getMessage());
Expand All @@ -109,7 +111,17 @@ protected TypeSpec generate() {
}

private FieldSpec buildField(ResultModel result) throws IOException {
return FieldSpec.builder(result.getConsumerType(), result.getConsumerName()).build();
return FieldSpec.builder(result.getConsumerType(), result.getConsumerName())
.addModifiers(Modifier.PRIVATE)
.build();
}

private MethodSpec buildSetter(ResultModel result) throws IOException {
return MethodSpec.methodBuilder("set" + result.getCapitalizeName() + "Consumer")
.addParameter(result.getConsumerType(), "consumer")
.addStatement("beforeAdd($L)","consumer")
.addStatement("this.$L = $L", result.getConsumerName(), "consumer")
.build();
}

private MethodSpec buildResultProcessor(ResultModel result) {
Expand All @@ -120,7 +132,7 @@ private MethodSpec buildResultProcessor(ResultModel result) {
.addParameter(TypeVariableName.get("A"), "activity")
.addParameter(Intent.class, "intent")
.beginControlFlow("if($L != null)", result.getConsumerName())
.addStatement("doCheck(activity,$L)", result.getConsumerName());
.addStatement("beforeExecute(activity,$L)", result.getConsumerName());
if (!result.getParameters().isEmpty()) {
StringBuilder literal = new StringBuilder(result.getConsumerName()).append(".accept(");
String[] names = new String[result.getParameters().size()];
Expand All @@ -141,6 +153,7 @@ private MethodSpec buildResultProcessor(ResultModel result) {
} else {
builder.addStatement("$L.run()", result.getConsumerName());
}
builder.addStatement("afterExecute(activity,$L)", result.getConsumerName());
builder.addStatement("return true");
builder.endControlFlow();
builder.beginControlFlow("else").addStatement("return false").endControlFlow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,22 @@ class ResultParameterSpec extends Specification {
}""")
.source()
def consumer = Source.consumer()
.field("BiConsumer<ArrayList,Character> textConsumer;",
.field("private BiConsumer<ArrayList,Character> textConsumer;",
["info.dourok.esactivity.function.BiConsumer", ArrayList, Character])
.method("""
void setTextConsumer(BiConsumer<ArrayList,Character> consumer) {
beforeAdd(consumer);
this.textConsumer = consumer;
}
""")
.method("""
private boolean processText(A activity,Intent intent) {
if(textConsumer != null) {
doCheck(activity, textConsumer);
beforeExecute(activity, textConsumer);
ArrayList ids = RefManager.getInstance().get(intent,"ids");
Character name = RefManager.getInstance().get(intent,"name");
textConsumer.accept(ids,name);
afterExecute(activity, textConsumer);
return true;
}
else {
Expand Down
26 changes: 20 additions & 6 deletions compiler/src/test/groovy/info/dourok/compiler/ResultSetSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ class ResultSetSpec extends Specification {
.hasConsumer()
.method("""
public EmptyActivityBuilder<A> forDate(Consumer<Long> dateConsumer) {
getConsumer().dateConsumer = dateConsumer;
getConsumer().setDateConsumer(dateConsumer);
return this;
}""", ["info.dourok.esactivity.function.Consumer", Long])
.method("""
public EmptyActivityBuilder<A> forText(BiConsumer<ArrayList, Character> textConsumer) {
getConsumer().textConsumer = textConsumer;
getConsumer().setTextConsumer(textConsumer);
return this;
}""", ["info.dourok.esactivity.function.BiConsumer", ArrayList, Character])
.source()
Expand Down Expand Up @@ -66,30 +66,44 @@ class ResultSetSpec extends Specification {
}""")
.source()
def consumer = Source.consumer()
.field("Consumer<Long> dateConsumer;",
.field("private Consumer<Long> dateConsumer;",
["info.dourok.esactivity.function.Consumer", Long])
.method("""
void setDateConsumer(Consumer<Long> consumer) {
beforeAdd(consumer);
this.dateConsumer = consumer;
}
""")
.method("""
private boolean processDate(A activity,Intent intent) {
if(dateConsumer != null) {
doCheck(activity, dateConsumer);
beforeExecute(activity, dateConsumer);
Long date = intent.getLongExtra("date",0);
dateConsumer.accept(date);
afterExecute(activity, dateConsumer);
return true;
}
else {
return false;
}
}
""")
.field("BiConsumer<ArrayList,Character> textConsumer;",
.field("private BiConsumer<ArrayList,Character> textConsumer;",
["info.dourok.esactivity.function.BiConsumer", ArrayList, Character])
.method("""
void setTextConsumer(BiConsumer<ArrayList,Character> consumer) {
beforeAdd(consumer);
this.textConsumer = consumer;
}
""")
.method("""
private boolean processText(A activity,Intent intent) {
if(textConsumer != null) {
doCheck(activity, textConsumer);
beforeExecute(activity, textConsumer);
ArrayList ids = RefManager.getInstance().get(intent,"ids");
Character name = intent.getCharExtra("name",(char)0);
textConsumer.accept(ids,name);
afterExecute(activity, textConsumer);
return true;
}
else {
Expand Down
91 changes: 70 additions & 21 deletions compiler/src/test/groovy/info/dourok/compiler/ResultSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ResultSpec extends Specification {
.hasConsumer()
.method("""
public EmptyActivityBuilder<A> forText(Runnable textConsumer) {
getConsumer().textConsumer = textConsumer;
getConsumer().setTextConsumer(textConsumer);
return this;
}""", [Runnable])
.source()
Expand All @@ -36,12 +36,19 @@ class ResultSpec extends Specification {
}""")
.source()
def consumer = Source.consumer()
.field("Runnable textConsumer;", [Runnable])
.field("private Runnable textConsumer;", [Runnable])
.method("""
void setTextConsumer(Runnable consumer) {
beforeAdd(consumer);
this.textConsumer = consumer;
}
""")
.method("""
private boolean processText(A activity, Intent intent) {
if(textConsumer != null) {
doCheck(activity, textConsumer);
beforeExecute(activity, textConsumer);
textConsumer.run();
afterExecute(activity, textConsumer);
return true;
}
else {
Expand Down Expand Up @@ -88,7 +95,7 @@ class ResultSpec extends Specification {
.hasConsumer()
.method("""
public EmptyActivityBuilder<A> forText(Runnable textConsumer) {
getConsumer().textConsumer = textConsumer;
getConsumer().setTextConsumer(textConsumer);
return this;
}""", [Runnable])
.source()
Expand All @@ -105,12 +112,19 @@ class ResultSpec extends Specification {
}""")
.source()
def consumer = Source.consumer()
.field("Runnable textConsumer;", [Runnable])
.field("private Runnable textConsumer;", [Runnable])
.method("""
void setTextConsumer(Runnable consumer) {
beforeAdd(consumer);
this.textConsumer = consumer;
}
""")
.method("""
private boolean processText(A activity, Intent intent) {
if(textConsumer != null) {
doCheck(activity, textConsumer);
beforeExecute(activity, textConsumer);
textConsumer.run();
afterExecute(activity, textConsumer);
return true;
}
else {
Expand Down Expand Up @@ -157,7 +171,7 @@ class ResultSpec extends Specification {
.hasConsumer()
.method("""
public EmptyActivityBuilder<A> forText(BiConsumer<ArrayList, Character> textConsumer) {
getConsumer().textConsumer = textConsumer;
getConsumer().setTextConsumer(textConsumer);
return this;
}""", ["info.dourok.esactivity.function.BiConsumer", ArrayList, Character])
.source()
Expand All @@ -177,15 +191,22 @@ class ResultSpec extends Specification {
}""")
.source()
def consumer = Source.consumer()
.field("BiConsumer<ArrayList,Character> textConsumer;",
.field("private BiConsumer<ArrayList,Character> textConsumer;",
["info.dourok.esactivity.function.BiConsumer", ArrayList, Character])
.method("""
void setTextConsumer(BiConsumer<ArrayList,Character> consumer) {
beforeAdd(consumer);
this.textConsumer = consumer;
}
""")
.method("""
private boolean processText(A activity,Intent intent) {
if(textConsumer != null) {
doCheck(activity, textConsumer);
beforeExecute(activity, textConsumer);
ArrayList ids = RefManager.getInstance().get(intent,"ids");
Character name = intent.getCharExtra("name",(char)0);
textConsumer.accept(ids,name);
afterExecute(activity, textConsumer);
return true;
}
else {
Expand Down Expand Up @@ -232,7 +253,7 @@ class ResultSpec extends Specification {
.hasConsumer()
.method("""
public EmptyActivityBuilder<A> forDate(Consumer<Long> dateConsumer) {
getConsumer().dateConsumer = dateConsumer;
getConsumer().setDateConsumer(dateConsumer);
return this;
}""", ["info.dourok.esactivity.function.Consumer", Long])
.source()
Expand All @@ -251,14 +272,21 @@ class ResultSpec extends Specification {
}""")
.source()
def consumer = Source.consumer()
.field("Consumer<Long> dateConsumer;",
.field("private Consumer<Long> dateConsumer;",
["info.dourok.esactivity.function.Consumer", Long])
.method("""
void setDateConsumer(Consumer<Long> consumer) {
beforeAdd(consumer);
this.dateConsumer = consumer;
}
""")
.method("""
private boolean processDate(A activity,Intent intent) {
if(dateConsumer != null) {
doCheck(activity, dateConsumer);
beforeExecute(activity, dateConsumer);
long date = intent.getLongExtra("date",0);
dateConsumer.accept(date);
afterExecute(activity, dateConsumer);
return true;
}
else {
Expand Down Expand Up @@ -305,7 +333,7 @@ class ResultSpec extends Specification {
.hasConsumer()
.method("""
public EmptyActivityBuilder<A> forMap(Consumer<Map<String,ArrayList<Integer>>> mapConsumer) {
getConsumer().mapConsumer = mapConsumer;
getConsumer().setMapConsumer(mapConsumer);
return this;
}""", ["info.dourok.esactivity.function.Consumer", Map, String, ArrayList, Integer])

Expand All @@ -325,14 +353,21 @@ class ResultSpec extends Specification {
}""")
.source()
def consumer = Source.consumer()
.field("Consumer<Map<String,ArrayList<Integer>>> mapConsumer;",
.field("private Consumer<Map<String,ArrayList<Integer>>> mapConsumer;",
["info.dourok.esactivity.function.Consumer", Map, String, ArrayList, Integer])
.method("""
void setMapConsumer(Consumer<Map<String,ArrayList<Integer>>> consumer) {
beforeAdd(consumer);
this.mapConsumer = consumer;
}
""")
.method("""
private boolean processMap(A activity,Intent intent) {
if(mapConsumer != null) {
doCheck(activity, mapConsumer);
beforeExecute(activity, mapConsumer);
Map<String,ArrayList<Integer>> map = RefManager.getInstance().get(intent,"map");
mapConsumer.accept(map);
afterExecute(activity, mapConsumer);
return true;
}
else {
Expand Down Expand Up @@ -384,12 +419,12 @@ class ResultSpec extends Specification {
.hasConsumer()
.method("""
public EmptyActivityBuilder<A> forDate(Consumer<Long> dateConsumer) {
getConsumer().dateConsumer = dateConsumer;
getConsumer().setDateConsumer(dateConsumer);
return this;
}""", ["info.dourok.esactivity.function.Consumer", Long])
.method("""
public EmptyActivityBuilder<A> forText(BiConsumer<ArrayList, Character> textConsumer) {
getConsumer().textConsumer = textConsumer;
getConsumer().setTextConsumer(textConsumer);
return this;
}""", ["info.dourok.esactivity.function.BiConsumer", ArrayList, Character])
.source()
Expand Down Expand Up @@ -421,30 +456,44 @@ class ResultSpec extends Specification {
}""")
.source()
def consumer = Source.consumer()
.field("Consumer<Long> dateConsumer;",
.field("private Consumer<Long> dateConsumer;",
["info.dourok.esactivity.function.Consumer", Long])
.method("""
void setDateConsumer(Consumer<Long> consumer) {
beforeAdd(consumer);
this.dateConsumer = consumer;
}
""")
.method("""
private boolean processDate(A activity,Intent intent) {
if(dateConsumer != null) {
doCheck(activity, dateConsumer);
beforeExecute(activity, dateConsumer);
long date = intent.getLongExtra("date",0);
dateConsumer.accept(date);
afterExecute(activity, dateConsumer);
return true;
}
else {
return false;
}
}
""")
.field("BiConsumer<ArrayList,Character> textConsumer;",
.field("private BiConsumer<ArrayList,Character> textConsumer;",
["info.dourok.esactivity.function.BiConsumer", ArrayList, Character])
.method("""
void setTextConsumer(BiConsumer<ArrayList,Character> consumer) {
beforeAdd(consumer);
this.textConsumer = consumer;
}
""")
.method("""
private boolean processText(A activity,Intent intent) {
if(textConsumer != null) {
doCheck(activity, textConsumer);
beforeExecute(activity, textConsumer);
ArrayList ids = RefManager.getInstance().get(intent,"ids");
Character name = intent.getCharExtra("name",(char)0);
textConsumer.accept(ids,name);
afterExecute(activity, textConsumer);
return true;
}
else {
Expand Down
6 changes: 5 additions & 1 deletion compiler/src/test/resources/mock/BaseResultConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ public final void accept(Activity context, Integer result, Intent intent) {
//
}

protected void doCheck(Activity activity, Object lambda) {}
public void beforeAdd(Object closure) {}

public void beforeExecute(Activity activity, Object closure) {}

public void afterExecute(Activity activity, Object closure) {}

protected boolean handleResult(A context, int result, Intent intent) {
return false;
Expand Down
1 change: 0 additions & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ dependencies {

implementation deps.support.annotations
implementation deps.support.fragment
implementation 'com.android.support:support-v4:26.1.0'
androidTestImplementation 'com.android.support:appcompat-v7:27.0.2'
androidTestImplementation 'com.android.support.constraint:constraint-layout:1.0.2'
androidTestAnnotationProcessor project(':compiler')
Expand Down
Loading

0 comments on commit 9870506

Please sign in to comment.