#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
* 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 velocationListener= new LocationListener(context);rsion.
* (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
@ -71,6 +71,7 @@ public class RecordWorkoutActivity extends FitoTrackActivity implements Location
private Handler mHandler= new Handler();
PowerManager.WakeLock wakeLock;
Intent locationListener;
private boolean saved= false;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -163,20 +164,31 @@ public class RecordWorkoutActivity extends FitoTrackActivity implements Location
}
private void saveAndClose(){
save();
finish();
}
private void save(){
if(recorder.getSampleCount() > 3){
recorder.save();
saved= true;
}
}
private void saveIfNotSaved(){
if(!saved){
save();
}
finish();
}
private void showEnterDescriptionDialog(){
final EditText editText= new EditText(this);
editText.setSingleLine(true);
new AlertDialog.Builder(this).setTitle(R.string.enterComment).setPositiveButton(R.string.okay, (dialog, which) -> {
dialog.cancel();
dialog.dismiss();
recorder.setComment(editText.getText().toString());
saveAndClose();
}).setView(editText).setCancelable(false).create().show();
}).setView(editText).setOnCancelListener(dialog -> saveAndClose()).create().show();
}
private void showAreYouSureToStopDialog(){
@ -234,6 +246,7 @@ public class RecordWorkoutActivity extends FitoTrackActivity implements Location
@Override
protected void onDestroy() {
recorder.stop();
saveIfNotSaved(); // Important
mapView.destroyAll();
AndroidGraphicFactory.clearResourceMemoryCache();
super.onDestroy();

View File

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

View File

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

View File

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