Skip to content

Commit

Permalink
Merge pull request #32 from FrantisekGazo/develop
Browse files Browse the repository at this point in the history
Fix presenter state restoration
  • Loading branch information
FrantisekGazo authored Apr 16, 2017
2 parents 14819f4 + 89a03ce commit a5df6ad
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 19 deletions.
26 changes: 16 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)*
----------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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",
"""
Expand Down Expand Up @@ -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",
"""
Expand Down Expand Up @@ -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",
"""
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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("<Extra-mTitle>", 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("<Stateful-mFlag>", 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("<Stateful-mFlag>", 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;' | _
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions module/mvp/src/main/java/blade/mvp/PresenterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down

0 comments on commit a5df6ad

Please sign in to comment.