From ff71102c771ec0768f76dc1697efe83d8f749366 Mon Sep 17 00:00:00 2001 From: Julian Reinhardt Date: Thu, 21 Oct 2021 13:38:42 +0200 Subject: [PATCH 1/5] Replaces ValueEditorDialogbox with dialogbox that can handle multiple values --- .../java/com/easyfitness/ProfileFragment.java | 54 +++--- .../com/easyfitness/ValueEditorDialogbox.java | 140 ---------------- .../easyfitness/ValuesEditorDialogbox.java | 154 ++++++++++++++++++ .../java/com/easyfitness/WeightFragment.java | 80 +++++---- .../bodymeasures/BodyPartDetailsFragment.java | 31 ++-- .../java/com/easyfitness/utils/Value.java | 67 ++++++++ .../views/SingleValueInputView.java | 5 +- .../main/res/layout/dialog_value_editor.xml | 11 +- 8 files changed, 300 insertions(+), 242 deletions(-) delete mode 100644 app/src/main/java/com/easyfitness/ValueEditorDialogbox.java create mode 100644 app/src/main/java/com/easyfitness/ValuesEditorDialogbox.java create mode 100644 app/src/main/java/com/easyfitness/utils/Value.java diff --git a/app/src/main/java/com/easyfitness/ProfileFragment.java b/app/src/main/java/com/easyfitness/ProfileFragment.java index eb47be22..b4ec0a31 100644 --- a/app/src/main/java/com/easyfitness/ProfileFragment.java +++ b/app/src/main/java/com/easyfitness/ProfileFragment.java @@ -7,7 +7,6 @@ import android.os.Build; import android.os.Bundle; import android.os.Environment; -import android.util.Log; import android.util.TypedValue; import android.view.Gravity; import android.view.LayoutInflater; @@ -31,16 +30,13 @@ import com.easyfitness.utils.DateConverter; import com.easyfitness.enums.Gender; import com.easyfitness.utils.ImageUtil; -import com.easyfitness.utils.RealPathUtil; +import com.easyfitness.utils.Value; import com.easyfitness.views.EditableInputView; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.mikhaellopez.circularimageview.CircularImageView; import com.onurkaganaldemir.ktoastlib.KToast; -import com.theartofdev.edmodo.cropper.CropImage; import java.io.File; -import java.io.IOException; -import java.text.SimpleDateFormat; import java.util.Date; import cn.pedant.SweetAlert.SweetAlertDialog; @@ -64,29 +60,26 @@ public class ProfileFragment extends Fragment { private AppViMo appViMo; private ProfileViMo profileViMo; private final OnClickListener mOnClickListener = view -> { - ValueEditorDialogbox editorDialogbox; if (view.getId() == R.id.size) { - BodyPart sizeBodyPart = daoBodyPart.getBodyPartfromBodyPartKey(BodyPartExtensions.SIZE); - BodyMeasure lastSizeValue = daoBodyMeasure.getLastBodyMeasures(sizeBodyPart.getId(), appViMo.getProfile().getValue()); - if (lastSizeValue == null) { - editorDialogbox = new ValueEditorDialogbox(getActivity(), new Date(), "", 0, SettingsFragment.getDefaultSizeUnit(getActivity())); - } else { - editorDialogbox = new ValueEditorDialogbox(getActivity(), new Date(), "", lastSizeValue.getBodyMeasure(), lastSizeValue.getUnit()); + BodyPart sizeBodyPart = daoBodyPart.getBodyPartfromBodyPartKey(BodyPartExtensions.SIZE); + BodyMeasure lastSizeMeasure = daoBodyMeasure.getLastBodyMeasures(sizeBodyPart.getId(), appViMo.getProfile().getValue()); + final Value lastSizeValue = lastSizeMeasure == null + ? new Value(0, SettingsFragment.getDefaultSizeUnit(getActivity())) + : new Value(lastSizeMeasure.getBodyMeasure(), lastSizeMeasure.getUnit()); + final ValuesEditorDialogbox editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastSizeValue}); + editorDialogbox.setTitle(R.string.AddLabel); + editorDialogbox.setPositiveButton(R.string.AddLabel); + editorDialogbox.setOnDismissListener(dialog -> { + if (!editorDialogbox.isCancelled()) { + Date date = DateConverter.localDateStrToDate(editorDialogbox.getDate(), getContext()); + final Value newValue = editorDialogbox.getValues()[0]; + daoBodyMeasure.addBodyMeasure(date, sizeBodyPart.getId(), newValue.getValue(), appViMo.getProfile().getValue().getId(), newValue.getUnit()); + profileViMo.setSize(newValue.getValue()); + profileViMo.setSizeUnit(newValue.getUnit()); + requestForSave(); } - editorDialogbox.setTitle(R.string.AddLabel); - editorDialogbox.setPositiveButton(R.string.AddLabel); - editorDialogbox.setOnDismissListener(dialog -> { - if (!editorDialogbox.isCancelled()) { - Date date = DateConverter.localDateStrToDate(editorDialogbox.getDate(), getContext()); - float value = Float.parseFloat(editorDialogbox.getValue().replaceAll(",", ".")); - Unit unit = Unit.fromString(editorDialogbox.getUnit()); - daoBodyMeasure.addBodyMeasure(date, sizeBodyPart.getId(), value, appViMo.getProfile().getValue().getId(), unit); - profileViMo.setSize(value); - profileViMo.setSizeUnit(unit); - requestForSave(); - } - }); - editorDialogbox.show(); + }); + editorDialogbox.show(); } }; private boolean isSaving; @@ -221,7 +214,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, } else { sizeEdit.setText(String.valueOf(profileViMo.getSize().getValue().toString()) + sizeUnit); } - }); + }); profileViMo.getName().observe(getViewLifecycleOwner(), name -> { nameEdit.setText(name); }); @@ -231,7 +224,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, success = ImageUtil.setPic(roundProfile, photo); roundProfile.invalidate(); } - if (!success){ + if (!success) { roundProfile.setImageDrawable(getActivity().getResources().getDrawable(R.drawable.profile)); } }); @@ -263,7 +256,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, } - @Override public void onStart() { super.onStart(); @@ -286,7 +278,7 @@ private void updateProfileViMo(Profile profile) { profileViMo.setPhoto(profile.getPhoto()); profileViMo.setName(profile.getName()); BodyMeasure sizeMeasure = daoBodyMeasure.getLastBodyMeasures(BodyPartExtensions.SIZE, profile); - if (sizeMeasure!=null) { + if (sizeMeasure != null) { profileViMo.setSize(sizeMeasure.getBodyMeasure()); profileViMo.setSizeUnit(sizeMeasure.getUnit()); } else { @@ -347,7 +339,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { imgUtil.saveThumb(newFile.getAbsolutePath()); profileViMo.setPhoto(newFile.getAbsolutePath()); requestForSave(); - } catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } } diff --git a/app/src/main/java/com/easyfitness/ValueEditorDialogbox.java b/app/src/main/java/com/easyfitness/ValueEditorDialogbox.java deleted file mode 100644 index 08dd8816..00000000 --- a/app/src/main/java/com/easyfitness/ValueEditorDialogbox.java +++ /dev/null @@ -1,140 +0,0 @@ -package com.easyfitness; - -import android.app.Activity; -import android.app.Dialog; -import android.os.Bundle; -import android.view.View; -import android.widget.Button; -import android.widget.TextView; - -import com.easyfitness.enums.Unit; -import com.easyfitness.utils.DateConverter; -import com.easyfitness.utils.Keyboard; -import com.easyfitness.views.SingleValueInputView; - -import java.util.Date; - -public class ValueEditorDialogbox extends Dialog implements View.OnClickListener { - - private final Date mDate; - private final String mTime; - private final double mValue; - private final Unit mUnit; - public Dialog d; - private SingleValueInputView dateEdit; - private SingleValueInputView timeEdit; - private SingleValueInputView valueEdit; - private Button updateButton; - private Button cancelButton; - private boolean mCancelled = false; - private TextView titleTextView; - private String mTitle = ""; - private String mUpdateText = ""; - - public ValueEditorDialogbox(Activity a, Date date, String time, double value) { - super(a); - mDate = date; - mTime = time; - mValue = value; - - mUnit = Unit.UNITLESS; - } - - public ValueEditorDialogbox(Activity a, Date date, String time, double value, Unit units) { - super(a); - mDate = date; - mTime = time; - mValue = value; - - mUnit = units; - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.dialog_value_editor); - this.setCanceledOnTouchOutside(false); - this.setCancelable(true); - - dateEdit = findViewById(R.id.EditorValueDateInput); - timeEdit = findViewById(R.id.EditorValueTimeInput); - valueEdit = findViewById(R.id.EditorValueInput); - titleTextView = findViewById(R.id.EditorValueTitle); - if (!mTitle.isEmpty()) titleTextView.setText(mTitle); - - switch (mUnit.getUnitType()) { - case SIZE: - valueEdit.setUnits(new CharSequence[]{Unit.CM.toString(), Unit.INCH.toString()}); - break; - case WEIGHT: - valueEdit.setUnits(new CharSequence[]{Unit.KG.toString(), Unit.LBS.toString(), Unit.STONES.toString()}); - break; - case DISTANCE: - valueEdit.setUnits(new CharSequence[]{Unit.KM.toString(), Unit.MILES.toString()}); - break; - case PERCENTAGE: - valueEdit.setUnits(new CharSequence[]{Unit.PERCENTAGE.toString()}); - valueEdit.showUnit(true); - break; - case NONE: - default: - valueEdit.setUnits(new CharSequence[]{Unit.UNITLESS.toString()}); - valueEdit.showUnit(false); - } - - dateEdit.setValue(DateConverter.dateToLocalDateStr(mDate, getContext())); - timeEdit.setValue(mTime); - valueEdit.setValue(String.format("%.1f", mValue)); - valueEdit.setSelectedUnit(mUnit.toString()); - - updateButton = findViewById(R.id.btn_update); - if (!mUpdateText.isEmpty()) updateButton.setText(mUpdateText); - cancelButton = findViewById(R.id.btn_cancel); - - updateButton.setOnClickListener(this); - cancelButton.setOnClickListener(this); - } - - @Override - public void onClick(View v) { - Keyboard.hide(getContext(), valueEdit); - Keyboard.hide(getContext(), timeEdit); - - if (v.getId() == R.id.btn_cancel) { - mCancelled = true; - cancel(); - } else if (v.getId() == R.id.btn_update) { - mCancelled = false; - dismiss(); - } - } - - public boolean isCancelled() { - return mCancelled; - } - - public String getDate() { - return dateEdit.getValue(); - } - - public String getTime() { - return timeEdit.getValue(); - } - - public String getValue() { - return valueEdit.getValue(); - } - - public String getUnit() { - return valueEdit.getSelectedUnit(); - } - - @Override - public void setTitle(int title) { - mTitle = getContext().getString(title); - } - - public void setPositiveButton(int text) { - mUpdateText = getContext().getString(text); - } -} diff --git a/app/src/main/java/com/easyfitness/ValuesEditorDialogbox.java b/app/src/main/java/com/easyfitness/ValuesEditorDialogbox.java new file mode 100644 index 00000000..6725b96d --- /dev/null +++ b/app/src/main/java/com/easyfitness/ValuesEditorDialogbox.java @@ -0,0 +1,154 @@ +package com.easyfitness; + +import android.app.Activity; +import android.app.Dialog; +import android.os.Bundle; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.TextView; + +import androidx.core.content.res.ResourcesCompat; + +import com.easyfitness.enums.Unit; +import com.easyfitness.utils.Value; +import com.easyfitness.utils.DateConverter; +import com.easyfitness.utils.Keyboard; +import com.easyfitness.views.SingleValueInputView; + +import java.util.Date; + +public class ValuesEditorDialogbox extends Dialog implements View.OnClickListener { + + private final Date mDate; + private final String mTime; + private final Value[] mValues; + public Dialog d; + private SingleValueInputView dateEdit; + private SingleValueInputView timeEdit; + private SingleValueInputView[] valueEdits; + private Button updateButton; + private Button cancelButton; + private boolean mCancelled = false; + private TextView titleTextView; + private String mTitle = ""; + private String mUpdateText = ""; + + public ValuesEditorDialogbox(Activity a, Date date, String time, Value[] values) { + super(a); + mDate = date; + mTime = time; + mValues = values; + valueEdits = new SingleValueInputView[values.length]; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.dialog_value_editor); + this.setCanceledOnTouchOutside(false); + this.setCancelable(true); + + dateEdit = findViewById(R.id.EditorValueDateInput); + timeEdit = findViewById(R.id.EditorValueTimeInput); + final ViewGroup valuesContainer = findViewById(R.id.EditorValuesContainer); + titleTextView = findViewById(R.id.EditorValueTitle); + if (!mTitle.isEmpty()) titleTextView.setText(mTitle); + + for (int i = 0; i < this.mValues.length; i++){ + final Value value = this.mValues[i]; + final Unit unit = value.getUnit(); + final String label = this.getContext().getResources().getString(value.getLabel() != ResourcesCompat.ID_NULL ? value.getLabel() : R.string.edit_value); + SingleValueInputView valueEdit = new SingleValueInputView(this.getContext()); + + switch (unit.getUnitType()) { + case SIZE: + valueEdit.setUnits(new CharSequence[]{Unit.CM.toString(), Unit.INCH.toString()}); + break; + case WEIGHT: + valueEdit.setUnits(new CharSequence[]{Unit.KG.toString(), Unit.LBS.toString(), Unit.STONES.toString()}); + break; + case DISTANCE: + valueEdit.setUnits(new CharSequence[]{Unit.KM.toString(), Unit.MILES.toString()}); + break; + case PERCENTAGE: + valueEdit.setUnits(new CharSequence[]{Unit.PERCENTAGE.toString()}); + break; + case NONE: + default: + valueEdit.setUnits(new CharSequence[]{Unit.UNITLESS.toString()}); + valueEdit.showUnit(false); + } + valueEdit.setTitle(label); + valueEdit.showUnit(true); + valueEdit.setValue(String.format("%.1f", value.getValue())); + valueEdit.setSelectedUnit(unit.toString()); + + valueEdits[i] = valueEdit; + valuesContainer.addView(valueEdit); + } + + dateEdit.setValue(DateConverter.dateToLocalDateStr(mDate, getContext())); + timeEdit.setValue(mTime); + + + updateButton = findViewById(R.id.btn_update); + if (!mUpdateText.isEmpty()) updateButton.setText(mUpdateText); + cancelButton = findViewById(R.id.btn_cancel); + + updateButton.setOnClickListener(this); + cancelButton.setOnClickListener(this); + } + + @Override + public void onClick(View v) { + for(SingleValueInputView valueEdit: this.valueEdits){ + Keyboard.hide(getContext(), valueEdit); + } + Keyboard.hide(getContext(), timeEdit); + + if (v.getId() == R.id.btn_cancel) { + mCancelled = true; + cancel(); + } else if (v.getId() == R.id.btn_update) { + mCancelled = false; + dismiss(); + } + } + + public boolean isCancelled() { + return mCancelled; + } + + public String getDate() { + return dateEdit.getValue(); + } + + public String getTime() { + return timeEdit.getValue(); + } + + public Value[] getValues() { + final Value[] returnValues = new Value[this.valueEdits.length]; + for(int i = 0; i < this.mValues.length; i++){ + final Value prevValue = this.mValues[i]; + final SingleValueInputView valueEdit = this.valueEdits[i]; + returnValues[i] = new Value( + Float.parseFloat(valueEdit.getValue().replaceAll(",", ".")), + Unit.fromString(valueEdit.getSelectedUnit()), + prevValue.getId(), + prevValue.getLabel()); + } + return returnValues; + } + + + @Override + public void setTitle(int title) { + mTitle = getContext().getString(title); + } + + public void setPositiveButton(int text) { + mUpdateText = getContext().getString(text); + } +} diff --git a/app/src/main/java/com/easyfitness/WeightFragment.java b/app/src/main/java/com/easyfitness/WeightFragment.java index a0c8a85c..c302db2d 100644 --- a/app/src/main/java/com/easyfitness/WeightFragment.java +++ b/app/src/main/java/com/easyfitness/WeightFragment.java @@ -30,6 +30,7 @@ import com.easyfitness.utils.DateConverter; import com.easyfitness.enums.Gender; import com.easyfitness.utils.UnitConverter; +import com.easyfitness.utils.Value; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.data.Entry; @@ -140,42 +141,39 @@ public class WeightFragment extends Fragment { private final OnClickListener mOnClickListener = new OnClickListener() { @Override public void onClick(View view) { - ValueEditorDialogbox editorDialogbox; + ValuesEditorDialogbox editorDialogbox; switch (view.getId()) { case R.id.weightInput: - BodyMeasure lastWeightValue = mDbBodyMeasure.getLastBodyMeasures(weightBodyPart.getId(), getProfile()); - if (lastWeightValue == null) { - editorDialogbox = new ValueEditorDialogbox(getActivity(), new Date(), "", 0, SettingsFragment.getDefaultWeightUnit(getActivity()).toUnit()); - } else { - editorDialogbox = new ValueEditorDialogbox(getActivity(), new Date(), "", lastWeightValue.getBodyMeasure(), lastWeightValue.getUnit()); - } + BodyMeasure lastWeightMeasure = mDbBodyMeasure.getLastBodyMeasures(weightBodyPart.getId(), getProfile()); + Value lastWeighValue = lastWeightMeasure == null + ? new Value(0, SettingsFragment.getDefaultWeightUnit(getActivity()).toUnit(), R.string.weightLabel) + : new Value(lastWeightMeasure.getBodyMeasure(), lastWeightMeasure.getUnit(), R.string.weightLabel); + editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastWeighValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); editorDialogbox.setOnDismissListener(dialog -> { if (!editorDialogbox.isCancelled()) { Date date = DateConverter.localDateStrToDate(editorDialogbox.getDate(), getContext()); - float value = Float.parseFloat(editorDialogbox.getValue().replaceAll(",", ".")); - Unit unit = Unit.fromString(editorDialogbox.getUnit()); - mDbBodyMeasure.addBodyMeasure(date, weightBodyPart.getId(), value, getProfile().getId(), unit); + Value newValue = editorDialogbox.getValues()[0]; + mDbBodyMeasure.addBodyMeasure(date, weightBodyPart.getId(), newValue.getValue(), getProfile().getId(), newValue.getUnit()); refreshData(); } }); editorDialogbox.show(); break; case R.id.fatInput: - BodyMeasure lastFatValue = mDbBodyMeasure.getLastBodyMeasures(fatBodyPart.getId(), getProfile()); - if (lastFatValue == null) { - editorDialogbox = new ValueEditorDialogbox(getActivity(), new Date(), "", 0, Unit.PERCENTAGE); - } else { - editorDialogbox = new ValueEditorDialogbox(getActivity(), new Date(), "", lastFatValue.getBodyMeasure(), Unit.PERCENTAGE); - } + BodyMeasure lastFatMeasure = mDbBodyMeasure.getLastBodyMeasures(fatBodyPart.getId(), getProfile()); + final Value lastFatValue = lastFatMeasure == null + ? new Value(0, Unit.PERCENTAGE, R.string.fatLabel) + : new Value(lastFatMeasure.getBodyMeasure(), lastFatMeasure.getUnit(), R.string.fatLabel); + editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastFatValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); editorDialogbox.setOnDismissListener(dialog -> { if (!editorDialogbox.isCancelled()) { Date date = DateConverter.localDateStrToDate(editorDialogbox.getDate(), getContext()); - float value = Float.parseFloat(editorDialogbox.getValue().replaceAll(",", ".")); - mDbBodyMeasure.addBodyMeasure(date, fatBodyPart.getId(), value, getProfile().getId(), Unit.PERCENTAGE); + Value newValue = editorDialogbox.getValues()[0]; + mDbBodyMeasure.addBodyMeasure(date, fatBodyPart.getId(), newValue.getValue(), getProfile().getId(), newValue.getUnit()); refreshData(); } }); @@ -183,19 +181,18 @@ public void onClick(View view) { editorDialogbox.show(); break; case R.id.musclesInput: - BodyMeasure lastMusclesValue = mDbBodyMeasure.getLastBodyMeasures(musclesBodyPart.getId(), getProfile()); - if (lastMusclesValue == null) { - editorDialogbox = new ValueEditorDialogbox(getActivity(), new Date(), "", 0, Unit.PERCENTAGE); - } else { - editorDialogbox = new ValueEditorDialogbox(getActivity(), new Date(), "", lastMusclesValue.getBodyMeasure(), Unit.PERCENTAGE); - } + BodyMeasure lastMusclesMeasure = mDbBodyMeasure.getLastBodyMeasures(musclesBodyPart.getId(), getProfile()); + Value lastMusclesValue = lastMusclesMeasure == null + ? new Value(0, Unit.PERCENTAGE, R.string.musclesLabel) + : new Value(lastMusclesMeasure.getBodyMeasure(), lastMusclesMeasure.getUnit(), R.string.musclesLabel); + editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastMusclesValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); editorDialogbox.setOnDismissListener(dialog -> { if (!editorDialogbox.isCancelled()) { Date date = DateConverter.localDateStrToDate(editorDialogbox.getDate(), getContext()); - float value = Float.parseFloat(editorDialogbox.getValue().replaceAll(",", ".")); - mDbBodyMeasure.addBodyMeasure(date, musclesBodyPart.getId(), value, getProfile().getId(), Unit.PERCENTAGE); + Value newValue = editorDialogbox.getValues()[0]; + mDbBodyMeasure.addBodyMeasure(date, musclesBodyPart.getId(), newValue.getValue(), getProfile().getId(), newValue.getUnit()); refreshData(); } }); @@ -203,19 +200,18 @@ public void onClick(View view) { editorDialogbox.show(); break; case R.id.waterInput: - BodyMeasure lastWaterValue = mDbBodyMeasure.getLastBodyMeasures(waterBodyPart.getId(), getProfile()); - if (lastWaterValue == null) { - editorDialogbox = new ValueEditorDialogbox(getActivity(), new Date(), "", 0, Unit.PERCENTAGE); - } else { - editorDialogbox = new ValueEditorDialogbox(getActivity(), new Date(), "", lastWaterValue.getBodyMeasure(), Unit.PERCENTAGE); - } + BodyMeasure lastWaterMeasure = mDbBodyMeasure.getLastBodyMeasures(waterBodyPart.getId(), getProfile()); + Value lastWaterValue = lastWaterMeasure == null + ? new Value(0, Unit.PERCENTAGE, R.string.waterLabel) + : new Value(lastWaterMeasure.getBodyMeasure(), lastWaterMeasure.getUnit(), R.string.waterLabel); + editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastWaterValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); editorDialogbox.setOnDismissListener(dialog -> { if (!editorDialogbox.isCancelled()) { Date date = DateConverter.localDateStrToDate(editorDialogbox.getDate(), getContext()); - float value = Float.parseFloat(editorDialogbox.getValue().replaceAll(",", ".")); - mDbBodyMeasure.addBodyMeasure(date, waterBodyPart.getId(), value, getProfile().getId(), Unit.PERCENTAGE); + Value newValue = editorDialogbox.getValues()[0]; + mDbBodyMeasure.addBodyMeasure(date, waterBodyPart.getId(), newValue.getValue(), getProfile().getId(), newValue.getUnit()); refreshData(); } }); @@ -223,20 +219,18 @@ public void onClick(View view) { editorDialogbox.show(); break; case R.id.sizeInput: - BodyMeasure lastSizeValue = mDbBodyMeasure.getLastBodyMeasures(sizeBodyPart.getId(), getProfile()); - if (lastSizeValue == null) { - editorDialogbox = new ValueEditorDialogbox(getActivity(), new Date(), "", 0, SettingsFragment.getDefaultSizeUnit(getActivity())); - } else { - editorDialogbox = new ValueEditorDialogbox(getActivity(), new Date(), "", lastSizeValue.getBodyMeasure(), lastSizeValue.getUnit()); - } + BodyMeasure lastSizeMeasure = mDbBodyMeasure.getLastBodyMeasures(sizeBodyPart.getId(), getProfile()); + Value lastSizeValue = lastSizeMeasure == null + ? new Value(0, SettingsFragment.getDefaultSizeUnit(getActivity()), R.string.size) + : new Value(lastSizeMeasure.getBodyMeasure(), lastSizeMeasure.getUnit(), R.string.size); + editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastSizeValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); editorDialogbox.setOnDismissListener(dialog -> { if (!editorDialogbox.isCancelled()) { Date date = DateConverter.localDateStrToDate(editorDialogbox.getDate(), getContext()); - float value = Float.parseFloat(editorDialogbox.getValue().replaceAll(",", ".")); - Unit unit = Unit.fromString(editorDialogbox.getUnit()); - mDbBodyMeasure.addBodyMeasure(date, sizeBodyPart.getId(), value, getProfile().getId(), unit); + Value newValue = editorDialogbox.getValues()[0]; + mDbBodyMeasure.addBodyMeasure(date, sizeBodyPart.getId(), newValue.getValue(), getProfile().getId(), newValue.getUnit()); refreshData(); } }); diff --git a/app/src/main/java/com/easyfitness/bodymeasures/BodyPartDetailsFragment.java b/app/src/main/java/com/easyfitness/bodymeasures/BodyPartDetailsFragment.java index b3793705..5d93a8b1 100644 --- a/app/src/main/java/com/easyfitness/bodymeasures/BodyPartDetailsFragment.java +++ b/app/src/main/java/com/easyfitness/bodymeasures/BodyPartDetailsFragment.java @@ -34,12 +34,13 @@ import com.easyfitness.AppViMo; import com.easyfitness.R; import com.easyfitness.SettingsFragment; -import com.easyfitness.ValueEditorDialogbox; +import com.easyfitness.ValuesEditorDialogbox; import com.easyfitness.enums.Unit; import com.easyfitness.enums.UnitType; import com.easyfitness.utils.DateConverter; import com.easyfitness.utils.ExpandedListView; import com.easyfitness.utils.UnitConverter; +import com.easyfitness.utils.Value; import com.easyfitness.views.EditableInputView; import com.easyfitness.views.GraphView; import com.github.mikephil.charting.data.Entry; @@ -77,23 +78,17 @@ public class BodyPartDetailsFragment extends Fragment implements DatePickerDialo @Override public void onClick(View v) { BodyMeasure lastBodyMeasure = mBodyMeasureDb.getLastBodyMeasures(mInitialBodyPart.getId(), getProfile()); - double lastValue; - if (lastBodyMeasure == null) { - lastValue = 0; - } else { - lastValue = lastBodyMeasure.getBodyMeasure(); - } - Unit unitDef = getValidUnit(lastBodyMeasure); - - ValueEditorDialogbox editorDialogbox = new ValueEditorDialogbox(getActivity(), new Date(), "", lastValue, unitDef); + final Value lastValue = lastBodyMeasure == null + ? new Value(0, getValidUnit(null)) + : new Value(lastBodyMeasure.getBodyMeasure(), getValidUnit(lastBodyMeasure)); + ValuesEditorDialogbox editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); editorDialogbox.setOnDismissListener(dialog -> { if (!editorDialogbox.isCancelled()) { Date date = DateConverter.localDateStrToDate(editorDialogbox.getDate(), getContext()); - float value = Float.parseFloat(editorDialogbox.getValue().replaceAll(",", ".")); - Unit unit = Unit.fromString(editorDialogbox.getUnit()); - mBodyMeasureDb.addBodyMeasure(date, mInitialBodyPart.getId(), value, getProfile().getId(), unit); + final Value newValue = editorDialogbox.getValues()[0]; + mBodyMeasureDb.addBodyMeasure(date, mInitialBodyPart.getId(), newValue.getValue(), getProfile().getId(), newValue.getUnit()); refreshData(); } }); @@ -359,14 +354,14 @@ private void showDeleteDialog(final long idToDelete) { private void showEditDialog(final long idToEdit) { BodyMeasure bodyMeasure = mBodyMeasureDb.getMeasure(idToEdit); - ValueEditorDialogbox editorDialogbox = new ValueEditorDialogbox(getActivity(), bodyMeasure.getDate(), "", bodyMeasure.getBodyMeasure(), getValidUnit(bodyMeasure)); + final Value lastValue = new Value(bodyMeasure.getBodyMeasure(), getValidUnit(bodyMeasure)); + + ValuesEditorDialogbox editorDialogbox = new ValuesEditorDialogbox(getActivity(), bodyMeasure.getDate(), "", new Value[]{lastValue}); editorDialogbox.setOnDismissListener(dialog -> { if (!editorDialogbox.isCancelled()) { Date date = DateConverter.localDateStrToDate(editorDialogbox.getDate(), getContext()); - float value = Float.parseFloat(editorDialogbox.getValue().replaceAll(",", ".")); - Unit unit = Unit.fromString(editorDialogbox.getUnit()); - - BodyMeasure updatedBodyMeasure = new BodyMeasure(bodyMeasure.getId(), date, bodyMeasure.getBodyPartID(), value, bodyMeasure.getProfileID(), unit); + final Value newValue = editorDialogbox.getValues()[0]; + BodyMeasure updatedBodyMeasure = new BodyMeasure(bodyMeasure.getId(), date, bodyMeasure.getBodyPartID(), newValue.getValue(), bodyMeasure.getProfileID(), newValue.getUnit()); int i = mBodyMeasureDb.updateMeasure(updatedBodyMeasure); refreshData(); } diff --git a/app/src/main/java/com/easyfitness/utils/Value.java b/app/src/main/java/com/easyfitness/utils/Value.java new file mode 100644 index 00000000..eec7a766 --- /dev/null +++ b/app/src/main/java/com/easyfitness/utils/Value.java @@ -0,0 +1,67 @@ +package com.easyfitness.utils; + +import androidx.annotation.Nullable; +import androidx.core.content.res.ResourcesCompat; + +import com.easyfitness.enums.Unit; + +public class Value { + private final float mValue; + private final Unit mUnit; + /** Identifier for this value so we can later differentiate between values */ + private final String mId; + private final int mLabel; + + /** + * Create a value + * @param value Actual value + * @param unit Unit of the value + * @param id Custom identifier to differentiate between values + * @param label String resource to label this value is described by + */ + public Value(float value, Unit unit, @Nullable String id, int label){ + this.mValue = value; + this.mUnit = unit; + this.mId = id; + this.mLabel = label; + } + /** + * Create a value + * @param value Actual value + * @param unit Unit of the value + * @param id Custom identifier to differentiate between values + */ + public Value(float value, Unit unit, @Nullable String id){ + this(value, unit, id, ResourcesCompat.ID_NULL); + } + /** + * Create a value + * @param value Actual value + * @param unit Unit of the value + * @param label String resource to label this value is described by + */ + public Value(float value, Unit unit, int label){ + this(value, unit, null, label); + } + /** + * Create a value + * @param value Actual value + * @param unit Unit of the value + */ + public Value(float value, Unit unit){ + this(value, unit, null, ResourcesCompat.ID_NULL); + } + + public float getValue(){ + return this.mValue; + } + public Unit getUnit(){ + return this.mUnit; + } + @Nullable public String getId(){ + return this.mId; + } + public int getLabel(){ + return this.mLabel; + } +} diff --git a/app/src/main/java/com/easyfitness/views/SingleValueInputView.java b/app/src/main/java/com/easyfitness/views/SingleValueInputView.java index e3691d97..cf320b2b 100644 --- a/app/src/main/java/com/easyfitness/views/SingleValueInputView.java +++ b/app/src/main/java/com/easyfitness/views/SingleValueInputView.java @@ -177,9 +177,10 @@ public String getSelectedUnit() { } public void setSelectedUnit(String selectedUnit) { - ArrayAdapter arrayAdapter = (ArrayAdapter) unitSpinner.getAdapter(); - if (arrayAdapter != null) + ArrayAdapter arrayAdapter = (ArrayAdapter) unitSpinner.getAdapter(); + if (arrayAdapter != null) { unitSpinner.setSelection(arrayAdapter.getPosition(selectedUnit)); + } } public void setSelectedUnit(int selectedUnit) { diff --git a/app/src/main/res/layout/dialog_value_editor.xml b/app/src/main/res/layout/dialog_value_editor.xml index 31e5de3a..1d1f685e 100644 --- a/app/src/main/res/layout/dialog_value_editor.xml +++ b/app/src/main/res/layout/dialog_value_editor.xml @@ -50,17 +50,12 @@ tool:visibility="visible" /> - + android:orientation="vertical"/> Date: Thu, 21 Oct 2021 15:38:22 +0200 Subject: [PATCH 2/5] Updates BodyMeasure to use new Value type and adds original unit to value --- .../java/com/easyfitness/DAO/CVSManager.java | 17 ++-- .../com/easyfitness/DAO/DatabaseHelper.java | 20 +++-- .../DAO/bodymeasures/BodyMeasure.java | 19 ++--- .../DAO/bodymeasures/DAOBodyMeasure.java | 82 +++++++++++-------- .../java/com/easyfitness/ProfileFragment.java | 8 +- .../easyfitness/ValuesEditorDialogbox.java | 6 +- .../java/com/easyfitness/WeightFragment.java | 58 +++++++------ .../bodymeasures/BodyPartDetailsFragment.java | 37 ++++++--- .../bodymeasures/BodyPartListAdapter.java | 2 +- .../java/com/easyfitness/enums/UnitType.java | 1 + .../java/com/easyfitness/utils/Value.java | 15 +++- 11 files changed, 154 insertions(+), 111 deletions(-) diff --git a/app/src/main/java/com/easyfitness/DAO/CVSManager.java b/app/src/main/java/com/easyfitness/DAO/CVSManager.java index da5cbcb4..988f3c7c 100644 --- a/app/src/main/java/com/easyfitness/DAO/CVSManager.java +++ b/app/src/main/java/com/easyfitness/DAO/CVSManager.java @@ -4,7 +4,6 @@ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; -import android.icu.util.Output; import android.net.Uri; import android.os.Build; import android.os.Environment; @@ -18,7 +17,6 @@ import com.easyfitness.DAO.bodymeasures.DAOBodyMeasure; import com.easyfitness.DAO.bodymeasures.DAOBodyPart; import com.easyfitness.DAO.cardio.DAOOldCardio; -import com.easyfitness.DAO.program.Program; import com.easyfitness.DAO.record.DAOCardio; import com.easyfitness.DAO.record.DAORecord; import com.easyfitness.DAO.record.Record; @@ -27,6 +25,7 @@ import com.easyfitness.enums.Unit; import com.easyfitness.enums.WeightUnit; import com.easyfitness.utils.DateConverter; +import com.easyfitness.utils.Value; import java.io.File; import java.io.FileNotFoundException; @@ -34,16 +33,11 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.io.PipedOutputStream; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; -import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; -import java.util.Locale; - -import static android.provider.MediaStore.Downloads.EXTERNAL_CONTENT_URI; // Uses http://javacsv.sourceforge.net/com/csvreader/CsvReader.html // @@ -206,6 +200,7 @@ private boolean exportBodyMeasures(Profile pProfile, String destFolder) { cvsOutput.write(DAOBodyMeasure.DATE); cvsOutput.write("bodypart_label"); cvsOutput.write(DAOBodyMeasure.MEASURE); + cvsOutput.write(DAOBodyMeasure.UNIT); cvsOutput.write(DAOBodyMeasure.PROFIL_KEY); cvsOutput.endRecord(); @@ -216,7 +211,8 @@ private boolean exportBodyMeasures(Profile pProfile, String destFolder) { cvsOutput.write(DateConverter.dateToDBDateStr(dateRecord)); BodyPart bp = daoBodyPart.getBodyPart(bodyMeasures.get(i).getBodyPartID()); cvsOutput.write(bp.getName(mContext)); // Write the full name of the BodyPart - cvsOutput.write(Float.toString(bodyMeasures.get(i).getBodyMeasure())); + cvsOutput.write(Float.toString(bodyMeasures.get(i).getBodyMeasure().getValue())); + cvsOutput.write(Integer.toString(bodyMeasures.get(i).getBodyMeasure().getUnit().ordinal())); cvsOutput.write(Long.toString(bodyMeasures.get(i).getProfileID())); cvsOutput.endRecord(); @@ -382,12 +378,13 @@ public boolean importDatabase(InputStream file, Profile pProfile) { break; } case DAOProfileWeight.TABLE_NAME: { + // This is the deprecated weight table DAOBodyMeasure dbcWeight = new DAOBodyMeasure(mContext); dbcWeight.open(); Date date = DateConverter.DBDateStrToDate(csvRecords.get(DAOProfileWeight.DATE)); float poids = Float.parseFloat(csvRecords.get(DAOProfileWeight.POIDS)); - dbcWeight.addBodyMeasure(date, BodyPartExtensions.WEIGHT, poids, pProfile.getId(), Unit.KG); + dbcWeight.addBodyMeasure(date, BodyPartExtensions.WEIGHT, new Value(poids, Unit.KG), pProfile.getId()); break; } @@ -403,7 +400,7 @@ public boolean importDatabase(InputStream file, Profile pProfile) { for (BodyPart bp : bodyParts) { if (bp.getName(mContext).equals(bodyPartName)) { float measure = Float.parseFloat(csvRecords.get(DAOBodyMeasure.MEASURE)); - dbcBodyMeasure.addBodyMeasure(date, bp.getId(), measure, pProfile.getId(), unit); + dbcBodyMeasure.addBodyMeasure(date, bp.getId(), new Value(measure, unit), pProfile.getId()); dbcBodyPart.close(); break; } diff --git a/app/src/main/java/com/easyfitness/DAO/DatabaseHelper.java b/app/src/main/java/com/easyfitness/DAO/DatabaseHelper.java index dbb5b25c..677add8f 100644 --- a/app/src/main/java/com/easyfitness/DAO/DatabaseHelper.java +++ b/app/src/main/java/com/easyfitness/DAO/DatabaseHelper.java @@ -24,6 +24,7 @@ import com.easyfitness.enums.Muscle; import com.easyfitness.enums.Unit; import com.easyfitness.utils.DateConverter; +import com.easyfitness.utils.Value; import java.io.File; import java.util.ArrayList; @@ -32,7 +33,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { - public static final int DATABASE_VERSION = 24; + public static final int DATABASE_VERSION = 25; public static final String OLD09_DATABASE_NAME = "easyfitness"; public static final String DATABASE_NAME = "easyfitness.db"; private static DatabaseHelper sInstance; @@ -145,7 +146,6 @@ public void onUpgrade( db.execSQL("ALTER TABLE " + DAORecord.TABLE_NAME + " ADD COLUMN " + DAORecord.EXERCISE_TYPE + " INTEGER DEFAULT " + ExerciseType.STRENGTH.ordinal()); break; case 16: - // Merge of Cardio DB and Fonte DB db.execSQL("ALTER TABLE " + DAOBodyMeasure.TABLE_NAME + " ADD COLUMN " + DAOBodyMeasure.UNIT + " INTEGER"); migrateWeightTable(db); break; @@ -196,11 +196,15 @@ public void onUpgrade( List profileList = daoProfile.getAllProfiles(db); for (Profile profile:profileList) { - daoBodyMeasure.addBodyMeasure(db, DateConverter.getNewDate(), sizeBodyPartId, profile.getSize(), profile.getId(), defaultSizeUnit); + daoBodyMeasure.addBodyMeasure(db, DateConverter.getNewDate(), sizeBodyPartId, new Value(profile.getSize(), defaultSizeUnit), profile.getId()); } break; case 24: updateMusclesToUseNewIds(db); + break; + case 25: + db.execSQL("ALTER TABLE " + DAOBodyMeasure.TABLE_NAME + " ADD COLUMN " + DAOBodyMeasure.ORIGINAL_UNIT + " INTEGER"); + break; } upgradeTo++; } @@ -293,6 +297,7 @@ private void migrateWeightTable(SQLiteDatabase db) { value.put(DAOBodyMeasure.DATE, mCursor.getString(mCursor.getColumnIndex(DAOProfileWeight.DATE))); value.put(DAOBodyMeasure.BODYPART_ID, BodyPartExtensions.WEIGHT); value.put(DAOBodyMeasure.MEASURE, mCursor.getFloat(mCursor.getColumnIndex(DAOProfileWeight.POIDS))); + value.put(DAOBodyMeasure.UNIT, Unit.KG.ordinal()); value.put(DAOBodyMeasure.PROFIL_KEY, mCursor.getLong(mCursor.getColumnIndex(DAOProfileWeight.PROFIL_KEY))); db.insert(DAOBodyMeasure.TABLE_NAME, null, value); @@ -308,6 +313,8 @@ private void upgradeBodyMeasureUnits(SQLiteDatabase db) { List valueList = daoBodyMeasure.getMeasuresList(db, selectQuery); for (BodyMeasure bodyMeasure : valueList) { + Value oldValue = bodyMeasure.getBodyMeasure(); + Value newValue = oldValue; switch (bodyMeasure.getBodyPartID()) { case BodyPartExtensions.LEFTBICEPS: case BodyPartExtensions.RIGHTBICEPS: @@ -318,17 +325,18 @@ private void upgradeBodyMeasureUnits(SQLiteDatabase db) { case BodyPartExtensions.RIGHTTHIGH: case BodyPartExtensions.LEFTCALVES: case BodyPartExtensions.RIGHTCALVES: - bodyMeasure.setUnit(Unit.CM); + newValue = new Value(oldValue.getValue(), Unit.CM); break; case BodyPartExtensions.WEIGHT: - bodyMeasure.setUnit(Unit.KG); + newValue = new Value(oldValue.getValue(), Unit.KG); break; case BodyPartExtensions.MUSCLES: case BodyPartExtensions.WATER: case BodyPartExtensions.FAT: - bodyMeasure.setUnit(Unit.PERCENTAGE); + newValue = new Value(oldValue.getValue(), Unit.PERCENTAGE); break; } + bodyMeasure.setBodyMeasure(newValue); daoBodyMeasure.updateMeasure(db, bodyMeasure); } } diff --git a/app/src/main/java/com/easyfitness/DAO/bodymeasures/BodyMeasure.java b/app/src/main/java/com/easyfitness/DAO/bodymeasures/BodyMeasure.java index f6e19e24..13cb7735 100644 --- a/app/src/main/java/com/easyfitness/DAO/bodymeasures/BodyMeasure.java +++ b/app/src/main/java/com/easyfitness/DAO/bodymeasures/BodyMeasure.java @@ -1,6 +1,7 @@ package com.easyfitness.DAO.bodymeasures; import com.easyfitness.enums.Unit; +import com.easyfitness.utils.Value; import java.util.Date; @@ -11,18 +12,16 @@ public class BodyMeasure { private final long mProfil_id; // Notez que l'identifiant est un long private long mId; - private float mMeasure; - private Unit mUnit; + private Value mMeasure; private String mTime; - public BodyMeasure(long id, Date pDate, int pBodypart_id, float pMeasure, long pProfil_id, Unit pUnit) { + public BodyMeasure(long id, Date pDate, int pBodypart_id, Value pMeasure, long pProfil_id) { super(); mId = id; mDate = pDate; mBodypart_id = pBodypart_id; mMeasure = pMeasure; mProfil_id = pProfil_id; - mUnit = pUnit; } public long getId() { @@ -45,11 +44,11 @@ public int getBodyPartID() { return mBodypart_id; } - public float getBodyMeasure() { + public Value getBodyMeasure() { return mMeasure; } - public void setBodyMeasure(float bodyMeasure) { + public void setBodyMeasure(Value bodyMeasure) { mMeasure = bodyMeasure; } @@ -57,12 +56,4 @@ public long getProfileID() { return mProfil_id; } - public Unit getUnit() { - return mUnit; - } - - public void setUnit(Unit unit) { - mUnit = unit; - } - } diff --git a/app/src/main/java/com/easyfitness/DAO/bodymeasures/DAOBodyMeasure.java b/app/src/main/java/com/easyfitness/DAO/bodymeasures/DAOBodyMeasure.java index 01cc9135..d160c1e4 100644 --- a/app/src/main/java/com/easyfitness/DAO/bodymeasures/DAOBodyMeasure.java +++ b/app/src/main/java/com/easyfitness/DAO/bodymeasures/DAOBodyMeasure.java @@ -5,10 +5,13 @@ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; +import androidx.core.content.res.ResourcesCompat; + import com.easyfitness.DAO.DAOBase; import com.easyfitness.DAO.Profile; import com.easyfitness.enums.Unit; import com.easyfitness.utils.DateConverter; +import com.easyfitness.utils.Value; import java.util.ArrayList; import java.util.Date; @@ -24,9 +27,11 @@ public class DAOBodyMeasure extends DAOBase { public static final String MEASURE = "mesure"; public static final String DATE = "date"; public static final String UNIT = "unit"; + /** Contains the original unit in case it was converted */ + public static final String ORIGINAL_UNIT = "original_unit"; public static final String PROFIL_KEY = "profil_id"; - public static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME + " (" + KEY + " INTEGER PRIMARY KEY AUTOINCREMENT, " + DATE + " DATE, " + BODYPART_ID + " INTEGER, " + MEASURE + " REAL , " + PROFIL_KEY + " INTEGER, " + UNIT + " INTEGER);"; + public static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME + " (" + KEY + " INTEGER PRIMARY KEY AUTOINCREMENT, " + DATE + " DATE, " + BODYPART_ID + " INTEGER, " + MEASURE + " REAL , " + PROFIL_KEY + " INTEGER, " + UNIT + " INTEGER, " + ORIGINAL_UNIT + " INTEGER);"; public static final String TABLE_DROP = "DROP TABLE IF EXISTS " + TABLE_NAME + ";"; private final Profile mProfile = null; @@ -39,22 +44,22 @@ public DAOBodyMeasure(Context context) { /** * @param pDate date of the weight measure * @param pBodyPartId id of the body part - * @param pMeasure body measure + * @param pValue body measure * @param pProfileId profil associated with the measure */ - public void addBodyMeasure(Date pDate, long pBodyPartId, float pMeasure, long pProfileId, Unit pUnit) { + public void addBodyMeasure(Date pDate, long pBodyPartId, Value pValue, long pProfileId) { SQLiteDatabase db = this.getWritableDatabase(); - addBodyMeasure(db, pDate, pBodyPartId, pMeasure, pProfileId, pUnit); + addBodyMeasure(db, pDate, pBodyPartId, pValue, pProfileId); } /** * @param db database * @param pDate date of the weight measure * @param pBodyPartId id of the body part - * @param pMeasure body measure + * @param pValue body measure * @param pProfileId profil associated with the measure */ - public void addBodyMeasure(SQLiteDatabase db, Date pDate, long pBodyPartId, float pMeasure, long pProfileId, Unit pUnit) { + public void addBodyMeasure(SQLiteDatabase db, Date pDate, long pBodyPartId, Value pValue, long pProfileId) { ContentValues value = new ContentValues(); // Only one measure pr day, so if one already existing, updates it. @@ -64,15 +69,15 @@ public void addBodyMeasure(SQLiteDatabase db, Date pDate, long pBodyPartId, floa String dateString = DateConverter.dateToDBDateStr(pDate); value.put(DAOBodyMeasure.DATE, dateString); value.put(DAOBodyMeasure.BODYPART_ID, pBodyPartId); - value.put(DAOBodyMeasure.MEASURE, pMeasure); + value.put(DAOBodyMeasure.MEASURE, pValue.getValue()); value.put(DAOBodyMeasure.PROFIL_KEY, pProfileId); - value.put(DAOBodyMeasure.UNIT, pUnit.ordinal()); + value.put(DAOBodyMeasure.UNIT, pValue.getUnit().ordinal()); + value.put(DAOBodyMeasure.ORIGINAL_UNIT, pValue.getOriginalUnit() != null ? pValue.getOriginalUnit().ordinal() : null); db.insert(DAOBodyMeasure.TABLE_NAME, null, value); } else { - existingBodyMeasure.setBodyMeasure(pMeasure); - existingBodyMeasure.setUnit(pUnit); + existingBodyMeasure.setBodyMeasure(pValue); updateMeasure(db, existingBodyMeasure); } @@ -86,7 +91,7 @@ public BodyMeasure getMeasure(long id) { mCursor = null; mCursor = db.query(TABLE_NAME, - new String[]{KEY, DATE, BODYPART_ID, MEASURE, PROFIL_KEY, UNIT}, + new String[]{KEY, DATE, BODYPART_ID, MEASURE, PROFIL_KEY, UNIT, ORIGINAL_UNIT}, KEY + "=?", new String[]{String.valueOf(id)}, null, null, null, null); @@ -95,18 +100,24 @@ public BodyMeasure getMeasure(long id) { Date date = DateConverter.DBDateStrToDate(mCursor.getString(mCursor.getColumnIndex(DATE))); - BodyMeasure value = new BodyMeasure(mCursor.getLong(mCursor.getColumnIndex(KEY)), - date, - mCursor.getInt(mCursor.getColumnIndex(BODYPART_ID)), + int origUnitColumnIndex = mCursor.getColumnIndex(ORIGINAL_UNIT); + Value value = new Value( mCursor.getFloat(mCursor.getColumnIndex(MEASURE)), - mCursor.getLong(mCursor.getColumnIndex(PROFIL_KEY)), - Unit.fromInteger(mCursor.getInt(mCursor.getColumnIndex(UNIT))) + Unit.fromInteger(mCursor.getInt(mCursor.getColumnIndex(UNIT))), + null, + ResourcesCompat.ID_NULL, + mCursor.isNull(origUnitColumnIndex) ? null : Unit.fromInteger(mCursor.getInt(origUnitColumnIndex)) ); //db.close(); // return value - return value; + return new BodyMeasure(mCursor.getLong(mCursor.getColumnIndex(KEY)), + date, + mCursor.getInt(mCursor.getColumnIndex(BODYPART_ID)), + value, + mCursor.getLong(mCursor.getColumnIndex(PROFIL_KEY)) + ); } // Getting All Measures @@ -122,16 +133,24 @@ public List getMeasuresList(SQLiteDatabase db, String pRequest) { do { Date date = DateConverter.DBDateStrToDate(mCursor.getString(mCursor.getColumnIndex(DATE))); - BodyMeasure value = new BodyMeasure(mCursor.getLong(mCursor.getColumnIndex(KEY)), + int origUnitColumnIndex = mCursor.getColumnIndex(ORIGINAL_UNIT); + Value value = new Value( + mCursor.getFloat(mCursor.getColumnIndex(MEASURE)), + Unit.fromInteger(mCursor.getInt(mCursor.getColumnIndex(UNIT))), + null, + ResourcesCompat.ID_NULL, + mCursor.isNull(origUnitColumnIndex) ? null : Unit.fromInteger(mCursor.getInt(origUnitColumnIndex)) + ); + + BodyMeasure measure = new BodyMeasure(mCursor.getLong(mCursor.getColumnIndex(KEY)), date, mCursor.getInt(mCursor.getColumnIndex(BODYPART_ID)), - mCursor.getFloat(mCursor.getColumnIndex(MEASURE)), - mCursor.getLong(mCursor.getColumnIndex(PROFIL_KEY)), - Unit.fromInteger(mCursor.getInt(mCursor.getColumnIndex(UNIT))) + value, + mCursor.getLong(mCursor.getColumnIndex(PROFIL_KEY)) ); // Adding value to list - valueList.add(value); + valueList.add(measure); } while (mCursor.moveToNext()); } @@ -194,15 +213,14 @@ public List getAllBodyMeasures() { } /** - * Getting All Measures associated to a Body part for a specific Profile + * Getting the latest measures associated to a Body part for a specific Profile * * @param pBodyPartID * @param pProfile - * @return List */ public BodyMeasure getLastBodyMeasures(long pBodyPartID, Profile pProfile) { // Select All Query - String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE " + BODYPART_ID + "=" + pBodyPartID + " AND " + PROFIL_KEY + "=" + pProfile.getId() + " ORDER BY date(" + DATE + ") DESC"; + String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE " + BODYPART_ID + "=" + pBodyPartID + " AND " + PROFIL_KEY + "=" + pProfile.getId() + " ORDER BY date(" + DATE + ") DESC LIMIT 1"; List array = getMeasuresList(getReadableDatabase(), selectQuery); if (array.size() <= 0) { @@ -210,21 +228,20 @@ public BodyMeasure getLastBodyMeasures(long pBodyPartID, Profile pProfile) { } // return value list - return getMeasuresList(getReadableDatabase(), selectQuery).get(0); + return array.get(0); } /** - * Getting All Measures associated to a Body part for a specific Profile + * Getting the measure associated to a Body part for a specific Profile on a specific date * * @param pBodyPartID * @param pProfileId - * @return List */ public BodyMeasure getBodyMeasuresFromDate(SQLiteDatabase db, long pBodyPartID, Date pDate, long pProfileId) { String dateString = DateConverter.dateToDBDateStr(pDate); // Select All Query - String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE " + BODYPART_ID + "=" + pBodyPartID + " AND " + DATE + "=\"" + dateString + "\" AND " + PROFIL_KEY + "=" + pProfileId + " ORDER BY date(" + DATE + ") DESC"; + String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE " + BODYPART_ID + "=" + pBodyPartID + " AND " + DATE + "=\"" + dateString + "\" AND " + PROFIL_KEY + "=" + pProfileId + " ORDER BY date(" + DATE + ") DESC LIMIT 1"; List array = getMeasuresList(db, selectQuery); if (array.size() <= 0) { @@ -232,7 +249,7 @@ public BodyMeasure getBodyMeasuresFromDate(SQLiteDatabase db, long pBodyPartID, } // return value list - return getMeasuresList(getReadableDatabase(), selectQuery).get(0); + return array.get(0); } // Updating single value @@ -246,9 +263,10 @@ public int updateMeasure(SQLiteDatabase db, BodyMeasure m) { String dateString = DateConverter.dateToDBDateStr(m.getDate()); value.put(DAOBodyMeasure.DATE, dateString); value.put(DAOBodyMeasure.BODYPART_ID, m.getBodyPartID()); - value.put(DAOBodyMeasure.MEASURE, m.getBodyMeasure()); + value.put(DAOBodyMeasure.MEASURE, m.getBodyMeasure().getValue()); value.put(DAOBodyMeasure.PROFIL_KEY, m.getProfileID()); - value.put(DAOBodyMeasure.UNIT, m.getUnit().ordinal()); + value.put(DAOBodyMeasure.UNIT, m.getBodyMeasure().getUnit().ordinal()); + value.put(DAOBodyMeasure.ORIGINAL_UNIT, m.getBodyMeasure().getOriginalUnit() != null ? m.getBodyMeasure().getOriginalUnit().ordinal() : null); // updating row return db.update(TABLE_NAME, value, KEY + " = ?", diff --git a/app/src/main/java/com/easyfitness/ProfileFragment.java b/app/src/main/java/com/easyfitness/ProfileFragment.java index b4ec0a31..7c88488e 100644 --- a/app/src/main/java/com/easyfitness/ProfileFragment.java +++ b/app/src/main/java/com/easyfitness/ProfileFragment.java @@ -65,7 +65,7 @@ public class ProfileFragment extends Fragment { BodyMeasure lastSizeMeasure = daoBodyMeasure.getLastBodyMeasures(sizeBodyPart.getId(), appViMo.getProfile().getValue()); final Value lastSizeValue = lastSizeMeasure == null ? new Value(0, SettingsFragment.getDefaultSizeUnit(getActivity())) - : new Value(lastSizeMeasure.getBodyMeasure(), lastSizeMeasure.getUnit()); + : lastSizeMeasure.getBodyMeasure(); final ValuesEditorDialogbox editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastSizeValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); @@ -73,7 +73,7 @@ public class ProfileFragment extends Fragment { if (!editorDialogbox.isCancelled()) { Date date = DateConverter.localDateStrToDate(editorDialogbox.getDate(), getContext()); final Value newValue = editorDialogbox.getValues()[0]; - daoBodyMeasure.addBodyMeasure(date, sizeBodyPart.getId(), newValue.getValue(), appViMo.getProfile().getValue().getId(), newValue.getUnit()); + daoBodyMeasure.addBodyMeasure(date, sizeBodyPart.getId(), newValue, appViMo.getProfile().getValue().getId()); profileViMo.setSize(newValue.getValue()); profileViMo.setSizeUnit(newValue.getUnit()); requestForSave(); @@ -279,8 +279,8 @@ private void updateProfileViMo(Profile profile) { profileViMo.setName(profile.getName()); BodyMeasure sizeMeasure = daoBodyMeasure.getLastBodyMeasures(BodyPartExtensions.SIZE, profile); if (sizeMeasure != null) { - profileViMo.setSize(sizeMeasure.getBodyMeasure()); - profileViMo.setSizeUnit(sizeMeasure.getUnit()); + profileViMo.setSize(sizeMeasure.getBodyMeasure().getValue()); + profileViMo.setSizeUnit(sizeMeasure.getBodyMeasure().getUnit()); } else { profileViMo.setSize(0); profileViMo.setSizeUnit(Unit.CM); diff --git a/app/src/main/java/com/easyfitness/ValuesEditorDialogbox.java b/app/src/main/java/com/easyfitness/ValuesEditorDialogbox.java index 6725b96d..28285f71 100644 --- a/app/src/main/java/com/easyfitness/ValuesEditorDialogbox.java +++ b/app/src/main/java/com/easyfitness/ValuesEditorDialogbox.java @@ -74,6 +74,9 @@ protected void onCreate(Bundle savedInstanceState) { case PERCENTAGE: valueEdit.setUnits(new CharSequence[]{Unit.PERCENTAGE.toString()}); break; + case WEIGHT_OR_PERCENTAGE: + valueEdit.setUnits(new CharSequence[]{Unit.PERCENTAGE.toString(), Unit.KG.toString(), Unit.LBS.toString(), Unit.STONES.toString()}); + break; case NONE: default: valueEdit.setUnits(new CharSequence[]{Unit.UNITLESS.toString()}); @@ -137,7 +140,8 @@ public Value[] getValues() { Float.parseFloat(valueEdit.getValue().replaceAll(",", ".")), Unit.fromString(valueEdit.getSelectedUnit()), prevValue.getId(), - prevValue.getLabel()); + prevValue.getLabel(), + prevValue.getOriginalUnit()); } return returnValues; } diff --git a/app/src/main/java/com/easyfitness/WeightFragment.java b/app/src/main/java/com/easyfitness/WeightFragment.java index c302db2d..932f99c0 100644 --- a/app/src/main/java/com/easyfitness/WeightFragment.java +++ b/app/src/main/java/com/easyfitness/WeightFragment.java @@ -147,7 +147,7 @@ public void onClick(View view) { BodyMeasure lastWeightMeasure = mDbBodyMeasure.getLastBodyMeasures(weightBodyPart.getId(), getProfile()); Value lastWeighValue = lastWeightMeasure == null ? new Value(0, SettingsFragment.getDefaultWeightUnit(getActivity()).toUnit(), R.string.weightLabel) - : new Value(lastWeightMeasure.getBodyMeasure(), lastWeightMeasure.getUnit(), R.string.weightLabel); + : new Value(lastWeightMeasure.getBodyMeasure().getValue(), lastWeightMeasure.getBodyMeasure().getUnit(), R.string.weightLabel); editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastWeighValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); @@ -155,7 +155,7 @@ public void onClick(View view) { if (!editorDialogbox.isCancelled()) { Date date = DateConverter.localDateStrToDate(editorDialogbox.getDate(), getContext()); Value newValue = editorDialogbox.getValues()[0]; - mDbBodyMeasure.addBodyMeasure(date, weightBodyPart.getId(), newValue.getValue(), getProfile().getId(), newValue.getUnit()); + mDbBodyMeasure.addBodyMeasure(date, weightBodyPart.getId(), newValue, getProfile().getId()); refreshData(); } }); @@ -165,7 +165,7 @@ public void onClick(View view) { BodyMeasure lastFatMeasure = mDbBodyMeasure.getLastBodyMeasures(fatBodyPart.getId(), getProfile()); final Value lastFatValue = lastFatMeasure == null ? new Value(0, Unit.PERCENTAGE, R.string.fatLabel) - : new Value(lastFatMeasure.getBodyMeasure(), lastFatMeasure.getUnit(), R.string.fatLabel); + : new Value(lastFatMeasure.getBodyMeasure().getValue(), lastFatMeasure.getBodyMeasure().getUnit(), R.string.fatLabel); editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastFatValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); @@ -173,7 +173,7 @@ public void onClick(View view) { if (!editorDialogbox.isCancelled()) { Date date = DateConverter.localDateStrToDate(editorDialogbox.getDate(), getContext()); Value newValue = editorDialogbox.getValues()[0]; - mDbBodyMeasure.addBodyMeasure(date, fatBodyPart.getId(), newValue.getValue(), getProfile().getId(), newValue.getUnit()); + mDbBodyMeasure.addBodyMeasure(date, fatBodyPart.getId(), newValue, getProfile().getId()); refreshData(); } }); @@ -184,7 +184,7 @@ public void onClick(View view) { BodyMeasure lastMusclesMeasure = mDbBodyMeasure.getLastBodyMeasures(musclesBodyPart.getId(), getProfile()); Value lastMusclesValue = lastMusclesMeasure == null ? new Value(0, Unit.PERCENTAGE, R.string.musclesLabel) - : new Value(lastMusclesMeasure.getBodyMeasure(), lastMusclesMeasure.getUnit(), R.string.musclesLabel); + : new Value(lastMusclesMeasure.getBodyMeasure().getValue(), lastMusclesMeasure.getBodyMeasure().getUnit(), R.string.musclesLabel); editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastMusclesValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); @@ -192,7 +192,7 @@ public void onClick(View view) { if (!editorDialogbox.isCancelled()) { Date date = DateConverter.localDateStrToDate(editorDialogbox.getDate(), getContext()); Value newValue = editorDialogbox.getValues()[0]; - mDbBodyMeasure.addBodyMeasure(date, musclesBodyPart.getId(), newValue.getValue(), getProfile().getId(), newValue.getUnit()); + mDbBodyMeasure.addBodyMeasure(date, musclesBodyPart.getId(), newValue, getProfile().getId()); refreshData(); } }); @@ -203,7 +203,7 @@ public void onClick(View view) { BodyMeasure lastWaterMeasure = mDbBodyMeasure.getLastBodyMeasures(waterBodyPart.getId(), getProfile()); Value lastWaterValue = lastWaterMeasure == null ? new Value(0, Unit.PERCENTAGE, R.string.waterLabel) - : new Value(lastWaterMeasure.getBodyMeasure(), lastWaterMeasure.getUnit(), R.string.waterLabel); + : new Value(lastWaterMeasure.getBodyMeasure().getValue(), lastWaterMeasure.getBodyMeasure().getUnit(), R.string.waterLabel); editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastWaterValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); @@ -211,7 +211,7 @@ public void onClick(View view) { if (!editorDialogbox.isCancelled()) { Date date = DateConverter.localDateStrToDate(editorDialogbox.getDate(), getContext()); Value newValue = editorDialogbox.getValues()[0]; - mDbBodyMeasure.addBodyMeasure(date, waterBodyPart.getId(), newValue.getValue(), getProfile().getId(), newValue.getUnit()); + mDbBodyMeasure.addBodyMeasure(date, waterBodyPart.getId(), newValue, getProfile().getId()); refreshData(); } }); @@ -222,7 +222,7 @@ public void onClick(View view) { BodyMeasure lastSizeMeasure = mDbBodyMeasure.getLastBodyMeasures(sizeBodyPart.getId(), getProfile()); Value lastSizeValue = lastSizeMeasure == null ? new Value(0, SettingsFragment.getDefaultSizeUnit(getActivity()), R.string.size) - : new Value(lastSizeMeasure.getBodyMeasure(), lastSizeMeasure.getUnit(), R.string.size); + : new Value(lastSizeMeasure.getBodyMeasure().getValue(), lastSizeMeasure.getBodyMeasure().getUnit(), R.string.size); editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastSizeValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); @@ -230,7 +230,7 @@ public void onClick(View view) { if (!editorDialogbox.isCancelled()) { Date date = DateConverter.localDateStrToDate(editorDialogbox.getDate(), getContext()); Value newValue = editorDialogbox.getValues()[0]; - mDbBodyMeasure.addBodyMeasure(date, sizeBodyPart.getId(), newValue.getValue(), getProfile().getId(), newValue.getUnit()); + mDbBodyMeasure.addBodyMeasure(date, sizeBodyPart.getId(), newValue, getProfile().getId()); refreshData(); } }); @@ -357,7 +357,13 @@ private void DrawGraph() { if (valueList.size() > 0) { for (int i = valueList.size() - 1; i >= 0; i--) { - Entry value = new Entry((float) DateConverter.nbDays(valueList.get(i).getDate()), UnitConverter.weightConverter(valueList.get(i).getBodyMeasure(), valueList.get(i).getUnit(), Unit.KG)); + Entry value = new Entry( + (float) DateConverter.nbDays(valueList.get(i).getDate()), + UnitConverter.weightConverter(valueList.get(i).getBodyMeasure().getValue(), + valueList.get(i).getBodyMeasure().getUnit(), + Unit.KG + ) + ); yVals.add(value); } @@ -381,7 +387,7 @@ private void DrawGraph() { if (valueList.size() > 0) { for (int i = valueList.size() - 1; i >= 0; i--) { - Entry value = new Entry((float) DateConverter.nbDays(valueList.get(i).getDate()), valueList.get(i).getBodyMeasure()); + Entry value = new Entry((float) DateConverter.nbDays(valueList.get(i).getDate()), valueList.get(i).getBodyMeasure().getValue()); yVals.add(value); } @@ -402,7 +408,7 @@ private void DrawGraph() { if (valueList.size() > 0) { for (int i = valueList.size() - 1; i >= 0; i--) { - Entry value = new Entry((float) DateConverter.nbDays(valueList.get(i).getDate()), valueList.get(i).getBodyMeasure()); + Entry value = new Entry((float) DateConverter.nbDays(valueList.get(i).getDate()), valueList.get(i).getBodyMeasure().getValue()); yVals.add(value); } @@ -424,7 +430,7 @@ private void DrawGraph() { if (valueList.size() > 0) { for (int i = valueList.size() - 1; i >= 0; i--) { - Entry value = new Entry((float) DateConverter.nbDays(valueList.get(i).getDate()), valueList.get(i).getBodyMeasure()); + Entry value = new Entry((float) DateConverter.nbDays(valueList.get(i).getDate()), valueList.get(i).getBodyMeasure().getValue()); yVals.add(value); } @@ -446,7 +452,7 @@ private void DrawGraph() { if (valueList.size() > 0) { for (int i = valueList.size() - 1; i >= 0; i--) { - Entry value = new Entry((float) DateConverter.nbDays(valueList.get(i).getDate()), valueList.get(i).getBodyMeasure()); + Entry value = new Entry((float) DateConverter.nbDays(valueList.get(i).getDate()), valueList.get(i).getBodyMeasure().getValue()); yVals.add(value); } @@ -613,24 +619,24 @@ private void refreshData() { BodyMeasure lastSizeValue = mDbBodyMeasure.getLastBodyMeasures(sizeBodyPart.getId(), getProfile()); if (lastWeightValue != null) { - String editText = String.format("%.1f", lastWeightValue.getBodyMeasure()) + lastWeightValue.getUnit().toString(); + String editText = String.format("%.1f", lastWeightValue.getBodyMeasure().getValue()) + lastWeightValue.getBodyMeasure().getUnit().toString(); weightEdit.setText(editText); // update IMC - if (lastSizeValue == null || lastSizeValue.getBodyMeasure() == 0) { + if (lastSizeValue == null || lastSizeValue.getBodyMeasure().getValue() == 0) { imcText.setText("-"); imcRank.setText(R.string.no_size_available); ffmiText.setText("-"); ffmiRank.setText(R.string.no_size_available); } else { - float imcValue = calculateImc(UnitConverter.weightConverter(lastWeightValue.getBodyMeasure(), lastWeightValue.getUnit(), Unit.KG), - UnitConverter.sizeConverter(lastSizeValue.getBodyMeasure(), lastSizeValue.getUnit(), Unit.CM) ); + float imcValue = calculateImc(UnitConverter.weightConverter(lastWeightValue.getBodyMeasure().getValue(), lastWeightValue.getBodyMeasure().getUnit(), Unit.KG), + UnitConverter.sizeConverter(lastSizeValue.getBodyMeasure().getValue(), lastSizeValue.getBodyMeasure().getUnit(), Unit.CM) ); imcText.setText(String.format("%.1f", imcValue)); imcRank.setText(getImcText(imcValue)); if (lastFatValue != null) { - double ffmiValue = calculateFfmi(UnitConverter.weightConverter(lastWeightValue.getBodyMeasure(), lastWeightValue.getUnit(), Unit.KG), - UnitConverter.sizeConverter(lastSizeValue.getBodyMeasure(), lastSizeValue.getUnit(), Unit.CM), - lastFatValue.getBodyMeasure()); + double ffmiValue = calculateFfmi(UnitConverter.weightConverter(lastWeightValue.getBodyMeasure().getValue(), lastWeightValue.getBodyMeasure().getUnit(), Unit.KG), + UnitConverter.sizeConverter(lastSizeValue.getBodyMeasure().getValue(), lastSizeValue.getBodyMeasure().getUnit(), Unit.CM), + lastFatValue.getBodyMeasure().getValue()); ffmiText.setText(String.format("%.1f", ffmiValue)); if (getProfile().getGender() == Gender.FEMALE) ffmiRank.setText(getFfmiTextForWomen(ffmiValue)); @@ -655,25 +661,25 @@ else if (getProfile().getGender() == Gender.OTHER) } if (lastWaterValue != null) { - String editText = String.format("%.1f", lastWaterValue.getBodyMeasure()) + lastWaterValue.getUnit().toString(); + String editText = String.format("%.1f", lastWaterValue.getBodyMeasure().getValue()) + lastWaterValue.getBodyMeasure().getUnit().toString(); waterEdit.setText(editText); } else waterEdit.setText("-"); if (lastFatValue != null) { - String editText = String.format("%.1f", lastFatValue.getBodyMeasure()) + lastFatValue.getUnit().toString(); + String editText = String.format("%.1f", lastFatValue.getBodyMeasure().getValue()) + lastFatValue.getBodyMeasure().getUnit().toString(); fatEdit.setText(editText); } else fatEdit.setText("-"); if (lastMusclesValue != null) { - String editText = String.format("%.1f", lastMusclesValue.getBodyMeasure()) + lastMusclesValue.getUnit().toString(); + String editText = String.format("%.1f", lastMusclesValue.getBodyMeasure().getValue()) + lastMusclesValue.getBodyMeasure().getUnit().toString(); musclesEdit.setText(editText); } else musclesEdit.setText("-"); if (lastSizeValue != null) { - String editText = String.format("%.1f", lastSizeValue.getBodyMeasure()) + lastSizeValue.getUnit().toString(); + String editText = String.format("%.1f", lastSizeValue.getBodyMeasure().getValue()) + lastSizeValue.getBodyMeasure().getUnit().toString(); sizeEdit.setText(editText); mSizeLineChart.setVisibility(View.VISIBLE); } else { diff --git a/app/src/main/java/com/easyfitness/bodymeasures/BodyPartDetailsFragment.java b/app/src/main/java/com/easyfitness/bodymeasures/BodyPartDetailsFragment.java index 5d93a8b1..5e2677dd 100644 --- a/app/src/main/java/com/easyfitness/bodymeasures/BodyPartDetailsFragment.java +++ b/app/src/main/java/com/easyfitness/bodymeasures/BodyPartDetailsFragment.java @@ -80,7 +80,7 @@ public void onClick(View v) { BodyMeasure lastBodyMeasure = mBodyMeasureDb.getLastBodyMeasures(mInitialBodyPart.getId(), getProfile()); final Value lastValue = lastBodyMeasure == null ? new Value(0, getValidUnit(null)) - : new Value(lastBodyMeasure.getBodyMeasure(), getValidUnit(lastBodyMeasure)); + : getValueWithValidUnit(lastBodyMeasure); ValuesEditorDialogbox editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); @@ -88,7 +88,7 @@ public void onClick(View v) { if (!editorDialogbox.isCancelled()) { Date date = DateConverter.localDateStrToDate(editorDialogbox.getDate(), getContext()); final Value newValue = editorDialogbox.getValues()[0]; - mBodyMeasureDb.addBodyMeasure(date, mInitialBodyPart.getId(), newValue.getValue(), getProfile().getId(), newValue.getUnit()); + mBodyMeasureDb.addBodyMeasure(date, mInitialBodyPart.getId(), newValue, getProfile().getId()); refreshData(); } }); @@ -272,22 +272,22 @@ private void DrawGraph(List valueList) { for (int i = valueList.size() - 1; i >= 0; i--) { float normalizedMeasure; - switch (valueList.get(i).getUnit().getUnitType()) { + switch (valueList.get(i).getBodyMeasure().getUnit().getUnitType()) { case WEIGHT: - normalizedMeasure = UnitConverter.weightConverter(valueList.get(i).getBodyMeasure(), valueList.get(i).getUnit(), SettingsFragment.getDefaultWeightUnit(getActivity()).toUnit()); + normalizedMeasure = UnitConverter.weightConverter(valueList.get(i).getBodyMeasure().getValue(), valueList.get(i).getBodyMeasure().getUnit(), SettingsFragment.getDefaultWeightUnit(getActivity()).toUnit()); break; case SIZE: - normalizedMeasure = UnitConverter.sizeConverter(valueList.get(i).getBodyMeasure(), valueList.get(i).getUnit(), SettingsFragment.getDefaultSizeUnit(getActivity())); + normalizedMeasure = UnitConverter.sizeConverter(valueList.get(i).getBodyMeasure().getValue(), valueList.get(i).getBodyMeasure().getUnit(), SettingsFragment.getDefaultSizeUnit(getActivity())); break; default: - normalizedMeasure = valueList.get(i).getBodyMeasure(); + normalizedMeasure = valueList.get(i).getBodyMeasure().getValue(); } Entry value = new Entry((float) DateConverter.nbDays(valueList.get(i).getDate()), normalizedMeasure); yVals.add(value); - if (minBodyMeasure == -1) minBodyMeasure = valueList.get(i).getBodyMeasure(); - else if (valueList.get(i).getBodyMeasure() < minBodyMeasure) - minBodyMeasure = valueList.get(i).getBodyMeasure(); + if (minBodyMeasure == -1) minBodyMeasure = valueList.get(i).getBodyMeasure().getValue(); + else if (valueList.get(i).getBodyMeasure().getValue() < minBodyMeasure) + minBodyMeasure = valueList.get(i).getBodyMeasure().getValue(); } mDateGraph.draw(yVals); @@ -354,14 +354,19 @@ private void showDeleteDialog(final long idToDelete) { private void showEditDialog(final long idToEdit) { BodyMeasure bodyMeasure = mBodyMeasureDb.getMeasure(idToEdit); - final Value lastValue = new Value(bodyMeasure.getBodyMeasure(), getValidUnit(bodyMeasure)); + final Value lastValue = bodyMeasure.getBodyMeasure(); ValuesEditorDialogbox editorDialogbox = new ValuesEditorDialogbox(getActivity(), bodyMeasure.getDate(), "", new Value[]{lastValue}); editorDialogbox.setOnDismissListener(dialog -> { if (!editorDialogbox.isCancelled()) { Date date = DateConverter.localDateStrToDate(editorDialogbox.getDate(), getContext()); final Value newValue = editorDialogbox.getValues()[0]; - BodyMeasure updatedBodyMeasure = new BodyMeasure(bodyMeasure.getId(), date, bodyMeasure.getBodyPartID(), newValue.getValue(), bodyMeasure.getProfileID(), newValue.getUnit()); + BodyMeasure updatedBodyMeasure = new BodyMeasure( + bodyMeasure.getId(), + date, + bodyMeasure.getBodyPartID(), + newValue, + bodyMeasure.getProfileID()); int i = mBodyMeasureDb.updateMeasure(updatedBodyMeasure); refreshData(); } @@ -412,7 +417,7 @@ public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { private Unit getValidUnit(BodyMeasure lastBodyMeasure) { UnitType unitType = BodyPartExtensions.getUnitType(mInitialBodyPart.getBodyPartResKey()); if (lastBodyMeasure != null) { - if (unitType != lastBodyMeasure.getUnit().getUnitType()) { + if (unitType != lastBodyMeasure.getBodyMeasure().getUnit().getUnitType()) { lastBodyMeasure = null; } } @@ -431,8 +436,14 @@ private Unit getValidUnit(BodyMeasure lastBodyMeasure) { break; } } else { - unitDef = lastBodyMeasure.getUnit(); + unitDef = lastBodyMeasure.getBodyMeasure().getUnit(); } return unitDef; } + + private Value getValueWithValidUnit(BodyMeasure lastBodyMeasure) { + Unit unitDef = getValidUnit(lastBodyMeasure); + Value oldValue = lastBodyMeasure.getBodyMeasure(); + return new Value(oldValue.getValue(), unitDef, oldValue.getId(), oldValue.getLabel(), oldValue.getOriginalUnit()); + } } diff --git a/app/src/main/java/com/easyfitness/bodymeasures/BodyPartListAdapter.java b/app/src/main/java/com/easyfitness/bodymeasures/BodyPartListAdapter.java index 80e54b6b..628054ca 100644 --- a/app/src/main/java/com/easyfitness/bodymeasures/BodyPartListAdapter.java +++ b/app/src/main/java/com/easyfitness/bodymeasures/BodyPartListAdapter.java @@ -95,7 +95,7 @@ public View getView(int position, View convertView, ViewGroup parent) { if (valueList.size() > 0) { for (int i = valueList.size() - 1; i >= 0; i--) { - Entry value = new Entry((float) DateConverter.nbDays(valueList.get(i).getDate()), valueList.get(i).getBodyMeasure()); + Entry value = new Entry((float) DateConverter.nbDays(valueList.get(i).getDate()), valueList.get(i).getBodyMeasure().getValue()); yVals.add(value); } diff --git a/app/src/main/java/com/easyfitness/enums/UnitType.java b/app/src/main/java/com/easyfitness/enums/UnitType.java index bd1ed3d1..06927398 100644 --- a/app/src/main/java/com/easyfitness/enums/UnitType.java +++ b/app/src/main/java/com/easyfitness/enums/UnitType.java @@ -5,5 +5,6 @@ public enum UnitType { DISTANCE, SIZE, PERCENTAGE, + WEIGHT_OR_PERCENTAGE, NONE } diff --git a/app/src/main/java/com/easyfitness/utils/Value.java b/app/src/main/java/com/easyfitness/utils/Value.java index eec7a766..0d907873 100644 --- a/app/src/main/java/com/easyfitness/utils/Value.java +++ b/app/src/main/java/com/easyfitness/utils/Value.java @@ -8,6 +8,8 @@ public class Value { private final float mValue; private final Unit mUnit; + /** Original unit in case this value was converted */ + private final Unit mOriginalUnit; /** Identifier for this value so we can later differentiate between values */ private final String mId; private final int mLabel; @@ -18,12 +20,14 @@ public class Value { * @param unit Unit of the value * @param id Custom identifier to differentiate between values * @param label String resource to label this value is described by + * @param originalUnit Original unit in case this value was converted */ - public Value(float value, Unit unit, @Nullable String id, int label){ + public Value(float value, Unit unit, @Nullable String id, int label, @Nullable Unit originalUnit){ this.mValue = value; this.mUnit = unit; this.mId = id; this.mLabel = label; + this.mOriginalUnit = originalUnit; } /** * Create a value @@ -32,7 +36,7 @@ public Value(float value, Unit unit, @Nullable String id, int label){ * @param id Custom identifier to differentiate between values */ public Value(float value, Unit unit, @Nullable String id){ - this(value, unit, id, ResourcesCompat.ID_NULL); + this(value, unit, id, ResourcesCompat.ID_NULL, null); } /** * Create a value @@ -41,7 +45,7 @@ public Value(float value, Unit unit, @Nullable String id){ * @param label String resource to label this value is described by */ public Value(float value, Unit unit, int label){ - this(value, unit, null, label); + this(value, unit, null, label, null); } /** * Create a value @@ -49,7 +53,7 @@ public Value(float value, Unit unit, int label){ * @param unit Unit of the value */ public Value(float value, Unit unit){ - this(value, unit, null, ResourcesCompat.ID_NULL); + this(value, unit, null, ResourcesCompat.ID_NULL, null); } public float getValue(){ @@ -58,6 +62,9 @@ public float getValue(){ public Unit getUnit(){ return this.mUnit; } + @Nullable public Unit getOriginalUnit(){ + return this.mOriginalUnit; + } @Nullable public String getId(){ return this.mId; } From 5c3b0d3958b9cedd29e059e38ebb2cec14daf752 Mon Sep 17 00:00:00 2001 From: Julian Reinhardt Date: Thu, 21 Oct 2021 16:38:20 +0200 Subject: [PATCH 3/5] Adds dialog to add all weight properties at once and add fat/muscle/water as weight to be converted to percent --- .../com/easyfitness/DAO/DatabaseHelper.java | 5 +- .../DAO/bodymeasures/DAOBodyMeasure.java | 16 +- .../easyfitness/ValuesEditorDialogbox.java | 3 +- .../java/com/easyfitness/WeightFragment.java | 124 ++- .../bodymeasures/BodyPartDetailsFragment.java | 2 +- .../main/java/com/easyfitness/enums/Unit.java | 5 +- .../java/com/easyfitness/utils/Value.java | 25 +- app/src/main/res/drawable/ic_add.xml | 5 + app/src/main/res/layout/tab_weight.xml | 746 +++++++++--------- 9 files changed, 517 insertions(+), 414 deletions(-) create mode 100644 app/src/main/res/drawable/ic_add.xml diff --git a/app/src/main/java/com/easyfitness/DAO/DatabaseHelper.java b/app/src/main/java/com/easyfitness/DAO/DatabaseHelper.java index 677add8f..bebabdde 100644 --- a/app/src/main/java/com/easyfitness/DAO/DatabaseHelper.java +++ b/app/src/main/java/com/easyfitness/DAO/DatabaseHelper.java @@ -33,7 +33,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { - public static final int DATABASE_VERSION = 25; + public static final int DATABASE_VERSION = 24; public static final String OLD09_DATABASE_NAME = "easyfitness"; public static final String DATABASE_NAME = "easyfitness.db"; private static DatabaseHelper sInstance; @@ -202,9 +202,6 @@ public void onUpgrade( case 24: updateMusclesToUseNewIds(db); break; - case 25: - db.execSQL("ALTER TABLE " + DAOBodyMeasure.TABLE_NAME + " ADD COLUMN " + DAOBodyMeasure.ORIGINAL_UNIT + " INTEGER"); - break; } upgradeTo++; } diff --git a/app/src/main/java/com/easyfitness/DAO/bodymeasures/DAOBodyMeasure.java b/app/src/main/java/com/easyfitness/DAO/bodymeasures/DAOBodyMeasure.java index d160c1e4..513da555 100644 --- a/app/src/main/java/com/easyfitness/DAO/bodymeasures/DAOBodyMeasure.java +++ b/app/src/main/java/com/easyfitness/DAO/bodymeasures/DAOBodyMeasure.java @@ -27,11 +27,9 @@ public class DAOBodyMeasure extends DAOBase { public static final String MEASURE = "mesure"; public static final String DATE = "date"; public static final String UNIT = "unit"; - /** Contains the original unit in case it was converted */ - public static final String ORIGINAL_UNIT = "original_unit"; public static final String PROFIL_KEY = "profil_id"; - public static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME + " (" + KEY + " INTEGER PRIMARY KEY AUTOINCREMENT, " + DATE + " DATE, " + BODYPART_ID + " INTEGER, " + MEASURE + " REAL , " + PROFIL_KEY + " INTEGER, " + UNIT + " INTEGER, " + ORIGINAL_UNIT + " INTEGER);"; + public static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME + " (" + KEY + " INTEGER PRIMARY KEY AUTOINCREMENT, " + DATE + " DATE, " + BODYPART_ID + " INTEGER, " + MEASURE + " REAL , " + PROFIL_KEY + " INTEGER, " + UNIT + " INTEGER);"; public static final String TABLE_DROP = "DROP TABLE IF EXISTS " + TABLE_NAME + ";"; private final Profile mProfile = null; @@ -72,7 +70,6 @@ public void addBodyMeasure(SQLiteDatabase db, Date pDate, long pBodyPartId, Valu value.put(DAOBodyMeasure.MEASURE, pValue.getValue()); value.put(DAOBodyMeasure.PROFIL_KEY, pProfileId); value.put(DAOBodyMeasure.UNIT, pValue.getUnit().ordinal()); - value.put(DAOBodyMeasure.ORIGINAL_UNIT, pValue.getOriginalUnit() != null ? pValue.getOriginalUnit().ordinal() : null); db.insert(DAOBodyMeasure.TABLE_NAME, null, value); @@ -91,7 +88,7 @@ public BodyMeasure getMeasure(long id) { mCursor = null; mCursor = db.query(TABLE_NAME, - new String[]{KEY, DATE, BODYPART_ID, MEASURE, PROFIL_KEY, UNIT, ORIGINAL_UNIT}, + new String[]{KEY, DATE, BODYPART_ID, MEASURE, PROFIL_KEY, UNIT}, KEY + "=?", new String[]{String.valueOf(id)}, null, null, null, null); @@ -100,13 +97,11 @@ public BodyMeasure getMeasure(long id) { Date date = DateConverter.DBDateStrToDate(mCursor.getString(mCursor.getColumnIndex(DATE))); - int origUnitColumnIndex = mCursor.getColumnIndex(ORIGINAL_UNIT); Value value = new Value( mCursor.getFloat(mCursor.getColumnIndex(MEASURE)), Unit.fromInteger(mCursor.getInt(mCursor.getColumnIndex(UNIT))), null, - ResourcesCompat.ID_NULL, - mCursor.isNull(origUnitColumnIndex) ? null : Unit.fromInteger(mCursor.getInt(origUnitColumnIndex)) + ResourcesCompat.ID_NULL ); //db.close(); @@ -133,13 +128,11 @@ public List getMeasuresList(SQLiteDatabase db, String pRequest) { do { Date date = DateConverter.DBDateStrToDate(mCursor.getString(mCursor.getColumnIndex(DATE))); - int origUnitColumnIndex = mCursor.getColumnIndex(ORIGINAL_UNIT); Value value = new Value( mCursor.getFloat(mCursor.getColumnIndex(MEASURE)), Unit.fromInteger(mCursor.getInt(mCursor.getColumnIndex(UNIT))), null, - ResourcesCompat.ID_NULL, - mCursor.isNull(origUnitColumnIndex) ? null : Unit.fromInteger(mCursor.getInt(origUnitColumnIndex)) + ResourcesCompat.ID_NULL ); BodyMeasure measure = new BodyMeasure(mCursor.getLong(mCursor.getColumnIndex(KEY)), @@ -266,7 +259,6 @@ public int updateMeasure(SQLiteDatabase db, BodyMeasure m) { value.put(DAOBodyMeasure.MEASURE, m.getBodyMeasure().getValue()); value.put(DAOBodyMeasure.PROFIL_KEY, m.getProfileID()); value.put(DAOBodyMeasure.UNIT, m.getBodyMeasure().getUnit().ordinal()); - value.put(DAOBodyMeasure.ORIGINAL_UNIT, m.getBodyMeasure().getOriginalUnit() != null ? m.getBodyMeasure().getOriginalUnit().ordinal() : null); // updating row return db.update(TABLE_NAME, value, KEY + " = ?", diff --git a/app/src/main/java/com/easyfitness/ValuesEditorDialogbox.java b/app/src/main/java/com/easyfitness/ValuesEditorDialogbox.java index 28285f71..84d52bd1 100644 --- a/app/src/main/java/com/easyfitness/ValuesEditorDialogbox.java +++ b/app/src/main/java/com/easyfitness/ValuesEditorDialogbox.java @@ -140,8 +140,7 @@ public Value[] getValues() { Float.parseFloat(valueEdit.getValue().replaceAll(",", ".")), Unit.fromString(valueEdit.getSelectedUnit()), prevValue.getId(), - prevValue.getLabel(), - prevValue.getOriginalUnit()); + prevValue.getLabel()); } return returnValues; } diff --git a/app/src/main/java/com/easyfitness/WeightFragment.java b/app/src/main/java/com/easyfitness/WeightFragment.java index 932f99c0..510b1d30 100644 --- a/app/src/main/java/com/easyfitness/WeightFragment.java +++ b/app/src/main/java/com/easyfitness/WeightFragment.java @@ -12,6 +12,7 @@ import android.widget.TextView; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.ViewModelProvider; @@ -26,6 +27,7 @@ import com.easyfitness.DAO.bodymeasures.DAOBodyPart; import com.easyfitness.bodymeasures.BodyPartDetailsFragment; import com.easyfitness.enums.Unit; +import com.easyfitness.enums.UnitType; import com.easyfitness.graph.MiniDateGraph; import com.easyfitness.utils.DateConverter; import com.easyfitness.enums.Gender; @@ -33,6 +35,7 @@ import com.easyfitness.utils.Value; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.data.Entry; +import com.google.android.material.floatingactionbutton.FloatingActionButton; import java.util.ArrayList; import java.util.Date; @@ -145,9 +148,7 @@ public void onClick(View view) { switch (view.getId()) { case R.id.weightInput: BodyMeasure lastWeightMeasure = mDbBodyMeasure.getLastBodyMeasures(weightBodyPart.getId(), getProfile()); - Value lastWeighValue = lastWeightMeasure == null - ? new Value(0, SettingsFragment.getDefaultWeightUnit(getActivity()).toUnit(), R.string.weightLabel) - : new Value(lastWeightMeasure.getBodyMeasure().getValue(), lastWeightMeasure.getBodyMeasure().getUnit(), R.string.weightLabel); + Value lastWeighValue = getValueFromLastMeasure(lastWeightMeasure, SettingsFragment.getDefaultWeightUnit(getActivity()).toUnit(), null, R.string.weightLabel); editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastWeighValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); @@ -163,9 +164,7 @@ public void onClick(View view) { break; case R.id.fatInput: BodyMeasure lastFatMeasure = mDbBodyMeasure.getLastBodyMeasures(fatBodyPart.getId(), getProfile()); - final Value lastFatValue = lastFatMeasure == null - ? new Value(0, Unit.PERCENTAGE, R.string.fatLabel) - : new Value(lastFatMeasure.getBodyMeasure().getValue(), lastFatMeasure.getBodyMeasure().getUnit(), R.string.fatLabel); + final Value lastFatValue = getValueFromLastMeasure(lastFatMeasure, Unit.PERCENTAGE, null, R.string.fatLabel); editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastFatValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); @@ -182,9 +181,7 @@ public void onClick(View view) { break; case R.id.musclesInput: BodyMeasure lastMusclesMeasure = mDbBodyMeasure.getLastBodyMeasures(musclesBodyPart.getId(), getProfile()); - Value lastMusclesValue = lastMusclesMeasure == null - ? new Value(0, Unit.PERCENTAGE, R.string.musclesLabel) - : new Value(lastMusclesMeasure.getBodyMeasure().getValue(), lastMusclesMeasure.getBodyMeasure().getUnit(), R.string.musclesLabel); + Value lastMusclesValue = getValueFromLastMeasure(lastMusclesMeasure, Unit.PERCENTAGE, null, R.string.musclesLabel); editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastMusclesValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); @@ -201,9 +198,7 @@ public void onClick(View view) { break; case R.id.waterInput: BodyMeasure lastWaterMeasure = mDbBodyMeasure.getLastBodyMeasures(waterBodyPart.getId(), getProfile()); - Value lastWaterValue = lastWaterMeasure == null - ? new Value(0, Unit.PERCENTAGE, R.string.waterLabel) - : new Value(lastWaterMeasure.getBodyMeasure().getValue(), lastWaterMeasure.getBodyMeasure().getUnit(), R.string.waterLabel); + Value lastWaterValue = getValueFromLastMeasure(lastWaterMeasure, Unit.PERCENTAGE, null, R.string.waterLabel); editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastWaterValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); @@ -220,9 +215,7 @@ public void onClick(View view) { break; case R.id.sizeInput: BodyMeasure lastSizeMeasure = mDbBodyMeasure.getLastBodyMeasures(sizeBodyPart.getId(), getProfile()); - Value lastSizeValue = lastSizeMeasure == null - ? new Value(0, SettingsFragment.getDefaultSizeUnit(getActivity()), R.string.size) - : new Value(lastSizeMeasure.getBodyMeasure().getValue(), lastSizeMeasure.getBodyMeasure().getUnit(), R.string.size); + Value lastSizeValue = getValueFromLastMeasure(lastSizeMeasure, SettingsFragment.getDefaultSizeUnit(getActivity()), null, R.string.size); editorDialogbox = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastSizeValue}); editorDialogbox.setTitle(R.string.AddLabel); editorDialogbox.setPositiveButton(R.string.AddLabel); @@ -240,6 +233,75 @@ public void onClick(View view) { } }; + private final OnClickListener mOnAddAllEntriesClickListener = new OnClickListener() { + @Override + public void onClick(View view) { + BodyMeasure lastWeightMeasure = mDbBodyMeasure.getLastBodyMeasures(weightBodyPart.getId(), getProfile()); + BodyMeasure lastFatMeasure = mDbBodyMeasure.getLastBodyMeasures(fatBodyPart.getId(), getProfile()); + BodyMeasure lastMusclesMeasure = mDbBodyMeasure.getLastBodyMeasures(musclesBodyPart.getId(), getProfile()); + BodyMeasure lastWaterMeasure = mDbBodyMeasure.getLastBodyMeasures(waterBodyPart.getId(), getProfile()); + + Value lastWeightValue = getValueFromLastMeasure(lastWeightMeasure, SettingsFragment.getDefaultWeightUnit(getActivity()).toUnit(), String.valueOf(weightBodyPart.getId()), R.string.weightLabel); + Value lastFatValue = getValueFromLastMeasure(lastFatMeasure, Unit.PERCENTAGE, String.valueOf(fatBodyPart.getId()), R.string.fatLabel); + Value lastMusclesValue = getValueFromLastMeasure(lastMusclesMeasure, Unit.PERCENTAGE, String.valueOf(musclesBodyPart.getId()), R.string.musclesLabel); + Value lastWaterValue = getValueFromLastMeasure(lastWaterMeasure, Unit.PERCENTAGE, String.valueOf(waterBodyPart.getId()), R.string.waterLabel); + + // Add other unit options in addition to percentages + Unit fatUnit = lastFatValue.getUnit(); + fatUnit.setUnitType(UnitType.WEIGHT_OR_PERCENTAGE); + lastFatValue.setUnit(fatUnit); + + Unit musclesUnit = lastMusclesValue.getUnit(); + musclesUnit.setUnitType(UnitType.WEIGHT_OR_PERCENTAGE); + lastMusclesValue.setUnit(musclesUnit); + + Unit waterUnit = lastWaterValue.getUnit(); + waterUnit.setUnitType(UnitType.WEIGHT_OR_PERCENTAGE); + lastWaterValue.setUnit(waterUnit); + + ValuesEditorDialogbox editorDialog = new ValuesEditorDialogbox(getActivity(), new Date(), "", new Value[]{lastWeightValue, lastFatValue, lastMusclesValue, lastWaterValue}); + editorDialog.setTitle(R.string.AddLabel); + editorDialog.setPositiveButton(R.string.AddLabel); + editorDialog.setOnDismissListener(dialog -> { + if (!editorDialog.isCancelled()) { + Date date = DateConverter.localDateStrToDate(editorDialog.getDate(), getContext()); + long profileId = getProfile().getId(); + Value[] newValues = editorDialog.getValues(); + Value newWeightValue = null; + // Find the weight value if we need it as the baseline + for (Value newValue : newValues) { + if(newValue.getId().equals(String.valueOf(weightBodyPart.getId()))){ + newWeightValue = newValue; + break; + } + } + // Fallback to generic weight in case it is not there, which should never happen + if(newWeightValue == null){ + newWeightValue = new Value(75, Unit.KG); + } + for (Value newValue : newValues) { + long bodyPartId = Long.parseLong(newValue.getId()); + // If the unit is not in percent and it is a percentage measurement convert it + if(newValue.getUnit() != Unit.PERCENTAGE && (bodyPartId == fatBodyPart.getId() || bodyPartId == musclesBodyPart.getId() || bodyPartId == waterBodyPart.getId())){ + // Convert them to the same unit if necessary + if(newValue.getUnit() != newWeightValue.getUnit()){ + newValue.setValue(UnitConverter.weightConverter(newValue.getValue(), newValue.getUnit(), newWeightValue.getUnit())); + newValue.setUnit(newWeightValue.getUnit()); + } + + // Convert absolute value to percentage + newValue.setValue((newValue.getValue() / newWeightValue.getValue())*100); + newValue.setUnit(Unit.PERCENTAGE); + } + mDbBodyMeasure.addBodyMeasure(date, bodyPartId, newValue, profileId); + } + refreshData(); + } + }); + editorDialog.show(); + } + }; + /** * Create a new instance of DetailsFragment, initialized to * show the text at 'index'. @@ -285,12 +347,15 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, ImageButton imcHelpButton = view.findViewById(R.id.imcHelp); ImageButton rfmHelpButton = view.findViewById(R.id.rfmHelp); + FloatingActionButton addAllButton = view.findViewById(R.id.addAllWeightEntries); + /* Initialisation des evenements */ weightEdit.setOnClickListener(mOnClickListener); fatEdit.setOnClickListener(mOnClickListener); musclesEdit.setOnClickListener(mOnClickListener); waterEdit.setOnClickListener(mOnClickListener); sizeEdit.setOnClickListener(mOnClickListener); + addAllButton.setOnClickListener(mOnAddAllEntriesClickListener); imcHelpButton.setOnClickListener(showHelp); ffmiHelpButton.setOnClickListener(showHelp); rfmHelpButton.setOnClickListener(showHelp); @@ -358,11 +423,11 @@ private void DrawGraph() { if (valueList.size() > 0) { for (int i = valueList.size() - 1; i >= 0; i--) { Entry value = new Entry( - (float) DateConverter.nbDays(valueList.get(i).getDate()), - UnitConverter.weightConverter(valueList.get(i).getBodyMeasure().getValue(), - valueList.get(i).getBodyMeasure().getUnit(), - Unit.KG - ) + (float) DateConverter.nbDays(valueList.get(i).getDate()), + UnitConverter.weightConverter(valueList.get(i).getBodyMeasure().getValue(), + valueList.get(i).getBodyMeasure().getUnit(), + Unit.KG + ) ); yVals.add(value); } @@ -608,6 +673,23 @@ private String getFfmiTextForWomen(double ffmi) { } } + /** + * Get a Value object which uses the data from the last measure if available + * + * @param lastMeasure Last measure to use data from if available + * @param defaultUnit Default unit to use in case there is no last measure + * @param id ID for the returned Value + * @param label Label for the returned Value + */ + private Value getValueFromLastMeasure(@Nullable BodyMeasure lastMeasure, Unit defaultUnit, @Nullable String id, int label) { + if (lastMeasure == null) { + return new Value(0, defaultUnit, id, label); + } else { + Value lastValue = lastMeasure.getBodyMeasure(); + return new Value(lastValue.getValue(), lastValue.getUnit(), id, label); + } + } + private void refreshData() { View fragmentView = getView(); if (fragmentView != null) { @@ -630,7 +712,7 @@ private void refreshData() { ffmiRank.setText(R.string.no_size_available); } else { float imcValue = calculateImc(UnitConverter.weightConverter(lastWeightValue.getBodyMeasure().getValue(), lastWeightValue.getBodyMeasure().getUnit(), Unit.KG), - UnitConverter.sizeConverter(lastSizeValue.getBodyMeasure().getValue(), lastSizeValue.getBodyMeasure().getUnit(), Unit.CM) ); + UnitConverter.sizeConverter(lastSizeValue.getBodyMeasure().getValue(), lastSizeValue.getBodyMeasure().getUnit(), Unit.CM)); imcText.setText(String.format("%.1f", imcValue)); imcRank.setText(getImcText(imcValue)); if (lastFatValue != null) { diff --git a/app/src/main/java/com/easyfitness/bodymeasures/BodyPartDetailsFragment.java b/app/src/main/java/com/easyfitness/bodymeasures/BodyPartDetailsFragment.java index 5e2677dd..6849e80f 100644 --- a/app/src/main/java/com/easyfitness/bodymeasures/BodyPartDetailsFragment.java +++ b/app/src/main/java/com/easyfitness/bodymeasures/BodyPartDetailsFragment.java @@ -444,6 +444,6 @@ private Unit getValidUnit(BodyMeasure lastBodyMeasure) { private Value getValueWithValidUnit(BodyMeasure lastBodyMeasure) { Unit unitDef = getValidUnit(lastBodyMeasure); Value oldValue = lastBodyMeasure.getBodyMeasure(); - return new Value(oldValue.getValue(), unitDef, oldValue.getId(), oldValue.getLabel(), oldValue.getOriginalUnit()); + return new Value(oldValue.getValue(), unitDef, oldValue.getId(), oldValue.getLabel()); } } diff --git a/app/src/main/java/com/easyfitness/enums/Unit.java b/app/src/main/java/com/easyfitness/enums/Unit.java index 0be8a7b9..ecd65ead 100644 --- a/app/src/main/java/com/easyfitness/enums/Unit.java +++ b/app/src/main/java/com/easyfitness/enums/Unit.java @@ -12,7 +12,7 @@ public enum Unit { UNITLESS("", UnitType.NONE); private final String mDisplayName; - private final UnitType mUnitType; + private UnitType mUnitType; //Constructeur Unit(String displayName, UnitType unitType) { @@ -66,4 +66,7 @@ public String toString() { public UnitType getUnitType() { return mUnitType; } + public void setUnitType(UnitType unitType) { + mUnitType = unitType; + } } diff --git a/app/src/main/java/com/easyfitness/utils/Value.java b/app/src/main/java/com/easyfitness/utils/Value.java index 0d907873..1add24bb 100644 --- a/app/src/main/java/com/easyfitness/utils/Value.java +++ b/app/src/main/java/com/easyfitness/utils/Value.java @@ -6,10 +6,8 @@ import com.easyfitness.enums.Unit; public class Value { - private final float mValue; - private final Unit mUnit; - /** Original unit in case this value was converted */ - private final Unit mOriginalUnit; + private float mValue; + private Unit mUnit; /** Identifier for this value so we can later differentiate between values */ private final String mId; private final int mLabel; @@ -20,14 +18,12 @@ public class Value { * @param unit Unit of the value * @param id Custom identifier to differentiate between values * @param label String resource to label this value is described by - * @param originalUnit Original unit in case this value was converted */ - public Value(float value, Unit unit, @Nullable String id, int label, @Nullable Unit originalUnit){ + public Value(float value, Unit unit, @Nullable String id, int label){ this.mValue = value; this.mUnit = unit; this.mId = id; this.mLabel = label; - this.mOriginalUnit = originalUnit; } /** * Create a value @@ -36,7 +32,7 @@ public Value(float value, Unit unit, @Nullable String id, int label, @Nullable U * @param id Custom identifier to differentiate between values */ public Value(float value, Unit unit, @Nullable String id){ - this(value, unit, id, ResourcesCompat.ID_NULL, null); + this(value, unit, id, ResourcesCompat.ID_NULL); } /** * Create a value @@ -45,7 +41,7 @@ public Value(float value, Unit unit, @Nullable String id){ * @param label String resource to label this value is described by */ public Value(float value, Unit unit, int label){ - this(value, unit, null, label, null); + this(value, unit, null, label); } /** * Create a value @@ -53,17 +49,20 @@ public Value(float value, Unit unit, int label){ * @param unit Unit of the value */ public Value(float value, Unit unit){ - this(value, unit, null, ResourcesCompat.ID_NULL, null); + this(value, unit, null, ResourcesCompat.ID_NULL); } - public float getValue(){ + public float getValue() { return this.mValue; } + public void setValue(float value) { + this.mValue = value; + } public Unit getUnit(){ return this.mUnit; } - @Nullable public Unit getOriginalUnit(){ - return this.mOriginalUnit; + public void setUnit(Unit unit){ + this.mUnit = unit; } @Nullable public String getId(){ return this.mId; diff --git a/app/src/main/res/drawable/ic_add.xml b/app/src/main/res/drawable/ic_add.xml new file mode 100644 index 00000000..d7c3b01f --- /dev/null +++ b/app/src/main/res/drawable/ic_add.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/tab_weight.xml b/app/src/main/res/layout/tab_weight.xml index b1b88186..c5dfc5d8 100644 --- a/app/src/main/res/layout/tab_weight.xml +++ b/app/src/main/res/layout/tab_weight.xml @@ -1,443 +1,469 @@ - - + android:layout_height="match_parent"> - - + + + android:layout_gravity="start" + android:gravity="center" + android:orientation="horizontal"> - + android:layout_margin="5dp" + android:layout_weight="1" + android:minWidth="70dp" + android:minHeight="70dp" + app:cardCornerRadius="5dp"> - + - + - + -