Open settings, Weight / Unit calculation feature stable

- #5 Change weight of user
- #4 Units defined by user
This commit is contained in:
jannis 2019-08-20 18:15:28 +02:00
parent 4ecf319fd4
commit 2f71b2a3d5
10 changed files with 103 additions and 16 deletions

View File

@ -20,8 +20,6 @@
package de.tadris.fitness; package de.tadris.fitness;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import androidx.room.Room; import androidx.room.Room;
@ -29,6 +27,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import de.tadris.fitness.data.AppDatabase; import de.tadris.fitness.data.AppDatabase;
import de.tadris.fitness.data.UserPreferences;
import de.tadris.fitness.location.LocationListener; import de.tadris.fitness.location.LocationListener;
import de.tadris.fitness.util.unit.UnitUtils; import de.tadris.fitness.util.unit.UnitUtils;
@ -47,8 +46,10 @@ public class Instance {
public AppDatabase db; public AppDatabase db;
public List<LocationListener.LocationChangeListener> locationChangeListeners= new ArrayList<>(); public List<LocationListener.LocationChangeListener> locationChangeListeners= new ArrayList<>();
public UserPreferences userPreferences;
private Instance(Context context) { private Instance(Context context) {
userPreferences= new UserPreferences(context);
db = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, DATABASE_NAME) db = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, DATABASE_NAME)
.allowMainThreadQueries() .allowMainThreadQueries()
.build(); .build();

View File

@ -33,8 +33,8 @@ import com.github.clans.fab.FloatingActionMenu;
import de.tadris.fitness.Instance; import de.tadris.fitness.Instance;
import de.tadris.fitness.R; import de.tadris.fitness.R;
import de.tadris.fitness.view.WorkoutAdapter;
import de.tadris.fitness.data.Workout; import de.tadris.fitness.data.Workout;
import de.tadris.fitness.view.WorkoutAdapter;
public class ListWorkoutsActivity extends Activity implements WorkoutAdapter.WorkoutAdapterListener { public class ListWorkoutsActivity extends Activity implements WorkoutAdapter.WorkoutAdapterListener {
@ -97,6 +97,12 @@ public class ListWorkoutsActivity extends Activity implements WorkoutAdapter.Wor
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId(); int id = item.getItemId();
switch (id){
case R.id.actionOpenSettings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }

View File

@ -1,29 +1,44 @@
/*
* Copyright (c) 2019 Jannis Scheibe <jannis@tadris.de>
*
* This file is part of FitoTrack
*
* FitoTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FitoTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.tadris.fitness.activity; package de.tadris.fitness.activity;
import android.app.ActionBar; import android.app.ActionBar;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.media.Ringtone; import android.media.Ringtone;
import android.media.RingtoneManager; import android.media.RingtoneManager;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle;
import android.preference.ListPreference; import android.preference.ListPreference;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.preference.RingtonePreference; import android.preference.RingtonePreference;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.Button;
import android.widget.NumberPicker; import android.widget.NumberPicker;
import androidx.core.app.NavUtils; import androidx.core.app.NavUtils;
import de.tadris.fitness.R; import de.tadris.fitness.R;
import de.tadris.fitness.util.unit.Unit;
import de.tadris.fitness.util.unit.UnitUtils; import de.tadris.fitness.util.unit.UnitUtils;
public class SettingsActivity extends PreferenceActivity { public class SettingsActivity extends PreferenceActivity {
@ -109,6 +124,8 @@ public class SettingsActivity extends PreferenceActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setupActionBar(); setupActionBar();
setTitle(R.string.settings);
addPreferencesFromResource(R.xml.preferences_main); addPreferencesFromResource(R.xml.preferences_main);
bindPreferenceSummaryToValue(findPreference("unitSystem")); bindPreferenceSummaryToValue(findPreference("unitSystem"));
@ -125,14 +142,14 @@ public class SettingsActivity extends PreferenceActivity {
NumberPicker np = v.findViewById(R.id.weightPicker); NumberPicker np = v.findViewById(R.id.weightPicker);
np.setMaxValue((int) UnitUtils.CHOSEN_SYSTEM.getWeightFromKilogram(150)); np.setMaxValue((int) UnitUtils.CHOSEN_SYSTEM.getWeightFromKilogram(150));
np.setMinValue((int) UnitUtils.CHOSEN_SYSTEM.getWeightFromKilogram(20)); np.setMinValue((int) UnitUtils.CHOSEN_SYSTEM.getWeightFromKilogram(20));
np.setFormatter(value -> value + " " + UnitUtils.CHOSEN_SYSTEM.getWeightUnit());
np.setValue(preferences.getInt("weight", 80)); np.setValue(preferences.getInt("weight", 80));
np.setFormatter(value -> value + UnitUtils.CHOSEN_SYSTEM.getWeightUnit());
np.setWrapSelectorWheel(false); np.setWrapSelectorWheel(false);
d.setView(v); d.setView(v);
d.setNegativeButton(R.string.cancel, null); d.setNegativeButton(R.string.cancel, null);
d.setPositiveButton(R.string.okay, (DialogInterface.OnClickListener) (dialog, which) -> { d.setPositiveButton(R.string.okay, (dialog, which) -> {
int unitValue= np.getValue(); int unitValue= np.getValue();
int kilograms= (int)Math.round(UnitUtils.CHOSEN_SYSTEM.getKilogramFromUnit(unitValue)); int kilograms= (int)Math.round(UnitUtils.CHOSEN_SYSTEM.getKilogramFromUnit(unitValue));
preferences.edit().putInt("weight", kilograms).apply(); preferences.edit().putInt("weight", kilograms).apply();

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2019 Jannis Scheibe <jannis@tadris.de>
*
* This file is part of FitoTrack
*
* FitoTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FitoTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.tadris.fitness.data;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class UserPreferences {
private SharedPreferences preferences;
public UserPreferences(Context context) {
this.preferences= PreferenceManager.getDefaultSharedPreferences(context);
}
public int getUserWeight(){
return preferences.getInt("weight", 80);
}
}

View File

@ -56,7 +56,7 @@ public class WorkoutManager {
workout.length= (int)length; workout.length= (int)length;
workout.avgSpeed= ((double) workout.length) / ((double) workout.duration / 1000); workout.avgSpeed= ((double) workout.length) / ((double) workout.duration / 1000);
workout.avgPace= ((double)workout.duration / 1000 / 60) / ((double) workout.length / 1000); workout.avgPace= ((double)workout.duration / 1000 / 60) / ((double) workout.length / 1000);
workout.calorie= CalorieCalculator.calculateCalories(workout, Instance.getInstance(context).userPreferences.weight); workout.calorie= CalorieCalculator.calculateCalories(workout, Instance.getInstance(context).userPreferences.getUserWeight());
// Setting workoutId in the samples // Setting workoutId in the samples
int i= 0; int i= 0;

View File

@ -214,7 +214,7 @@ public class WorkoutRecorder implements LocationListener.LocationChangeListener
public int getCalories(){ public int getCalories(){
workout.avgSpeed= getAvgSpeed(); workout.avgSpeed= getAvgSpeed();
workout.duration= getDuration(); workout.duration= getDuration();
int calories= CalorieCalculator.calculateCalories(workout, Instance.getInstance(context).userPreferences.weight); int calories= CalorieCalculator.calculateCalories(workout, Instance.getInstance(context).userPreferences.getUserWeight());
if(calories > maxCalories){ if(calories > maxCalories){
maxCalories= calories; maxCalories= calories;
} }

View File

@ -35,7 +35,8 @@ public class UnitUtils {
public static Unit CHOSEN_SYSTEM= UNITS_METRIC; public static Unit CHOSEN_SYSTEM= UNITS_METRIC;
public static void setUnit(Context context){ public static void setUnit(Context context){
PreferenceManager.getDefaultSharedPreferences(context).getInt("unitSystem", UnitUtils.UNITS_METRIC.getId()); int id= Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(context).getString("unitSystem", String.valueOf(UnitUtils.UNITS_METRIC.getId())));
setUnit(id);
} }
public static void setUnit(int id){ public static void setUnit(int id){

View File

@ -18,4 +18,8 @@
~ along with this program. If not, see <http://www.gnu.org/licenses/>. ~ along with this program. If not, see <http://www.gnu.org/licenses/>.
--> -->
<menu xmlns:android="http://schemas.android.com/apk/res/android"/> <menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/actionOpenSettings"
android:title="@string/settings" />
</menu>

View File

@ -79,4 +79,5 @@
<string name="pref_weight">Your Weight</string> <string name="pref_weight">Your Weight</string>
<string name="pref_weight_summary">Your weight is needed to calculate the burned calories</string> <string name="pref_weight_summary">Your weight is needed to calculate the burned calories</string>
<string name="pref_unit_system">Preferred system of units</string> <string name="pref_unit_system">Preferred system of units</string>
<string name="settings">Settings</string>
</resources> </resources>

View File

@ -1,4 +1,23 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2019 Jannis Scheibe <jannis@tadris.de>
~
~ This file is part of FitoTrack
~
~ FitoTrack is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ FitoTrack is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<resources> <resources>
<string-array name="pref_unit_systems"> <string-array name="pref_unit_systems">
@ -8,11 +27,11 @@
<item>Imperial with meters</item> <item>Imperial with meters</item>
</string-array> </string-array>
<integer-array name="pref_unit_system_values"> <string-array name="pref_unit_system_values">
<item>1</item> <item>1</item>
<item>2</item> <item>2</item>
<item>3</item> <item>3</item>
<item>4</item> <item>4</item>
</integer-array> </string-array>
</resources> </resources>