diff --git a/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java b/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java index 3d5950d1..dbe04ac8 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java +++ b/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java @@ -26,8 +26,12 @@ import android.support.v4.app.Fragment; import android.util.Log; import android.widget.Toast; +import com.health.openscale.R; import com.health.openscale.core.alarm.AlarmHandler; import com.health.openscale.core.bluetooth.BluetoothCommunication; +import com.health.openscale.core.bodymetric.EstimatedFatMetric; +import com.health.openscale.core.bodymetric.EstimatedLBWMetric; +import com.health.openscale.core.bodymetric.EstimatedWaterMetric; import com.health.openscale.core.database.AppDatabase; import com.health.openscale.core.database.ScaleDatabase; import com.health.openscale.core.database.ScaleMeasurementDAO; @@ -60,7 +64,7 @@ public class OpenScale { private ScaleUserDAO userDAO; private ScaleDatabase scaleDB; private ScaleUserDatabase scaleUserDB; - private ArrayList scaleDataList; + private List scaleDataList; private BluetoothCommunication btCom; private String btDeviceName; @@ -144,7 +148,7 @@ public class OpenScale { userDAO.update(user); } - public ArrayList getScaleDataList() { + public List getScaleDataList() { return scaleDataList; } @@ -155,35 +159,13 @@ public class OpenScale { } public int addScaleData(final ScaleData scaleData) { - - AsyncTask.execute(new Runnable() { - @Override - public void run() { - Log.d("TEST", "ADD START"); - - measurementDAO.insert(scaleData); - - Log.d("TEST", "ADD END"); - - Log.d("TEST", "READ ALL"); - - List listScaleData = measurementDAO.getAll(); - - for(ScaleData nextScaleData : listScaleData) { - Log.d("TEST", "DB : " + nextScaleData.getDateTime() + " Weight: " + nextScaleData.getWeight()); - } - } - }); - - - return -1; -/* SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); if (scaleData.getUserId() == -1) { if (prefs.getBoolean("smartUserAssign", false)) { scaleData.setUserId(getSmartUserAssignment(scaleData.getWeight(), 15.0f)); } else { - scaleData.setUserId(getSelectedScaleUser().id); + scaleData.setUserId(getSelectedScaleUser().getId()); } // don't add scale data if no user is selected @@ -210,16 +192,17 @@ public class OpenScale { scaleData.setFat(fatMetric.getFat(getScaleUser(scaleData.getUserId()), scaleData)); } - if (scaleDB.insertEntry(scaleData)) { + if (measurementDAO.get(scaleData.getDateTime(), scaleData.getUserId()) == null) { + measurementDAO.insert(scaleData); ScaleUser scaleUser = getScaleUser(scaleData.getUserId()); - String infoText = String.format(context.getString(R.string.info_new_data_added), scaleData.getConvertedWeight(scaleUser.scaleUnit), scaleUser.UNIT_STRING[scaleUser.scaleUnit], dateTimeFormat.format(scaleData.getDateTime()), scaleUser.userName); + String infoText = String.format(context.getString(R.string.info_new_data_added), scaleData.getConvertedWeight(scaleUser.getScaleUnit()), scaleUser.UNIT_STRING[scaleUser.getScaleUnit()], dateTimeFormat.format(scaleData.getDateTime()), scaleUser.getUserName()); Toast.makeText(context, infoText, Toast.LENGTH_LONG).show(); alarmHandler.entryChanged(context, scaleData); updateScaleData(); } - return scaleData.getUserId();*/ + return scaleData.getUserId(); } private int getSmartUserAssignment(float weight, float range) { @@ -259,7 +242,7 @@ public class OpenScale { } public void updateScaleData(ScaleData scaleData) { - scaleDB.updateEntry(scaleData.getId(), scaleData); + measurementDAO.update(scaleData); alarmHandler.entryChanged(context, scaleData); updateScaleData(); @@ -305,7 +288,7 @@ public class OpenScale { newScaleData.setUserId(getSelectedScaleUser().getId()); - scaleDB.insertEntry(newScaleData); + measurementDAO.insert(newScaleData); line = csvReader.readLine(); } @@ -414,7 +397,7 @@ public class OpenScale { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); int selectedUserId = prefs.getInt("selectedUserId", -1); - scaleDataList = scaleDB.getScaleDataList(selectedUserId); + scaleDataList = measurementDAO.getAll(selectedUserId); fragment.updateOnView(scaleDataList); } @@ -424,7 +407,7 @@ public class OpenScale { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); int selectedUserId = prefs.getInt("selectedUserId", -1); - scaleDataList = scaleDB.getScaleDataList(selectedUserId); + scaleDataList = measurementDAO.getAll(selectedUserId); for (FragmentUpdateListener fragment : fragmentList) { if (fragment != null) { diff --git a/android_app/app/src/main/java/com/health/openscale/core/database/ScaleMeasurementDAO.java b/android_app/app/src/main/java/com/health/openscale/core/database/ScaleMeasurementDAO.java index 0850afe8..0fde1780 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/database/ScaleMeasurementDAO.java +++ b/android_app/app/src/main/java/com/health/openscale/core/database/ScaleMeasurementDAO.java @@ -24,12 +24,16 @@ import android.arch.persistence.room.Update; import com.health.openscale.core.datatypes.ScaleData; +import java.util.Date; import java.util.List; @Dao public interface ScaleMeasurementDAO { - @Query("SELECT * FROM scaledata") - List getAll(); + @Query("SELECT * FROM scaledata WHERE datetime = :datetime AND userId = :userId") + ScaleData get(Date datetime, int userId); + + @Query("SELECT * FROM scaledata WHERE userId = :userId") + List getAll(int userId); @Query("SELECT * FROM scaledata WHERE id IS :id") ScaleData loadById(int id); diff --git a/android_app/app/src/main/java/com/health/openscale/core/database/ScaleUserDAO.java b/android_app/app/src/main/java/com/health/openscale/core/database/ScaleUserDAO.java index 393f8c19..1f2baa9a 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/database/ScaleUserDAO.java +++ b/android_app/app/src/main/java/com/health/openscale/core/database/ScaleUserDAO.java @@ -31,10 +31,10 @@ public interface ScaleUserDAO { @Query("SELECT * FROM scaleuser") List getAll(); - @Query("SELECT * FROM scaleuser WHERE id IS :id") + @Query("SELECT * FROM scaleuser WHERE id = :id") ScaleUser getById(int id); - @Query("SELECT * FROM scaleuser WHERE id IS :id") + @Query("SELECT * FROM scaleuser WHERE id = :id") ScaleUser loadById(int id); @Insert diff --git a/android_app/app/src/main/java/com/health/openscale/gui/fragments/FragmentUpdateListener.java b/android_app/app/src/main/java/com/health/openscale/gui/fragments/FragmentUpdateListener.java index a93c8fd9..dcfff5e2 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/fragments/FragmentUpdateListener.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/fragments/FragmentUpdateListener.java @@ -18,8 +18,8 @@ package com.health.openscale.gui.fragments; import com.health.openscale.core.datatypes.ScaleData; -import java.util.ArrayList; +import java.util.List; public interface FragmentUpdateListener { - public void updateOnView(ArrayList scaleDataList); + public void updateOnView(List scaleDataList); } diff --git a/android_app/app/src/main/java/com/health/openscale/gui/fragments/GraphFragment.java b/android_app/app/src/main/java/com/health/openscale/gui/fragments/GraphFragment.java index b9f9ba32..84bd1861 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/fragments/GraphFragment.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/fragments/GraphFragment.java @@ -186,7 +186,7 @@ public class GraphFragment extends Fragment implements FragmentUpdateListener { } @Override - public void updateOnView(ArrayList scaleDataList) + public void updateOnView(List scaleDataList) { generateGraphs(); } diff --git a/android_app/app/src/main/java/com/health/openscale/gui/fragments/OverviewFragment.java b/android_app/app/src/main/java/com/health/openscale/gui/fragments/OverviewFragment.java index ca76de13..354df286 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/fragments/OverviewFragment.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/fragments/OverviewFragment.java @@ -174,7 +174,7 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener } @Override - public void updateOnView(ArrayList scaleDataList) { + public void updateOnView(List scaleDataList) { if (scaleDataList.isEmpty()) { lastScaleData = new ScaleData(); } else if (userSelectedData != null) { @@ -237,7 +237,7 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener } - private void updateLastLineChart(ArrayList scaleDataList) { + private void updateLastLineChart(List scaleDataList) { List axisValues = new ArrayList(); List valuesWeight = new ArrayList(); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/fragments/StatisticsFragment.java b/android_app/app/src/main/java/com/health/openscale/gui/fragments/StatisticsFragment.java index 202c810e..8985fc40 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/fragments/StatisticsFragment.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/fragments/StatisticsFragment.java @@ -33,8 +33,8 @@ import com.health.openscale.core.datatypes.ScaleUser; import com.health.openscale.core.utils.DateTimeHelpers; import java.text.DateFormat; -import java.util.ArrayList; import java.util.Calendar; +import java.util.List; public class StatisticsFragment extends Fragment implements FragmentUpdateListener { @@ -88,7 +88,7 @@ public class StatisticsFragment extends Fragment implements FragmentUpdateListen } @Override - public void updateOnView(ArrayList scaleDataList) { + public void updateOnView(List scaleDataList) { if (scaleDataList.isEmpty()) { lastScaleData = new ScaleData(); } else { @@ -105,7 +105,7 @@ public class StatisticsFragment extends Fragment implements FragmentUpdateListen updateGoal(scaleDataList); } - private void updateGoal(ArrayList scaleDataList) { + private void updateGoal(List scaleDataList) { ScaleData goalScaleData = new ScaleData(); goalScaleData.setConvertedWeight(currentScaleUser.getGoalWeight(), currentScaleUser.getScaleUnit()); @@ -157,7 +157,7 @@ public class StatisticsFragment extends Fragment implements FragmentUpdateListen ); // currentScaleUser.goalDate } - private void updateStatistics(ArrayList scaleDataList) { + private void updateStatistics(List scaleDataList) { Calendar histDate = Calendar.getInstance(); Calendar weekPastDate = Calendar.getInstance(); Calendar monthPastDate = Calendar.getInstance(); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/fragments/TableFragment.java b/android_app/app/src/main/java/com/health/openscale/gui/fragments/TableFragment.java index 29236924..e1d2905f 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/fragments/TableFragment.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/fragments/TableFragment.java @@ -68,6 +68,7 @@ import com.health.openscale.gui.views.WeightMeasurementView; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import lecho.lib.hellocharts.util.ChartUtils; @@ -129,7 +130,7 @@ public class TableFragment extends Fragment implements FragmentUpdateListener { } @Override - public void updateOnView(ArrayList scaleDataList) + public void updateOnView(List scaleDataList) { tableDataView.setAdapter(new ListViewAdapter(new ArrayList>())); // delete all data in the table with an empty adapter array list