1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-23 16:53:04 +02:00

additional support for people with amputations, which automatically adds a correction factor to the added weight

This commit is contained in:
oliexdev
2020-06-18 12:06:38 +02:00
parent 44580f04bf
commit fd552c41e8
9 changed files with 224 additions and 6 deletions

View File

@@ -2,7 +2,7 @@
"formatVersion": 1, "formatVersion": 1,
"database": { "database": {
"version": 5, "version": 5,
"identityHash": "f0e80a69f9fff0c48f2b7df10de3228f", "identityHash": "d66fc1fc2752b2d6f41700fa2102492a",
"entities": [ "entities": [
{ {
"tableName": "scaleMeasurements", "tableName": "scaleMeasurements",
@@ -174,7 +174,7 @@
}, },
{ {
"tableName": "scaleUsers", "tableName": "scaleUsers",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `username` TEXT NOT NULL, `birthday` INTEGER NOT NULL, `bodyHeight` REAL NOT NULL, `scaleUnit` INTEGER NOT NULL, `gender` INTEGER NOT NULL, `initialWeight` REAL NOT NULL, `goalWeight` REAL NOT NULL, `goalDate` INTEGER, `measureUnit` INTEGER NOT NULL, `activityLevel` INTEGER NOT NULL, `assistedWeighing` INTEGER NOT NULL)", "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `username` TEXT NOT NULL, `birthday` INTEGER NOT NULL, `bodyHeight` REAL NOT NULL, `scaleUnit` INTEGER NOT NULL, `gender` INTEGER NOT NULL, `initialWeight` REAL NOT NULL, `goalWeight` REAL NOT NULL, `goalDate` INTEGER, `measureUnit` INTEGER NOT NULL, `activityLevel` INTEGER NOT NULL, `assistedWeighing` INTEGER NOT NULL, `leftAmputationLevel` INTEGER NOT NULL, `rightAmputationLevel` INTEGER NOT NULL)",
"fields": [ "fields": [
{ {
"fieldPath": "id", "fieldPath": "id",
@@ -247,6 +247,18 @@
"columnName": "assistedWeighing", "columnName": "assistedWeighing",
"affinity": "INTEGER", "affinity": "INTEGER",
"notNull": true "notNull": true
},
{
"fieldPath": "leftAmputationLevel",
"columnName": "leftAmputationLevel",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "rightAmputationLevel",
"columnName": "rightAmputationLevel",
"affinity": "INTEGER",
"notNull": true
} }
], ],
"primaryKey": { "primaryKey": {
@@ -262,7 +274,7 @@
"views": [], "views": [],
"setupQueries": [ "setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'f0e80a69f9fff0c48f2b7df10de3228f')" "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'd66fc1fc2752b2d6f41700fa2102492a')"
] ]
} }
} }

View File

@@ -274,6 +274,7 @@ public class OpenScale {
public int addScaleMeasurement(final ScaleMeasurement scaleMeasurement, boolean silent) { public int addScaleMeasurement(final ScaleMeasurement scaleMeasurement, boolean silent) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
// Check user id and do a smart user assign if option is enabled
if (scaleMeasurement.getUserId() == -1) { if (scaleMeasurement.getUserId() == -1) {
if (prefs.getBoolean("smartUserAssign", false)) { if (prefs.getBoolean("smartUserAssign", false)) {
scaleMeasurement.setUserId(getSmartUserAssignment(scaleMeasurement.getWeight(), 15.0f)); scaleMeasurement.setUserId(getSmartUserAssignment(scaleMeasurement.getWeight(), 15.0f));
@@ -288,6 +289,7 @@ public class OpenScale {
} }
} }
// Assisted weighing
if (getScaleUser(scaleMeasurement.getUserId()).isAssistedWeighing()) { if (getScaleUser(scaleMeasurement.getUserId()).isAssistedWeighing()) {
int assistedWeighingRefUserId = prefs.getInt("assistedWeighingRefUserId", -1); int assistedWeighingRefUserId = prefs.getInt("assistedWeighingRefUserId", -1);
if (assistedWeighingRefUserId != -1) { if (assistedWeighingRefUserId != -1) {
@@ -303,6 +305,10 @@ public class OpenScale {
} }
} }
// 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); MeasurementViewSettings settings = new MeasurementViewSettings(prefs, WaterMeasurementView.KEY);
if (settings.isEnabled() && settings.isEstimationEnabled()) { if (settings.isEnabled() && settings.isEstimationEnabled()) {
EstimatedWaterMetric waterMetric = EstimatedWaterMetric.getEstimatedMetric( EstimatedWaterMetric waterMetric = EstimatedWaterMetric.getEstimatedMetric(
@@ -325,6 +331,7 @@ public class OpenScale {
scaleMeasurement.setLbm(lbmMetric.getLBM(getScaleUser(scaleMeasurement.getUserId()), scaleMeasurement)); 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) { if (measurementDAO.insert(scaleMeasurement) != -1) {
Timber.d("Added measurement: %s", scaleMeasurement); Timber.d("Added measurement: %s", scaleMeasurement);
if (!silent) { if (!silent) {

View File

@@ -182,8 +182,10 @@ public abstract class AppDatabase extends RoomDatabase {
public void migrate(SupportSQLiteDatabase database) { public void migrate(SupportSQLiteDatabase database) {
database.beginTransaction(); database.beginTransaction();
try { try {
// Add assisted weighing to table // Add assisted weighing and left/right amputation level to table
database.execSQL("ALTER TABLE scaleUsers ADD assistedWeighing INTEGER NOT NULL default 0"); database.execSQL("ALTER TABLE scaleUsers ADD assistedWeighing INTEGER NOT NULL default 0");
database.execSQL("ALTER TABLE scaleUsers ADD leftAmputationLevel INTEGER NOT NULL default 0");
database.execSQL("ALTER TABLE scaleUsers ADD rightAmputationLevel INTEGER NOT NULL default 0");
database.setTransactionSuccessful(); database.setTransactionSuccessful();
} }

View File

@@ -60,6 +60,12 @@ public class ScaleUser {
private Converters.ActivityLevel activityLevel; private Converters.ActivityLevel activityLevel;
@ColumnInfo(name = "assistedWeighing") @ColumnInfo(name = "assistedWeighing")
private boolean assistedWeighing; private boolean assistedWeighing;
@NonNull
@ColumnInfo(name = "leftAmputationLevel")
private Converters.AmputationLevel leftAmputationLevel;
@NonNull
@ColumnInfo(name = "rightAmputationLevel")
private Converters.AmputationLevel rightAmputationLevel;
public ScaleUser() { public ScaleUser() {
userName = ""; userName = "";
@@ -73,6 +79,8 @@ public class ScaleUser {
measureUnit = Converters.MeasureUnit.CM; measureUnit = Converters.MeasureUnit.CM;
activityLevel = Converters.ActivityLevel.SEDENTARY; activityLevel = Converters.ActivityLevel.SEDENTARY;
assistedWeighing = false; assistedWeighing = false;
leftAmputationLevel = Converters.AmputationLevel.NONE;
rightAmputationLevel = Converters.AmputationLevel.NONE;
} }
public int getId() { public int getId() {
@@ -187,6 +195,76 @@ public class ScaleUser {
this.assistedWeighing = assistedWeighing; this.assistedWeighing = assistedWeighing;
} }
@NonNull
public Converters.AmputationLevel getLeftAmputationLevel() {
return leftAmputationLevel;
}
public void setLeftAmputationLevel(@NonNull Converters.AmputationLevel leftAmputationLevel) {
this.leftAmputationLevel = leftAmputationLevel;
}
@NonNull
public Converters.AmputationLevel getRightAmputationLevel() {
return rightAmputationLevel;
}
public void setRightAmputationLevel(@NonNull Converters.AmputationLevel rightAmputationLevel) {
this.rightAmputationLevel = rightAmputationLevel;
}
public float getAmputationCorrectionFactor() {
float correctionFactor = 100.0f;
switch (rightAmputationLevel) {
case NONE:
break;
case HAND:
correctionFactor -= 0.8f;
break;
case FOREARM_HAND:
correctionFactor -= 3.0f;
break;
case ARM:
correctionFactor -= 11.5f;
break;
case FOOT:
correctionFactor -= 1.8f;
break;
case LOWER_LEG_FOOT:
correctionFactor -= 7.1f;
break;
case LEG:
correctionFactor -= 18.7f;
break;
}
switch (leftAmputationLevel) {
case NONE:
break;
case HAND:
correctionFactor -= 0.8f;
break;
case FOREARM_HAND:
correctionFactor -= 3.0f;
break;
case ARM:
correctionFactor -= 11.5f;
break;
case FOOT:
correctionFactor -= 1.8f;
break;
case LOWER_LEG_FOOT:
correctionFactor -= 7.1f;
break;
case LEG:
correctionFactor -= 18.7f;
break;
}
return correctionFactor;
}
public static String getPreferenceKey(int userId, String key) { public static String getPreferenceKey(int userId, String key) {
return String.format("user.%d.%s", userId, key); return String.format("user.%d.%s", userId, key);
} }

View File

@@ -145,6 +145,52 @@ public class Converters {
} }
} }
public enum AmputationLevel {
NONE, HAND, FOREARM_HAND, ARM, FOOT, LOWER_LEG_FOOT, LEG;
public static AmputationLevel fromInt(int unit) {
switch (unit) {
case 0:
return NONE;
case 1:
return HAND;
case 2:
return FOREARM_HAND;
case 3:
return ARM;
case 4:
return FOOT;
case 5:
return LOWER_LEG_FOOT;
case 6:
return LEG;
}
return NONE;
}
public int toInt() {
switch (this) {
case NONE:
return 0;
case HAND:
return 1;
case FOREARM_HAND:
return 2;
case ARM:
return 3;
case FOOT:
return 4;
case LOWER_LEG_FOOT:
return 5;
case LEG:
return 6;
}
return 0;
}
}
private static final float KG_LB = 2.20462f; private static final float KG_LB = 2.20462f;
private static final float KG_ST = 0.157473f; private static final float KG_ST = 0.157473f;
private static final float CM_IN = 0.393701f; private static final float CM_IN = 0.393701f;
@@ -199,6 +245,16 @@ public class Converters {
return level.toInt(); return level.toInt();
} }
@TypeConverter
public static AmputationLevel fromAmputationLevelInt(int level) {
return AmputationLevel.fromInt(level);
}
@TypeConverter
public static int toAmputationLevelInt(AmputationLevel level) {
return level.toInt();
}
public static float toCentimeter(float value, MeasureUnit unit) { public static float toCentimeter(float value, MeasureUnit unit) {
switch (unit) { switch (unit) {
case INCH: case INCH:

View File

@@ -68,6 +68,8 @@ public class UserSettingsFragment extends Fragment {
private CheckBox assistedWeighing; private CheckBox assistedWeighing;
private RadioGroup radioMeasurementUnit; private RadioGroup radioMeasurementUnit;
private Spinner spinnerActivityLevel; private Spinner spinnerActivityLevel;
private Spinner spinnerLeftAmputationLevel;
private Spinner spinnerRightAmputationLevel;
private final DateFormat dateFormat = DateFormat.getDateInstance(); private final DateFormat dateFormat = DateFormat.getDateInstance();
@@ -93,6 +95,8 @@ public class UserSettingsFragment extends Fragment {
assistedWeighing = root.findViewById(R.id.asisstedWeighing); assistedWeighing = root.findViewById(R.id.asisstedWeighing);
radioMeasurementUnit = root.findViewById(R.id.groupMeasureUnit); radioMeasurementUnit = root.findViewById(R.id.groupMeasureUnit);
spinnerActivityLevel = root.findViewById(R.id.spinnerActivityLevel); spinnerActivityLevel = root.findViewById(R.id.spinnerActivityLevel);
spinnerLeftAmputationLevel = root.findViewById(R.id.spinnerLeftAmputationLevel);
spinnerRightAmputationLevel = root.findViewById(R.id.spinnerRightAmputationLevel);
txtInitialWeight = root.findViewById(R.id.txtInitialWeight); txtInitialWeight = root.findViewById(R.id.txtInitialWeight);
txtGoalWeight = root.findViewById(R.id.txtGoalWeight); txtGoalWeight = root.findViewById(R.id.txtGoalWeight);
@@ -300,6 +304,8 @@ public class UserSettingsFragment extends Fragment {
assistedWeighing.setChecked(scaleUser.isAssistedWeighing()); assistedWeighing.setChecked(scaleUser.isAssistedWeighing());
spinnerActivityLevel.setSelection(scaleUser.getActivityLevel().toInt()); spinnerActivityLevel.setSelection(scaleUser.getActivityLevel().toInt());
spinnerLeftAmputationLevel.setSelection(scaleUser.getLeftAmputationLevel().toInt());
spinnerRightAmputationLevel.setSelection(scaleUser.getRightAmputationLevel().toInt());
} }
private boolean validateInput() private boolean validateInput()
@@ -444,8 +450,9 @@ public class UserSettingsFragment extends Fragment {
scaleUser.setBodyHeight(Converters.toCentimeter(body_height, measure_unit)); scaleUser.setBodyHeight(Converters.toCentimeter(body_height, measure_unit));
scaleUser.setScaleUnit(scale_unit); scaleUser.setScaleUnit(scale_unit);
scaleUser.setMeasureUnit(measure_unit); scaleUser.setMeasureUnit(measure_unit);
scaleUser.setActivityLevel(Converters.fromActivityLevelInt( scaleUser.setActivityLevel(Converters.fromActivityLevelInt(spinnerActivityLevel.getSelectedItemPosition()));
spinnerActivityLevel.getSelectedItemPosition())); scaleUser.setLeftAmputationLevel(Converters.fromAmputationLevelInt(spinnerLeftAmputationLevel.getSelectedItemPosition()));
scaleUser.setRightAmputationLevel(Converters.fromAmputationLevelInt(spinnerRightAmputationLevel.getSelectedItemPosition()));
scaleUser.setGender(gender); scaleUser.setGender(gender);
scaleUser.setAssistedWeighing(assistedWeighing.isChecked()); scaleUser.setAssistedWeighing(assistedWeighing.isChecked());
scaleUser.setInitialWeight(Converters.toKilogram(initial_weight, scale_unit)); scaleUser.setInitialWeight(Converters.toKilogram(initial_weight, scale_unit));

View File

@@ -126,6 +126,42 @@
</CheckBox> </CheckBox>
</TableRow> </TableRow>
<TableRow
android:id="@+id/rowLeftAmputationLevel"
android:layout_weight="1"
android:gravity="center_vertical">
<TextView
android:id="@+id/lblLeftAmputationLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_amputation_left" />
<Spinner
android:id="@+id/spinnerLeftAmputationLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/amputation_level_entries" />
</TableRow>
<TableRow
android:id="@+id/rowRightAmputationLevel"
android:layout_weight="1"
android:gravity="center_vertical">
<TextView
android:id="@+id/lblRightAmputationLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_amputation_right" />
<Spinner
android:id="@+id/spinnerRightAmputationLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/amputation_level_entries" />
</TableRow>
<TableRow <TableRow
android:id="@+id/rowMeasureUnit" android:id="@+id/rowMeasureUnit"
android:layout_weight="1" android:layout_weight="1"

View File

@@ -84,4 +84,14 @@
<item>@string/activity_level_heavy</item> <item>@string/activity_level_heavy</item>
<item>@string/activity_level_extreme</item> <item>@string/activity_level_extreme</item>
</string-array> </string-array>
<string-array name="amputation_level_entries">
<item>@string/amputation_level_none</item>
<item>@string/amputation_level_hand</item>
<item>@string/amputation_level_forearm_hand</item>
<item>@string/amputation_level_arm</item>
<item>@string/amputation_level_foot</item>
<item>@string/amputation_level_lower_leg_foot</item>
<item>@string/amputation_level_leg</item>
</string-array>
</resources> </resources>

View File

@@ -57,6 +57,9 @@
<string name="label_scale_unit">Scale unit</string> <string name="label_scale_unit">Scale unit</string>
<string name="label_gender">Gender</string> <string name="label_gender">Gender</string>
<string name="label_assisted_weighing">Assisted weighing</string> <string name="label_assisted_weighing">Assisted weighing</string>
<string name="label_amputation">Amputation</string>
<string name="label_amputation_left">Amputation left</string>
<string name="label_amputation_right">Amputation right</string>
<string name="label_male">Male</string> <string name="label_male">Male</string>
<string name="label_female">Female</string> <string name="label_female">Female</string>
<string name="label_goal_weight">Goal weight</string> <string name="label_goal_weight">Goal weight</string>
@@ -238,6 +241,13 @@
<string name="activity_level_moderate">Moderate</string> <string name="activity_level_moderate">Moderate</string>
<string name="activity_level_heavy">Heavy</string> <string name="activity_level_heavy">Heavy</string>
<string name="activity_level_extreme">Extreme</string> <string name="activity_level_extreme">Extreme</string>
<string name="amputation_level_none">No amputation</string>
<string name="amputation_level_hand">Hand</string>
<string name="amputation_level_forearm_hand">Forearm and leg</string>
<string name="amputation_level_arm">Arm</string>
<string name="amputation_level_foot">Foot</string>
<string name="amputation_level_lower_leg_foot">Lower leg and foot</string>
<string name="amputation_level_leg">Leg</string>
<string name="label_upgrade_to_openScale_pro">Please upgrade to openScale pro for Bluetooth support</string> <string name="label_upgrade_to_openScale_pro">Please upgrade to openScale pro for Bluetooth support</string>
<string name="label_slide_welcome_top_text">Welcome to\nopenScale</string> <string name="label_slide_welcome_top_text">Welcome to\nopenScale</string>
<string name="label_slide_welcome_main_text">Open-source software weight and body metrics tracker, with Bluetooth scale support.</string> <string name="label_slide_welcome_main_text">Open-source software weight and body metrics tracker, with Bluetooth scale support.</string>