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);
}
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();
}
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);
}
}
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());
}
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();
}
Aggregations