Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Parceler support #20

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ protected String emitRestoreState(AnnotatedField field) {
case SERIALIZABLE:
return " target." + field.getName() + " = (" + field.getFieldType() +
") savedInstanceState.getSerializable(" + BASE_KEY + " + \"" + field.getName() + "\");\n";
case PARCEL:
return " target." + field.getName() + " = org.parceler.Parceles.unwrap(" +
"savedInstanceState.getParcelable(" + BASE_KEY + " + \"" + field.getName() + "\"));\n";
default:
return " target." + field.getName() + " = " + "unwrap(" +
return " target." + field.getName() + " = unwrap(" +
"savedInstanceState.getParcelable(" + BASE_KEY + " + \"" + field.getName() + "\"));\n";
}
}
Expand All @@ -86,6 +89,9 @@ protected String emitSaveState(AnnotatedField field) {
case SERIALIZABLE:
return " outState.putSerializable(" + BASE_KEY + " + \""
+ field.getName() + "\", target." + field.getName() + ");\n";
case PARCEL:
return " outState.putParcelable(" + BASE_KEY + " + \""
+ field.getName() + "\", org.parceler.Parceles.wrap(target." + field.getName() + "));\n";
default:
return " outState.putParcelable(" + BASE_KEY + " + \""
+ field.getName() + "\", wrap(target." + field.getName() + "));\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class AnnotatedField {
enum WrappingStrategy {
SERIALIZABLE,
PARCELABLE,
PARCEL,
CUSTOM
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.Messager;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
Expand Down Expand Up @@ -83,10 +84,25 @@ private AnnotatedField.WrappingStrategy wrappingStrategy(TypeMirror type) {
return AnnotatedField.WrappingStrategy.SERIALIZABLE;
}

if (isAnnotated(typeUtils.asElement(type), "org.parceler.Parcel")){
return AnnotatedField.WrappingStrategy.PARCEL;
}

return AnnotatedField.WrappingStrategy.CUSTOM;
}
}

private boolean isAnnotated(Element element, String annotationName) {
if (element != null) {
for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
if (annotationMirror.getAnnotationType().asElement().toString().equals(annotationName)) {
return true;
}
}
}
return false;
}

private class ToErasedEnclosingClass implements Function<AnnotatedField, TypeMirror> {
@Override public TypeMirror apply(AnnotatedField field) {
TypeElement enclosingClassType = field.getEnclosingClassType();
Expand Down
2 changes: 2 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ dependencies {
compile 'joda-time:joda-time:2.3'
compile 'com.github.frankiesardo:icepick:2.3.1'
provided 'com.github.frankiesardo:icepick-processor:2.3.1'
compile 'org.parceler:parceler:0.2.6'
provided 'org.parceler:parceler-api:0.2.6'
}

android {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.frankiesardo.icepick;

import org.parceler.Parcel;
import org.parceler.ParcelConstructor;

@Parcel
public class ExampleParcel {
String name;
int age;

@ParcelConstructor
public ExampleParcel(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class MainActivity extends BaseActivity {
@Icicle String message;
@Icicle Bundle someUnusedExtras = new Bundle();
@Icicle DateTime timestamp = DateTime.now();
@Icicle ExampleParcel exampleParcel;

CustomView customView;

Expand Down