Search in sources :

Example 1 with ScaleMeasurement

use of com.health.openscale.core.datatypes.ScaleMeasurement in project openScale by oliexdev.

the class GraphFragment method generateLineData.

private void generateLineData(int field, List<ScaleMeasurement> scaleMeasurementList) {
    SimpleDateFormat day_date = new SimpleDateFormat("D", Locale.getDefault());
    if (field == Calendar.DAY_OF_MONTH) {
        day_date = new SimpleDateFormat("dd", Locale.getDefault());
        if (prefs.getBoolean("showWeek", false)) {
            field = Calendar.WEEK_OF_MONTH;
            day_date = new SimpleDateFormat("w", Locale.getDefault());
        }
    } else if (field == Calendar.DAY_OF_YEAR) {
        day_date = new SimpleDateFormat("D", Locale.getDefault());
        if (prefs.getBoolean("averageData", true)) {
            field = Calendar.MONTH;
            day_date = new SimpleDateFormat("MMM", Locale.getDefault());
        }
        if (prefs.getBoolean("showWeek", false)) {
            field = Calendar.WEEK_OF_YEAR;
            day_date = new SimpleDateFormat("w", Locale.getDefault());
        }
    }
    Calendar calDays = (Calendar) calLastSelected.clone();
    calDays.setMinimalDaysInFirstWeek(7);
    calDays.set(field, calDays.getMinimum(field));
    int maxDays = calDays.getMaximum(field);
    List<AxisValue> axisValues = new ArrayList<>();
    for (int i = 0; i < calDays.getMaximum(field) + 1; i++) {
        String day_name = day_date.format(calDays.getTime());
        AxisValue xAxisValue = new AxisValue(i + calDays.getActualMinimum(field));
        xAxisValue.setLabel(day_name);
        axisValues.add(xAxisValue);
        calDays.add(field, 1);
    }
    List<Line> diagramLineList = new ArrayList<Line>();
    Calendar calDB = Calendar.getInstance();
    pointIndexScaleMeasurementList = new ArrayList<>();
    floatingActionBar.removeAllViews();
    PolynomialFitter polyFitter = new PolynomialFitter(Integer.parseInt(prefs.getString("regressionLineOrder", "1")));
    for (MeasurementView view : measurementViews) {
        if (view instanceof FloatMeasurementView) {
            FloatMeasurementView measurementView = (FloatMeasurementView) view;
            if (measurementView instanceof BMRMeasurementView) {
                continue;
            }
            Stack<PointValue> valuesStack = new Stack<>();
            ArrayList<Float>[] avgBins = new ArrayList[maxDays + 1];
            ScaleMeasurement[] indexScaleMeasurement = new ScaleMeasurement[maxDays + 1];
            for (ScaleMeasurement measurement : scaleMeasurementList) {
                measurementView.loadFrom(measurement, null);
                calDB.setTime(measurement.getDateTime());
                calDB.setMinimalDaysInFirstWeek(7);
                if (avgBins[calDB.get(field)] == null) {
                    avgBins[calDB.get(field)] = new ArrayList<>();
                }
                if (measurementView.getValue() != 0.0f) {
                    avgBins[calDB.get(field)].add(measurementView.getValue());
                    indexScaleMeasurement[calDB.get(field)] = measurement;
                }
            }
            for (int i = 0; i < maxDays + 1; i++) {
                ArrayList avgBin = avgBins[i];
                if (avgBin == null) {
                    continue;
                }
                float sum = 0.0f;
                for (int j = 0; j < avgBin.size(); j++) {
                    sum += (float) avgBin.get(j);
                }
                PointValue avgValue = new PointValue(i, sum / avgBin.size());
                if (prefs.getBoolean("regressionLine", false) && measurementView instanceof WeightMeasurementView) {
                    polyFitter.addPoint((double) avgValue.getX(), (double) avgValue.getY());
                }
                if (avgBin.size() > 1) {
                    avgValue.setLabel(String.format("Ø %.2f", avgValue.getY()));
                }
                valuesStack.push(avgValue);
                pointIndexScaleMeasurementList.add(indexScaleMeasurement[i]);
            }
            Line diagramLine = new Line(valuesStack).setColor(measurementView.getColor()).setHasLabels(prefs.getBoolean("labelsEnable", true)).setHasPoints(prefs.getBoolean("pointsEnable", true)).setFormatter(new SimpleLineChartValueFormatter(1));
            if (measurementView.isVisible()) {
                addFloatingActionButton(measurementView);
                if (measurementView.getSettings().isInGraph()) {
                    diagramLineList.add(diagramLine);
                }
            }
        }
    }
    LineChartData lineData = new LineChartData(diagramLineList);
    lineData.setAxisXBottom(new Axis(axisValues).setHasLines(true).setTextColor(textColor));
    lineData.setAxisYLeft(new Axis().setHasLines(true).setMaxLabelChars(5).setTextColor(textColor));
    chartBottom.setLineChartData(lineData);
    defaultTopViewport = new Viewport(calDays.getActualMinimum(field), chartBottom.getCurrentViewport().top, calDays.getMaximum(field) + 1, chartBottom.getCurrentViewport().bottom);
    if (prefs.getBoolean("goalLine", true)) {
        Stack<PointValue> valuesGoalLine = new Stack<PointValue>();
        float goalWeight = openScale.getSelectedScaleUser().getGoalWeight();
        valuesGoalLine.push(new PointValue(0, goalWeight));
        valuesGoalLine.push(new PointValue(maxDays, goalWeight));
        Line goalLine = new Line(valuesGoalLine).setHasPoints(false);
        goalLine.setPathEffect(new DashPathEffect(new float[] { 10, 30 }, 0));
        diagramLineList.add(goalLine);
    }
    if (prefs.getBoolean("regressionLine", false)) {
        PolynomialFitter.Polynomial polynom = polyFitter.getBestFit();
        Stack<PointValue> valuesLinearRegression = new Stack<>();
        for (int i = 0; i < maxDays; i++) {
            double y_value = polynom.getY(i);
            valuesLinearRegression.push(new PointValue((float) i, (float) y_value));
        }
        Line linearRegressionLine = new Line(valuesLinearRegression).setColor(ChartUtils.COLOR_VIOLET).setHasPoints(false).setCubic(true);
        linearRegressionLine.setPathEffect(new DashPathEffect(new float[] { 10, 30 }, 0));
        diagramLineList.add(linearRegressionLine);
    }
    chartBottom.setLineChartData(lineData);
    chartBottom.setCurrentViewport(defaultTopViewport);
}
Also used : FloatMeasurementView(com.health.openscale.gui.views.FloatMeasurementView) MeasurementView(com.health.openscale.gui.views.MeasurementView) BMRMeasurementView(com.health.openscale.gui.views.BMRMeasurementView) WeightMeasurementView(com.health.openscale.gui.views.WeightMeasurementView) ArrayList(java.util.ArrayList) BMRMeasurementView(com.health.openscale.gui.views.BMRMeasurementView) ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) LineChartData(lecho.lib.hellocharts.model.LineChartData) Axis(lecho.lib.hellocharts.model.Axis) FloatMeasurementView(com.health.openscale.gui.views.FloatMeasurementView) PointValue(lecho.lib.hellocharts.model.PointValue) Calendar(java.util.Calendar) Viewport(lecho.lib.hellocharts.model.Viewport) DashPathEffect(android.graphics.DashPathEffect) WeightMeasurementView(com.health.openscale.gui.views.WeightMeasurementView) Stack(java.util.Stack) SimpleLineChartValueFormatter(lecho.lib.hellocharts.formatter.SimpleLineChartValueFormatter) Line(lecho.lib.hellocharts.model.Line) AxisValue(lecho.lib.hellocharts.model.AxisValue) PolynomialFitter(com.health.openscale.core.utils.PolynomialFitter) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with ScaleMeasurement

use of com.health.openscale.core.datatypes.ScaleMeasurement in project openScale by oliexdev.

the class StatisticsFragment method updateOnView.

@Override
public void updateOnView(List<ScaleMeasurement> scaleMeasurementList) {
    currentScaleUser = OpenScale.getInstance(getContext()).getSelectedScaleUser();
    if (scaleMeasurementList.isEmpty()) {
        lastScaleMeasurement = new ScaleMeasurement();
        lastScaleMeasurement.setUserId(currentScaleUser.getId());
    } else {
        lastScaleMeasurement = scaleMeasurementList.get(0);
    }
    updateStatistics(scaleMeasurementList);
    updateGoal();
}
Also used : ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement)

Example 3 with ScaleMeasurement

use of com.health.openscale.core.datatypes.ScaleMeasurement in project openScale by oliexdev.

the class StatisticsFragment method updateStatistics.

private void updateStatistics(List<ScaleMeasurement> scaleMeasurementList) {
    Calendar histDate = Calendar.getInstance();
    Calendar weekPastDate = Calendar.getInstance();
    Calendar monthPastDate = Calendar.getInstance();
    weekPastDate.setTime(lastScaleMeasurement.getDateTime());
    weekPastDate.add(Calendar.DATE, -7);
    monthPastDate.setTime(lastScaleMeasurement.getDateTime());
    monthPastDate.add(Calendar.DATE, -30);
    int weekSize = 0;
    int monthSize = 0;
    ScaleMeasurement averageWeek = new ScaleMeasurement();
    ScaleMeasurement averageMonth = new ScaleMeasurement();
    for (ScaleMeasurement measurement : scaleMeasurementList) {
        histDate.setTime(measurement.getDateTime());
        if (weekPastDate.before(histDate)) {
            averageWeek.add(measurement);
            weekSize++;
        }
        if (monthPastDate.before(histDate)) {
            averageMonth.add(measurement);
            monthSize++;
        }
    }
    if (weekSize > 0) {
        averageWeek.divide(weekSize);
    }
    if (monthSize > 0) {
        averageMonth.divide(monthSize);
    }
    for (MeasurementView measurement : viewMeasurementsListWeek) {
        measurement.loadFrom(averageWeek, null);
    }
    for (MeasurementView measurement : viewMeasurementsListMonth) {
        measurement.loadFrom(averageMonth, null);
    }
}
Also used : LBWMeasurementView(com.health.openscale.gui.views.LBWMeasurementView) FatMeasurementView(com.health.openscale.gui.views.FatMeasurementView) HipMeasurementView(com.health.openscale.gui.views.HipMeasurementView) BoneMeasurementView(com.health.openscale.gui.views.BoneMeasurementView) MuscleMeasurementView(com.health.openscale.gui.views.MuscleMeasurementView) WaistMeasurementView(com.health.openscale.gui.views.WaistMeasurementView) WaterMeasurementView(com.health.openscale.gui.views.WaterMeasurementView) WeightMeasurementView(com.health.openscale.gui.views.WeightMeasurementView) MeasurementView(com.health.openscale.gui.views.MeasurementView) ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) Calendar(java.util.Calendar)

Example 4 with ScaleMeasurement

use of com.health.openscale.core.datatypes.ScaleMeasurement in project openScale by oliexdev.

the class DatabaseTest method measurementOperations.

@Test
public void measurementOperations() throws Exception {
    final ScaleUser scaleUser1 = new ScaleUser();
    final ScaleUser scaleUser2 = new ScaleUser();
    scaleUser1.setId((int) userDao.insert(scaleUser1));
    scaleUser2.setId((int) userDao.insert(scaleUser2));
    // User 1 data initialization
    final int user1 = scaleUser1.getId();
    ScaleMeasurement measurement11 = new ScaleMeasurement();
    ScaleMeasurement measurement12 = new ScaleMeasurement();
    ScaleMeasurement measurement13 = new ScaleMeasurement();
    measurement11.setUserId(user1);
    measurement12.setUserId(user1);
    measurement13.setUserId(user1);
    measurement11.setWeight(10.0f);
    measurement12.setWeight(20.0f);
    measurement13.setWeight(30.0f);
    measurement11.setDateTime(new Date(100));
    measurement12.setDateTime(new Date(200));
    measurement13.setDateTime(new Date(300));
    // User 2 data initialization
    final int user2 = scaleUser2.getId();
    ScaleMeasurement measurement21 = new ScaleMeasurement();
    ScaleMeasurement measurement22 = new ScaleMeasurement();
    measurement21.setUserId(user2);
    measurement22.setUserId(user2);
    measurement21.setWeight(15.0f);
    measurement22.setWeight(25.0f);
    measurement21.setDateTime(new Date(150));
    measurement22.setDateTime(new Date(250));
    // check if database is empty
    assertTrue(measurementDAO.getAll(user1).isEmpty());
    assertTrue(measurementDAO.getAll(user2).isEmpty());
    // insert measurement as list and single insertion
    List<ScaleMeasurement> scaleMeasurementList = new ArrayList<>();
    scaleMeasurementList.add(measurement11);
    scaleMeasurementList.add(measurement13);
    scaleMeasurementList.add(measurement12);
    measurementDAO.insertAll(scaleMeasurementList);
    assertEquals(3, measurementDAO.getAll(user1).size());
    measurementDAO.insert(measurement22);
    measurementDAO.insert(measurement21);
    assertEquals(2, measurementDAO.getAll(user2).size());
    // check if sorted DESC by date correctly
    assertEquals(30.0f, measurementDAO.getAll(user1).get(0).getWeight(), DELTA);
    assertEquals(25.0f, measurementDAO.getAll(user2).get(0).getWeight(), DELTA);
    // don't allow insertion with the same date
    long id = measurementDAO.insert(measurement11);
    assertEquals(-1, id);
    assertEquals(3, measurementDAO.getAll(user1).size());
    // test get(datetime) method
    assertEquals(20.0f, measurementDAO.get(new Date(200), user1).getWeight(), DELTA);
    // test get(id) method
    scaleMeasurementList = measurementDAO.getAll(user1);
    assertEquals(scaleMeasurementList.get(2).getWeight(), measurementDAO.get(scaleMeasurementList.get(2).getId()).getWeight(), DELTA);
    // test getPrevious(id) method
    assertNull(measurementDAO.getPrevious(scaleMeasurementList.get(2).getId(), user1));
    assertEquals(scaleMeasurementList.get(2).getWeight(), measurementDAO.getPrevious(scaleMeasurementList.get(1).getId(), user1).getWeight(), DELTA);
    assertEquals(scaleMeasurementList.get(1).getWeight(), measurementDAO.getPrevious(scaleMeasurementList.get(0).getId(), user1).getWeight(), DELTA);
    // test getNext(id) method
    assertNull(measurementDAO.getNext(scaleMeasurementList.get(0).getId(), user1));
    assertEquals(scaleMeasurementList.get(0).getWeight(), measurementDAO.getNext(scaleMeasurementList.get(1).getId(), user1).getWeight(), DELTA);
    assertEquals(scaleMeasurementList.get(1).getWeight(), measurementDAO.getNext(scaleMeasurementList.get(2).getId(), user1).getWeight(), DELTA);
    // test getAllInRange method
    assertEquals(1, measurementDAO.getAllInRange(new Date(0), new Date(200), user1).size());
    assertEquals(0, measurementDAO.getAllInRange(new Date(0), new Date(50), user1).size());
    assertEquals(2, measurementDAO.getAllInRange(new Date(100), new Date(201), user1).size());
    assertEquals(1, measurementDAO.getAllInRange(new Date(0), new Date(200), user1).size());
    assertEquals(3, measurementDAO.getAllInRange(new Date(0), new Date(1000), user1).size());
    assertEquals(2, measurementDAO.getAllInRange(new Date(150), new Date(400), user1).size());
    assertEquals(0, measurementDAO.getAllInRange(new Date(10), new Date(20), user2).size());
    assertEquals(1, measurementDAO.getAllInRange(new Date(70), new Date(200), user2).size());
    assertEquals(2, measurementDAO.getAllInRange(new Date(0), new Date(1000), user2).size());
    // test update method
    assertEquals(30.0f, measurementDAO.get(scaleMeasurementList.get(0).getId()).getWeight(), DELTA);
    scaleMeasurementList.get(0).setWeight(42.0f);
    measurementDAO.update(scaleMeasurementList.get(0));
    assertEquals(42.0f, measurementDAO.get(scaleMeasurementList.get(0).getId()).getWeight(), DELTA);
    // test delete method
    assertEquals(3, measurementDAO.getAll(user1).size());
    measurementDAO.delete(scaleMeasurementList.get(0).getId());
    assertEquals(2, measurementDAO.getAll(user1).size());
    // test delete all method
    assertEquals(2, measurementDAO.getAll(user1).size());
    assertEquals(2, measurementDAO.getAll(user2).size());
    measurementDAO.deleteAll(user1);
    measurementDAO.deleteAll(user2);
    assertEquals(0, measurementDAO.getAll(user1).size());
    assertEquals(0, measurementDAO.getAll(user2).size());
    assertTrue(measurementDAO.getAll(user1).isEmpty());
    assertTrue(measurementDAO.getAll(user2).isEmpty());
}
Also used : ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) ScaleUser(com.health.openscale.core.datatypes.ScaleUser) ArrayList(java.util.ArrayList) Date(java.util.Date) Test(org.junit.Test)

Example 5 with ScaleMeasurement

use of com.health.openscale.core.datatypes.ScaleMeasurement in project openScale by oliexdev.

the class OpenScale method migrateSQLtoRoom.

private void migrateSQLtoRoom() {
    if (!context.getDatabasePath(ScaleUserDatabase.DATABASE_NAME).exists() || !context.getDatabasePath(ScaleDatabase.DATABASE_NAME).exists()) {
        return;
    }
    ScaleDatabase scaleDB = new ScaleDatabase(context);
    ScaleUserDatabase scaleUserDB = new ScaleUserDatabase(context);
    List<ScaleUser> oldScaleUserList = scaleUserDB.getScaleUserList();
    if (scaleDB.getReadableDatabase().getVersion() == 6 && userDAO.getAll().isEmpty() && !oldScaleUserList.isEmpty()) {
        Toast.makeText(context, "Migrating old SQL database to new database format...", Toast.LENGTH_LONG).show();
        userDAO.insertAll(oldScaleUserList);
        for (ScaleUser user : oldScaleUserList) {
            List<ScaleMeasurement> oldScaleMeasurementList = scaleDB.getScaleDataList(user.getId());
            measurementDAO.insertAll(oldScaleMeasurementList);
        }
        Toast.makeText(context, "Finished migrating old SQL database to new database format", Toast.LENGTH_LONG).show();
    }
    scaleUserDB.close();
    scaleDB.close();
}
Also used : ScaleUserDatabase(com.health.openscale.core.database.ScaleUserDatabase) ScaleDatabase(com.health.openscale.core.database.ScaleDatabase) ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) ScaleUser(com.health.openscale.core.datatypes.ScaleUser)

Aggregations

ScaleMeasurement (com.health.openscale.core.datatypes.ScaleMeasurement)39 Date (java.util.Date)13 ScaleUser (com.health.openscale.core.datatypes.ScaleUser)12 BufferedReader (java.io.BufferedReader)9 ParseException (java.text.ParseException)9 StringReader (java.io.StringReader)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 Calendar (java.util.Calendar)7 MeasurementView (com.health.openscale.gui.views.MeasurementView)6 SimpleDateFormat (java.text.SimpleDateFormat)5 BMRMeasurementView (com.health.openscale.gui.views.BMRMeasurementView)4 FloatMeasurementView (com.health.openscale.gui.views.FloatMeasurementView)4 WeightMeasurementView (com.health.openscale.gui.views.WeightMeasurementView)4 IOException (java.io.IOException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Stack (java.util.Stack)2 SimpleLineChartValueFormatter (lecho.lib.hellocharts.formatter.SimpleLineChartValueFormatter)2 Axis (lecho.lib.hellocharts.model.Axis)2 AxisValue (lecho.lib.hellocharts.model.AxisValue)2