diff --git a/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java b/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java index a892934f..bbcb82a1 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java +++ b/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java @@ -695,7 +695,15 @@ public class OpenScale { Intent intent = new Intent(); intent.setComponent(new ComponentName("com.health.openscale.sync", "com.health.openscale.sync.core.service.SyncService")); intent.putExtra("mode", "delete"); - intent.putExtra("date", date); + intent.putExtra("date", date.getTime()); ContextCompat.startForegroundService(context, intent); } + + public ScaleMeasurementDAO getScaleMeasurementDAO() { + return measurementDAO; + } + + public ScaleUserDAO getScaleUserDAO() { + return userDAO; + } } diff --git a/android_app/app/src/main/java/com/health/openscale/core/database/ScaleDatabaseProvider.java b/android_app/app/src/main/java/com/health/openscale/core/database/ScaleDatabaseProvider.java index 01837785..470cf2a0 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/database/ScaleDatabaseProvider.java +++ b/android_app/app/src/main/java/com/health/openscale/core/database/ScaleDatabaseProvider.java @@ -27,9 +27,7 @@ import com.health.openscale.BuildConfig; import com.health.openscale.core.OpenScale; import com.health.openscale.core.datatypes.ScaleMeasurement; -import java.util.Calendar; import java.util.Date; -import java.util.List; import timber.log.Timber; @@ -128,25 +126,48 @@ public class ScaleDatabaseProvider extends android.content.ContentProvider { @Override public Uri insert(Uri uri, ContentValues values) { - throw new UnsupportedOperationException("Not supported"); - } + Date date = new Date(values.getAsLong("datetime")); + float weight = values.getAsFloat("weight"); + int userId = values.getAsInteger("userId"); + + ScaleMeasurement scaleMeasurement = new ScaleMeasurement(); + + scaleMeasurement.setUserId(userId); + scaleMeasurement.setWeight(weight); + scaleMeasurement.setDateTime(date); + + ScaleMeasurementDAO measurementDAO = OpenScale.getInstance().getScaleMeasurementDAO(); + + if (measurementDAO.insert(scaleMeasurement) == -1) { + update(uri, values, "", new String[]{}); + } + + return null; + }; @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { - Calendar cal = Calendar.getInstance(); - cal.setTime(new Date((long)values.get("datetime"))); + Date date = new Date(values.getAsLong("datetime")); + float weight = values.getAsFloat("weight"); + int userId = values.getAsInteger("userId"); - List measurementList = OpenScale.getInstance().getScaleDataOfDay(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)); + ScaleMeasurement scaleMeasurement = new ScaleMeasurement(); - if (!measurementList.isEmpty()) { - ScaleMeasurement measurement = measurementList.get(0); + scaleMeasurement.setWeight(weight); + scaleMeasurement.setDateTime(date); - float calories = (float)values.get("calories"); - measurement.setCalories(calories); + ScaleMeasurementDAO measurementDAO = OpenScale.getInstance().getScaleMeasurementDAO(); + + ScaleMeasurement databaseMeasurement = measurementDAO.get(date, userId); + + if (databaseMeasurement != null) { + databaseMeasurement.merge(scaleMeasurement); + databaseMeasurement.setEnabled(true); + + measurementDAO.update(databaseMeasurement); - OpenScale.getInstance().updateScaleData(measurement); return 1; } else { Timber.e("no measurement for an update found"); diff --git a/android_app/app/src/main/java/com/health/openscale/core/database/ScaleMeasurementDAO.java b/android_app/app/src/main/java/com/health/openscale/core/database/ScaleMeasurementDAO.java index 2a03a6be..c02fcb86 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/database/ScaleMeasurementDAO.java +++ b/android_app/app/src/main/java/com/health/openscale/core/database/ScaleMeasurementDAO.java @@ -16,11 +16,6 @@ package com.health.openscale.core.database; -import androidx.room.Dao; -import androidx.room.Insert; -import androidx.room.OnConflictStrategy; -import androidx.room.Query; -import androidx.room.Update; import android.database.Cursor; import com.health.openscale.core.datatypes.ScaleMeasurement; @@ -28,12 +23,18 @@ import com.health.openscale.core.datatypes.ScaleMeasurement; import java.util.Date; import java.util.List; +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Query; +import androidx.room.Update; + @Dao public interface ScaleMeasurementDAO { - @Query("SELECT * FROM scaleMeasurements WHERE datetime = :datetime AND userId = :userId AND enabled = 1") + @Query("SELECT * FROM scaleMeasurements WHERE datetime = :datetime AND userId = :userId") ScaleMeasurement get(Date datetime, int userId); - @Query("SELECT * FROM scaleMeasurements WHERE id = :id AND enabled = 1") + @Query("SELECT * FROM scaleMeasurements WHERE id = :id") ScaleMeasurement get(int id); @Query("SELECT * FROM scaleMeasurements WHERE datetime < (SELECT datetime FROM scaleMeasurements WHERE id = :id) AND userId = :userId AND enabled = 1 ORDER BY datetime DESC LIMIT 0,1")