Search in sources :

Example 1 with ScaleUser

use of com.health.openscale.core.datatypes.ScaleUser in project openScale by oliexdev.

the class UserSettingsActivity method saveUserData.

private boolean saveUserData() {
    try {
        if (validateInput()) {
            OpenScale openScale = OpenScale.getInstance(getApplicationContext());
            String name = txtUserName.getText().toString();
            int body_height = Integer.valueOf(txtBodyHeight.getText().toString());
            int checkedRadioButtonId = radioScaleUnit.getCheckedRadioButtonId();
            int checkedGenderId = radioGender.getCheckedRadioButtonId();
            float initial_weight = Float.valueOf(txtInitialWeight.getText().toString());
            float goal_weight = Float.valueOf(txtGoalWeight.getText().toString());
            Converters.WeightUnit scale_unit = Converters.WeightUnit.KG;
            switch(checkedRadioButtonId) {
                case R.id.btnRadioKG:
                    scale_unit = Converters.WeightUnit.KG;
                    break;
                case R.id.btnRadioLB:
                    scale_unit = Converters.WeightUnit.LB;
                    break;
                case R.id.btnRadioST:
                    scale_unit = Converters.WeightUnit.ST;
                    break;
            }
            Converters.Gender gender = Converters.Gender.MALE;
            switch(checkedGenderId) {
                case R.id.btnRadioMale:
                    gender = Converters.Gender.MALE;
                    break;
                case R.id.btnRadioWoman:
                    gender = Converters.Gender.FEMALE;
                    break;
            }
            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(EXTRA_MODE) == EDIT_USER_REQUEST) {
                int id = getIntent().getExtras().getInt(EXTRA_ID);
                scaleUser.setId(id);
                openScale.updateScaleUser(scaleUser);
            } else {
                int id = openScale.addScaleUser(scaleUser);
                scaleUser.setId(id);
            }
            openScale.selectScaleUser(scaleUser.getId());
            openScale.updateScaleData();
            return true;
        }
    } catch (NumberFormatException ex) {
        Toast.makeText(context, getResources().getString(R.string.error_value_range) + "(" + ex.getMessage() + ")", Toast.LENGTH_SHORT).show();
    }
    return false;
}
Also used : ScaleUser(com.health.openscale.core.datatypes.ScaleUser) Converters(com.health.openscale.core.utils.Converters) OpenScale(com.health.openscale.core.OpenScale)

Example 2 with ScaleUser

use of com.health.openscale.core.datatypes.ScaleUser in project openScale by oliexdev.

the class UserSettingsActivity method editMode.

private void editMode() {
    int id = getIntent().getExtras().getInt(EXTRA_ID);
    OpenScale openScale = OpenScale.getInstance(getApplicationContext());
    ScaleUser scaleUser = openScale.getScaleUser(id);
    getSupportActionBar().setTitle(scaleUser.getUserName());
    birthday = scaleUser.getBirthday();
    goal_date = scaleUser.getGoalDate();
    txtUserName.setText(scaleUser.getUserName());
    txtBodyHeight.setText(Integer.toString(scaleUser.getBodyHeight()));
    txtBirthday.setText(dateFormat.format(birthday));
    txtGoalDate.setText(dateFormat.format(goal_date));
    txtInitialWeight.setText(Math.round(scaleUser.getConvertedInitialWeight() * 100.0f) / 100.0f + "");
    txtGoalWeight.setText(scaleUser.getGoalWeight() + "");
    switch(scaleUser.getScaleUnit()) {
        case KG:
            radioScaleUnit.check(R.id.btnRadioKG);
            break;
        case LB:
            radioScaleUnit.check(R.id.btnRadioLB);
            break;
        case ST:
            radioScaleUnit.check(R.id.btnRadioST);
            break;
    }
    switch(scaleUser.getGender()) {
        case MALE:
            radioGender.check(R.id.btnRadioMale);
            break;
        case FEMALE:
            radioGender.check(R.id.btnRadioWoman);
            break;
    }
}
Also used : ScaleUser(com.health.openscale.core.datatypes.ScaleUser) OpenScale(com.health.openscale.core.OpenScale)

Example 3 with ScaleUser

use of com.health.openscale.core.datatypes.ScaleUser in project openScale by oliexdev.

the class DatabaseTest method measurementOperations.

@Test
public void measurementOperations() throws Exception {
    final ScaleUser scaleUser1 = new ScaleUser();
    final ScaleUser scaleUser2 = new ScaleUser();
    scaleUser1.setId((int) userDao.insert(scaleUser1));
    scaleUser2.setId((int) userDao.insert(scaleUser2));
    // User 1 data initialization
    final int user1 = scaleUser1.getId();
    ScaleMeasurement measurement11 = new ScaleMeasurement();
    ScaleMeasurement measurement12 = new ScaleMeasurement();
    ScaleMeasurement measurement13 = new ScaleMeasurement();
    measurement11.setUserId(user1);
    measurement12.setUserId(user1);
    measurement13.setUserId(user1);
    measurement11.setWeight(10.0f);
    measurement12.setWeight(20.0f);
    measurement13.setWeight(30.0f);
    measurement11.setDateTime(new Date(100));
    measurement12.setDateTime(new Date(200));
    measurement13.setDateTime(new Date(300));
    // User 2 data initialization
    final int user2 = scaleUser2.getId();
    ScaleMeasurement measurement21 = new ScaleMeasurement();
    ScaleMeasurement measurement22 = new ScaleMeasurement();
    measurement21.setUserId(user2);
    measurement22.setUserId(user2);
    measurement21.setWeight(15.0f);
    measurement22.setWeight(25.0f);
    measurement21.setDateTime(new Date(150));
    measurement22.setDateTime(new Date(250));
    // check if database is empty
    assertTrue(measurementDAO.getAll(user1).isEmpty());
    assertTrue(measurementDAO.getAll(user2).isEmpty());
    // insert measurement as list and single insertion
    List<ScaleMeasurement> scaleMeasurementList = new ArrayList<>();
    scaleMeasurementList.add(measurement11);
    scaleMeasurementList.add(measurement13);
    scaleMeasurementList.add(measurement12);
    measurementDAO.insertAll(scaleMeasurementList);
    assertEquals(3, measurementDAO.getAll(user1).size());
    measurementDAO.insert(measurement22);
    measurementDAO.insert(measurement21);
    assertEquals(2, measurementDAO.getAll(user2).size());
    // check if sorted DESC by date correctly
    assertEquals(30.0f, measurementDAO.getAll(user1).get(0).getWeight(), DELTA);
    assertEquals(25.0f, measurementDAO.getAll(user2).get(0).getWeight(), DELTA);
    // don't allow insertion with the same date
    long id = measurementDAO.insert(measurement11);
    assertEquals(-1, id);
    assertEquals(3, measurementDAO.getAll(user1).size());
    // test get(datetime) method
    assertEquals(20.0f, measurementDAO.get(new Date(200), user1).getWeight(), DELTA);
    // test get(id) method
    scaleMeasurementList = measurementDAO.getAll(user1);
    assertEquals(scaleMeasurementList.get(2).getWeight(), measurementDAO.get(scaleMeasurementList.get(2).getId()).getWeight(), DELTA);
    // test getPrevious(id) method
    assertNull(measurementDAO.getPrevious(scaleMeasurementList.get(2).getId(), user1));
    assertEquals(scaleMeasurementList.get(2).getWeight(), measurementDAO.getPrevious(scaleMeasurementList.get(1).getId(), user1).getWeight(), DELTA);
    assertEquals(scaleMeasurementList.get(1).getWeight(), measurementDAO.getPrevious(scaleMeasurementList.get(0).getId(), user1).getWeight(), DELTA);
    // test getNext(id) method
    assertNull(measurementDAO.getNext(scaleMeasurementList.get(0).getId(), user1));
    assertEquals(scaleMeasurementList.get(0).getWeight(), measurementDAO.getNext(scaleMeasurementList.get(1).getId(), user1).getWeight(), DELTA);
    assertEquals(scaleMeasurementList.get(1).getWeight(), measurementDAO.getNext(scaleMeasurementList.get(2).getId(), user1).getWeight(), DELTA);
    // test getAllInRange method
    assertEquals(1, measurementDAO.getAllInRange(new Date(0), new Date(200), user1).size());
    assertEquals(0, measurementDAO.getAllInRange(new Date(0), new Date(50), user1).size());
    assertEquals(2, measurementDAO.getAllInRange(new Date(100), new Date(201), user1).size());
    assertEquals(1, measurementDAO.getAllInRange(new Date(0), new Date(200), user1).size());
    assertEquals(3, measurementDAO.getAllInRange(new Date(0), new Date(1000), user1).size());
    assertEquals(2, measurementDAO.getAllInRange(new Date(150), new Date(400), user1).size());
    assertEquals(0, measurementDAO.getAllInRange(new Date(10), new Date(20), user2).size());
    assertEquals(1, measurementDAO.getAllInRange(new Date(70), new Date(200), user2).size());
    assertEquals(2, measurementDAO.getAllInRange(new Date(0), new Date(1000), user2).size());
    // test update method
    assertEquals(30.0f, measurementDAO.get(scaleMeasurementList.get(0).getId()).getWeight(), DELTA);
    scaleMeasurementList.get(0).setWeight(42.0f);
    measurementDAO.update(scaleMeasurementList.get(0));
    assertEquals(42.0f, measurementDAO.get(scaleMeasurementList.get(0).getId()).getWeight(), DELTA);
    // test delete method
    assertEquals(3, measurementDAO.getAll(user1).size());
    measurementDAO.delete(scaleMeasurementList.get(0).getId());
    assertEquals(2, measurementDAO.getAll(user1).size());
    // test delete all method
    assertEquals(2, measurementDAO.getAll(user1).size());
    assertEquals(2, measurementDAO.getAll(user2).size());
    measurementDAO.deleteAll(user1);
    measurementDAO.deleteAll(user2);
    assertEquals(0, measurementDAO.getAll(user1).size());
    assertEquals(0, measurementDAO.getAll(user2).size());
    assertTrue(measurementDAO.getAll(user1).isEmpty());
    assertTrue(measurementDAO.getAll(user2).isEmpty());
}
Also used : ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) ScaleUser(com.health.openscale.core.datatypes.ScaleUser) ArrayList(java.util.ArrayList) Date(java.util.Date) Test(org.junit.Test)

Example 4 with ScaleUser

use of com.health.openscale.core.datatypes.ScaleUser in project openScale by oliexdev.

the class ScreenshotRecorder method getTestUser.

ScaleUser getTestUser() {
    ScaleUser user = new ScaleUser();
    user.setUserName("Test");
    user.setBodyHeight(180);
    user.setInitialWeight(80.0f);
    user.setGoalWeight(60.0f);
    Calendar birthday = Calendar.getInstance();
    birthday.add(Calendar.YEAR, -28);
    birthday.set(birthday.get(Calendar.YEAR), Calendar.JANUARY, 19, 0, 0, 0);
    birthday.set(Calendar.MILLISECOND, 0);
    user.setBirthday(birthday.getTime());
    Calendar goalDate = Calendar.getInstance();
    goalDate.add(Calendar.YEAR, 1);
    goalDate.set(goalDate.get(Calendar.YEAR), Calendar.JANUARY, 31, 0, 0, 0);
    goalDate.set(Calendar.MILLISECOND, 0);
    user.setGoalDate(goalDate.getTime());
    return user;
}
Also used : ScaleUser(com.health.openscale.core.datatypes.ScaleUser) Calendar(java.util.Calendar)

Example 5 with ScaleUser

use of com.health.openscale.core.datatypes.ScaleUser in project openScale by oliexdev.

the class UserAddTest method verifyUserAdd.

@After
public void verifyUserAdd() {
    ScaleUser user = OpenScale.getInstance(context).getSelectedScaleUser();
    assertEquals("test", user.getUserName());
    assertEquals(180, user.getBodyHeight());
    assertEquals(80, user.getInitialWeight(), DELTA);
    assertEquals(60, user.getGoalWeight(), DELTA);
    Calendar birthday = Calendar.getInstance();
    birthday.setTimeInMillis(0);
    birthday.set(Calendar.YEAR, 1990);
    birthday.set(Calendar.MONTH, Calendar.JANUARY);
    birthday.set(Calendar.DAY_OF_MONTH, 19);
    birthday.set(Calendar.HOUR_OF_DAY, 0);
    assertEquals(birthday.getTime().getTime(), user.getBirthday().getTime());
    Calendar goalDate = Calendar.getInstance();
    goalDate.setTimeInMillis(0);
    goalDate.set(Calendar.YEAR, 2018);
    goalDate.set(Calendar.MONTH, Calendar.JANUARY);
    goalDate.set(Calendar.DAY_OF_MONTH, 31);
    goalDate.set(Calendar.HOUR_OF_DAY, 0);
    assertEquals(goalDate.getTime().getTime(), user.getGoalDate().getTime());
}
Also used : ScaleUser(com.health.openscale.core.datatypes.ScaleUser) Calendar(java.util.Calendar) After(org.junit.After)

Aggregations

ScaleUser (com.health.openscale.core.datatypes.ScaleUser)35 Date (java.util.Date)13 ScaleMeasurement (com.health.openscale.core.datatypes.ScaleMeasurement)12 OpenScale (com.health.openscale.core.OpenScale)6 ParseException (java.text.ParseException)5 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Calendar (java.util.Calendar)3 Intent (android.content.Intent)2 SharedPreferences (android.content.SharedPreferences)2 Uri (android.net.Uri)2 Converters (com.health.openscale.core.utils.Converters)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Test (org.junit.Test)2 SuppressLint (android.annotation.SuppressLint)1 AlertDialog (android.app.AlertDialog)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1 Context (android.content.Context)1