Skip to content

Commit b3c3efb

Browse files
committed
Implementing view scopes (androidannotations#331) by introducing a new "onViewChanged" abstract, with an event bus kind of structure.
<no-one-cares-but-Im-happy> This big momma commit was coded on a flight to SF! The plane is currently between Montreal and Las Vegas, and I'm about to run out of battery. Maven builds + two eclipse instances + jrebel = an empty battery. Time to watch Battlestar Galactica! </no-one-cares-but-Im-happy>
1 parent 580552a commit b3c3efb

58 files changed

Lines changed: 425 additions & 213 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎AndroidAnnotations/androidannotations-api/META-INF/MANIFEST.MF‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Export-Package: org.androidannotations.annotations,
77
org.androidannotations.annotations.res,
88
org.androidannotations.annotations.rest,
99
org.androidannotations.annotations.sharedpreferences,
10-
org.androidannotations.api.rest
10+
org.androidannotations.api.rest,
11+
org.androidannotations.api.view
1112
Bundle-Vendor: androidannotations
1213
Bundle-Activator: org.androidannotations.api.Activator
1314
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.androidannotations.api.view;
2+
3+
import android.view.View;
4+
5+
public interface HasViews {
6+
View findViewById(int id);
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.androidannotations.api.view;
2+
3+
public interface OnViewChangedListener {
4+
void onViewChanged(HasViews hasViews);
5+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.androidannotations.api.view;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
public class OnViewChangedNotifier {
7+
8+
private static OnViewChangedNotifier currentNotifier;
9+
10+
public static OnViewChangedNotifier replaceNotifier(OnViewChangedNotifier notifier) {
11+
OnViewChangedNotifier previousNotifier = currentNotifier;
12+
currentNotifier = notifier;
13+
return previousNotifier;
14+
}
15+
16+
public static void registerOnViewChangedListener(OnViewChangedListener listener) {
17+
if (currentNotifier != null) {
18+
currentNotifier.listeners.add(listener);
19+
}
20+
}
21+
22+
private final List<OnViewChangedListener> listeners = new LinkedList<OnViewChangedListener>();
23+
24+
public void notifyViewChanged(HasViews hasViews) {
25+
for (OnViewChangedListener listener : listeners) {
26+
listener.onViewChanged(hasViews);
27+
}
28+
}
29+
30+
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/APTCodeModelHelper.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public JBlock ifContextInstanceOfActivity(EBeanHolder holder, JBlock methodBody)
268268
return methodBody._if(holder.contextRef._instanceof(holder.classes().ACTIVITY))._then();
269269
}
270270

271-
public void copyConstructorsAndAddStaticEViewBuilders(Element element, JCodeModel codeModel, JClass eBeanClass, EBeanHolder holder, JMethod setContentViewMethod) {
271+
public void copyConstructorsAndAddStaticEViewBuilders(Element element, JCodeModel codeModel, JClass eBeanClass, EBeanHolder holder, JMethod setContentViewMethod, JMethod init) {
272272
List<ExecutableElement> constructors = new ArrayList<ExecutableElement>();
273273
for (Element e : element.getEnclosedElements()) {
274274
if (e.getKind() == CONSTRUCTOR) {
@@ -294,7 +294,7 @@ public void copyConstructorsAndAddStaticEViewBuilders(Element element, JCodeMode
294294
JVar newCall = staticHelper.body().decl(holder.generatedClass, "instance", newInvocation);
295295
staticHelper.body().invoke(newCall, "onFinishInflate");
296296
staticHelper.body()._return(newCall);
297-
body.invoke(holder.init);
297+
body.invoke(init);
298298
}
299299
}
300300

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/OnSeekBarChangeListenerHelper.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.androidannotations.helper;
1717

18+
import static com.sun.codemodel.JExpr.cast;
19+
1820
import java.lang.annotation.Annotation;
1921

2022
import javax.annotation.processing.ProcessingEnvironment;
@@ -23,6 +25,7 @@
2325
import org.androidannotations.processing.EBeanHolder;
2426
import org.androidannotations.processing.OnSeekBarChangeListenerHolder;
2527
import org.androidannotations.rclass.IRClass;
28+
2629
import com.sun.codemodel.JBlock;
2730
import com.sun.codemodel.JClass;
2831
import com.sun.codemodel.JCodeModel;
@@ -74,14 +77,14 @@ public OnSeekBarChangeListenerHolder getOrCreateListener(JCodeModel codeModel, E
7477
onStopTrackingTouchMethod.param(seekBarClass, "seekBar");
7578
onStopTrackingTouchMethod.annotate(Override.class);
7679

77-
JBlock block = holder.afterSetContentView.body().block();
80+
JBlock block = holder.onViewChanged().body().block();
7881

7982
TypeMirror viewParameterType = typeElementFromQualifiedName(CanonicalNameConstants.SEEKBAR).asType();
8083

8184
String viewParameterTypeString = viewParameterType.toString();
8285
JClass viewClass = holder.refClass(viewParameterTypeString);
8386

84-
JExpression findViewById = JExpr.cast(viewClass, JExpr.invoke("findViewById").arg(idRef));
87+
JExpression findViewById = cast(viewClass, holder.onViewChanged().findViewById(idRef));
8588

8689
JVar viewVariable = block.decl(JMod.FINAL, viewClass, "view", findViewById);
8790
block._if(viewVariable.ne(JExpr._null()))._then().invoke(viewVariable, "setOnSeekBarChangeListener").arg(JExpr._new(onSeekbarChangeListenerClass));

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/helper/TextWatcherHelper.java‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
package org.androidannotations.helper;
1717

18+
import static com.sun.codemodel.JExpr._new;
19+
import static com.sun.codemodel.JExpr.cast;
20+
import static com.sun.codemodel.JMod.FINAL;
21+
1822
import java.lang.annotation.Annotation;
1923

2024
import javax.annotation.processing.ProcessingEnvironment;
@@ -23,6 +27,7 @@
2327
import org.androidannotations.processing.EBeanHolder;
2428
import org.androidannotations.processing.TextWatcherHolder;
2529
import org.androidannotations.rclass.IRClass;
30+
2631
import com.sun.codemodel.JBlock;
2732
import com.sun.codemodel.JClass;
2833
import com.sun.codemodel.JCodeModel;
@@ -78,7 +83,7 @@ public TextWatcherHolder getOrCreateListener(JCodeModel codeModel, EBeanHolder h
7883
beforeTextChangedMethod.param(codeModel.INT, "after");
7984
beforeTextChangedMethod.annotate(Override.class);
8085

81-
JBlock block = holder.afterSetContentView.body().block();
86+
JBlock block = holder.onViewChanged().body().block();
8287

8388
JClass viewClass;
8489
if (viewParameterType != null) {
@@ -87,10 +92,10 @@ public TextWatcherHolder getOrCreateListener(JCodeModel codeModel, EBeanHolder h
8792
} else {
8893
viewClass = holder.classes().TEXT_VIEW;
8994
}
90-
JExpression findViewById = JExpr.cast(viewClass, JExpr.invoke("findViewById").arg(idRef));
95+
JExpression findViewById = cast(viewClass, holder.onViewChanged().findViewById(idRef));
9196

92-
JVar viewVariable = block.decl(JMod.FINAL, viewClass, "view", findViewById);
93-
block._if(viewVariable.ne(JExpr._null()))._then().invoke(viewVariable, "addTextChangedListener").arg(JExpr._new(onTextChangeListenerClass));
97+
JVar viewVariable = block.decl(FINAL, viewClass, "view", findViewById);
98+
block._if(viewVariable.ne(JExpr._null()))._then().invoke(viewVariable, "addTextChangedListener").arg(_new(onTextChangeListenerClass));
9499

95100
textWatcherHolder = new TextWatcherHolder(//
96101
afterTextChangedMethod, //

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/AfterInjectProcessor.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Class<? extends Annotation> getTarget() {
3333
public void process(Element element, JCodeModel codeModel, EBeanHolder holder) {
3434
String methodName = element.getSimpleName().toString();
3535

36-
holder.init.body().invoke(methodName);
36+
holder.initBody.invoke(methodName);
3737
}
3838

3939
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/AfterViewsProcessor.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import javax.lang.model.element.Element;
2121

2222
import org.androidannotations.annotations.AfterViews;
23+
2324
import com.sun.codemodel.JCodeModel;
2425

2526
public class AfterViewsProcessor implements DecoratingElementProcessor {
@@ -33,7 +34,7 @@ public Class<? extends Annotation> getTarget() {
3334
public void process(Element element, JCodeModel codeModel, EBeanHolder holder) {
3435
String methodName = element.getSimpleName().toString();
3536

36-
holder.afterSetContentView.body().invoke(methodName);
37+
holder.onViewChanged().body().invoke(methodName);
3738
}
3839

3940
}

‎AndroidAnnotations/androidannotations/src/main/java/org/androidannotations/processing/AppProcessor.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void process(Element element, JCodeModel codeModel, EBeanHolder holder) {
4242

4343
String fieldName = element.getSimpleName().toString();
4444

45-
holder.init.body().assign(ref(fieldName), applicationClass.staticInvoke(EApplicationProcessor.GET_APPLICATION_INSTANCE));
45+
holder.initBody.assign(ref(fieldName), applicationClass.staticInvoke(EApplicationProcessor.GET_APPLICATION_INSTANCE));
4646

4747
}
4848

0 commit comments

Comments
 (0)