Skip to content

Commit

Permalink
#46 Restore current workout onResume
Browse files Browse the repository at this point in the history
  • Loading branch information
avalax committed Jul 29, 2014
1 parent 371b13e commit bd5251e
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package de.avalax.fitbuddy.application.workout;

import de.avalax.fitbuddy.domain.model.exercise.Exercise;
import de.avalax.fitbuddy.domain.model.exercise.ExerciseId;
import de.avalax.fitbuddy.domain.model.exercise.ExerciseNotFoundException;
import de.avalax.fitbuddy.domain.model.exercise.ExerciseRepository;
import de.avalax.fitbuddy.domain.model.set.SetId;
import de.avalax.fitbuddy.domain.model.workout.Workout;
import de.avalax.fitbuddy.domain.model.workout.WorkoutId;
import de.avalax.fitbuddy.domain.model.workout.WorkoutNotFoundException;
Expand All @@ -15,10 +17,12 @@ public class WorkoutApplicationService {

private ExerciseRepository exerciseRepository;
private WorkoutRepository workoutRepository;
private WorkoutSession workoutSession;

public WorkoutApplicationService(ExerciseRepository exerciseRepository, WorkoutRepository workoutRepository) {
public WorkoutApplicationService(ExerciseRepository exerciseRepository, WorkoutRepository workoutRepository, WorkoutSession workoutSession) {
this.exerciseRepository = exerciseRepository;
this.workoutRepository = workoutRepository;
this.workoutSession = workoutSession;
}

public int countOfExercises(String workoutId) {
Expand All @@ -27,11 +31,32 @@ public int countOfExercises(String workoutId) {
}

public Exercise exerciseFromPosition(String workoutId, int position) throws ExerciseNotFoundException {
//TODO: set currentset and reps after loading from repo
return exerciseRepository.loadExerciseFromWorkoutWithPosition(new WorkoutId(workoutId), position);
Exercise exercise = exerciseRepository.loadExerciseFromWorkoutWithPosition(new WorkoutId(workoutId), position);
Integer index = workoutSession.selectedSetOfExercise(exercise.getExerciseId());
if (exercise.getSets().size() > index) {
exercise.setCurrentSet(index);
exercise.getCurrentSet().setReps(workoutSession.repsForSet(exercise.getCurrentSet().getSetId()));
}
return exercise;
}

public Workout requestWorkout(String workoutId) throws WorkoutNotFoundException {
return workoutRepository.load(new WorkoutId(workoutId));
}

public void setSelectedSetOfExercise(ExerciseId exerciseId, Integer position) {
workoutSession.setSelectedSetOfExercise(exerciseId, position);
}

public void setRepsOfSet(SetId setId, int reps) {
workoutSession.setRepsOfSet(setId, reps);
}

public void setSelectedExercise(int selectedExercise) {
workoutSession.setSelectedExercise(selectedExercise);
}

public int selectedExercise() {
return workoutSession.selectedExercise();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,113 @@
package de.avalax.fitbuddy.application.workout;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import de.avalax.fitbuddy.domain.model.exercise.ExerciseId;
import de.avalax.fitbuddy.domain.model.set.SetId;
import de.avalax.fitbuddy.domain.model.workout.WorkoutId;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class WorkoutSession {
private SharedPreferences sharedPreferences;
private Context context;
private Map<ExerciseId, Integer> selectedSets;
private Map<SetId, Integer> repsForSet;


public WorkoutSession(SharedPreferences sharedPreferences) {
public WorkoutSession(SharedPreferences sharedPreferences, Context context) {
this.sharedPreferences = sharedPreferences;
this.context = context;
this.selectedSets = readSelectedSetsFromFile();
this.repsForSet = readRepsForSetFromFile();
}

public void switchWorkoutById(WorkoutId workoutId) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(WorkoutApplicationService.WORKOUT_ID_SHARED_KEY, workoutId.id());
editor.commit();
}

public Integer selectedSetOfExercise(ExerciseId exerciseId) {
return selectedSets.containsKey(exerciseId) ? selectedSets.get(exerciseId) : 0;
}

private Map<ExerciseId, Integer> readSelectedSetsFromFile() {
Map<ExerciseId, Integer> selectedSets;
File file = new File(context.getDir("data", Context.MODE_PRIVATE), "selectedSets");
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
selectedSets = (Map<ExerciseId, Integer>) ois.readObject();
ois.close();
fis.close();
} catch (IOException | ClassNotFoundException e) {
selectedSets = new HashMap<>();
}
return selectedSets;
}

private Map<SetId, Integer> readRepsForSetFromFile() {
Map<SetId, Integer> repsForSet;
File file = new File(context.getDir("data", Context.MODE_PRIVATE), "repsForSet");
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
repsForSet = (Map<SetId, Integer>) ois.readObject();
ois.close();
fis.close();
} catch (IOException | ClassNotFoundException e) {
repsForSet = new HashMap<>();
}
return repsForSet;
}

public void setSelectedSetOfExercise(ExerciseId exerciseId, int expectedPosition) {
selectedSets.put(exerciseId, expectedPosition);
writeSelectedSetsToFile();
}

private void writeSelectedSetsToFile() {
File file = new File(context.getDir("data", Context.MODE_PRIVATE), "selectedSets");
try {
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
outputStream.writeObject(selectedSets);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
Log.d("Failed to write file", e.getMessage(), e);
}
}

private void writeRepsForSetToFile() {
File file = new File(context.getDir("data", Context.MODE_PRIVATE), "repsForSet");
try {
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
outputStream.writeObject(repsForSet);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
Log.d("Failed to write file", e.getMessage(), e);
}
}

public void setRepsOfSet(SetId setId, int reps) {
repsForSet.put(setId, reps);
writeRepsForSetToFile();
}

public Integer repsForSet(SetId setId) {
return repsForSet.containsKey(setId) ? repsForSet.get(setId) : 0;
}

public int selectedExercise() {
return sharedPreferences.getInt("selectedExercise", 0);
}

public void setSelectedExercise(int selectedExercise) {
sharedPreferences.edit().putInt("selectedExercise", selectedExercise).commit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public FitbuddyModule(Context context, SharedPreferences sharedPreferences) {
@Provides
@Singleton
WorkoutSession provideWorkoutSession(SharedPreferences sharedPreferences) {
return new WorkoutSession(sharedPreferences);
return new WorkoutSession(sharedPreferences, context);
}

@Provides
Expand Down Expand Up @@ -83,8 +83,8 @@ SharedPreferences provideSharedPreferences() {

@Provides
@Singleton
WorkoutApplicationService provideWorkoutApplicationService(WorkoutRepository workoutRepository, ExerciseRepository exerciseRepository) {
return new WorkoutApplicationService(exerciseRepository, workoutRepository);
WorkoutApplicationService provideWorkoutApplicationService(WorkoutRepository workoutRepository, ExerciseRepository exerciseRepository, WorkoutSession workoutSession) {
return new WorkoutApplicationService(exerciseRepository, workoutRepository, workoutSession);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class CurrentExerciseFragment extends Fragment {
protected VerticalProgressbarView setsProgressBar;
@Inject
protected WorkoutApplicationService workoutApplicationService;
private Exercise exercise;
private int exerciseIndex;
private String workoutId;

public static CurrentExerciseFragment newInstance(String workoutId, int exerciseIndex) {
CurrentExerciseFragment fragment = new CurrentExerciseFragment();
Expand All @@ -56,20 +56,28 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final Context context = getActivity();
exerciseIndex = getArguments().getInt(ARGS_EXERCISE_INDEX);
String workoutId = getArguments().getString(ARGS_WORKOUT_ID);
workoutId = getArguments().getString(ARGS_WORKOUT_ID);
try {
this.exercise = workoutApplicationService.exerciseFromPosition(workoutId, exerciseIndex);
Exercise exercise = workoutApplicationService.exerciseFromPosition(workoutId, exerciseIndex);
repsProgressBar.setOnTouchListener(new SwipeBarOnTouchListener(context, repsProgressBar, getMaxMoveForReps(exercise)) {
@Override
public void onFlingEvent(int moved) {
changeReps(moved);
try {
changeReps(moved);
} catch (ExerciseNotFoundException e) {
Log.d("Can't execute onFlingEvent", e.getMessage(), e);
}
}
});

setsProgressBar.setOnTouchListener(new SwipeBarOnTouchListener(context, setsProgressBar, getMaxMoveForSets(exercise)) {
@Override
public void onFlingEvent(int moved) {
changeSets(moved);
try {
changeSets(moved);
} catch (ExerciseNotFoundException e) {
Log.d("Can't execute onFlingEvent", e.getMessage(), e);
}
}
});

Expand All @@ -90,32 +98,28 @@ private int getMaxMoveForReps(Exercise exercise) {
return exercise.getCurrentSet().getMaxReps();
}

private void changeReps(int moved) {
private void changeReps(int moved) throws ExerciseNotFoundException {
setReps(moved);
setViews();
updateWorkoutProgress();
}

private void changeSets(int moved) {
setSet(exercise.indexOfCurrentSet() + moved);
private void changeSets(int moved) throws ExerciseNotFoundException {
Exercise exercise = workoutApplicationService.exerciseFromPosition(workoutId, exerciseIndex);
Integer position = exercise.indexOfCurrentSet() + moved;
workoutApplicationService.setSelectedSetOfExercise(exercise.getExerciseId(), position);
setViews();
updateWorkoutProgress();
updatePage();
}

private void setReps(int moved) {
if (exercise.getSets().isEmpty()) {
return;
}
//TODO: add to Set addRep, removeRep
exercise.getCurrentSet().setReps(exercise.getCurrentSet().getReps() + moved);
}

private void setSet(int setNumber) {
exercise.setCurrentSet(setNumber);
private void setReps(int moved)throws ExerciseNotFoundException {
Exercise exercise = workoutApplicationService.exerciseFromPosition(workoutId, exerciseIndex);
workoutApplicationService.setRepsOfSet(exercise.getCurrentSet().getSetId(), exercise.getCurrentSet().getReps() + moved);
}

private void setViews() {
private void setViews() throws ExerciseNotFoundException {
Exercise exercise = workoutApplicationService.exerciseFromPosition(workoutId, exerciseIndex);
if (exercise.getSets().size() > 0) {
repsProgressBar.updateProgressbar(exercise.getCurrentSet());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class MainActivity extends FragmentActivity implements EditWeightDialogFr
private DecimalFormat decimalFormat;
private String weightTitle;
private MenuItem menuItem;
private int index;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -59,16 +58,15 @@ public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
this.menuItem = menu.findItem(R.id.action_change_weight);
updatePage(this.index);
viewPager.setCurrentItem(workoutApplicationService.selectedExercise());
updatePage(workoutApplicationService.selectedExercise());
return super.onCreateOptionsMenu(menu);
}

private void init() {
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setBackgroundDrawable(null);

this.index = 0;
this.decimalFormat = new DecimalFormat("###.###");
this.weightTitle = getResources().getString(R.string.title_weight);
String workoutId = sharedPreferences.getString(WorkoutApplicationService.WORKOUT_ID_SHARED_KEY, "1");
Expand All @@ -80,7 +78,7 @@ private void init() {

@OnPageChange(R.id.pager)
protected void updatePage(int index) {
this.index = index;
workoutApplicationService.setSelectedExercise(index);
String workoutId = sharedPreferences.getString(WorkoutApplicationService.WORKOUT_ID_SHARED_KEY, "1");
try {
Exercise exercise = workoutApplicationService.exerciseFromPosition(workoutId, index);
Expand Down Expand Up @@ -135,6 +133,7 @@ private void showEditDialog() {
FragmentManager fm = getSupportFragmentManager();
String workoutId = sharedPreferences.getString(WorkoutApplicationService.WORKOUT_ID_SHARED_KEY, "1");
try {
int index = workoutApplicationService.selectedExercise();
Exercise exercise = workoutApplicationService.exerciseFromPosition(workoutId, index);
double weight = exercise.getCurrentSet().getWeight();
EditWeightDialogFragment.newInstance(weight).show(fm, "fragment_edit_name");
Expand All @@ -161,6 +160,7 @@ private int calculateProgressbarHeight(double progess) {
public void onDialogPositiveClick(EditWeightDialogFragment editWeightDialogFragment) {
String workoutId = sharedPreferences.getString(WorkoutApplicationService.WORKOUT_ID_SHARED_KEY, "1");
try {
int index = workoutApplicationService.selectedExercise();
Exercise exercise = workoutApplicationService.exerciseFromPosition(workoutId, index);
exercise.getCurrentSet().setWeight(editWeightDialogFragment.getWeight());
updatePage(index);
Expand Down
Loading

0 comments on commit bd5251e

Please sign in to comment.