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:
@@ -695,7 +695,15 @@ public class OpenScale {
|
|||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
intent.setComponent(new ComponentName("com.health.openscale.sync", "com.health.openscale.sync.core.service.SyncService"));
|
intent.setComponent(new ComponentName("com.health.openscale.sync", "com.health.openscale.sync.core.service.SyncService"));
|
||||||
intent.putExtra("mode", "delete");
|
intent.putExtra("mode", "delete");
|
||||||
intent.putExtra("date", date);
|
intent.putExtra("date", date.getTime());
|
||||||
ContextCompat.startForegroundService(context, intent);
|
ContextCompat.startForegroundService(context, intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ScaleMeasurementDAO getScaleMeasurementDAO() {
|
||||||
|
return measurementDAO;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScaleUserDAO getScaleUserDAO() {
|
||||||
|
return userDAO;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -27,9 +27,7 @@ import com.health.openscale.BuildConfig;
|
|||||||
import com.health.openscale.core.OpenScale;
|
import com.health.openscale.core.OpenScale;
|
||||||
import com.health.openscale.core.datatypes.ScaleMeasurement;
|
import com.health.openscale.core.datatypes.ScaleMeasurement;
|
||||||
|
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
@@ -128,25 +126,48 @@ public class ScaleDatabaseProvider extends android.content.ContentProvider {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Uri insert(Uri uri, ContentValues values) {
|
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
|
@Override
|
||||||
public int update(Uri uri, ContentValues values, String selection,
|
public int update(Uri uri, ContentValues values, String selection,
|
||||||
String[] selectionArgs) {
|
String[] selectionArgs) {
|
||||||
|
|
||||||
Calendar cal = Calendar.getInstance();
|
Date date = new Date(values.getAsLong("datetime"));
|
||||||
cal.setTime(new Date((long)values.get("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.setWeight(weight);
|
||||||
ScaleMeasurement measurement = measurementList.get(0);
|
scaleMeasurement.setDateTime(date);
|
||||||
|
|
||||||
float calories = (float)values.get("calories");
|
ScaleMeasurementDAO measurementDAO = OpenScale.getInstance().getScaleMeasurementDAO();
|
||||||
measurement.setCalories(calories);
|
|
||||||
|
ScaleMeasurement databaseMeasurement = measurementDAO.get(date, userId);
|
||||||
|
|
||||||
|
if (databaseMeasurement != null) {
|
||||||
|
databaseMeasurement.merge(scaleMeasurement);
|
||||||
|
databaseMeasurement.setEnabled(true);
|
||||||
|
|
||||||
|
measurementDAO.update(databaseMeasurement);
|
||||||
|
|
||||||
OpenScale.getInstance().updateScaleData(measurement);
|
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
Timber.e("no measurement for an update found");
|
Timber.e("no measurement for an update found");
|
||||||
|
@@ -16,11 +16,6 @@
|
|||||||
|
|
||||||
package com.health.openscale.core.database;
|
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 android.database.Cursor;
|
||||||
|
|
||||||
import com.health.openscale.core.datatypes.ScaleMeasurement;
|
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.Date;
|
||||||
import java.util.List;
|
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
|
@Dao
|
||||||
public interface ScaleMeasurementDAO {
|
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);
|
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);
|
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")
|
@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")
|
||||||
|
Reference in New Issue
Block a user