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

replace SQL scale user database with user DAO by Room library

This commit is contained in:
OliE
2017-12-31 13:33:46 +01:00
parent 525e6abe23
commit dbd67c741b
5 changed files with 43 additions and 44 deletions

View File

@@ -47,7 +47,6 @@ 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.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
@@ -80,7 +79,7 @@ public class OpenScale {
alarmHandler = new AlarmHandler(); alarmHandler = new AlarmHandler();
btCom = null; btCom = null;
fragmentList = new ArrayList<>(); fragmentList = new ArrayList<>();
appDB = Room.databaseBuilder(context, AppDatabase.class, "openScaleDatabase").build(); appDB = Room.databaseBuilder(context, AppDatabase.class, "openScaleDatabase").allowMainThreadQueries().build();
measurementDAO = appDB.measurementDAO(); measurementDAO = appDB.measurementDAO();
userDAO = appDB.userDAO(); userDAO = appDB.userDAO();
@@ -95,30 +94,24 @@ public class OpenScale {
return instance; return instance;
} }
public void addScaleUser(String name, Date birthday, int body_height, int scale_unit, int gender, float initial_weight, float goal_weight, Date goal_date) public void addScaleUser(final ScaleUser user)
{ {
ScaleUser scaleUser = new ScaleUser(); AsyncTask.execute(new Runnable() {
@Override
scaleUser.setUserName(name); public void run() {
scaleUser.setBirthday(birthday); userDAO.insert(user);
scaleUser.setBodyHeight(body_height); }
scaleUser.setScaleUnit(scale_unit); });
scaleUser.setGender(gender);
scaleUser.setConvertedInitialWeight(initial_weight);
scaleUser.setGoalWeight(goal_weight);
scaleUser.setGoalDate(goal_date);
scaleUserDB.insertEntry(scaleUser);
} }
public ArrayList<ScaleUser> getScaleUserList() public List<ScaleUser> getScaleUserList()
{ {
return scaleUserDB.getScaleUserList(); return userDAO.getAll();
} }
public ScaleUser getScaleUser(int userId) public ScaleUser getScaleUser(int userId)
{ {
return scaleUserDB.getScaleUser(userId); return userDAO.getById(userId);
} }
public ScaleUser getSelectedScaleUser() public ScaleUser getSelectedScaleUser()
@@ -133,7 +126,7 @@ public class OpenScale {
return scaleUser; return scaleUser;
} }
scaleUser = scaleUserDB.getScaleUser(selectedUserId); scaleUser = userDAO.getById(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();
} }
@@ -143,27 +136,14 @@ public class OpenScale {
public void deleteScaleUser(int id) public void deleteScaleUser(int id)
{ {
scaleUserDB.deleteEntry(id); userDAO.delete(userDAO.getById(id));
} }
public void updateScaleUser(int id, String name, Date birthday, int body_height, int scale_unit, int gender, float initial_weight, float goal_weight, Date goal_date) public void updateScaleUser(ScaleUser user)
{ {
ScaleUser scaleUser = new ScaleUser(); userDAO.update(user);
scaleUser.setId(id);
scaleUser.setUserName(name);
scaleUser.setBirthday(birthday);
scaleUser.setBodyHeight(body_height);
scaleUser.setScaleUnit(scale_unit);
scaleUser.setGender(gender);
scaleUser.setConvertedInitialWeight(initial_weight);
scaleUser.setGoalWeight(goal_weight);
scaleUser.setGoalDate(goal_date);
scaleUserDB.updateScaleUser(scaleUser);
} }
public ArrayList<ScaleData> getScaleDataList() { public ArrayList<ScaleData> getScaleDataList() {
return scaleDataList; return scaleDataList;
} }
@@ -243,7 +223,7 @@ public class OpenScale {
} }
private int getSmartUserAssignment(float weight, float range) { private int getSmartUserAssignment(float weight, float range) {
ArrayList<ScaleUser> scaleUser = getScaleUserList(); List<ScaleUser> scaleUser = 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 < scaleUser.size(); i++) {

View File

@@ -20,6 +20,7 @@ import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete; 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 com.health.openscale.core.datatypes.ScaleUser; import com.health.openscale.core.datatypes.ScaleUser;
@@ -30,6 +31,9 @@ public interface ScaleUserDAO {
@Query("SELECT * FROM scaleuser") @Query("SELECT * FROM scaleuser")
List<ScaleUser> getAll(); List<ScaleUser> getAll();
@Query("SELECT * FROM scaleuser WHERE id IS :id")
ScaleUser getById(int id);
@Query("SELECT * FROM scaleuser WHERE id IS :id") @Query("SELECT * FROM scaleuser WHERE id IS :id")
ScaleUser loadById(int id); ScaleUser loadById(int id);
@@ -39,6 +43,9 @@ public interface ScaleUserDAO {
@Insert @Insert
void insertAll(ScaleUser... users); void insertAll(ScaleUser... users);
@Update
void update(ScaleUser user);
@Delete @Delete
void delete(ScaleUser user); void delete(ScaleUser user);
} }

View File

@@ -36,9 +36,9 @@ import com.health.openscale.core.OpenScale;
import com.health.openscale.core.datatypes.ScaleUser; import com.health.openscale.core.datatypes.ScaleUser;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List;
public class UserSettingsActivity extends Activity { public class UserSettingsActivity extends Activity {
@@ -231,7 +231,7 @@ public class UserSettingsActivity extends Activity {
openScale.clearScaleData(userId); openScale.clearScaleData(userId);
openScale.deleteScaleUser(userId); openScale.deleteScaleUser(userId);
ArrayList<ScaleUser> scaleUser = openScale.getScaleUserList(); List<ScaleUser> scaleUser = openScale.getScaleUserList();
int lastUserId = -1; int lastUserId = -1;
@@ -302,11 +302,23 @@ public class UserSettingsActivity extends Activity {
int id = 0; int id = 0;
final ScaleUser scaleUser = new ScaleUser();
scaleUser.setUserName(name);
scaleUser.setBirthday(birthday);
scaleUser.setBodyHeight(body_height);
scaleUser.setScaleUnit(scale_unit);
scaleUser.setGender(gender);
scaleUser.setConvertedInitialWeight(initial_weight);
scaleUser.setGoalWeight(goal_weight);
scaleUser.setGoalDate(goal_date);
if (getIntent().getExtras().getInt("mode") == EDIT_USER_REQUEST) { if (getIntent().getExtras().getInt("mode") == EDIT_USER_REQUEST) {
id = getIntent().getExtras().getInt("id"); id = getIntent().getExtras().getInt("id");
openScale.updateScaleUser(id, name, birthday, body_height, scale_unit, gender, initial_weight, goal_weight, goal_date); scaleUser.setId(id);
openScale.updateScaleUser(scaleUser);
} else { } else {
openScale.addScaleUser(name, birthday, body_height, scale_unit, gender, initial_weight, goal_weight, goal_date); openScale.addScaleUser(scaleUser);
id = openScale.getScaleUserList().get(openScale.getScaleUserList().size() - 1).getId(); id = openScale.getScaleUserList().get(openScale.getScaleUserList().size() - 1).getId();
} }

View File

@@ -215,7 +215,7 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
userSelectedData = null; userSelectedData = null;
spinUserAdapter.clear(); spinUserAdapter.clear();
ArrayList<ScaleUser> scaleUserList = OpenScale.getInstance(getContext()).getScaleUserList(); List<ScaleUser> scaleUserList = OpenScale.getInstance(getContext()).getScaleUserList();
int posUser = 0; int posUser = 0;
@@ -480,7 +480,7 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener
if (parent.getChildCount() > 0) { if (parent.getChildCount() > 0) {
((TextView) parent.getChildAt(0)).setTextColor(Color.GRAY); ((TextView) parent.getChildAt(0)).setTextColor(Color.GRAY);
ArrayList<ScaleUser> scaleUserList = OpenScale.getInstance(getContext()).getScaleUserList(); List<ScaleUser> scaleUserList = OpenScale.getInstance(getContext()).getScaleUserList();
ScaleUser scaleUser = scaleUserList.get(position); ScaleUser scaleUser = scaleUserList.get(position);

View File

@@ -27,7 +27,7 @@ import com.health.openscale.core.OpenScale;
import com.health.openscale.core.datatypes.ScaleUser; import com.health.openscale.core.datatypes.ScaleUser;
import com.health.openscale.gui.activities.UserSettingsActivity; import com.health.openscale.gui.activities.UserSettingsActivity;
import java.util.ArrayList; import java.util.List;
import static android.app.Activity.RESULT_OK; import static android.app.Activity.RESULT_OK;
@@ -50,7 +50,7 @@ public class UsersPreferences extends PreferenceFragment {
OpenScale openScale = OpenScale.getInstance(getActivity().getApplicationContext()); OpenScale openScale = OpenScale.getInstance(getActivity().getApplicationContext());
ArrayList<ScaleUser> scaleUserList = openScale.getScaleUserList(); List<ScaleUser> scaleUserList = openScale.getScaleUserList();
for (ScaleUser scaleUser : scaleUserList) for (ScaleUser scaleUser : scaleUserList)
{ {