Created
July 29, 2014 21:33
-
-
Save snadjafi/a694a59dd34730cbe5d9 to your computer and use it in GitHub Desktop.
parcelable example
This file contains 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
package com.sf.photointake.intake.model; | |
import android.os.Parcel; | |
import android.os.Parcelable; | |
import com.google.gson.annotations.SerializedName; | |
import java.io.Closeable; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class Question implements Parcelable, Cloneable, Model { | |
//region Variables | |
@SerializedName("to_alert") private Boolean toAlert; | |
@SerializedName("required") private Boolean required; | |
@SerializedName("question") private String question; | |
@SerializedName("question_id") private String id; | |
@SerializedName("question_title") private String title; | |
@SerializedName("photo_slots") private List<PhotoSlot> photoSlots; | |
@SerializedName("question_title_has_tokens")private Boolean titleHasToken; | |
@SerializedName("additional_fields") private AdditionalFields additionalFields; | |
@SerializedName("answered_photo_sections") private List<AnsweredPhotoSection> answeredPhotoSections; | |
private int rowNumber = -1; | |
//endregion | |
//region Constructor | |
public Question(Parcel in) { | |
setToAlert(in.readInt() == 1); | |
setRequired(in.readInt() == 1); | |
setQuestion(in.readString()); | |
setId(in.readString()); | |
setTitle(in.readString()); | |
Object[] o = in.readArray(Question.class.getClassLoader()); | |
ArrayList<PhotoSlot> ps = new ArrayList<PhotoSlot>(); | |
for (Object item : o) { | |
ps.add(((PhotoSlot) item)); | |
} | |
setPhotoSlots(ps); | |
} | |
//endregion | |
//region Setters | |
public void setRowNumber(int rowNumber) { | |
this.rowNumber = rowNumber; | |
updatePhotoSlots(rowNumber); | |
} | |
private void updatePhotoSlots(int rowNumber) { | |
for (PhotoSlot photoSlot : photoSlots) { | |
photoSlot.setQuestionRowNumber(rowNumber); | |
} | |
} | |
public void setToAlert(Boolean toAlert) { | |
this.toAlert = toAlert; | |
} | |
public void setRequired(Boolean required) { | |
this.required = required; | |
} | |
public void setQuestion(String question) { | |
this.question = question; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public void setPhotoSlots(List<PhotoSlot> photoSlots) { | |
this.photoSlots = photoSlots; | |
} | |
public void addPhotoSlot(PhotoSlot ps) { | |
photoSlots.add(ps); | |
} | |
public void setAnsweredPhotoSections(List<AnsweredPhotoSection> answeredPhotoSections) { | |
this.answeredPhotoSections = answeredPhotoSections; | |
} | |
public void setAdditionalFields(AdditionalFields additionalFields) { | |
this.additionalFields = additionalFields; | |
} | |
//endregion | |
//region Getters | |
public boolean isToAlert() { | |
return toAlert == null ? false : toAlert; | |
} | |
public boolean isRequired() { | |
return required == null ? false : required; | |
} | |
public String getId() { | |
return id == null ? "" : id; | |
} | |
public String getTitle() { | |
return title == null ? "" : title; | |
} | |
public List<PhotoSlot> getPhotoSlots() { | |
return photoSlots; | |
} | |
public List<AnsweredPhotoSection> getAnsweredPhotoSections() { | |
return answeredPhotoSections; | |
} | |
public Boolean isAllowMultipleSections() { | |
return additionalFields != null | |
&& (additionalFields.allowMultipleSections != null | |
? additionalFields.allowMultipleSections | |
: false); | |
} | |
public Boolean isUserDefinedTitle() { | |
return additionalFields != null | |
&& (additionalFields.userDefinedTitle != null | |
? additionalFields.userDefinedTitle | |
: false); | |
} | |
public String getQuestion() { | |
return question == null ? "" : question; | |
} | |
public Boolean getTitleHasToken() { | |
return titleHasToken == null ? false : titleHasToken; | |
} | |
public AdditionalFields getAdditionalFields() { | |
return additionalFields; | |
} | |
public int getRowNumber() { | |
return rowNumber; | |
} | |
//endregion | |
@Override public Question clone() throws CloneNotSupportedException { | |
Question q = (Question) super.clone(); | |
ArrayList<PhotoSlot> slots = new ArrayList<PhotoSlot>(); | |
for (PhotoSlot photoSlot : photoSlots) { | |
slots.add((PhotoSlot) photoSlot.clone()); | |
} | |
q.setAdditionalFields((AdditionalFields) getAdditionalFields().clone()); | |
q.setAnsweredPhotoSections(null); | |
q.setPhotoSlots(slots); | |
return q; | |
} | |
@Override public int describeContents() { | |
return 0; | |
} | |
@Override public void writeToParcel(Parcel parcel, int i) { | |
parcel.writeInt(isToAlert() ? 1 : 0); | |
parcel.writeInt(isRequired() ? 1 : 0); | |
parcel.writeString(getQuestion()); | |
parcel.writeString(getId()); | |
parcel.writeString(getTitle()); | |
parcel.writeArray(photoSlots.toArray()); | |
} | |
public static final Parcelable.Creator<Question> CREATOR = new Parcelable.Creator<Question>() { | |
public Question createFromParcel(Parcel in) { | |
return new Question(in); | |
} | |
public Question[] newArray(int size) { | |
return new Question[size]; | |
} | |
}; | |
private class AdditionalFields implements Cloneable { | |
//region Variables | |
@SerializedName("allows_multiple_sections") public Boolean allowMultipleSections; | |
@SerializedName("user_defined_section_title") public Boolean userDefinedTitle; | |
//endregion | |
@Override public Object clone() throws CloneNotSupportedException { | |
return super.clone(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment