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 19326965..3d5950d1 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 @@ -47,7 +47,6 @@ import java.io.OutputStreamWriter; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Date; import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -80,7 +79,7 @@ public class OpenScale { alarmHandler = new AlarmHandler(); btCom = null; fragmentList = new ArrayList<>(); - appDB = Room.databaseBuilder(context, AppDatabase.class, "openScaleDatabase").build(); + appDB = Room.databaseBuilder(context, AppDatabase.class, "openScaleDatabase").allowMainThreadQueries().build(); measurementDAO = appDB.measurementDAO(); userDAO = appDB.userDAO(); @@ -95,30 +94,24 @@ public class OpenScale { 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(); - - 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.insertEntry(scaleUser); + AsyncTask.execute(new Runnable() { + @Override + public void run() { + userDAO.insert(user); + } + }); } - public ArrayList getScaleUserList() + public List getScaleUserList() { - return scaleUserDB.getScaleUserList(); + return userDAO.getAll(); } public ScaleUser getScaleUser(int userId) { - return scaleUserDB.getScaleUser(userId); + return userDAO.getById(userId); } public ScaleUser getSelectedScaleUser() @@ -133,7 +126,7 @@ public class OpenScale { return scaleUser; } - scaleUser = scaleUserDB.getScaleUser(selectedUserId); + scaleUser = userDAO.getById(selectedUserId); } catch (Exception e) { Toast.makeText(context, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show(); } @@ -143,27 +136,14 @@ public class OpenScale { 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(); - - 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); + userDAO.update(user); } - public ArrayList getScaleDataList() { return scaleDataList; } @@ -243,7 +223,7 @@ public class OpenScale { } private int getSmartUserAssignment(float weight, float range) { - ArrayList scaleUser = getScaleUserList(); + List scaleUser = getScaleUserList(); Map inRangeWeights = new TreeMap<>(); for (int i = 0; i < scaleUser.size(); i++) { 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 a5389d5d..393f8c19 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 @@ -20,6 +20,7 @@ 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; import com.health.openscale.core.datatypes.ScaleUser; @@ -30,6 +31,9 @@ public interface ScaleUserDAO { @Query("SELECT * FROM scaleuser") List getAll(); + @Query("SELECT * FROM scaleuser WHERE id IS :id") + ScaleUser getById(int id); + @Query("SELECT * FROM scaleuser WHERE id IS :id") ScaleUser loadById(int id); @@ -39,6 +43,9 @@ public interface ScaleUserDAO { @Insert void insertAll(ScaleUser... users); + @Update + void update(ScaleUser user); + @Delete void delete(ScaleUser user); } diff --git a/android_app/app/src/main/java/com/health/openscale/gui/activities/UserSettingsActivity.java b/android_app/app/src/main/java/com/health/openscale/gui/activities/UserSettingsActivity.java index 49ba9134..73afca1d 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/activities/UserSettingsActivity.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/activities/UserSettingsActivity.java @@ -36,9 +36,9 @@ import com.health.openscale.core.OpenScale; import com.health.openscale.core.datatypes.ScaleUser; import java.text.DateFormat; -import java.util.ArrayList; import java.util.Calendar; import java.util.Date; +import java.util.List; public class UserSettingsActivity extends Activity { @@ -231,7 +231,7 @@ public class UserSettingsActivity extends Activity { openScale.clearScaleData(userId); openScale.deleteScaleUser(userId); - ArrayList scaleUser = openScale.getScaleUserList(); + List scaleUser = openScale.getScaleUserList(); int lastUserId = -1; @@ -302,11 +302,23 @@ public class UserSettingsActivity extends Activity { 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) { 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 { - 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(); } diff --git a/android_app/app/src/main/java/com/health/openscale/gui/fragments/OverviewFragment.java b/android_app/app/src/main/java/com/health/openscale/gui/fragments/OverviewFragment.java index e8c8dc27..ca76de13 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/fragments/OverviewFragment.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/fragments/OverviewFragment.java @@ -215,7 +215,7 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener userSelectedData = null; spinUserAdapter.clear(); - ArrayList scaleUserList = OpenScale.getInstance(getContext()).getScaleUserList(); + List scaleUserList = OpenScale.getInstance(getContext()).getScaleUserList(); int posUser = 0; @@ -480,7 +480,7 @@ public class OverviewFragment extends Fragment implements FragmentUpdateListener if (parent.getChildCount() > 0) { ((TextView) parent.getChildAt(0)).setTextColor(Color.GRAY); - ArrayList scaleUserList = OpenScale.getInstance(getContext()).getScaleUserList(); + List scaleUserList = OpenScale.getInstance(getContext()).getScaleUserList(); ScaleUser scaleUser = scaleUserList.get(position); diff --git a/android_app/app/src/main/java/com/health/openscale/gui/preferences/UsersPreferences.java b/android_app/app/src/main/java/com/health/openscale/gui/preferences/UsersPreferences.java index 965c65b7..0e5bc1af 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/preferences/UsersPreferences.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/preferences/UsersPreferences.java @@ -27,7 +27,7 @@ import com.health.openscale.core.OpenScale; import com.health.openscale.core.datatypes.ScaleUser; import com.health.openscale.gui.activities.UserSettingsActivity; -import java.util.ArrayList; +import java.util.List; import static android.app.Activity.RESULT_OK; @@ -50,7 +50,7 @@ public class UsersPreferences extends PreferenceFragment { OpenScale openScale = OpenScale.getInstance(getActivity().getApplicationContext()); - ArrayList scaleUserList = openScale.getScaleUserList(); + List scaleUserList = openScale.getScaleUserList(); for (ScaleUser scaleUser : scaleUserList) {