Search in sources :

Example 1 with MeasurementViewSettings

use of com.health.openscale.gui.measurement.MeasurementViewSettings in project openScale by oliexdev.

the class OpenScale method addScaleMeasurement.

public int addScaleMeasurement(final ScaleMeasurement scaleMeasurement, boolean silent) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    // Check user id and do a smart user assign if option is enabled
    if (scaleMeasurement.getUserId() == -1) {
        if (prefs.getBoolean("smartUserAssign", false)) {
            scaleMeasurement.setUserId(getSmartUserAssignment(scaleMeasurement.getWeight(), 15.0f));
        } else {
            scaleMeasurement.setUserId(getSelectedScaleUser().getId());
        }
        // don't add scale data if no user is selected
        if (scaleMeasurement.getUserId() == -1) {
            Timber.e("to be added measurement are thrown away because no user is selected");
            return -1;
        }
    }
    // Assisted weighing
    if (getScaleUser(scaleMeasurement.getUserId()).isAssistedWeighing()) {
        int assistedWeighingRefUserId = prefs.getInt("assistedWeighingRefUserId", -1);
        if (assistedWeighingRefUserId != -1) {
            ScaleMeasurement lastRefScaleMeasurement = getLastScaleMeasurement(assistedWeighingRefUserId);
            if (lastRefScaleMeasurement != null) {
                float refWeight = lastRefScaleMeasurement.getWeight();
                float diffToRef = scaleMeasurement.getWeight() - refWeight;
                scaleMeasurement.setWeight(diffToRef);
            }
        } else {
            Timber.e("assisted weighing reference user id is -1");
        }
    }
    // Calculate the amputation correction factor for the weight, if available
    scaleMeasurement.setWeight((scaleMeasurement.getWeight() * 100.0f) / getScaleUser(scaleMeasurement.getUserId()).getAmputationCorrectionFactor());
    // If option is enabled then calculate body measurements from generic formulas
    MeasurementViewSettings settings = new MeasurementViewSettings(prefs, WaterMeasurementView.KEY);
    if (settings.isEnabled() && settings.isEstimationEnabled()) {
        EstimatedWaterMetric waterMetric = EstimatedWaterMetric.getEstimatedMetric(EstimatedWaterMetric.FORMULA.valueOf(settings.getEstimationFormula()));
        scaleMeasurement.setWater(waterMetric.getWater(getScaleUser(scaleMeasurement.getUserId()), scaleMeasurement));
    }
    settings = new MeasurementViewSettings(prefs, FatMeasurementView.KEY);
    if (settings.isEnabled() && settings.isEstimationEnabled()) {
        EstimatedFatMetric fatMetric = EstimatedFatMetric.getEstimatedMetric(EstimatedFatMetric.FORMULA.valueOf(settings.getEstimationFormula()));
        scaleMeasurement.setFat(fatMetric.getFat(getScaleUser(scaleMeasurement.getUserId()), scaleMeasurement));
    }
    // Must be after fat estimation as one formula is based on fat
    settings = new MeasurementViewSettings(prefs, LBMMeasurementView.KEY);
    if (settings.isEnabled() && settings.isEstimationEnabled()) {
        EstimatedLBMMetric lbmMetric = EstimatedLBMMetric.getEstimatedMetric(EstimatedLBMMetric.FORMULA.valueOf(settings.getEstimationFormula()));
        scaleMeasurement.setLbm(lbmMetric.getLBM(getScaleUser(scaleMeasurement.getUserId()), scaleMeasurement));
    }
    // Insert measurement into the database, check return if it was successful inserted
    if (measurementDAO.insert(scaleMeasurement) != -1) {
        Timber.d("Added measurement: %s", scaleMeasurement);
        if (!silent) {
            ScaleUser scaleUser = getScaleUser(scaleMeasurement.getUserId());
            final java.text.DateFormat dateFormat = DateFormat.getDateFormat(context);
            final java.text.DateFormat timeFormat = DateFormat.getTimeFormat(context);
            final Date dateTime = scaleMeasurement.getDateTime();
            final Converters.WeightUnit unit = scaleUser.getScaleUnit();
            String infoText = String.format(context.getString(R.string.info_new_data_added), Converters.fromKilogram(scaleMeasurement.getWeight(), unit), unit.toString(), dateFormat.format(dateTime) + " " + timeFormat.format(dateTime), scaleUser.getUserName());
            runUiToastMsg(infoText);
        }
        syncInsertMeasurement(scaleMeasurement);
        alarmHandler.entryChanged(context, scaleMeasurement);
        triggerWidgetUpdate();
    } else {
        Timber.d("to be added measurement is thrown away because measurement with the same date and time already exist");
        if (!silent) {
            runUiToastMsg(context.getString(R.string.info_new_data_duplicated));
        }
    }
    return scaleMeasurement.getUserId();
}
Also used : SharedPreferences(android.content.SharedPreferences) Converters(com.health.openscale.core.utils.Converters) MeasurementViewSettings(com.health.openscale.gui.measurement.MeasurementViewSettings) Date(java.util.Date) EstimatedWaterMetric(com.health.openscale.core.bodymetric.EstimatedWaterMetric) ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) EstimatedLBMMetric(com.health.openscale.core.bodymetric.EstimatedLBMMetric) EstimatedFatMetric(com.health.openscale.core.bodymetric.EstimatedFatMetric) ScaleUser(com.health.openscale.core.datatypes.ScaleUser) Context(android.content.Context)

Example 2 with MeasurementViewSettings

use of com.health.openscale.gui.measurement.MeasurementViewSettings in project openScale by oliexdev.

the class StatisticsFragment method updateGoal.

private void updateGoal() {
    final Converters.WeightUnit unit = currentScaleUser.getScaleUnit();
    ScaleMeasurement goalScaleMeasurement = new ScaleMeasurement();
    goalScaleMeasurement.setUserId(currentScaleUser.getId());
    goalScaleMeasurement.setWeight(currentScaleUser.getGoalWeight());
    txtGoalWeight.setText(String.format("%.1f %s", Converters.fromKilogram(goalScaleMeasurement.getWeight(), unit), unit.toString()));
    txtGoalDiff.setText(String.format("%.1f %s", Converters.fromKilogram(goalScaleMeasurement.getWeight() - lastScaleMeasurement.getWeight(), unit), unit.toString()));
    Calendar goalCalendar = Calendar.getInstance();
    goalCalendar.setTime(currentScaleUser.getGoalDate());
    int days = Math.max(0, DateTimeHelpers.daysBetween(Calendar.getInstance(), goalCalendar));
    txtGoalDayLeft.setText(getResources().getQuantityString(R.plurals.label_days, days, days));
    boolean isBmiEnabled = new MeasurementViewSettings(PreferenceManager.getDefaultSharedPreferences(getActivity()), BMIMeasurementView.KEY).isEnabled();
    final float goalBmi = goalScaleMeasurement.getBMI(currentScaleUser.getBodyHeight());
    txtLabelGoalWeight.setText(isBmiEnabled ? Html.fromHtml(String.format("%s<br><font color='grey'><small>%s: %.1f</small></font>", getResources().getString(R.string.label_goal_weight), getResources().getString(R.string.label_bmi), goalBmi)) : getResources().getString(R.string.label_goal_weight));
    txtLabelGoalDiff.setText(isBmiEnabled ? Html.fromHtml(String.format("%s<br><font color='grey'><small>%s: %.1f</small></font>", getResources().getString(R.string.label_weight_difference), getResources().getString(R.string.label_bmi), lastScaleMeasurement.getBMI(currentScaleUser.getBodyHeight()) - goalBmi)) : getResources().getString(R.string.label_weight_difference));
    txtLabelDayLeft.setText(Html.fromHtml(String.format("%s<br><font color='grey'><small>%s %s</small></font>", getResources().getString(R.string.label_days_left), getResources().getString(R.string.label_goal_date_is), DateFormat.getDateInstance(DateFormat.LONG).format(currentScaleUser.getGoalDate()))));
}
Also used : ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) Calendar(java.util.Calendar) Converters(com.health.openscale.core.utils.Converters) MeasurementViewSettings(com.health.openscale.gui.measurement.MeasurementViewSettings)

Aggregations

ScaleMeasurement (com.health.openscale.core.datatypes.ScaleMeasurement)2 Converters (com.health.openscale.core.utils.Converters)2 MeasurementViewSettings (com.health.openscale.gui.measurement.MeasurementViewSettings)2 Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 EstimatedFatMetric (com.health.openscale.core.bodymetric.EstimatedFatMetric)1 EstimatedLBMMetric (com.health.openscale.core.bodymetric.EstimatedLBMMetric)1 EstimatedWaterMetric (com.health.openscale.core.bodymetric.EstimatedWaterMetric)1 ScaleUser (com.health.openscale.core.datatypes.ScaleUser)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1