Search in sources :

Example 1 with MeasurementViewSettings

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

the class OpenScale method addScaleData.

public int addScaleData(final ScaleMeasurement scaleMeasurement, boolean silent) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    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) {
            return -1;
        }
    }
    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, LBWMeasurementView.KEY);
    if (settings.isEnabled() && settings.isEstimationEnabled()) {
        EstimatedLBWMetric lbwMetric = EstimatedLBWMetric.getEstimatedMetric(EstimatedLBWMetric.FORMULA.valueOf(settings.getEstimationFormula()));
        scaleMeasurement.setLbw(lbwMetric.getLBW(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));
    }
    if (measurementDAO.insert(scaleMeasurement) != -1) {
        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();
        if (!silent) {
            String infoText = String.format(context.getString(R.string.info_new_data_added), scaleMeasurement.getConvertedWeight(unit), unit.toString(), dateFormat.format(dateTime) + " " + timeFormat.format(dateTime), scaleUser.getUserName());
            Toast.makeText(context, infoText, Toast.LENGTH_LONG).show();
        }
        alarmHandler.entryChanged(context, scaleMeasurement);
        updateScaleData();
    } else {
        if (!silent) {
            Toast.makeText(context, context.getString(R.string.info_new_data_duplicated), Toast.LENGTH_LONG).show();
        }
    }
    return scaleMeasurement.getUserId();
}
Also used : EstimatedWaterMetric(com.health.openscale.core.bodymetric.EstimatedWaterMetric) SharedPreferences(android.content.SharedPreferences) EstimatedFatMetric(com.health.openscale.core.bodymetric.EstimatedFatMetric) ScaleUser(com.health.openscale.core.datatypes.ScaleUser) EstimatedLBWMetric(com.health.openscale.core.bodymetric.EstimatedLBWMetric) Converters(com.health.openscale.core.utils.Converters) Context(android.content.Context) MeasurementViewSettings(com.health.openscale.gui.views.MeasurementViewSettings) Date(java.util.Date)

Aggregations

Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 EstimatedFatMetric (com.health.openscale.core.bodymetric.EstimatedFatMetric)1 EstimatedLBWMetric (com.health.openscale.core.bodymetric.EstimatedLBWMetric)1 EstimatedWaterMetric (com.health.openscale.core.bodymetric.EstimatedWaterMetric)1 ScaleUser (com.health.openscale.core.datatypes.ScaleUser)1 Converters (com.health.openscale.core.utils.Converters)1 MeasurementViewSettings (com.health.openscale.gui.views.MeasurementViewSettings)1 Date (java.util.Date)1