Search in sources :

Example 21 with ScaleMeasurement

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

the class CsvHelperTest method oldVersion12SingleEntry.

@Test
public void oldVersion12SingleEntry() throws Exception {
    final String data = "01.03.2018 12:45,8.0,2.0,7.0,5.0,some text\n";
    List<ScaleMeasurement> list = CsvHelper.importFrom(new BufferedReader(new StringReader(data)));
    assertEquals(1, list.size());
    validateEntry(list.get(0), 1);
}
Also used : ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 22 with ScaleMeasurement

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

the class CsvHelperTest method exportImport.

@Test
public void exportImport() throws Exception {
    ScaleMeasurement m = new ScaleMeasurement();
    m.setWeight(8.0f);
    m.setFat(2.0f);
    m.setWater(7.0f);
    m.setMuscle(5.0f);
    m.setLbw(4.0f);
    m.setBone(1.0f);
    m.setWaist(6.0f);
    m.setHip(3.0f);
    m.setComment("some text");
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(2018, 2, 1, 12, 45);
    m.setDateTime(cal.getTime());
    List<ScaleMeasurement> measurements = new ArrayList<>();
    for (int i = 0; i < 10; ++i) {
        measurements.add(m);
    }
    StringWriter writer = new StringWriter();
    CsvHelper.exportTo(writer, measurements);
    List<ScaleMeasurement> imported = CsvHelper.importFrom(new BufferedReader(new StringReader(writer.toString())));
    assertEquals(measurements.size(), imported.size());
    for (ScaleMeasurement newM : imported) {
        validateEntry(newM, 5);
    }
}
Also used : StringWriter(java.io.StringWriter) ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 23 with ScaleMeasurement

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

the class GraphFragment method generateGraphs.

private void generateGraphs() {
    final int selectedYear = calYears.get(Calendar.YEAR);
    int firstYear = selectedYear;
    int lastYear = selectedYear;
    List<ScaleMeasurement> scaleMeasurementList = openScale.getScaleMeasurementList();
    if (!scaleMeasurementList.isEmpty()) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(scaleMeasurementList.get(scaleMeasurementList.size() - 1).getDateTime());
        firstYear = cal.get(Calendar.YEAR);
        cal.setTime(scaleMeasurementList.get(0).getDateTime());
        lastYear = cal.get(Calendar.YEAR);
    }
    btnLeftYear.setEnabled(selectedYear > firstYear);
    btnRightYear.setEnabled(selectedYear < lastYear);
    if (selectedYear == firstYear && selectedYear == lastYear) {
        btnLeftYear.setVisibility(View.GONE);
        btnRightYear.setVisibility(View.GONE);
    } else {
        btnLeftYear.setVisibility(View.VISIBLE);
        btnRightYear.setVisibility(View.VISIBLE);
    }
    // show monthly diagram
    if (prefs.getBoolean("showMonth", true)) {
        chartTop.setVisibility(View.VISIBLE);
        chartBottom.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 0.7f));
        generateColumnData();
        scaleMeasurementList = openScale.getScaleDataOfMonth(selectedYear, calLastSelected.get(Calendar.MONTH));
        generateLineData(Calendar.DAY_OF_MONTH, scaleMeasurementList);
    // show only yearly diagram and hide monthly diagram
    } else {
        chartTop.setVisibility(View.GONE);
        chartBottom.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 0.9f));
        scaleMeasurementList = openScale.getScaleDataOfYear(selectedYear);
        generateLineData(Calendar.DAY_OF_YEAR, scaleMeasurementList);
    }
}
Also used : ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) Calendar(java.util.Calendar) LinearLayout(android.widget.LinearLayout)

Example 24 with ScaleMeasurement

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

the class GraphFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    openScale = OpenScale.getInstance(getContext());
    if (savedInstanceState == null) {
        List<ScaleMeasurement> scaleMeasurementList = openScale.getScaleMeasurementList();
        if (!scaleMeasurementList.isEmpty()) {
            calYears.setTime(scaleMeasurementList.get(0).getDateTime());
            calLastSelected.setTime(scaleMeasurementList.get(0).getDateTime());
        }
    } else {
        calYears.setTimeInMillis(savedInstanceState.getLong(CAL_YEARS_KEY));
        calLastSelected.setTimeInMillis(savedInstanceState.getLong(CAL_LAST_SELECTED_KEY));
    }
    graphView = inflater.inflate(R.layout.fragment_graph, container, false);
    chartBottom = (LineChartView) graphView.findViewById(R.id.chart_bottom);
    chartTop = (ColumnChartView) graphView.findViewById(R.id.chart_top);
    chartBottom.setOnTouchListener(new chartBottomListener());
    chartBottom.setOnValueTouchListener(new chartBottomValueTouchListener());
    chartTop.setOnValueTouchListener(new chartTopValueTouchListener());
    // HACK: get default text color from hidden text view to set the correct axis colors
    textColor = ((TextView) graphView.findViewById(R.id.colorHack)).getCurrentTextColor();
    txtYear = (TextView) graphView.findViewById(R.id.txtYear);
    txtYear.setText(Integer.toString(calYears.get(Calendar.YEAR)));
    floatingActionBar = (LinearLayout) graphView.findViewById(R.id.floatingActionBar);
    optionMenu = (ImageView) graphView.findViewById(R.id.optionMenu);
    optionMenu.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            popup.show();
        }
    });
    btnLeftYear = graphView.findViewById(R.id.btnLeftYear);
    btnLeftYear.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            calYears.roll(Calendar.YEAR, false);
            txtYear.setText(Integer.toString(calYears.get(Calendar.YEAR)));
            List<ScaleMeasurement> scaleMeasurementList = OpenScale.getInstance(getContext()).getScaleDataOfYear(calYears.get(Calendar.YEAR));
            if (!scaleMeasurementList.isEmpty()) {
                calLastSelected.setTime(scaleMeasurementList.get(0).getDateTime());
            }
            generateGraphs();
        }
    });
    btnRightYear = graphView.findViewById(R.id.btnRightYear);
    btnRightYear.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            calYears.roll(Calendar.YEAR, true);
            txtYear.setText(Integer.toString(calYears.get(Calendar.YEAR)));
            List<ScaleMeasurement> scaleMeasurementList = OpenScale.getInstance(getContext()).getScaleDataOfYear(calYears.get(Calendar.YEAR));
            if (!scaleMeasurementList.isEmpty()) {
                calLastSelected.setTime(scaleMeasurementList.get(scaleMeasurementList.size() - 1).getDateTime());
            }
            generateGraphs();
        }
    });
    measurementViews = MeasurementView.getMeasurementList(getContext(), MeasurementView.DateTimeOrder.NONE);
    popup = new PopupMenu(getContext(), optionMenu);
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch(item.getItemId()) {
                case R.id.enableMonth:
                    if (item.isChecked()) {
                        item.setChecked(false);
                        prefs.edit().putBoolean("showMonth", false).commit();
                    } else {
                        item.setChecked(true);
                        prefs.edit().putBoolean("showMonth", true).commit();
                    }
                    generateGraphs();
                    return true;
                case R.id.enableWeek:
                    if (item.isChecked()) {
                        item.setChecked(false);
                        prefs.edit().putBoolean("showWeek", false).commit();
                    } else {
                        item.setChecked(true);
                        prefs.edit().putBoolean("showWeek", true).commit();
                    }
                    generateGraphs();
                    return true;
                default:
                    return false;
            }
        }
    });
    popup.getMenuInflater().inflate(R.menu.graph_menu, popup.getMenu());
    prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
    MenuItem enableMonth = popup.getMenu().findItem(R.id.enableMonth);
    enableMonth.setChecked(prefs.getBoolean("showMonth", true));
    MenuItem enableWeek = popup.getMenu().findItem(R.id.enableWeek);
    enableWeek.setChecked(prefs.getBoolean("showWeek", false));
    openScale.registerFragment(this);
    return graphView;
}
Also used : ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) ColorStateList(android.content.res.ColorStateList) List(java.util.List) ArrayList(java.util.ArrayList) MenuItem(android.view.MenuItem) ImageView(android.widget.ImageView) View(android.view.View) FloatMeasurementView(com.health.openscale.gui.views.FloatMeasurementView) TextView(android.widget.TextView) MeasurementView(com.health.openscale.gui.views.MeasurementView) BMRMeasurementView(com.health.openscale.gui.views.BMRMeasurementView) LineChartView(lecho.lib.hellocharts.view.LineChartView) WeightMeasurementView(com.health.openscale.gui.views.WeightMeasurementView) ColumnChartView(lecho.lib.hellocharts.view.ColumnChartView) PopupMenu(android.widget.PopupMenu)

Example 25 with ScaleMeasurement

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

the class OverviewFragment method updateLastLineChart.

private void updateLastLineChart(List<ScaleMeasurement> scaleMeasurementList) {
    final Calendar now = Calendar.getInstance();
    Calendar histCalendar = Calendar.getInstance();
    scaleMeasurementLastDays = new ArrayList<>();
    List<AxisValue> axisValues = new ArrayList<>();
    int max_i = Math.min(7, scaleMeasurementList.size());
    for (int i = 0; i < max_i; ++i) {
        ScaleMeasurement measurement = scaleMeasurementList.get(max_i - i - 1);
        scaleMeasurementLastDays.add(measurement);
        histCalendar.setTime(measurement.getDateTime());
        int days = DateTimeHelpers.daysBetween(now, histCalendar);
        String label = getResources().getQuantityString(R.plurals.label_days, Math.abs(days), days);
        axisValues.add(new AxisValue(i, label.toCharArray()));
    }
    List<Line> diagramLineList = new ArrayList<>();
    for (MeasurementView view : measurementViews) {
        if (!view.isVisible() || !view.getSettings().isInOverviewGraph() || !(view instanceof FloatMeasurementView)) {
            continue;
        }
        FloatMeasurementView measurementView = (FloatMeasurementView) view;
        Stack<PointValue> valuesStack = new Stack<PointValue>();
        for (int i = 0; i < max_i; ++i) {
            ScaleMeasurement measurement = scaleMeasurementList.get(max_i - i - 1);
            measurementView.loadFrom(measurement, null);
            if (measurementView.getValue() != 0.0f) {
                valuesStack.push(new PointValue(i, measurementView.getValue()));
            }
        }
        diagramLineList.add(new Line(valuesStack).setColor(measurementView.getColor()).setHasLabels(prefs.getBoolean("labelsEnable", true)).setHasPoints(prefs.getBoolean("pointsEnable", true)).setFormatter(new SimpleLineChartValueFormatter(1)));
    }
    LineChartData lineData = new LineChartData(diagramLineList);
    lineData.setAxisXBottom(new Axis(axisValues).setHasLines(true).setTextColor(txtTitleLastMeasurement.getCurrentTextColor()));
    lineData.setAxisYLeft(new Axis().setHasLines(true).setMaxLabelChars(5).setTextColor(txtTitleLastMeasurement.getCurrentTextColor()));
    lineChartLast.setLineChartData(lineData);
    lineChartLast.setViewportCalculationEnabled(true);
    lineChartLast.setZoomEnabled(false);
}
Also used : BMRMeasurementView(com.health.openscale.gui.views.BMRMeasurementView) FloatMeasurementView(com.health.openscale.gui.views.FloatMeasurementView) MeasurementView(com.health.openscale.gui.views.MeasurementView) PointValue(lecho.lib.hellocharts.model.PointValue) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Stack(java.util.Stack) SimpleLineChartValueFormatter(lecho.lib.hellocharts.formatter.SimpleLineChartValueFormatter) Line(lecho.lib.hellocharts.model.Line) AxisValue(lecho.lib.hellocharts.model.AxisValue) 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)

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