This repository was archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Support instance state for views #1911
Merged
WonderCsabo
merged 3 commits into
androidannotations:develop
from
dodgex:1312_InstanceState_support_for_Views
Dec 10, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...notations-test/src/main/java/org/androidannotations/test/eview/ViewWithInstanceState.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /** | ||
| * Copyright (C) 2016 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 | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed To in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package org.androidannotations.test.eview; | ||
|
|
||
| import org.androidannotations.annotations.EView; | ||
| import org.androidannotations.annotations.InstanceState; | ||
| import org.androidannotations.test.instancestate.MyParcelableBean; | ||
|
|
||
| import android.content.Context; | ||
| import android.os.Bundle; | ||
| import android.os.Parcelable; | ||
| import android.view.View; | ||
|
|
||
| @EView | ||
| public class ViewWithInstanceState extends View { | ||
|
|
||
| @InstanceState | ||
| String stateTest = "does it work?"; | ||
|
|
||
| String stringState = "does it work?"; | ||
| MyParcelableBean beanState = new MyParcelableBean(0); | ||
|
|
||
| public ViewWithInstanceState(Context context) { | ||
| super(context); | ||
| } | ||
|
|
||
| @Override | ||
| public Parcelable onSaveInstanceState() { | ||
| Bundle bundle = new Bundle(); | ||
| bundle.putString("stringState", stringState); | ||
| bundle.putParcelable("beanState", beanState); | ||
| return bundle; | ||
| } | ||
|
|
||
| @Override | ||
| public void onRestoreInstanceState(Parcelable state) { | ||
| Bundle bundle = (Bundle) state; | ||
| stringState = bundle.getString("stringState"); | ||
| beanState = bundle.getParcelable("beanState"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...notations-test/src/test/java/org/androidannotations/test/eview/ViewInstanceStateTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /** | ||
| * Copyright (C) 2016 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 | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed To in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package org.androidannotations.test.eview; | ||
|
|
||
| import static org.fest.assertions.api.Assertions.assertThat; | ||
|
|
||
| import org.androidannotations.test.EmptyActivityWithoutLayout_; | ||
| import org.androidannotations.test.instancestate.MyParcelableBean; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.robolectric.Robolectric; | ||
| import org.robolectric.RobolectricTestRunner; | ||
|
|
||
| import android.content.Context; | ||
| import android.os.Parcelable; | ||
|
|
||
| @RunWith(RobolectricTestRunner.class) | ||
| public class ViewInstanceStateTest { | ||
|
|
||
| @Test | ||
| public void viewInstanceStateSaveTest() { | ||
| Context context = Robolectric.buildActivity(EmptyActivityWithoutLayout_.class).create().get(); | ||
| ViewWithInstanceState_ savingView = (ViewWithInstanceState_) ViewWithInstanceState_.build(context); | ||
|
|
||
| assertThat(savingView.stateTest).isEqualTo("does it work?"); | ||
| assertThat(savingView.stringState).isEqualTo("does it work?"); | ||
| assertThat(savingView.beanState.getX()).isEqualTo(0); | ||
|
|
||
| savingView.stateTest = "it works!"; | ||
| savingView.stringState = "it works!"; | ||
| savingView.beanState = new MyParcelableBean(1); | ||
| Parcelable savedInstanceState = savingView.onSaveInstanceState(); | ||
|
|
||
| ViewWithInstanceState_ restoringView = (ViewWithInstanceState_) ViewWithInstanceState_.build(context); | ||
| assertThat(restoringView.stateTest).isEqualTo("does it work?"); | ||
| assertThat(restoringView.stringState).isEqualTo("does it work?"); | ||
| assertThat(restoringView.beanState.getX()).isEqualTo(0); | ||
|
|
||
| restoringView.onRestoreInstanceState(savedInstanceState); | ||
| assertThat(restoringView.stateTest).isEqualTo("it works!"); | ||
| assertThat(restoringView.stringState).isEqualTo("it works!"); | ||
| assertThat(restoringView.beanState.getX()).isEqualTo(1); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...oidannotations/src/main/java/org/androidannotations/holder/ViewInstanceStateDelegate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /** | ||
| * Copyright (C) 2016 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 | ||
| * the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed To in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package org.androidannotations.holder; | ||
|
|
||
| import static com.helger.jcodemodel.JExpr._new; | ||
| import static com.helger.jcodemodel.JExpr._super; | ||
| import static com.helger.jcodemodel.JExpr.cast; | ||
| import static com.helger.jcodemodel.JExpr.lit; | ||
| import static com.helger.jcodemodel.JMod.FINAL; | ||
| import static com.helger.jcodemodel.JMod.PRIVATE; | ||
| import static com.helger.jcodemodel.JMod.PUBLIC; | ||
| import static com.helger.jcodemodel.JMod.STATIC; | ||
| import static org.androidannotations.helper.ModelConstants.generationSuffix; | ||
|
|
||
| import com.helger.jcodemodel.JBlock; | ||
| import com.helger.jcodemodel.JMethod; | ||
| import com.helger.jcodemodel.JVar; | ||
|
|
||
| public class ViewInstanceStateDelegate extends GeneratedClassHolderDelegate<EComponentHolder> implements HasInstanceState { | ||
|
|
||
| private JVar instanceStateKey; | ||
| private JBlock saveStateMethodBody; | ||
| private JVar saveStateBundleParam; | ||
| private JMethod restoreStateMethod; | ||
| private JVar restoreStateBundleParam; | ||
|
|
||
| public ViewInstanceStateDelegate(EComponentHolder holder) { | ||
| super(holder); | ||
| } | ||
|
|
||
| @Override | ||
| public JBlock getSaveStateMethodBody() { | ||
| if (saveStateMethodBody == null) { | ||
| setSaveStateMethod(); | ||
| } | ||
| return saveStateMethodBody; | ||
| } | ||
|
|
||
| @Override | ||
| public JVar getSaveStateBundleParam() { | ||
| if (saveStateBundleParam == null) { | ||
| setSaveStateMethod(); | ||
| } | ||
| return saveStateBundleParam; | ||
| } | ||
|
|
||
| private void setSaveStateMethod() { | ||
| JMethod method = getGeneratedClass().method(PUBLIC, getClasses().PARCELABLE, "onSaveInstanceState"); | ||
| method.annotate(Override.class); | ||
|
|
||
| JMethod saveStateMethod = getGeneratedClass().method(PRIVATE, codeModel().VOID, "saveInstanceState"); | ||
| saveStateBundleParam = saveStateMethod.param(getClasses().BUNDLE, "bundle"); | ||
| saveStateMethodBody = saveStateMethod.body(); | ||
|
|
||
| JBlock methodBody = method.body(); | ||
| JVar onSaveSuperInstanceState = methodBody.decl(getClasses().PARCELABLE, "instanceState", _super().invoke("onSaveInstanceState")); | ||
|
|
||
| JVar bundleParam = methodBody.decl(getClasses().BUNDLE, "bundle" + generationSuffix(), _new(getClasses().BUNDLE)); | ||
| methodBody.invoke(bundleParam, "putParcelable").arg(getInstanceStateKey()).arg(onSaveSuperInstanceState); | ||
|
|
||
| methodBody.invoke(saveStateMethod).arg(bundleParam); | ||
|
|
||
| methodBody._return(bundleParam); | ||
| } | ||
|
|
||
| @Override | ||
| public JMethod getRestoreStateMethod() { | ||
| if (restoreStateMethod == null) { | ||
| setRestoreStateMethod(); | ||
| } | ||
| return restoreStateMethod; | ||
| } | ||
|
|
||
| @Override | ||
| public JVar getRestoreStateBundleParam() { | ||
| if (restoreStateBundleParam == null) { | ||
| setRestoreStateMethod(); | ||
| } | ||
| return restoreStateBundleParam; | ||
| } | ||
|
|
||
| private void setRestoreStateMethod() { | ||
| restoreStateMethod = getGeneratedClass().method(PUBLIC, codeModel().VOID, "onRestoreInstanceState"); | ||
| restoreStateMethod.annotate(Override.class); | ||
| JVar state = restoreStateMethod.param(getClasses().PARCELABLE, "state"); | ||
|
|
||
| JBlock restoreStateMethodBody = restoreStateMethod.body(); | ||
| restoreStateBundleParam = restoreStateMethodBody.decl(getClasses().BUNDLE, "bundle" + generationSuffix(), cast(getClasses().BUNDLE, state)); | ||
| JVar instanceState = restoreStateMethodBody.decl(getClasses().PARCELABLE, "instanceState", restoreStateBundleParam.invoke("getParcelable").arg(getInstanceStateKey())); | ||
| restoreStateMethodBody.invoke(_super(), "onRestoreInstanceState").arg(instanceState); | ||
| } | ||
|
|
||
| private JVar getInstanceStateKey() { | ||
| if (instanceStateKey == null) { | ||
| instanceStateKey = getGeneratedClass().field(PUBLIC | STATIC | FINAL, getClasses().STRING, "INSTANCE_STATE_KEY", lit("instanceState")); | ||
| } | ||
| return instanceStateKey; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think we should have another test, which asserts we correctly retain the saved instance information from the super class. So there should be a non-AA Android custom View class, which have
onSaveInstanceState()andonRestoreInstanceState(), then an enhanced class should subclass it, and add@InstanceState. We should assert that the state restoration in parent works as well.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.
do we need an extra non aa custom view? shouldn't it be enough to have custom save/restore in this view class?
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.
It is enough, if you can check if the state in the superclass is properly saved, however i think you cannot do that. Maybe you can extend from another
Viewclass, which saves state we can assert against, but the most clean would be writing a non-AA customView.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 added this case into the current one. hope it is ok?