From 41c23fa120e5184dec0c1445432483ab9172a68f Mon Sep 17 00:00:00 2001 From: conrad69 Date: Tue, 7 Jul 2020 18:23:56 +0200 Subject: [PATCH] fix #79: weekly average not calculated correctly --- .../1.json | 107 ++++++++++++++++++ .../ui/MonthStatisticFragment.java | 4 +- .../ui/WeekStatisticFragment.java | 5 +- .../ui/helper/DateHelper.java | 16 +++ 4 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 app/schemas/org.secuso.privacyfriendlyfoodtracker.database.ApplicationDatabase/1.json diff --git a/app/schemas/org.secuso.privacyfriendlyfoodtracker.database.ApplicationDatabase/1.json b/app/schemas/org.secuso.privacyfriendlyfoodtracker.database.ApplicationDatabase/1.json new file mode 100644 index 0000000..8e794e2 --- /dev/null +++ b/app/schemas/org.secuso.privacyfriendlyfoodtracker.database.ApplicationDatabase/1.json @@ -0,0 +1,107 @@ +{ + "formatVersion": 1, + "database": { + "version": 1, + "identityHash": "1c4b4d71bcbdb586cc4af0a0585902ca", + "entities": [ + { + "tableName": "ConsumedEntries", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `productId` INTEGER NOT NULL, `amount` INTEGER NOT NULL, `date` INTEGER, `name` TEXT, FOREIGN KEY(`productId`) REFERENCES `Product`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "productId", + "columnName": "productId", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "amount", + "columnName": "amount", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "date", + "columnName": "date", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": false + } + ], + "primaryKey": { + "columnNames": [ + "id" + ], + "autoGenerate": true + }, + "indices": [], + "foreignKeys": [ + { + "table": "Product", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "productId" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "Product", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT, `energy` REAL NOT NULL, `barcode` TEXT)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": false + }, + { + "fieldPath": "energy", + "columnName": "energy", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "barcode", + "columnName": "barcode", + "affinity": "TEXT", + "notNull": false + } + ], + "primaryKey": { + "columnNames": [ + "id" + ], + "autoGenerate": true + }, + "indices": [], + "foreignKeys": [] + } + ], + "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, \"1c4b4d71bcbdb586cc4af0a0585902ca\")" + ] + } +} \ No newline at end of file diff --git a/app/src/main/java/org/secuso/privacyfriendlyfoodtracker/ui/MonthStatisticFragment.java b/app/src/main/java/org/secuso/privacyfriendlyfoodtracker/ui/MonthStatisticFragment.java index 78e61f2..0cb9f04 100644 --- a/app/src/main/java/org/secuso/privacyfriendlyfoodtracker/ui/MonthStatisticFragment.java +++ b/app/src/main/java/org/secuso/privacyfriendlyfoodtracker/ui/MonthStatisticFragment.java @@ -134,8 +134,8 @@ void UpdateGraph() { try { - final Date startDate = getMonthByValue(-1); - final Date endDate = getMonthByValue(0); + final Date startDate = DateHelper.changeDateTimeToMidnight(getMonthByValue(-1)); + final Date endDate = DateHelper.changeDateTimeToMidnight(getMonthByValue(0)); List consumedEntriesList = databaseFacade.getCaloriesPerDayinPeriod(startDate,endDate); List calories = databaseFacade.getPeriodCalories(startDate,endDate); DataPoint[] dataPointInterfaces = new DataPoint[consumedEntriesList.size()]; diff --git a/app/src/main/java/org/secuso/privacyfriendlyfoodtracker/ui/WeekStatisticFragment.java b/app/src/main/java/org/secuso/privacyfriendlyfoodtracker/ui/WeekStatisticFragment.java index e421d33..cdc6506 100644 --- a/app/src/main/java/org/secuso/privacyfriendlyfoodtracker/ui/WeekStatisticFragment.java +++ b/app/src/main/java/org/secuso/privacyfriendlyfoodtracker/ui/WeekStatisticFragment.java @@ -135,14 +135,16 @@ void UpdateGraph() { try { Date startDate = getWeekByValue(-1); + startDate = DateHelper.changeDateTimeToMidnight(startDate); //set time to midnight so the period does not depend on the time of day Date endDate = getWeekByValue(0); + endDate = DateHelper.changeDateTimeToMidnight(endDate); consumedEntriesList = databaseFacade.getCaloriesPerDayinPeriod(startDate,endDate); calories = databaseFacade.getPeriodCalories(startDate,endDate); DataPoint[] dataPointInterfaces = new DataPoint[consumedEntriesList.size()]; for (int i = 0; i < consumedEntriesList.size(); i++) { dataPointInterfaces[i] = (new DataPoint(consumedEntriesList.get(i).unique1.getTime(), consumedEntriesList.get(i).unique2/100)); } - int weekdays = 8; + int weekdays = 7; float averageCalories = calories.get(0).unique2 / weekdays; @@ -181,4 +183,5 @@ Date getWeekByValue(int value) { return date; } + } diff --git a/app/src/main/java/org/secuso/privacyfriendlyfoodtracker/ui/helper/DateHelper.java b/app/src/main/java/org/secuso/privacyfriendlyfoodtracker/ui/helper/DateHelper.java index 67a529b..80d5ad2 100644 --- a/app/src/main/java/org/secuso/privacyfriendlyfoodtracker/ui/helper/DateHelper.java +++ b/app/src/main/java/org/secuso/privacyfriendlyfoodtracker/ui/helper/DateHelper.java @@ -51,4 +51,20 @@ public static String dateToString(Date date) { DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault()); return dateFormat.format(date); } + + /** + * Sets a given Date's time to 00:00:00.00, so getting a weeks daily calories works reliably. + * @param date the date whose time shall be midnight + * @return the new changed date + */ + public static Date changeDateTimeToMidnight(Date date){ + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + calendar.set(Calendar.HOUR_OF_DAY, 0); + calendar.set(Calendar.MINUTE, 0); + calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MILLISECOND, 0); + + return calendar.getTime(); + } }