diff --git a/.travis.yml b/.travis.yml index d4f958ff..371ee3e2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,4 +23,4 @@ after_success: - chmod +x ./upload_apk.sh - ./upload_apk.sh -script: cd android_app && ./gradlew assembleDebug +script: cd android_app && ./gradlew testDebug assembleDebug diff --git a/android_app/app/build.gradle b/android_app/app/build.gradle index 291ecff2..b6f97e31 100644 --- a/android_app/app/build.gradle +++ b/android_app/app/build.gradle @@ -31,4 +31,13 @@ dependencies { compile "com.android.support:support-v4:${supportLibVersion}" compile "com.android.support:appcompat-v7:${supportLibVersion}" compile 'com.github.lecho:hellocharts-library:1.5.8@aar' + compile 'junit:junit:4.12' +} + +tasks.withType(Test) { + testLogging { + exceptionFormat "full" + events "started", "skipped", "passed", "failed" + showStandardStreams true + } } diff --git a/android_app/app/src/main/java/com/health/openscale/core/utils/DateTimeHelpers.java b/android_app/app/src/main/java/com/health/openscale/core/utils/DateTimeHelpers.java new file mode 100644 index 00000000..6017019d --- /dev/null +++ b/android_app/app/src/main/java/com/health/openscale/core/utils/DateTimeHelpers.java @@ -0,0 +1,42 @@ +/* Copyright (C) 2017 Erik Johansson +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see +*/ + +package com.health.openscale.core.utils; + +import java.util.Calendar; + +public final class DateTimeHelpers { + static public int daysBetween(Calendar start, Calendar end) { + if (start.after(end)) { + return -daysBetween(end, start); + } + + int days = 0; + + Calendar current = (Calendar)start.clone(); + while (current.get(Calendar.YEAR) < end.get(Calendar.YEAR)) { + final int daysInYear = + current.getActualMaximum(Calendar.DAY_OF_YEAR) + - current.get(Calendar.DAY_OF_YEAR) + 1; + days += daysInYear; + current.add(Calendar.DAY_OF_YEAR, daysInYear); + } + + days += end.get(Calendar.DAY_OF_YEAR) - current.get(Calendar.DAY_OF_YEAR); + + return days; + } +} diff --git a/android_app/app/src/main/java/com/health/openscale/gui/fragments/OverviewFragment.java b/android_app/app/src/main/java/com/health/openscale/gui/fragments/OverviewFragment.java index 6b4dc187..61396f16 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/fragments/OverviewFragment.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/fragments/OverviewFragment.java @@ -37,6 +37,7 @@ import com.health.openscale.R; import com.health.openscale.core.OpenScale; import com.health.openscale.core.datatypes.ScaleData; import com.health.openscale.core.datatypes.ScaleUser; +import com.health.openscale.core.utils.DateTimeHelpers; import com.health.openscale.gui.activities.DataEntryActivity; import com.health.openscale.gui.views.BMIMeasurementView; import com.health.openscale.gui.views.BMRMeasurementView; @@ -55,6 +56,7 @@ import com.health.openscale.gui.views.WeightMeasurementView; import java.text.DateFormat; import java.util.ArrayList; import java.util.Calendar; +import java.util.Date; import java.util.List; import lecho.lib.hellocharts.formatter.SimpleLineChartValueFormatter; @@ -255,12 +257,8 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener max_i = scaleDataList.size(); } - Calendar histDate = Calendar.getInstance(); - Calendar lastDate = Calendar.getInstance(); - - if (!scaleDataList.isEmpty()) { - lastDate.setTime(scaleDataList.get(0).getDateTime()); - } + final Calendar now = Calendar.getInstance(); + Calendar histCalendar = Calendar.getInstance(); scaleDataLastDays = new ArrayList(); @@ -285,11 +283,10 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener if (histData.getBone() != 0.0f) valuesBone.add(new PointValue(i, histData.getBone())); - histDate.setTime(histData.getDateTime()); - - long days = 0 - daysBetween(lastDate, histDate); - - axisValues.add(new AxisValue(i, String.format("%d " + getResources().getString(R.string.label_days), days).toCharArray())); + histCalendar.setTime(histData.getDateTime()); + int days = DateTimeHelpers.daysBetween(now, histCalendar); + String label = getResources().getQuantityString(R.plurals.label_days, Math.abs(days), days); + axisValues.add(new AxisValue(i, label.toCharArray())); } Line lineWeight = new Line(valuesWeight). @@ -428,10 +425,6 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener pieChartLast.setPieChartData(pieChartData); } - private long daysBetween(Calendar startDate, Calendar endDate) { - return startDate.get(Calendar.DAY_OF_YEAR) - endDate.get(Calendar.DAY_OF_YEAR); - } - public void btnOnClickInsertData() { Intent intent = new Intent(overviewView.getContext(), DataEntryActivity.class); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/fragments/StatisticsFragment.java b/android_app/app/src/main/java/com/health/openscale/gui/fragments/StatisticsFragment.java index 276c2549..d7831157 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/fragments/StatisticsFragment.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/fragments/StatisticsFragment.java @@ -30,11 +30,12 @@ import com.health.openscale.R; import com.health.openscale.core.OpenScale; import com.health.openscale.core.datatypes.ScaleData; import com.health.openscale.core.datatypes.ScaleUser; +import com.health.openscale.core.utils.DateTimeHelpers; import java.text.DateFormat; import java.util.ArrayList; import java.util.Calendar; -import java.util.concurrent.TimeUnit; +import java.util.Date; public class StatisticsFragment extends Fragment implements FragmentUpdateListener { @@ -99,7 +100,7 @@ public class StatisticsFragment extends Fragment implements FragmentUpdateListen txtTitleStatistics.setText(getResources().getString(R.string.label_title_statistics).toUpperCase()); prefs = PreferenceManager.getDefaultSharedPreferences(statisticsView.getContext()); - currentScaleUser = OpenScale.getInstance(getContext()).getSelectedScaleUser(); + currentScaleUser = OpenScale.getInstance(getContext()).getSelectedScaleUser(); updateStatistics(scaleDataList); updateGoal(scaleDataList); @@ -114,12 +115,10 @@ public class StatisticsFragment extends Fragment implements FragmentUpdateListen double weight_diff = goalScaleData.getConvertedWeight(currentScaleUser.scale_unit) - lastScaleData.getConvertedWeight(currentScaleUser.scale_unit); txtGoalDiff.setText(String.format("%.1f " + ScaleUser.UNIT_STRING[currentScaleUser.scale_unit], weight_diff)); - Calendar goalDate = Calendar.getInstance(); - Calendar curDate = Calendar.getInstance(); - goalDate.setTime(currentScaleUser.goal_date); - - long days = daysBetween(curDate, goalDate); - txtGoalDayLeft.setText(days + " " + getResources().getString(R.string.label_days)); + Calendar goalCalendar = Calendar.getInstance(); + goalCalendar.setTime(currentScaleUser.goal_date); + int days = Math.max(0, DateTimeHelpers.daysBetween(Calendar.getInstance(), goalCalendar)); + txtGoalDayLeft.setText(getResources().getQuantityString(R.plurals.label_days, days, days)); lastScaleData.setUserId(currentScaleUser.id); @@ -334,10 +333,4 @@ public class StatisticsFragment extends Fragment implements FragmentUpdateListen txtAvgWeek.setText(weekSize + " " + getResources().getString(R.string.label_measures)); txtAvgMonth.setText(monthSize + " " + getResources().getString(R.string.label_measures)); } - - private long daysBetween(Calendar startDate, Calendar endDate) { - long end = endDate.getTimeInMillis(); - long start = startDate.getTimeInMillis(); - return TimeUnit.MILLISECONDS.toDays(Math.abs(end - start)); - } } diff --git a/android_app/app/src/main/java/com/health/openscale/junit/DateTimeHelpersTest.java b/android_app/app/src/main/java/com/health/openscale/junit/DateTimeHelpersTest.java new file mode 100644 index 00000000..2e626c14 --- /dev/null +++ b/android_app/app/src/main/java/com/health/openscale/junit/DateTimeHelpersTest.java @@ -0,0 +1,80 @@ +/* Copyright (C) 2017 Erik Johansson +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see +*/ + +package com.health.openscale.junit; + +import com.health.openscale.core.utils.DateTimeHelpers; + +import org.junit.Test; + +import java.util.Calendar; + +import static org.junit.Assert.assertEquals; + +public class DateTimeHelpersTest { + Calendar getDate(int year, int month, int day, int hour, int minute, int second, int ms) { + Calendar cal = Calendar.getInstance(); + cal.set(year, month - 1, day, hour, minute, second); + cal.set(Calendar.MILLISECOND, ms); + return cal; + } + Calendar getDate(int year, int month, int day) { + return getDate(year, month, day, 0, 0, 0, 0); + } + + @Test + public void daysBetween() throws Exception { + assertEquals(0, + DateTimeHelpers.daysBetween( + getDate(2017, 1, 1), + getDate(2017, 1, 1))); + + assertEquals(0, + DateTimeHelpers.daysBetween( + getDate(2017, 1, 1, 12, 10, 10, 0), + getDate(2017, 1, 1, 12, 9, 0, 0))); + + assertEquals(0, + DateTimeHelpers.daysBetween( + getDate(2017, 1, 1, 23, 59, 59, 999), + getDate(2017, 1, 1))); + + assertEquals(1, + DateTimeHelpers.daysBetween( + getDate(2017, 1, 1), + getDate(2017, 1, 2))); + + assertEquals(-1, + DateTimeHelpers.daysBetween( + getDate(2017, 1, 2), + getDate(2017, 1, 1))); + + assertEquals(1, + DateTimeHelpers.daysBetween( + getDate(2017, 1, 1, 23, 59, 59, 999), + getDate(2017, 1, 2))); + + assertEquals(29 - 10 + 4 * 30 + 6 * 31 + 2, + DateTimeHelpers.daysBetween( + getDate(2016, 2, 10, 1, 2, 3, 10), + getDate(2017, 1, 2))); + + assertEquals(1 + 365 + 366, + DateTimeHelpers.daysBetween( + getDate(2014, 12, 31), + getDate(2017, 1, 1))); + } +} diff --git a/android_app/app/src/main/res/values-de/strings.xml b/android_app/app/src/main/res/values-de/strings.xml index 69943f6a..1b8df397 100644 --- a/android_app/app/src/main/res/values-de/strings.xml +++ b/android_app/app/src/main/res/values-de/strings.xml @@ -36,7 +36,10 @@ Körpergröße Kommentar Datum - Tage + + %d Tag + %d Tage + Tage übrig Löschen Alles löschen diff --git a/android_app/app/src/main/res/values-es/strings.xml b/android_app/app/src/main/res/values-es/strings.xml index aa468726..81621e74 100644 --- a/android_app/app/src/main/res/values-es/strings.xml +++ b/android_app/app/src/main/res/values-es/strings.xml @@ -38,7 +38,10 @@ Masa ósea Asignación inteligente de usuario - días + + %d día + %d días + medidas Últimos 7 días Últimos 30 días diff --git a/android_app/app/src/main/res/values-fr/strings.xml b/android_app/app/src/main/res/values-fr/strings.xml index 35cd961f..8988b076 100644 --- a/android_app/app/src/main/res/values-fr/strings.xml +++ b/android_app/app/src/main/res/values-fr/strings.xml @@ -33,7 +33,10 @@ Rapport taille-hanches Affectation intelligente de l\'Utilisateur - jours + + %d jour + %d jours + mesures 7 derniers jours 30 derniers jours diff --git a/android_app/app/src/main/res/values-ja/strings.xml b/android_app/app/src/main/res/values-ja/strings.xml index fd95cb3f..829998e8 100644 --- a/android_app/app/src/main/res/values-ja/strings.xml +++ b/android_app/app/src/main/res/values-ja/strings.xml @@ -23,7 +23,9 @@ 身長 キャンセル コメント - + + %d 日 + 期日 デリート レコードのエクスポート diff --git a/android_app/app/src/main/res/values-pl/strings.xml b/android_app/app/src/main/res/values-pl/strings.xml index 56228709..6cbae2b5 100644 --- a/android_app/app/src/main/res/values-pl/strings.xml +++ b/android_app/app/src/main/res/values-pl/strings.xml @@ -36,7 +36,9 @@ Masa kostna Inteligente przypisywanie użytkowników - dni + + %d dni + pomiarów Ostatnie 7 dni Ostatnie 30 dni diff --git a/android_app/app/src/main/res/values-pt/strings.xml b/android_app/app/src/main/res/values-pt/strings.xml index 962d17b4..733f1acd 100644 --- a/android_app/app/src/main/res/values-pt/strings.xml +++ b/android_app/app/src/main/res/values-pt/strings.xml @@ -69,7 +69,9 @@ Cancelar Comentário Data - dias + + %d dias + Dias restantes Deletar Deletar tudo diff --git a/android_app/app/src/main/res/values-sk/strings.xml b/android_app/app/src/main/res/values-sk/strings.xml index 9a7b7292..c24f70cb 100644 --- a/android_app/app/src/main/res/values-sk/strings.xml +++ b/android_app/app/src/main/res/values-sk/strings.xml @@ -31,7 +31,9 @@ Pomer pásu a bokov Hmotnosť kostí Chytré priradenie používateľa - dni + + %d dni + merania Posledných 7 dní Posledných 30 dní diff --git a/android_app/app/src/main/res/values-sv/strings.xml b/android_app/app/src/main/res/values-sv/strings.xml index fc6c5c62..6e44683e 100644 --- a/android_app/app/src/main/res/values-sv/strings.xml +++ b/android_app/app/src/main/res/values-sv/strings.xml @@ -60,7 +60,10 @@ Dagar kvar Måldatum är Viktskillnad - dagar + + %d dag + %d dagar + mätningar Senast 7 dagarna Senast 30 dagarna diff --git a/android_app/app/src/main/res/values-tr/strings.xml b/android_app/app/src/main/res/values-tr/strings.xml index f7fea94b..efb1415b 100644 --- a/android_app/app/src/main/res/values-tr/strings.xml +++ b/android_app/app/src/main/res/values-tr/strings.xml @@ -37,7 +37,9 @@ Kemik kütlesi Akýllý kullanýcý atamasý - Günler + + %d Günler + Ölçüler Son 7 Gün Son 30 Gün diff --git a/android_app/app/src/main/res/values/strings.xml b/android_app/app/src/main/res/values/strings.xml index 5d2baf0d..d4177fa8 100644 --- a/android_app/app/src/main/res/values/strings.xml +++ b/android_app/app/src/main/res/values/strings.xml @@ -38,7 +38,10 @@ Bone mass Smart User assignment - days + + %d day + %d days + measures Last 7 days Last 30 days