Skip to content

Commit

Permalink
Making parcelable optional
Browse files Browse the repository at this point in the history
  • Loading branch information
shehabic committed May 4, 2017
1 parent 0248b0e commit 469a403
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ dependencies {
//... other dependencies here
provided 'com.github.foodora.android-auto-mapper:library:1.0.4'
apt 'com.github.foodora.android-auto-mapper:compiler:1.0.4'
provided 'com.github.foodora.android-auto-mapper:library:1.0.5'
apt 'com.github.foodora.android-auto-mapper:compiler:1.0.5'
}
```

Expand Down Expand Up @@ -135,7 +135,7 @@ Parcel adapters are optional and the require the `ParcelTypeAdapter` runtime com
To use them just add to your gradle the following dependency.

```
compile 'com.github.foodora.android-auto-mapper:adapter:1.0.4'
compile 'com.github.foodora.android-auto-mapper:adapter:1.0.5'
```

## Version-able Parcels
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
* limitations under the License.
*/

import android.os.Parcelable;

import de.foodora.automapper.AutoMapper;

@AutoMapper(map = ApiRestaurant.class, targetName = "Restaurant")
public abstract class RestaurantAutoMapper implements Parcelable {
@AutoMapper(map = ApiRestaurant.class, targetName = "Restaurant", parcelable = true)
public abstract class RestaurantAutoMapper {

public String frontSign;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,10 @@ private String generateClass(TypeElement type, String className, String classToE
List<VariableElement> nonPrivateFields = new ArrayList<>();
List<VariableElement> mappedOnlyFields = new ArrayList<>();
addNonPrivateFields(type, nonPrivateFields);
TypeElement sourceElement = null;

if (mapFrom != null) {
addNonPrivateFields((TypeElement) mapFrom, mappedOnlyFields);
addNonPrivateFields(mapFrom, mappedOnlyFields);
nonPrivateFields.addAll(mappedOnlyFields);
sourceElement = (TypeElement) mapFrom;
}

if (nonPrivateFields.isEmpty()) {
Expand All @@ -268,6 +266,8 @@ private String generateClass(TypeElement type, String className, String classToE
// get the parcel version
//noinspection ConstantConditions
int version = type.getAnnotation(AutoMapper.class).version();
boolean isImplementingParcelable = ancestoIsParcelable(processingEnv, type);
boolean isParcelable = isImplementingParcelable || type.getAnnotation(AutoMapper.class).parcelable();

// Generate the AutoParcel_??? class
String pkg = TypeUtil.packageNameOf(type);
Expand All @@ -283,30 +283,34 @@ private String generateClass(TypeElement type, String className, String classToE
.addMethod(generateConstructor(properties))
// Add the private constructor
.addMethod(generateConstructorFromParcel(processingEnv, properties, typeAdapters))
// overrides describeContents()
.addMethod(generateDescribeContents())
// static final CREATOR
.addField(generateCreator(processingEnv, properties, classTypeName, typeAdapters))
// overrides writeToParcel()
.addMethod(generateWriteToParcel(version, processingEnv, properties, typeAdapters)) // generate writeToParcel()
// create empty constructor
.addMethod(MethodSpec.constructorBuilder().addModifiers(PUBLIC).build())
// Add fields from mapping only
.addFields(generateFieldSpecs(properties))
// Add map from constructor
.addMethod(generateMapFromCreator(type, classTypeName, sourceElement, mappedProperties));
.addMethod(generateMapFromCreator(type, classTypeName, mapFrom, mappedProperties));

if (isParcelable) {
subClass
// overrides describeContents()
.addMethod(generateDescribeContents())
// static final CREATOR
.addField(generateCreator(processingEnv, properties, classTypeName, typeAdapters))
// overrides writeToParcel()
.addMethod(generateWriteToParcel(version, processingEnv, properties, typeAdapters));

if (!ancestoIsParcelable(processingEnv, type)) {
// Implement android.os.Parcelable if the ancestor does not do it.
subClass.addSuperinterface(ClassName.get("android.os", "Parcelable"));
if (!isImplementingParcelable) {
// Implement android.os.Parcelable if the ancestor does not do it.
subClass.addSuperinterface(ClassName.get("android.os", "Parcelable"));
}
}

if (!typeAdapters.isEmpty()) {
typeAdapters.values().forEach(subClass::addField);
}


JavaFile javaFile = JavaFile.builder(pkg, subClass.build()).build();

return javaFile.toString();
}

Expand Down

0 comments on commit 469a403

Please sign in to comment.