mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-30 19:49:59 +02:00
replaced insert / update operation of SQL scale database with Room library operations
This commit is contained in:
@@ -26,8 +26,12 @@ import android.support.v4.app.Fragment;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.health.openscale.R;
|
||||
import com.health.openscale.core.alarm.AlarmHandler;
|
||||
import com.health.openscale.core.bluetooth.BluetoothCommunication;
|
||||
import com.health.openscale.core.bodymetric.EstimatedFatMetric;
|
||||
import com.health.openscale.core.bodymetric.EstimatedLBWMetric;
|
||||
import com.health.openscale.core.bodymetric.EstimatedWaterMetric;
|
||||
import com.health.openscale.core.database.AppDatabase;
|
||||
import com.health.openscale.core.database.ScaleDatabase;
|
||||
import com.health.openscale.core.database.ScaleMeasurementDAO;
|
||||
@@ -60,7 +64,7 @@ public class OpenScale {
|
||||
private ScaleUserDAO userDAO;
|
||||
private ScaleDatabase scaleDB;
|
||||
private ScaleUserDatabase scaleUserDB;
|
||||
private ArrayList<ScaleData> scaleDataList;
|
||||
private List<ScaleData> scaleDataList;
|
||||
|
||||
private BluetoothCommunication btCom;
|
||||
private String btDeviceName;
|
||||
@@ -144,7 +148,7 @@ public class OpenScale {
|
||||
userDAO.update(user);
|
||||
}
|
||||
|
||||
public ArrayList<ScaleData> getScaleDataList() {
|
||||
public List<ScaleData> getScaleDataList() {
|
||||
return scaleDataList;
|
||||
}
|
||||
|
||||
@@ -155,35 +159,13 @@ public class OpenScale {
|
||||
}
|
||||
|
||||
public int addScaleData(final ScaleData scaleData) {
|
||||
|
||||
AsyncTask.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Log.d("TEST", "ADD START");
|
||||
|
||||
measurementDAO.insert(scaleData);
|
||||
|
||||
Log.d("TEST", "ADD END");
|
||||
|
||||
Log.d("TEST", "READ ALL");
|
||||
|
||||
List<ScaleData> listScaleData = measurementDAO.getAll();
|
||||
|
||||
for(ScaleData nextScaleData : listScaleData) {
|
||||
Log.d("TEST", "DB : " + nextScaleData.getDateTime() + " Weight: " + nextScaleData.getWeight());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return -1;
|
||||
/* SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
|
||||
if (scaleData.getUserId() == -1) {
|
||||
if (prefs.getBoolean("smartUserAssign", false)) {
|
||||
scaleData.setUserId(getSmartUserAssignment(scaleData.getWeight(), 15.0f));
|
||||
} else {
|
||||
scaleData.setUserId(getSelectedScaleUser().id);
|
||||
scaleData.setUserId(getSelectedScaleUser().getId());
|
||||
}
|
||||
|
||||
// don't add scale data if no user is selected
|
||||
@@ -210,16 +192,17 @@ public class OpenScale {
|
||||
scaleData.setFat(fatMetric.getFat(getScaleUser(scaleData.getUserId()), scaleData));
|
||||
}
|
||||
|
||||
if (scaleDB.insertEntry(scaleData)) {
|
||||
if (measurementDAO.get(scaleData.getDateTime(), scaleData.getUserId()) == null) {
|
||||
measurementDAO.insert(scaleData);
|
||||
ScaleUser scaleUser = getScaleUser(scaleData.getUserId());
|
||||
|
||||
String infoText = String.format(context.getString(R.string.info_new_data_added), scaleData.getConvertedWeight(scaleUser.scaleUnit), scaleUser.UNIT_STRING[scaleUser.scaleUnit], dateTimeFormat.format(scaleData.getDateTime()), scaleUser.userName);
|
||||
String infoText = String.format(context.getString(R.string.info_new_data_added), scaleData.getConvertedWeight(scaleUser.getScaleUnit()), scaleUser.UNIT_STRING[scaleUser.getScaleUnit()], dateTimeFormat.format(scaleData.getDateTime()), scaleUser.getUserName());
|
||||
Toast.makeText(context, infoText, Toast.LENGTH_LONG).show();
|
||||
alarmHandler.entryChanged(context, scaleData);
|
||||
updateScaleData();
|
||||
}
|
||||
|
||||
return scaleData.getUserId();*/
|
||||
return scaleData.getUserId();
|
||||
}
|
||||
|
||||
private int getSmartUserAssignment(float weight, float range) {
|
||||
@@ -259,7 +242,7 @@ public class OpenScale {
|
||||
}
|
||||
|
||||
public void updateScaleData(ScaleData scaleData) {
|
||||
scaleDB.updateEntry(scaleData.getId(), scaleData);
|
||||
measurementDAO.update(scaleData);
|
||||
alarmHandler.entryChanged(context, scaleData);
|
||||
|
||||
updateScaleData();
|
||||
@@ -305,7 +288,7 @@ public class OpenScale {
|
||||
|
||||
newScaleData.setUserId(getSelectedScaleUser().getId());
|
||||
|
||||
scaleDB.insertEntry(newScaleData);
|
||||
measurementDAO.insert(newScaleData);
|
||||
|
||||
line = csvReader.readLine();
|
||||
}
|
||||
@@ -414,7 +397,7 @@ public class OpenScale {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
int selectedUserId = prefs.getInt("selectedUserId", -1);
|
||||
|
||||
scaleDataList = scaleDB.getScaleDataList(selectedUserId);
|
||||
scaleDataList = measurementDAO.getAll(selectedUserId);
|
||||
|
||||
fragment.updateOnView(scaleDataList);
|
||||
}
|
||||
@@ -424,7 +407,7 @@ public class OpenScale {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
int selectedUserId = prefs.getInt("selectedUserId", -1);
|
||||
|
||||
scaleDataList = scaleDB.getScaleDataList(selectedUserId);
|
||||
scaleDataList = measurementDAO.getAll(selectedUserId);
|
||||
|
||||
for (FragmentUpdateListener fragment : fragmentList) {
|
||||
if (fragment != null) {
|
||||
|
@@ -24,12 +24,16 @@ import android.arch.persistence.room.Update;
|
||||
|
||||
import com.health.openscale.core.datatypes.ScaleData;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Dao
|
||||
public interface ScaleMeasurementDAO {
|
||||
@Query("SELECT * FROM scaledata")
|
||||
List<ScaleData> getAll();
|
||||
@Query("SELECT * FROM scaledata WHERE datetime = :datetime AND userId = :userId")
|
||||
ScaleData get(Date datetime, int userId);
|
||||
|
||||
@Query("SELECT * FROM scaledata WHERE userId = :userId")
|
||||
List<ScaleData> getAll(int userId);
|
||||
|
||||
@Query("SELECT * FROM scaledata WHERE id IS :id")
|
||||
ScaleData loadById(int id);
|
||||
|
@@ -31,10 +31,10 @@ public interface ScaleUserDAO {
|
||||
@Query("SELECT * FROM scaleuser")
|
||||
List<ScaleUser> getAll();
|
||||
|
||||
@Query("SELECT * FROM scaleuser WHERE id IS :id")
|
||||
@Query("SELECT * FROM scaleuser WHERE id = :id")
|
||||
ScaleUser getById(int id);
|
||||
|
||||
@Query("SELECT * FROM scaleuser WHERE id IS :id")
|
||||
@Query("SELECT * FROM scaleuser WHERE id = :id")
|
||||
ScaleUser loadById(int id);
|
||||
|
||||
@Insert
|
||||
|
@@ -18,8 +18,8 @@ package com.health.openscale.gui.fragments;
|
||||
|
||||
import com.health.openscale.core.datatypes.ScaleData;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public interface FragmentUpdateListener {
|
||||
public void updateOnView(ArrayList<ScaleData> scaleDataList);
|
||||
public void updateOnView(List<ScaleData> scaleDataList);
|
||||
}
|
||||
|
@@ -186,7 +186,7 @@ public class GraphFragment extends Fragment implements FragmentUpdateListener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateOnView(ArrayList<ScaleData> scaleDataList)
|
||||
public void updateOnView(List<ScaleData> scaleDataList)
|
||||
{
|
||||
generateGraphs();
|
||||
}
|
||||
|
@@ -174,7 +174,7 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateOnView(ArrayList<ScaleData> scaleDataList) {
|
||||
public void updateOnView(List<ScaleData> scaleDataList) {
|
||||
if (scaleDataList.isEmpty()) {
|
||||
lastScaleData = new ScaleData();
|
||||
} else if (userSelectedData != null) {
|
||||
@@ -237,7 +237,7 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
|
||||
}
|
||||
|
||||
|
||||
private void updateLastLineChart(ArrayList<ScaleData> scaleDataList) {
|
||||
private void updateLastLineChart(List<ScaleData> scaleDataList) {
|
||||
List<AxisValue> axisValues = new ArrayList<AxisValue>();
|
||||
|
||||
List<PointValue> valuesWeight = new ArrayList<PointValue>();
|
||||
|
@@ -33,8 +33,8 @@ 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.List;
|
||||
|
||||
public class StatisticsFragment extends Fragment implements FragmentUpdateListener {
|
||||
|
||||
@@ -88,7 +88,7 @@ public class StatisticsFragment extends Fragment implements FragmentUpdateListen
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateOnView(ArrayList<ScaleData> scaleDataList) {
|
||||
public void updateOnView(List<ScaleData> scaleDataList) {
|
||||
if (scaleDataList.isEmpty()) {
|
||||
lastScaleData = new ScaleData();
|
||||
} else {
|
||||
@@ -105,7 +105,7 @@ public class StatisticsFragment extends Fragment implements FragmentUpdateListen
|
||||
updateGoal(scaleDataList);
|
||||
}
|
||||
|
||||
private void updateGoal(ArrayList<ScaleData> scaleDataList) {
|
||||
private void updateGoal(List<ScaleData> scaleDataList) {
|
||||
ScaleData goalScaleData = new ScaleData();
|
||||
goalScaleData.setConvertedWeight(currentScaleUser.getGoalWeight(), currentScaleUser.getScaleUnit());
|
||||
|
||||
@@ -157,7 +157,7 @@ public class StatisticsFragment extends Fragment implements FragmentUpdateListen
|
||||
); // currentScaleUser.goalDate
|
||||
}
|
||||
|
||||
private void updateStatistics(ArrayList<ScaleData> scaleDataList) {
|
||||
private void updateStatistics(List<ScaleData> scaleDataList) {
|
||||
Calendar histDate = Calendar.getInstance();
|
||||
Calendar weekPastDate = Calendar.getInstance();
|
||||
Calendar monthPastDate = Calendar.getInstance();
|
||||
|
@@ -68,6 +68,7 @@ import com.health.openscale.gui.views.WeightMeasurementView;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import lecho.lib.hellocharts.util.ChartUtils;
|
||||
|
||||
@@ -129,7 +130,7 @@ public class TableFragment extends Fragment implements FragmentUpdateListener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateOnView(ArrayList<ScaleData> scaleDataList)
|
||||
public void updateOnView(List<ScaleData> scaleDataList)
|
||||
{
|
||||
tableDataView.setAdapter(new ListViewAdapter(new ArrayList<HashMap<Integer, String>>())); // delete all data in the table with an empty adapter array list
|
||||
|
||||
|
Reference in New Issue
Block a user