-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add support for new method signature of findViewById in Android O #2008
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| /** | ||
| * Copyright (C) 2010-2016 eBusiness Information, Excilys Group | ||
| * Copyright (C) 2016-2017 the AndroidAnnotations project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
| * use this file except in compliance with the License. You may obtain a copy of | ||
|
|
@@ -20,25 +21,34 @@ | |
| import static com.helger.jcodemodel.JExpr._this; | ||
| import static com.helger.jcodemodel.JMod.FINAL; | ||
| import static com.helger.jcodemodel.JMod.PRIVATE; | ||
| import static com.helger.jcodemodel.JMod.PUBLIC; | ||
| import static org.androidannotations.helper.ModelConstants.generationSuffix; | ||
|
|
||
| import org.androidannotations.AndroidAnnotationsEnvironment; | ||
| import org.androidannotations.api.view.HasViews; | ||
| import org.androidannotations.api.view.OnViewChangedNotifier; | ||
| import org.androidannotations.holder.EComponentHolder; | ||
| import org.androidannotations.holder.EComponentWithViewSupportHolder; | ||
| import org.androidannotations.holder.EViewHolder; | ||
|
|
||
| import com.helger.jcodemodel.AbstractJClass; | ||
| import com.helger.jcodemodel.IJExpression; | ||
| import com.helger.jcodemodel.JBlock; | ||
| import com.helger.jcodemodel.JCodeModel; | ||
| import com.helger.jcodemodel.JDirectClass; | ||
| import com.helger.jcodemodel.JExpr; | ||
| import com.helger.jcodemodel.JFieldVar; | ||
| import com.helger.jcodemodel.JMethod; | ||
| import com.helger.jcodemodel.JVar; | ||
|
|
||
| public class ViewNotifierHelper { | ||
|
|
||
| private EComponentHolder holder; | ||
| private AndroidAnnotationsEnvironment environment; | ||
| private EComponentWithViewSupportHolder holder; | ||
| private JFieldVar notifier; | ||
|
|
||
| public ViewNotifierHelper(EComponentHolder holder) { | ||
| public ViewNotifierHelper(EComponentWithViewSupportHolder holder, AndroidAnnotationsEnvironment environment) { | ||
| this.holder = holder; | ||
| this.environment = environment; | ||
| } | ||
|
|
||
| public void invokeViewChanged(JBlock block) { | ||
|
|
@@ -49,11 +59,25 @@ public JVar replacePreviousNotifier(JBlock block) { | |
| AbstractJClass notifierClass = holder.getEnvironment().getJClass(OnViewChangedNotifier.class); | ||
| if (notifier == null) { | ||
| notifier = holder.getGeneratedClass().field(PRIVATE | FINAL, notifierClass, "onViewChangedNotifier" + generationSuffix(), _new(notifierClass)); | ||
| holder.getGeneratedClass()._implements(HasViews.class); | ||
| implementHasViewsInHolder(); | ||
| } | ||
| return block.decl(notifierClass, "previousNotifier", notifierClass.staticInvoke("replaceNotifier").arg(notifier)); | ||
| } | ||
|
|
||
| private void implementHasViewsInHolder() { | ||
| holder.getGeneratedClass()._implements(HasViews.class); | ||
| JCodeModel codeModel = environment.getCodeModel(); | ||
|
|
||
| JDirectClass genericType = codeModel.directClass("T"); | ||
| JMethod findViewById = holder.getGeneratedClass().method(PUBLIC, genericType, "internalFindViewById"); | ||
| findViewById.generify("T", environment.getClasses().VIEW); | ||
| findViewById.annotate(Override.class); | ||
|
|
||
| JVar idParam = findViewById.param(codeModel.INT, "id"); | ||
| IJExpression findViewByIdExpression = holder.getFindViewByIdExpression(idParam); | ||
| findViewById.body()._return(JExpr.cast(genericType, findViewByIdExpression)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two things:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Additional Note to 1: It looks like the AppCompatActivity does not yet have the generic i checked the code in android-o-preview-2 and master branches.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, it would be nice to gain something from this new signature, if we doing so much work because of it. 😆 Since you already have the check code, i guess you can add it now.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, they are overriding
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i just found that
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool. Then try to remove the cast with O.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The cast is now gone for Android O. But it now requires to be on appcompat 26.0.0-beta2 (maybe beta1 works too; alpha1 does not). |
||
| } | ||
|
|
||
| public JVar replacePreviousNotifierWithNull(JBlock block) { | ||
| AbstractJClass notifierClass = holder.getEnvironment().getJClass(OnViewChangedNotifier.class); | ||
| return block.decl(notifierClass, "previousNotifier", notifierClass.staticInvoke("replaceNotifier").arg(_null())); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
getFindViewByIdExpressionshould be anabstractmethod?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was considering this. Then we'd have the
return _null()inEBeanHolder. I decided to keep it this way, but with no solid reason. If you prefer to have this abstract I can change it.