#12 Save workout when App is being closed

This commit is contained in:
jannis 2019-08-21 10:25:14 +02:00
parent 9329670ca8
commit 28535f653a
4 changed files with 36 additions and 11 deletions

View File

@ -6,7 +6,7 @@
* FitoTrack is free software: you can redistribute it and/or modify * FitoTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later velocationListener= new LocationListener(context);rsion. * (at your option) any later version.
* *
* FitoTrack is distributed in the hope that it will be useful, * FitoTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
@ -71,6 +71,7 @@ public class RecordWorkoutActivity extends FitoTrackActivity implements Location
private Handler mHandler= new Handler(); private Handler mHandler= new Handler();
PowerManager.WakeLock wakeLock; PowerManager.WakeLock wakeLock;
Intent locationListener; Intent locationListener;
private boolean saved= false;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -163,20 +164,31 @@ public class RecordWorkoutActivity extends FitoTrackActivity implements Location
} }
private void saveAndClose(){ private void saveAndClose(){
save();
finish();
}
private void save(){
if(recorder.getSampleCount() > 3){ if(recorder.getSampleCount() > 3){
recorder.save(); recorder.save();
saved= true;
}
}
private void saveIfNotSaved(){
if(!saved){
save();
} }
finish();
} }
private void showEnterDescriptionDialog(){ private void showEnterDescriptionDialog(){
final EditText editText= new EditText(this); final EditText editText= new EditText(this);
editText.setSingleLine(true); editText.setSingleLine(true);
new AlertDialog.Builder(this).setTitle(R.string.enterComment).setPositiveButton(R.string.okay, (dialog, which) -> { new AlertDialog.Builder(this).setTitle(R.string.enterComment).setPositiveButton(R.string.okay, (dialog, which) -> {
dialog.cancel(); dialog.dismiss();
recorder.setComment(editText.getText().toString()); recorder.setComment(editText.getText().toString());
saveAndClose(); saveAndClose();
}).setView(editText).setCancelable(false).create().show(); }).setView(editText).setOnCancelListener(dialog -> saveAndClose()).create().show();
} }
private void showAreYouSureToStopDialog(){ private void showAreYouSureToStopDialog(){
@ -234,6 +246,7 @@ public class RecordWorkoutActivity extends FitoTrackActivity implements Location
@Override @Override
protected void onDestroy() { protected void onDestroy() {
recorder.stop(); recorder.stop();
saveIfNotSaved(); // Important
mapView.destroyAll(); mapView.destroyAll();
AndroidGraphicFactory.clearResourceMemoryCache(); AndroidGraphicFactory.clearResourceMemoryCache();
super.onDestroy(); super.onDestroy();

View File

@ -59,12 +59,17 @@ public class WorkoutRecorder implements LocationListener.LocationChangeListener
private long lastPause= 0; private long lastPause= 0;
private long lastSampleTime= 0; private long lastSampleTime= 0;
private double distance= 0; private double distance= 0;
private boolean hasBegan = false;
public WorkoutRecorder(Context context, String workoutType) { public WorkoutRecorder(Context context, String workoutType) {
this.context= context; this.context= context;
this.state= RecordingState.IDLE; this.state= RecordingState.IDLE;
this.workout= new Workout(); this.workout= new Workout();
// Default values
this.workout.comment= "";
this.workout.workoutType= workoutType; this.workout.workoutType= workoutType;
} }
@ -178,13 +183,16 @@ public class WorkoutRecorder implements LocationListener.LocationChangeListener
} }
lastSampleTime= System.currentTimeMillis(); lastSampleTime= System.currentTimeMillis();
if(state == RecordingState.RUNNING && location.getTime() > workout.start){ if(state == RecordingState.RUNNING && location.getTime() > workout.start){
if(samples.size() == 2){ if(samples.size() == 2 && !hasBegan){
lastResume= System.currentTimeMillis(); lastResume= System.currentTimeMillis();
workout.start= System.currentTimeMillis(); workout.start= System.currentTimeMillis();
lastPause= 0; lastPause= 0;
time= 0; time= 0;
pauseTime= 0; pauseTime= 0;
this.distance= 0; this.distance= 0;
samples.clear();
hasBegan = true; // Do not clear a second time
} }
this.distance+= distance; this.distance+= distance;
WorkoutSample sample= new WorkoutSample(); WorkoutSample sample= new WorkoutSample();

View File

@ -26,9 +26,9 @@ public class WorkoutTypeCalculator {
public static int getType(Workout workout){ public static int getType(Workout workout){
if(workout.workoutType.equals(Workout.WORKOUT_TYPE_RUNNING)){ if(workout.workoutType.equals(Workout.WORKOUT_TYPE_RUNNING)){
if(workout.avgSpeed < 7){ if(workout.avgSpeed < 1.9){
return R.string.workoutTypeWalking; return R.string.workoutTypeWalking;
}else if(workout.avgSpeed < 9.6){ }else if(workout.avgSpeed < 2.7){
return R.string.workoutTypeJogging; return R.string.workoutTypeJogging;
}else{ }else{
return R.string.workoutTypeRunning; return R.string.workoutTypeRunning;

View File

@ -32,8 +32,8 @@ import java.util.Date;
import de.tadris.fitness.R; import de.tadris.fitness.R;
import de.tadris.fitness.data.Workout; import de.tadris.fitness.data.Workout;
import de.tadris.fitness.util.unit.UnitUtils;
import de.tadris.fitness.util.WorkoutTypeCalculator; import de.tadris.fitness.util.WorkoutTypeCalculator;
import de.tadris.fitness.util.unit.UnitUtils;
public class WorkoutAdapter extends RecyclerView.Adapter<WorkoutAdapter.WorkoutViewHolder>{ public class WorkoutAdapter extends RecyclerView.Adapter<WorkoutAdapter.WorkoutViewHolder>{
@ -75,10 +75,14 @@ public class WorkoutAdapter extends RecyclerView.Adapter<WorkoutAdapter.WorkoutV
Workout workout= workouts[position]; Workout workout= workouts[position];
holder.dateText.setText(SimpleDateFormat.getDateTimeInstance().format(new Date(workout.start))); holder.dateText.setText(SimpleDateFormat.getDateTimeInstance().format(new Date(workout.start)));
holder.typeText.setText(WorkoutTypeCalculator.getType(workout)); holder.typeText.setText(WorkoutTypeCalculator.getType(workout));
if(workout.comment.length() > 33){ if(workout.comment != null){
holder.commentText.setText(workout.comment.substring(0, 30) + "..."); if(workout.comment.length() > 33){
holder.commentText.setText(workout.comment.substring(0, 30) + "...");
}else{
holder.commentText.setText(workout.comment);
}
}else{ }else{
holder.commentText.setText(workout.comment); holder.commentText.setText("");
} }
holder.lengthText.setText(UnitUtils.getDistance(workout.length)); holder.lengthText.setText(UnitUtils.getDistance(workout.length));
holder.timeText.setText(UnitUtils.getHourMinuteTime(workout.duration)); holder.timeText.setText(UnitUtils.getHourMinuteTime(workout.duration));