1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-29 19:20:36 +02:00

manipulate directly database on scale database provider

This commit is contained in:
oliexdev
2019-03-17 07:19:00 +01:00
parent d29daea2e1
commit d9fa6a7026
3 changed files with 50 additions and 20 deletions

View File

@@ -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;
}
}

View File

@@ -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<ScaleMeasurement> 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");

View File

@@ -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")