1
0
mirror of https://github.com/oliexdev/openScale.git synced 2025-08-29 11:10:35 +02:00

replaced completely old scale database with Room database

included migrating method
This commit is contained in:
OliE
2018-01-01 10:51:15 +01:00
parent 03534a12d3
commit 1ec1ae577b
7 changed files with 106 additions and 39 deletions

View File

@@ -51,6 +51,7 @@ import java.io.OutputStreamWriter;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
@@ -87,6 +88,7 @@ public class OpenScale {
measurementDAO = appDB.measurementDAO(); measurementDAO = appDB.measurementDAO();
userDAO = appDB.userDAO(); userDAO = appDB.userDAO();
migrateSQLtoRoom();
updateScaleData(); updateScaleData();
} }
@@ -98,6 +100,20 @@ public class OpenScale {
return instance; return instance;
} }
private void migrateSQLtoRoom() {
List<ScaleUser> 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<ScaleData> scaleDataList = scaleDB.getScaleDataList(user.getId());
measurementDAO.insertAll(scaleDataList);
}
}
}
public void addScaleUser(final ScaleUser user) public void addScaleUser(final ScaleUser user)
{ {
AsyncTask.execute(new Runnable() { AsyncTask.execute(new Runnable() {
@@ -115,7 +131,7 @@ public class OpenScale {
public ScaleUser getScaleUser(int userId) public ScaleUser getScaleUser(int userId)
{ {
return userDAO.getById(userId); return userDAO.get(userId);
} }
public ScaleUser getSelectedScaleUser() public ScaleUser getSelectedScaleUser()
@@ -130,7 +146,7 @@ public class OpenScale {
return scaleUser; return scaleUser;
} }
scaleUser = userDAO.getById(selectedUserId); scaleUser = userDAO.get(selectedUserId);
} catch (Exception e) { } catch (Exception e) {
Toast.makeText(context, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show(); Toast.makeText(context, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
} }
@@ -140,7 +156,7 @@ public class OpenScale {
public void deleteScaleUser(int id) public void deleteScaleUser(int id)
{ {
userDAO.delete(userDAO.getById(id)); userDAO.delete(userDAO.get(id));
} }
public void updateScaleUser(ScaleUser user) 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) { public int addScaleData(final ScaleData scaleData) {
@@ -206,22 +228,22 @@ public class OpenScale {
} }
private int getSmartUserAssignment(float weight, float range) { private int getSmartUserAssignment(float weight, float range) {
List<ScaleUser> scaleUser = getScaleUserList(); List<ScaleUser> scaleUsers = getScaleUserList();
Map<Float, Integer> inRangeWeights = new TreeMap<>(); Map<Float, Integer> inRangeWeights = new TreeMap<>();
for (int i = 0; i < scaleUser.size(); i++) { for (int i = 0; i < scaleUsers.size(); i++) {
ArrayList<ScaleData> scaleUserData = scaleDB.getScaleDataList(scaleUser.get(i).getId()); List<ScaleData> scaleUserData = measurementDAO.getAll(scaleUsers.get(i).getId());
float lastWeight = 0; float lastWeight = 0;
if (scaleUserData.size() > 0) { if (scaleUserData.size() > 0) {
lastWeight = scaleUserData.get(0).getWeight(); lastWeight = scaleUserData.get(0).getWeight();
} else { } else {
lastWeight = scaleUser.get(i).getInitialWeight(); lastWeight = scaleUsers.get(i).getInitialWeight();
} }
if ((lastWeight - range) <= weight && (lastWeight + range) >= weight) { 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(); updateScaleData();
} }
public void deleteScaleData(long id) public void deleteScaleData(int id)
{ {
scaleDB.deleteEntry(id); measurementDAO.delete(id);
updateScaleData(); updateScaleData();
} }
@@ -339,7 +361,7 @@ public class OpenScale {
public void clearScaleData(int userId) { public void clearScaleData(int userId) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putInt("uniqueNumber", 0x00).commit(); prefs.edit().putInt("uniqueNumber", 0x00).commit();
scaleDB.clearScaleData(userId); measurementDAO.deleteAll(userId);
updateScaleData(); updateScaleData();
} }
@@ -348,21 +370,47 @@ public class OpenScale {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int selectedUserId = prefs.getInt("selectedUserId", -1); 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();
} }
public ArrayList<ScaleData> getScaleDataOfMonth(int year, int month) { return numOfMonth;
}
public List<ScaleData> getScaleDataOfMonth(int year, int month) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int selectedUserId = prefs.getInt("selectedUserId", -1); 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<ScaleData> getScaleDataOfYear(int year) { public List<ScaleData> getScaleDataOfYear(int year) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int selectedUserId = prefs.getInt("selectedUserId", -1); 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) { public boolean startSearchingForBluetooth(String deviceName, Handler callbackBtHandler) {

View File

@@ -17,7 +17,6 @@
package com.health.openscale.core.database; package com.health.openscale.core.database;
import android.arch.persistence.room.Dao; import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert; import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query; import android.arch.persistence.room.Query;
import android.arch.persistence.room.Update; import android.arch.persistence.room.Update;
@@ -29,24 +28,36 @@ import java.util.List;
@Dao @Dao
public interface ScaleMeasurementDAO { 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); 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<ScaleData> getAll(int userId); List<ScaleData> getAll(int userId);
@Query("SELECT * FROM scaledata WHERE id IS :id") @Query("SELECT * FROM scaledata WHERE datetime >= :startYear AND datetime < :endYear AND userId = :userId AND enabled = 1 ORDER BY datetime DESC")
ScaleData loadById(int id); List<ScaleData> getAllInRange(Date startYear, Date endYear, int userId);
@Insert @Insert
void insert(ScaleData measurement); void insert(ScaleData measurement);
@Insert @Insert
void insertAll(ScaleData... measurements); void insertAll(List<ScaleData> measurementList);
@Update @Update
void update(ScaleData measurement); void update(ScaleData measurement);
@Delete @Query("UPDATE scaledata SET enabled = 0 WHERE id = :id")
void delete(ScaleData measurement); void delete(int id);
@Query("DELETE FROM scaledata WHERE userId = :userId")
void deleteAll(int userId);
} }

View File

@@ -32,16 +32,13 @@ public interface ScaleUserDAO {
List<ScaleUser> getAll(); List<ScaleUser> getAll();
@Query("SELECT * FROM scaleuser WHERE id = :id") @Query("SELECT * FROM scaleuser WHERE id = :id")
ScaleUser getById(int id); ScaleUser get(int id);
@Query("SELECT * FROM scaleuser WHERE id = :id")
ScaleUser loadById(int id);
@Insert @Insert
void insert(ScaleUser user); void insert(ScaleUser user);
@Insert @Insert
void insertAll(ScaleUser... users); void insertAll(List<ScaleUser> userList);
@Update @Update
void update(ScaleUser user); void update(ScaleUser user);

View File

@@ -37,14 +37,14 @@ public class ScaleUserDatabase extends SQLiteOpenHelper {
private static final String TABLE_NAME = "scaleuserdata"; private static final String TABLE_NAME = "scaleuserdata";
private static final String COLUMN_NAME_ID = "id"; 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_BIRTHDAY = "birthday";
private static final String COLUMN_NAME_BODY_HEIGHT = "bodyHeight"; private static final String COLUMN_NAME_BODY_HEIGHT = "body_height";
private static final String COLUMN_NAME_SCALE_UNIT = "scaleUnit"; private static final String COLUMN_NAME_SCALE_UNIT = "scale_unit";
private static final String COLUMN_NAME_GENDER = "gender"; private static final String COLUMN_NAME_GENDER = "gender";
private static final String COLUMN_NAME_INITIAL_WEIGHT = "initial_weight"; 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_WEIGHT = "goal_weight";
private static final String COLUMN_NAME_GOAL_DATE = "goalDate"; private static final String COLUMN_NAME_GOAL_DATE = "goal_date";
private static final String SQL_CREATE_ENTRIES = private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + TABLE_NAME + " (" + "CREATE TABLE " + TABLE_NAME + " (" +

View File

@@ -32,6 +32,8 @@ public class ScaleData {
@ColumnInfo(name = "userId") @ColumnInfo(name = "userId")
private int userId; private int userId;
@ColumnInfo(name = "enabled")
private boolean enabled;
@ColumnInfo(name = "datetime") @ColumnInfo(name = "datetime")
private Date dateTime; private Date dateTime;
@ColumnInfo(name = "weight") @ColumnInfo(name = "weight")
@@ -56,6 +58,7 @@ public class ScaleData {
public ScaleData() public ScaleData()
{ {
userId = -1; userId = -1;
enabled = true;
dateTime = new Date(); dateTime = new Date();
weight = 0.0f; weight = 0.0f;
fat = 0.0f; fat = 0.0f;
@@ -84,6 +87,14 @@ public class ScaleData {
this.userId = user_id; this.userId = user_id;
} }
public boolean getEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public Date getDateTime() { public Date getDateTime() {
return dateTime; return dateTime;
} }

View File

@@ -464,7 +464,7 @@ public class DataEntryActivity extends Activity {
} }
void deleteMeasurement() { void deleteMeasurement() {
long delId = id; int delId = id;
boolean hasNext = moveLeft(); boolean hasNext = moveLeft();

View File

@@ -85,8 +85,8 @@ public class GraphFragment extends Fragment implements FragmentUpdateListener {
private Calendar calYears; private Calendar calYears;
private Calendar calLastSelected; private Calendar calLastSelected;
private ArrayList<ScaleData> scaleDataList; private List<ScaleData> scaleDataList;
private ArrayList<ScaleData> pointIndexScaleDataList; private List<ScaleData> pointIndexScaleDataList;
public GraphFragment() { public GraphFragment() {
calYears = Calendar.getInstance(); calYears = Calendar.getInstance();