mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-21 16:02:04 +02:00
Add database support for height unit and activity level
Only add the fields to the database for now, when we're bumping the version number. For the actual work to add them so that they can be used, see #265 and #266.
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
"formatVersion": 1,
|
||||
"database": {
|
||||
"version": 3,
|
||||
"identityHash": "83d2f44ffc34383c4f1103f001329440",
|
||||
"identityHash": "2c2512af7063099727b54e5560c277e3",
|
||||
"entities": [
|
||||
{
|
||||
"tableName": "scaleMeasurements",
|
||||
@@ -168,7 +168,7 @@
|
||||
},
|
||||
{
|
||||
"tableName": "scaleUsers",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `username` TEXT, `birthday` INTEGER, `bodyHeight` INTEGER NOT NULL, `scaleUnit` INTEGER NOT NULL, `gender` INTEGER NOT NULL, `initialWeight` REAL NOT NULL, `goalWeight` REAL NOT NULL, `goalDate` INTEGER)",
|
||||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `username` TEXT, `birthday` INTEGER, `bodyHeight` INTEGER NOT NULL, `scaleUnit` INTEGER NOT NULL, `gender` INTEGER NOT NULL, `initialWeight` REAL NOT NULL, `goalWeight` REAL NOT NULL, `goalDate` INTEGER, `heightUnit` INTEGER NOT NULL, `activityLevel` INTEGER NOT NULL)",
|
||||
"fields": [
|
||||
{
|
||||
"fieldPath": "id",
|
||||
@@ -223,6 +223,18 @@
|
||||
"columnName": "goalDate",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": false
|
||||
},
|
||||
{
|
||||
"fieldPath": "heightUnit",
|
||||
"columnName": "heightUnit",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
},
|
||||
{
|
||||
"fieldPath": "activityLevel",
|
||||
"columnName": "activityLevel",
|
||||
"affinity": "INTEGER",
|
||||
"notNull": true
|
||||
}
|
||||
],
|
||||
"primaryKey": {
|
||||
@@ -237,7 +249,7 @@
|
||||
],
|
||||
"setupQueries": [
|
||||
"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, \"83d2f44ffc34383c4f1103f001329440\")"
|
||||
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"2c2512af7063099727b54e5560c277e3\")"
|
||||
]
|
||||
}
|
||||
}
|
@@ -146,6 +146,9 @@ public class DatabaseMigrationTest {
|
||||
|
||||
// MigrationTestHelper automatically verifies the schema changes.
|
||||
|
||||
assertEquals(3, db.query("SELECT * FROM scaleUsers WHERE heightUnit = 0").getCount());
|
||||
assertEquals(3, db.query("SELECT * FROM scaleUsers WHERE activityLevel = 0").getCount());
|
||||
|
||||
Cursor cursor = db.query("SELECT * FROM scaleMeasurements ORDER BY id, userId");
|
||||
assertEquals(2 * 2, cursor.getCount());
|
||||
|
||||
|
@@ -78,6 +78,10 @@ public abstract class AppDatabase extends RoomDatabase {
|
||||
public void migrate(SupportSQLiteDatabase database) {
|
||||
database.beginTransaction();
|
||||
try {
|
||||
// Add new columns to scaleUsers
|
||||
database.execSQL("ALTER TABLE scaleUsers ADD COLUMN heightUnit INTEGER NOT NULL DEFAULT 0");
|
||||
database.execSQL("ALTER TABLE scaleUsers ADD COLUMN activityLevel INTEGER NOT NULL DEFAULT 0");
|
||||
|
||||
// Drop old index
|
||||
database.execSQL("DROP INDEX index_scaleMeasurements_userId_datetime");
|
||||
|
||||
|
@@ -50,6 +50,10 @@ public class ScaleUser {
|
||||
private float goalWeight;
|
||||
@ColumnInfo(name = "goalDate")
|
||||
private Date goalDate;
|
||||
@ColumnInfo(name = "heightUnit")
|
||||
private int heightUnit;
|
||||
@ColumnInfo(name = "activityLevel")
|
||||
private int activityLevel;
|
||||
|
||||
public ScaleUser() {
|
||||
userName = "";
|
||||
@@ -60,6 +64,8 @@ public class ScaleUser {
|
||||
initialWeight = -1;
|
||||
goalWeight = -1;
|
||||
goalDate = new Date();
|
||||
heightUnit = 0;
|
||||
activityLevel = 0;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
@@ -158,6 +164,22 @@ public class ScaleUser {
|
||||
return Converters.fromKilogram(initialWeight, scaleUnit);
|
||||
}
|
||||
|
||||
public void setHeightUnit(int unit) {
|
||||
heightUnit = unit;
|
||||
}
|
||||
|
||||
public int getHeightUnit() {
|
||||
return heightUnit;
|
||||
}
|
||||
|
||||
public void setActivityLevel(int level) {
|
||||
activityLevel = level;
|
||||
}
|
||||
|
||||
public int getActivityLevel() {
|
||||
return activityLevel;
|
||||
}
|
||||
|
||||
public static String getPreferenceKey(int userId, String key) {
|
||||
return String.format("user.%d.%s", userId, key);
|
||||
}
|
||||
|
Reference in New Issue
Block a user