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 dbe04ac8..ecf3acc5 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 @@ -51,6 +51,7 @@ import java.io.OutputStreamWriter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Calendar; import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -87,6 +88,7 @@ public class OpenScale { measurementDAO = appDB.measurementDAO(); userDAO = appDB.userDAO(); + migrateSQLtoRoom(); updateScaleData(); } @@ -98,6 +100,20 @@ public class OpenScale { return instance; } + private void migrateSQLtoRoom() { + List scaleUserList = scaleUserDB.getScaleUserList(); + + if (scaleDB.getReadableDatabase().getVersion() == 6 && userDAO.getAll().isEmpty() && !scaleUserList.isEmpty()) { + Toast.makeText(context, "Migrating old SQL database to new database format...", Toast.LENGTH_LONG).show(); + userDAO.insertAll(scaleUserList); + + for (ScaleUser user : scaleUserList) { + List scaleDataList = scaleDB.getScaleDataList(user.getId()); + measurementDAO.insertAll(scaleDataList); + } + } + } + public void addScaleUser(final ScaleUser user) { AsyncTask.execute(new Runnable() { @@ -115,7 +131,7 @@ public class OpenScale { public ScaleUser getScaleUser(int userId) { - return userDAO.getById(userId); + return userDAO.get(userId); } public ScaleUser getSelectedScaleUser() @@ -130,7 +146,7 @@ public class OpenScale { return scaleUser; } - scaleUser = userDAO.getById(selectedUserId); + scaleUser = userDAO.get(selectedUserId); } catch (Exception e) { Toast.makeText(context, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show(); } @@ -140,7 +156,7 @@ public class OpenScale { public void deleteScaleUser(int id) { - userDAO.delete(userDAO.getById(id)); + userDAO.delete(userDAO.get(id)); } public void updateScaleUser(ScaleUser user) @@ -153,9 +169,15 @@ public class OpenScale { } - public ScaleData[] getTupleScaleData(long id) + public ScaleData[] getTupleScaleData(int id) { - return scaleDB.getTupleDataEntry(getSelectedScaleUser().getId(), id); + ScaleData[] tupleScaleData = new ScaleData[3]; + + tupleScaleData[0] = measurementDAO.getPrevious(id, getSelectedScaleUser().getId()); + tupleScaleData[1] = measurementDAO.get(id); + tupleScaleData[2] = measurementDAO.getNext(id, getSelectedScaleUser().getId()); + + return tupleScaleData; } public int addScaleData(final ScaleData scaleData) { @@ -206,22 +228,22 @@ public class OpenScale { } private int getSmartUserAssignment(float weight, float range) { - List scaleUser = getScaleUserList(); + List scaleUsers = getScaleUserList(); Map inRangeWeights = new TreeMap<>(); - for (int i = 0; i < scaleUser.size(); i++) { - ArrayList scaleUserData = scaleDB.getScaleDataList(scaleUser.get(i).getId()); + for (int i = 0; i < scaleUsers.size(); i++) { + List scaleUserData = measurementDAO.getAll(scaleUsers.get(i).getId()); float lastWeight = 0; if (scaleUserData.size() > 0) { lastWeight = scaleUserData.get(0).getWeight(); } else { - lastWeight = scaleUser.get(i).getInitialWeight(); + lastWeight = scaleUsers.get(i).getInitialWeight(); } if ((lastWeight - range) <= weight && (lastWeight + range) >= weight) { - inRangeWeights.put(Math.abs(lastWeight - weight), scaleUser.get(i).getId()); + inRangeWeights.put(Math.abs(lastWeight - weight), scaleUsers.get(i).getId()); } } @@ -248,9 +270,9 @@ public class OpenScale { updateScaleData(); } - public void deleteScaleData(long id) + public void deleteScaleData(int id) { - scaleDB.deleteEntry(id); + measurementDAO.delete(id); updateScaleData(); } @@ -339,7 +361,7 @@ public class OpenScale { public void clearScaleData(int userId) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); prefs.edit().putInt("uniqueNumber", 0x00).commit(); - scaleDB.clearScaleData(userId); + measurementDAO.deleteAll(userId); updateScaleData(); } @@ -348,21 +370,47 @@ public class OpenScale { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); int selectedUserId = prefs.getInt("selectedUserId", -1); - return scaleDB.getCountsOfAllMonth(selectedUserId, year); + int [] numOfMonth = new int[12]; + + Calendar startCalender = Calendar.getInstance(); + Calendar endCalender = Calendar.getInstance(); + + for (int i=0; i<12; i++) { + startCalender.set(year, i, 1, 0, 0, 0); + endCalender.set(year, i, 1, 0, 0, 0); + endCalender.add(Calendar.MONTH, 1); + + numOfMonth[i] = measurementDAO.getAllInRange(startCalender.getTime(), endCalender.getTime(), selectedUserId).size(); + } + + return numOfMonth; } - public ArrayList getScaleDataOfMonth(int year, int month) { + public List getScaleDataOfMonth(int year, int month) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); int selectedUserId = prefs.getInt("selectedUserId", -1); - return scaleDB.getScaleDataOfMonth(selectedUserId, year, month); + Calendar startCalender = Calendar.getInstance(); + Calendar endCalender = Calendar.getInstance(); + + startCalender.set(year, month, 1, 0, 0, 0); + endCalender.set(year, month, 1, 0, 0, 0); + endCalender.add(Calendar.MONTH, 1); + + return measurementDAO.getAllInRange(startCalender.getTime(), endCalender.getTime(), selectedUserId); } - public ArrayList getScaleDataOfYear(int year) { + public List getScaleDataOfYear(int year) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); int selectedUserId = prefs.getInt("selectedUserId", -1); - return scaleDB.getScaleDataOfYear(selectedUserId, year); + Calendar startCalender = Calendar.getInstance(); + Calendar endCalender = Calendar.getInstance(); + + startCalender.set(year, Calendar.JANUARY, 1, 0, 0, 0); + endCalender.set(year+1, Calendar.JANUARY, 1, 0, 0, 0); + + return measurementDAO.getAllInRange(startCalender.getTime(), endCalender.getTime(), selectedUserId); } public boolean startSearchingForBluetooth(String deviceName, Handler callbackBtHandler) { 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 0fde1780..13c41ad6 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 @@ -17,7 +17,6 @@ package com.health.openscale.core.database; import android.arch.persistence.room.Dao; -import android.arch.persistence.room.Delete; import android.arch.persistence.room.Insert; import android.arch.persistence.room.Query; import android.arch.persistence.room.Update; @@ -29,24 +28,36 @@ import java.util.List; @Dao public interface ScaleMeasurementDAO { - @Query("SELECT * FROM scaledata WHERE datetime = :datetime AND userId = :userId") + @Query("SELECT * FROM scaledata WHERE datetime = :datetime AND userId = :userId AND enabled = 1") ScaleData get(Date datetime, int userId); - @Query("SELECT * FROM scaledata WHERE userId = :userId") + @Query("SELECT * FROM scaledata WHERE id = :id AND enabled = 1") + ScaleData get(int id); + + @Query("SELECT * FROM scaledata WHERE datetime < (SELECT datetime FROM scaledata WHERE id = :id) AND userId = :userId AND enabled = 1 ORDER BY datetime DESC LIMIT 0,1") + ScaleData getPrevious(int id, int userId); + + @Query("SELECT * FROM scaledata WHERE datetime > (SELECT datetime FROM scaledata WHERE id = :id) AND userId = :userId AND enabled = 1 LIMIT 0,1") + ScaleData getNext(int id, int userId); + + @Query("SELECT * FROM scaledata WHERE userId = :userId AND enabled = 1 ORDER BY datetime DESC") List getAll(int userId); - @Query("SELECT * FROM scaledata WHERE id IS :id") - ScaleData loadById(int id); + @Query("SELECT * FROM scaledata WHERE datetime >= :startYear AND datetime < :endYear AND userId = :userId AND enabled = 1 ORDER BY datetime DESC") + List getAllInRange(Date startYear, Date endYear, int userId); @Insert void insert(ScaleData measurement); @Insert - void insertAll(ScaleData... measurements); + void insertAll(List measurementList); @Update void update(ScaleData measurement); - @Delete - void delete(ScaleData measurement); + @Query("UPDATE scaledata SET enabled = 0 WHERE id = :id") + void delete(int id); + + @Query("DELETE FROM scaledata WHERE userId = :userId") + void deleteAll(int userId); } diff --git a/android_app/app/src/main/java/com/health/openscale/core/database/ScaleUserDAO.java b/android_app/app/src/main/java/com/health/openscale/core/database/ScaleUserDAO.java index 1f2baa9a..19e79e9e 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/database/ScaleUserDAO.java +++ b/android_app/app/src/main/java/com/health/openscale/core/database/ScaleUserDAO.java @@ -32,16 +32,13 @@ public interface ScaleUserDAO { List getAll(); @Query("SELECT * FROM scaleuser WHERE id = :id") - ScaleUser getById(int id); - - @Query("SELECT * FROM scaleuser WHERE id = :id") - ScaleUser loadById(int id); + ScaleUser get(int id); @Insert void insert(ScaleUser user); @Insert - void insertAll(ScaleUser... users); + void insertAll(List userList); @Update void update(ScaleUser user); diff --git a/android_app/app/src/main/java/com/health/openscale/core/database/ScaleUserDatabase.java b/android_app/app/src/main/java/com/health/openscale/core/database/ScaleUserDatabase.java index 41233380..91d1865d 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/database/ScaleUserDatabase.java +++ b/android_app/app/src/main/java/com/health/openscale/core/database/ScaleUserDatabase.java @@ -37,14 +37,14 @@ public class ScaleUserDatabase extends SQLiteOpenHelper { private static final String TABLE_NAME = "scaleuserdata"; private static final String COLUMN_NAME_ID = "id"; - private static final String COLUMN_NAME_USER_NAME = "userName"; + private static final String COLUMN_NAME_USER_NAME = "user_name"; private static final String COLUMN_NAME_BIRTHDAY = "birthday"; - private static final String COLUMN_NAME_BODY_HEIGHT = "bodyHeight"; - private static final String COLUMN_NAME_SCALE_UNIT = "scaleUnit"; + private static final String COLUMN_NAME_BODY_HEIGHT = "body_height"; + private static final String COLUMN_NAME_SCALE_UNIT = "scale_unit"; private static final String COLUMN_NAME_GENDER = "gender"; private static final String COLUMN_NAME_INITIAL_WEIGHT = "initial_weight"; - private static final String COLUMN_NAME_GOAL_WEIGHT = "goalWeight"; - private static final String COLUMN_NAME_GOAL_DATE = "goalDate"; + private static final String COLUMN_NAME_GOAL_WEIGHT = "goal_weight"; + private static final String COLUMN_NAME_GOAL_DATE = "goal_date"; private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " + TABLE_NAME + " (" + diff --git a/android_app/app/src/main/java/com/health/openscale/core/datatypes/ScaleData.java b/android_app/app/src/main/java/com/health/openscale/core/datatypes/ScaleData.java index 0579e03b..b096ffa8 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/datatypes/ScaleData.java +++ b/android_app/app/src/main/java/com/health/openscale/core/datatypes/ScaleData.java @@ -32,6 +32,8 @@ public class ScaleData { @ColumnInfo(name = "userId") private int userId; + @ColumnInfo(name = "enabled") + private boolean enabled; @ColumnInfo(name = "datetime") private Date dateTime; @ColumnInfo(name = "weight") @@ -56,6 +58,7 @@ public class ScaleData { public ScaleData() { userId = -1; + enabled = true; dateTime = new Date(); weight = 0.0f; fat = 0.0f; @@ -84,6 +87,14 @@ public class ScaleData { this.userId = user_id; } + public boolean getEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + public Date getDateTime() { return dateTime; } diff --git a/android_app/app/src/main/java/com/health/openscale/gui/activities/DataEntryActivity.java b/android_app/app/src/main/java/com/health/openscale/gui/activities/DataEntryActivity.java index 056e10a6..1f318b9d 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/activities/DataEntryActivity.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/activities/DataEntryActivity.java @@ -464,7 +464,7 @@ public class DataEntryActivity extends Activity { } void deleteMeasurement() { - long delId = id; + int delId = id; boolean hasNext = moveLeft(); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/fragments/GraphFragment.java b/android_app/app/src/main/java/com/health/openscale/gui/fragments/GraphFragment.java index 84bd1861..bc60d0d6 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/fragments/GraphFragment.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/fragments/GraphFragment.java @@ -85,8 +85,8 @@ public class GraphFragment extends Fragment implements FragmentUpdateListener { private Calendar calYears; private Calendar calLastSelected; - private ArrayList scaleDataList; - private ArrayList pointIndexScaleDataList; + private List scaleDataList; + private List pointIndexScaleDataList; public GraphFragment() { calYears = Calendar.getInstance();