From 3e1e44c7a3ff6252a09c1466c28364de20ce8901 Mon Sep 17 00:00:00 2001 From: Majo Cecunda Date: Wed, 16 Apr 2014 17:28:19 +0200 Subject: [PATCH] #1 #2 #3 model changes now implements Parcelable --- .../android/formulamanager/app/Formula.java | 73 ++++++++++++++----- .../android/formulamanager/app/Parameter.java | 68 ++++++++++++++++- 2 files changed, 122 insertions(+), 19 deletions(-) diff --git a/FormulaManager/app/src/main/java/cz/fi/android/formulamanager/app/Formula.java b/FormulaManager/app/src/main/java/cz/fi/android/formulamanager/app/Formula.java index 2a952de..a9fa547 100644 --- a/FormulaManager/app/src/main/java/cz/fi/android/formulamanager/app/Formula.java +++ b/FormulaManager/app/src/main/java/cz/fi/android/formulamanager/app/Formula.java @@ -1,26 +1,58 @@ package cz.fi.android.formulamanager.app; +import android.os.Parcel; +import android.os.Parcelable; + import java.util.ArrayList; -import java.util.Collections; import java.util.List; /** * Created by Majo on 9. 4. 2014. * Just example formula class, it is not used yet */ -public class Formula { +public class Formula implements Parcelable { private Long id; private String name; private List params; - - private String parsable; - private String description; + private String rawFormula; public Formula() { params = new ArrayList(); } + public Formula(Parcel parcel) { + this.id = parcel.readLong(); + this.name = parcel.readString(); + this.rawFormula = parcel.readString(); + + params = parcel.readArrayList(getClass().getClassLoader()); + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel parcel, int i) { + parcel.writeLong(this.id); + parcel.writeString(this.name); + parcel.writeString(this.rawFormula); + + parcel.writeList(params); + } + + public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { + public Formula createFromParcel(Parcel in) { + return new Formula(in); + } + + public Formula[] newArray(int size) { + return new Formula[size]; + } + }; + public Long getId() { return id; } @@ -45,28 +77,33 @@ public void setParams(List params) { this.params = params; } - public String getParsable() { - return parsable; - } - - public void setParsable(String parsable) { - this.parsable = parsable; - } - - public String getDescription() { - return description; + public String getRawFormula() { + return rawFormula; } - public void setDescription(String description) { - this.description = description; + public void setRawFormula(String rawFormula) { + this.rawFormula = rawFormula; } + /** + * Adds parameter to formula, if it exists, method will edit it + * @param p parameter to be added/edited + */ public void addParam(Parameter p) { - if(p != null) { + if(p == null) { + return; + } + int index = params.indexOf(p); + if (index > -1){ + Parameter ex = params.get(index); + ex.setName(p.getName()); + ex.setType(p.getType()); + } else { params.add(p); } } + public String getParamsAsString(){ String ret = ""; for(Parameter p : params) { diff --git a/FormulaManager/app/src/main/java/cz/fi/android/formulamanager/app/Parameter.java b/FormulaManager/app/src/main/java/cz/fi/android/formulamanager/app/Parameter.java index 7582a2b..c9cbc64 100644 --- a/FormulaManager/app/src/main/java/cz/fi/android/formulamanager/app/Parameter.java +++ b/FormulaManager/app/src/main/java/cz/fi/android/formulamanager/app/Parameter.java @@ -1,12 +1,61 @@ package cz.fi.android.formulamanager.app; +import android.os.Parcel; +import android.os.Parcelable; + /** * Created by Majo on 13. 4. 2014. */ -public class Parameter { +public class Parameter implements Parcelable { + + //types of parameters + public static final int PARAM_REGULAR = 0; //regular one value param + public static final int PARAM_INDEX = 1; //index in summation etc + public static final int PARAM_STEP = 2; //increment after each calculation + + Long id; String name; int type; + public Parameter() { + } + + public Parameter(Parcel in) { + this.id = in.readLong(); + this.name = in.readString(); + this.type = in.readInt(); + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel parcel, int i) { + parcel.writeLong(this.id); + parcel.writeString(this.name); + parcel.writeInt(this.type); + } + + public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { + public Parameter createFromParcel(Parcel in) { + return new Parameter(in); + } + + public Parameter[] newArray(int size) { + return new Parameter[size]; + } + }; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + public String getName() { return name; } @@ -22,4 +71,21 @@ public int getType() { public void setType(int type) { this.type = type; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Parameter parameter = (Parameter) o; + + if (!id.equals(parameter.id)) return false; + + return true; + } + + @Override + public int hashCode() { + return id.hashCode(); + } }