Search in sources :

Example 1 with OpenScale

use of com.health.openscale.core.OpenScale 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 OpenScale

use of com.health.openscale.core.OpenScale 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 OpenScale

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

the class UserSettingsActivity method deleteUser.

private void deleteUser() {
    AlertDialog.Builder deleteAllDialog = new AlertDialog.Builder(context);
    deleteAllDialog.setMessage(getResources().getString(R.string.question_really_delete_user));
    deleteAllDialog.setPositiveButton(getResources().getString(R.string.label_yes), new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {
            int userId = getIntent().getExtras().getInt(EXTRA_ID);
            OpenScale openScale = OpenScale.getInstance(getApplicationContext());
            openScale.clearScaleData(userId);
            openScale.deleteScaleUser(userId);
            List<ScaleUser> scaleUser = openScale.getScaleUserList();
            int lastUserId = -1;
            if (!scaleUser.isEmpty()) {
                lastUserId = scaleUser.get(0).getId();
            }
            openScale.selectScaleUser(lastUserId);
            openScale.updateScaleData();
            Intent returnIntent = new Intent();
            setResult(RESULT_OK, returnIntent);
            finish();
        }
    });
    deleteAllDialog.setNegativeButton(getResources().getString(R.string.label_no), new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });
    deleteAllDialog.show();
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) List(java.util.List) Intent(android.content.Intent) OpenScale(com.health.openscale.core.OpenScale)

Example 4 with OpenScale

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

the class UsersPreferences method updateUserPreferences.

private void updateUserPreferences() {
    OpenScale openScale = OpenScale.getInstance(getActivity().getApplicationContext());
    int selectedUserId = openScale.getSelectedScaleUserId();
    getPreferenceScreen().removeAll();
    List<ScaleUser> scaleUserList = openScale.getScaleUserList();
    for (ScaleUser scaleUser : scaleUserList) {
        Preference prefUser = new Preference(getActivity());
        prefUser.setOnPreferenceClickListener(new onClickListenerUserSelect());
        if (scaleUser.getId() == selectedUserId) {
            prefUser.setTitle("> " + scaleUser.getUserName());
        } else {
            prefUser.setTitle(scaleUser.getUserName());
        }
        prefUser.setKey(Integer.toString(scaleUser.getId()));
        getPreferenceScreen().addPreference(prefUser);
    }
    Preference prefAddUser = new Preference(getActivity());
    prefAddUser.setOnPreferenceClickListener(new onClickListenerAddUser());
    prefAddUser.setTitle("+ " + getResources().getString(R.string.label_add_user));
    getPreferenceScreen().addPreference(prefAddUser);
}
Also used : Preference(android.preference.Preference) ScaleUser(com.health.openscale.core.datatypes.ScaleUser) OpenScale(com.health.openscale.core.OpenScale)

Example 5 with OpenScale

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

the class BackupPreferences method importBackup.

private boolean importBackup() {
    File exportDir = new File(Environment.getExternalStorageDirectory(), PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()).getString("exportDir", "openScale Backup"));
    String databaseName = "openScale.db";
    if (!isExternalStoragePresent())
        return false;
    File exportFile = getActivity().getApplicationContext().getDatabasePath(databaseName);
    File importFile = new File(exportDir, databaseName);
    if (!importFile.exists()) {
        Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.error_importing) + " " + exportDir + "/" + databaseName + " " + getResources().getString(R.string.label_not_found), Toast.LENGTH_SHORT).show();
        return false;
    }
    try {
        exportFile.createNewFile();
        copyFile(importFile, exportFile);
        Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.info_data_imported) + " " + exportDir + "/" + databaseName, Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        Toast.makeText(getActivity().getApplicationContext(), getResources().getString(R.string.error_importing) + " " + e.getMessage(), Toast.LENGTH_SHORT).show();
        return false;
    }
    OpenScale openScale = OpenScale.getInstance(getActivity().getApplicationContext());
    openScale.reopenDatabase();
    List<ScaleUser> scaleUserList = openScale.getScaleUserList();
    if (!scaleUserList.isEmpty()) {
        openScale.selectScaleUser(scaleUserList.get(0).getId());
        openScale.updateScaleData();
    }
    return true;
}
Also used : ScaleUser(com.health.openscale.core.datatypes.ScaleUser) IOException(java.io.IOException) OpenScale(com.health.openscale.core.OpenScale) File(java.io.File)

Aggregations

OpenScale (com.health.openscale.core.OpenScale)11 ScaleUser (com.health.openscale.core.datatypes.ScaleUser)6 AlertDialog (android.app.AlertDialog)2 ActivityNotFoundException (android.content.ActivityNotFoundException)2 DialogInterface (android.content.DialogInterface)2 Intent (android.content.Intent)2 SuppressLint (android.annotation.SuppressLint)1 SharedPreferences (android.content.SharedPreferences)1 Uri (android.net.Uri)1 Preference (android.preference.Preference)1 ScaleMeasurement (com.health.openscale.core.datatypes.ScaleMeasurement)1 Converters (com.health.openscale.core.utils.Converters)1 DateMeasurementView (com.health.openscale.gui.views.DateMeasurementView)1 MeasurementView (com.health.openscale.gui.views.MeasurementView)1 TimeMeasurementView (com.health.openscale.gui.views.TimeMeasurementView)1 WeightMeasurementView (com.health.openscale.gui.views.WeightMeasurementView)1 File (java.io.File)1 IOException (java.io.IOException)1 Date (java.util.Date)1 List (java.util.List)1