From 7b10e5e8de9488b43178b6c1c15190e2aed86693 Mon Sep 17 00:00:00 2001 From: FrantisekGazo Date: Thu, 13 Apr 2017 23:36:23 +0200 Subject: [PATCH 1/2] Add module combination test --- .../combination/CombinedSpecification.groovy | 119 +++++++++++++++++- 1 file changed, 114 insertions(+), 5 deletions(-) diff --git a/core-compiler/src/test/groovy/eu/f3rog/blade/compiler/combination/CombinedSpecification.groovy b/core-compiler/src/test/groovy/eu/f3rog/blade/compiler/combination/CombinedSpecification.groovy index df51546..4723a4b 100644 --- a/core-compiler/src/test/groovy/eu/f3rog/blade/compiler/combination/CombinedSpecification.groovy +++ b/core-compiler/src/test/groovy/eu/f3rog/blade/compiler/combination/CombinedSpecification.groovy @@ -14,6 +14,7 @@ import eu.f3rog.blade.compiler.BladeProcessor import eu.f3rog.blade.compiler.util.JavaFile import eu.f3rog.blade.core.BundleWrapper import eu.f3rog.blade.core.Weave +import eu.f3rog.blade.mvp.WeavedMvpActivity import eu.f3rog.blade.mvp.WeavedMvpFragment import spock.lang.Unroll @@ -24,7 +25,7 @@ public final class CombinedSpecification extends BaseSpecification { @Unroll - def "generate _Helper for a class with a @Arg+@State field (#annotations)"() { + def "generate _Helper for a Fragment class with a @Arg+@State field (#annotations)"() { given: final JavaFileObject input = JavaFile.newFile("com.example", "MyFragment", """ @@ -109,7 +110,7 @@ public final class CombinedSpecification } @Unroll - def "generate _Helper for a class with a @Extra+@State field (#annotations)"() { + def "generate _Helper for an Activity class with a @Extra+@State field (#annotations)"() { given: final JavaFileObject input = JavaFile.newFile("com.example", "MyActivity", """ @@ -196,7 +197,7 @@ public final class CombinedSpecification } @Unroll - def "generate _Helper for a class with #field1 #field2 #field3"() { + def "generate _Helper for a Fragment class with #field1 #field2 #field3"() { given: final JavaFileObject presenter = JavaFile.newFile("com.example", "MyPresenter", """ @@ -249,7 +250,7 @@ public final class CombinedSpecification @Weave( into = "0_onSaveInstanceState", args = {"android.os.Bundle"}, - statement = "com.example.#T.saveState(this, \\\$1);" + statement = "com.example.#T.saveState(this, \$1);" ) public static void saveState(#I target, Bundle state) { if (state == null) { @@ -262,7 +263,7 @@ public final class CombinedSpecification @Weave( into = "1^onCreate", args = {"android.os.Bundle"}, - statement = "com.example.#T.restoreState(this, \\\$1);" + statement = "com.example.#T.restoreState(this, \$1);" ) public static void restoreState(#I target, Bundle state) { if (state == null) { @@ -300,4 +301,112 @@ public final class CombinedSpecification '@#S boolean mFlag;' | '@#A String mTitle;' | '@#I #P mPresenter;' | _ '@#S boolean mFlag;' | '@#I #P mPresenter;' | '@#A String mTitle;' | _ } + + @Unroll + def "generate _Helper for an Activity class with #field1 #field2 #field3"() { + given: + final JavaFileObject presenter = JavaFile.newFile("com.example", "MyPresenter", + """ + public class #T extends #P<#V> { + } + """, + [ + P: BasePresenter.class, + V: IView.class, + _: [] + ] + ) + final JavaFileObject input = JavaFile.newFile("com.example", "MyActivity", + """ + public class #T extends Activity implements #V { + + $field1 + $field2 + $field3 + } + """, + [ + E: Extra.class, + I: Inject.class, + S: State.class, + P: presenter, + V: IView.class, + _: [Activity.class] + ] + ) + + expect: + final JavaFileObject expected = JavaFile.newGeneratedFile("com.example", "MyActivity_Helper", + """ + abstract class #T implements WeavedMvpActivity { + + @Weave( + into="0^onCreate", + args = {"android.os.Bundle"}, + statement = "com.example.#T.inject(this);" + ) + public static void inject(#I target) { + Intent intent = target.getIntent(); + if (intent == null || intent.getExtras() == null) { + return; + } + BundleWrapper extras = BundleWrapper.from(intent.getExtras()); + target.mTitle = extras.get("", target.mTitle); + } + + @Weave( + into = "0_onSaveInstanceState", + args = {"android.os.Bundle"}, + statement = "com.example.#T.saveState(this, \$1);" + ) + public static void saveState(#I target, Bundle state) { + if (state == null) { + throw new #E("State cannot be null!"); + } + BundleWrapper bundleWrapper = BundleWrapper.from(state); + bundleWrapper.put("", target.mFlag); + } + + @Weave( + into = "1^onCreate", + args = {"android.os.Bundle"}, + statement = "com.example.#T.restoreState(this, \$1);" + ) + public static void restoreState(#I target, Bundle state) { + if (state == null) { + return; + } + BundleWrapper bundleWrapper = BundleWrapper.from(state); + target.mFlag = bundleWrapper.get("", target.mFlag); + } + } + """, + [ + I: input, + E: IllegalArgumentException.class, + _: [ + Bundle.class, + BundleWrapper.class, + Intent.class, + Weave.class, + WeavedMvpActivity.class + ] + ] + ) + + assertFiles(presenter, input) + .with(BladeProcessor.Module.EXTRA, BladeProcessor.Module.MVP, BladeProcessor.Module.STATE) + .compilesWithoutError() + .and() + .generatesSources(expected) + + where: + field1 | field2 | field3 | _ + '@#E String mTitle;' | '@#I #P mPresenter;' | '@#S boolean mFlag;' | _ + '@#E String mTitle;' | '@#S boolean mFlag;' | '@#I #P mPresenter;' | _ + '@#I #P mPresenter;' | '@#S boolean mFlag;' | '@#E String mTitle;' | _ + '@#I #P mPresenter;' | '@#E String mTitle;' | '@#S boolean mFlag;' | _ + '@#S boolean mFlag;' | '@#E String mTitle;' | '@#I #P mPresenter;' | _ + '@#S boolean mFlag;' | '@#I #P mPresenter;' | '@#E String mTitle;' | _ + } } \ No newline at end of file From 89a03ce3719061d443d61d4b0a6ccb71e295efe3 Mon Sep 17 00:00:00 2001 From: FrantisekGazo Date: Sun, 16 Apr 2017 21:06:52 +0200 Subject: [PATCH 2/2] Fix presenter state restoration --- CHANGELOG.md | 26 ++++++++++++------- gradle.properties | 2 +- .../main/java/blade/mvp/PresenterManager.java | 11 ++++++-- .../eu/f3rog/blade/plugin/BladePlugin.groovy | 2 +- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index deb36ce..9bff307 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,44 +1,50 @@ Change Log ========== +Version 2.6.3 *(2017-04-16)* +---------------------------- + + * Fix: Presenter state restoration. + * New: Add more unit tests. + Version 2.6.2 *(2017-03-11)* ---------------------------- - * Fix: Processing of `@Inject` annotations for `mvp` module + * Fix: Processing of `@Inject` annotations for `mvp` module. Version 2.6.1 *(2017-01-28)* ---------------------------- - * Fix: `mvp` generated Activity classes + * Fix: `mvp` generated Activity classes. Version 2.6.0 *(2017-01-01)* ---------------------------- - * New: Add support for custom `Bundler` for `@Arg`, `@Extra` and `@State` + * New: Add support for custom `Bundler` for `@Arg`, `@Extra` and `@State`. Version 2.5.1 *(2016-12-26)* ---------------------------- - * Change: Configuration file is now required + * Change: Configuration file is now required. Version 2.5.0 *(2016-12-26)* ---------------------------- - * Change: Add support for android gradle plugin 2.2+ `annotationProcessor`. (android-apt is no longer added automatically) - * New: Add support for **YAML** format of configuration file `blade.yaml` + * Change: Add support for android gradle plugin 2.2+ `annotationProcessor`. (android-apt is no longer added automatically). + * New: Add support for **YAML** format of configuration file `blade.yaml`. Version 2.4.0 *(2016-12-07)* ---------------------------- - * Change: Refactor **mvp** module to use Dagger2 and support Fragments - * Fix: `@State` inside android View subclasses + * Change: Refactor **mvp** module to use Dagger2 and support Fragments. + * Fix: `@State` inside android View subclasses. Version 2.3.0 *(2016-10-28)* ---------------------------- * New: Add more plugin tests - * Fix: Refactor code for bytecode weaving - * Fix: Handle order of weaved code correctly + * Fix: Refactor code for bytecode weaving. + * Fix: Handle order of weaved code correctly. Version 2.2.0 *(2016-05-21)* ---------------------------- diff --git a/gradle.properties b/gradle.properties index f9fbd7b..c61a456 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ org.gradle.jvmargs=-Xmx1536M LIB_GROUP_ID = eu.f3rog.blade ARTIFACT_ID = none -LIB_VERSION = 2.6.2 +LIB_VERSION = 2.6.3 LIB_VERSION_DESC = Android Library for Boilerplate Destruction diff --git a/module/mvp/src/main/java/blade/mvp/PresenterManager.java b/module/mvp/src/main/java/blade/mvp/PresenterManager.java index 1ace20c..be63b92 100644 --- a/module/mvp/src/main/java/blade/mvp/PresenterManager.java +++ b/module/mvp/src/main/java/blade/mvp/PresenterManager.java @@ -189,7 +189,9 @@ P get(@NonNull final V view, @NonNull final String fieldName, @NonNull final Pro presenter = provider.get(); apm.put(view, fieldName, presenter); - presenter.onCreate(view.getWeavedState()); + final Bundle viewState = view.getWeavedState(); + final Bundle presenterState = (viewState != null) ? viewState.getBundle(getPresenterStateKey(fieldName)) : null; + presenter.onCreate(presenterState); } presenter.onBind(view); @@ -216,7 +218,12 @@ void save(@NonNull final Bundle outState, @NonNull final String fieldName, @Null final Bundle presenterState = new Bundle(); presenter.onSaveState(presenterState); - outState.putBundle(String.format(STATE_VIEW_PRESENTER, fieldName), presenterState); + outState.putBundle(getPresenterStateKey(fieldName), presenterState); + } + + @NonNull + private String getPresenterStateKey(@NonNull final String fieldName) { + return String.format(STATE_VIEW_PRESENTER, fieldName); } //endregion save presenter diff --git a/plugin/src/main/groovy/eu/f3rog/blade/plugin/BladePlugin.groovy b/plugin/src/main/groovy/eu/f3rog/blade/plugin/BladePlugin.groovy index 2523240..d86973b 100644 --- a/plugin/src/main/groovy/eu/f3rog/blade/plugin/BladePlugin.groovy +++ b/plugin/src/main/groovy/eu/f3rog/blade/plugin/BladePlugin.groovy @@ -32,7 +32,7 @@ public final class BladePlugin public static String ERROR_CONFIG_FILE_IS_MISSING = "Blade configuration file is missing! (more info here: https://github.com/FrantisekGazo/Blade/wiki#1-create-configuration-file)" public static String LIB_GROUP_ID = "eu.f3rog.blade" - public static String LIB_VERSION = "2.6.2" + public static String LIB_VERSION = "2.6.3" public static String LIB_CONFIG_FILE_NAME = "blade" public static String[] LIB_MODULES = ["arg", "extra", "mvp", "parcel", "state"]