<commit_before>package de.avalax.fitbuddy.presentation.workout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.IOException;

import javax.inject.Inject;

import de.avalax.fitbuddy.R;
import de.avalax.fitbuddy.application.summary.FinishedWorkoutApplicationService;
import de.avalax.fitbuddy.application.workout.WorkoutApplicationService;
import de.avalax.fitbuddy.domain.model.ResourceException;
import de.avalax.fitbuddy.domain.model.exercise.Exercise;
import de.avalax.fitbuddy.domain.model.finished_workout.FinishedWorkoutId;
import de.avalax.fitbuddy.domain.model.set.Set;
import de.avalax.fitbuddy.presentation.FitbuddyApplication;
import de.avalax.fitbuddy.presentation.MainActivity;
import de.avalax.fitbuddy.presentation.helper.ExerciseViewHelper;
import de.avalax.fitbuddy.presentation.summary.FinishedWorkoutDetailFragment;
import de.avalax.fitbuddy.presentation.workout.swipe_bar.PagerOnTouchListener;
import de.avalax.fitbuddy.presentation.workout.swipe_bar.SwipeBarOnTouchListener;
import de.avalax.fitbuddy.presentation.workout.swipe_bar.VerticalProgressbarView;

public class ExerciseFragment extends Fragment {

    private VerticalProgressbarView setProgressBar;
    private VerticalProgressbarView exerciseProgressBar;
    private ProgressBar workoutProgressBar;
    private TextView exerciseNameTextView;
    private TextView exerciseWeightTextView;
    private TextView exercisePreviousTextView;
    private TextView exerciseNextTextView;
    @Inject
    WorkoutApplicationService workoutApplicationService;
    @Inject
    ExerciseViewHelper exerciseViewHelper;
    @Inject
    FinishedWorkoutApplicationService finishedWorkoutApplicationService;

    int exerciseIndex;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_exercise, container, false);
        ((FitbuddyApplication) getActivity().getApplication()).getComponent().inject(this);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        setProgressBar = view.findViewById(R.id.leftProgressBar);
        exerciseProgressBar = view.findViewById(R.id.rightProgressBar);
        workoutProgressBar = view.findViewById(R.id.workoutProgressBar);
        exerciseNameTextView = view.findViewById(R.id.exercises_bar_exercise_name);
        exerciseWeightTextView = view.findViewById(R.id.exercises_bar_set_weight);
        exercisePreviousTextView = view.findViewById(R.id.exercises_bar_previous_exercise_name);
        exerciseNextTextView = view.findViewById(R.id.exercises_bar_next_exercise_name);

        try {
            exerciseIndex = workoutApplicationService.indexOfCurrentExercise();
            Exercise exercise = workoutApplicationService.requestExercise(exerciseIndex);
            int reps = exerciseViewHelper.maxRepsOfExercise(exercise);
            int sets = exerciseViewHelper.setCountOfExercise(exercise);
            setProgressBar.setOnTouchListener(
                    new SwipeBarOnTouchListener(getActivity(), setProgressBar, reps) {
                        @Override
                        public void onFlingEvent(int moved) {
                            try {
                                changeReps(moved);
                            } catch (ResourceException | IOException e) {
                                Log.d("Can't change reps", e.getMessage(), e);
                            }
                        }
                    });

            exerciseProgressBar.setOnTouchListener(
                    new SwipeBarOnTouchListener(getActivity(), exerciseProgressBar, sets) {
                        @Override
                        public void onFlingEvent(int moved) {
                            try {
                                moveToSet(moved);
                            } catch (ResourceException | IOException e) {
                                Log.d("Can't change set", e.getMessage(), e);
                            }
                        }
                    });

            view.findViewById(R.id.pager_exercise)
                    .setOnTouchListener(new PagerOnTouchListener(getActivity()) {
                        @Override
                        protected void onSwipeRight() {
                            if (workoutApplicationService.hasPreviousExercise(exerciseIndex)) {
                                exerciseIndex--;
                                changeExercise();
                            }
                        }

                        @Override
                        protected void onSwipeLeft() {
                            if (workoutApplicationService.hasNextExercise(exerciseIndex)) {
                                exerciseIndex++;
                                changeExercise();
                            } else {
                                finishWorkout();
                            }
                        }
                    });

            updateExerciseProgress();
            updateSetProgress();
            updatePage();
        } catch (ResourceException e) {
            Log.d("Can't create fragment", e.getMessage(), e);
        }
    }

    void finishWorkout() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setMessage(R.string.message_finish_workout)
                .setPositiveButton(android.R.string.yes, (dialog, which) -> {
                    //TODO: switch to summary
                    try {
                        workoutApplicationService.finishCurrentWorkout();
                        ((MainActivity) getActivity()).updateBottomNavigation();
                        ((MainActivity) getActivity()).showSummary();
                    } catch (ResourceException e) {
                        Log.d("Can't finish workout", e.getMessage(), e);
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    }

    void changeExercise() {
        updatePage();
        try {
            updateSetProgress();
            updateExerciseProgress();
        } catch (ResourceException e) {
            Log.d("Can't change exercise", e.getMessage(), e);
        }
    }

    private void changeReps(int moved) throws ResourceException, IOException {
        workoutApplicationService.addRepsToSet(exerciseIndex, moved);
        updateWorkoutProgress();
        updateExerciseProgress();
        updateSetProgress();
    }
<commit_msg>Checkstyle<commit_after>package de.avalax.fitbuddy.presentation.workout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.IOException;

import javax.inject.Inject;

import de.avalax.fitbuddy.R;
import de.avalax.fitbuddy.application.summary.FinishedWorkoutApplicationService;
import de.avalax.fitbuddy.application.workout.WorkoutApplicationService;
import de.avalax.fitbuddy.domain.model.ResourceException;
import de.avalax.fitbuddy.domain.model.exercise.Exercise;
import de.avalax.fitbuddy.domain.model.set.Set;
import de.avalax.fitbuddy.presentation.FitbuddyApplication;
import de.avalax.fitbuddy.presentation.MainActivity;
import de.avalax.fitbuddy.presentation.helper.ExerciseViewHelper;
import de.avalax.fitbuddy.presentation.workout.swipe_bar.PagerOnTouchListener;
import de.avalax.fitbuddy.presentation.workout.swipe_bar.SwipeBarOnTouchListener;
import de.avalax.fitbuddy.presentation.workout.swipe_bar.VerticalProgressbarView;

public class ExerciseFragment extends Fragment {

    private VerticalProgressbarView setProgressBar;
    private VerticalProgressbarView exerciseProgressBar;
    private ProgressBar workoutProgressBar;
    private TextView exerciseNameTextView;
    private TextView exerciseWeightTextView;
    private TextView exercisePreviousTextView;
    private TextView exerciseNextTextView;
    @Inject
    WorkoutApplicationService workoutApplicationService;
    @Inject
    ExerciseViewHelper exerciseViewHelper;
    @Inject
    FinishedWorkoutApplicationService finishedWorkoutApplicationService;

    int exerciseIndex;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_exercise, container, false);
        ((FitbuddyApplication) getActivity().getApplication()).getComponent().inject(this);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        setProgressBar = view.findViewById(R.id.leftProgressBar);
        exerciseProgressBar = view.findViewById(R.id.rightProgressBar);
        workoutProgressBar = view.findViewById(R.id.workoutProgressBar);
        exerciseNameTextView = view.findViewById(R.id.exercises_bar_exercise_name);
        exerciseWeightTextView = view.findViewById(R.id.exercises_bar_set_weight);
        exercisePreviousTextView = view.findViewById(R.id.exercises_bar_previous_exercise_name);
        exerciseNextTextView = view.findViewById(R.id.exercises_bar_next_exercise_name);

        try {
            exerciseIndex = workoutApplicationService.indexOfCurrentExercise();
            Exercise exercise = workoutApplicationService.requestExercise(exerciseIndex);
            int reps = exerciseViewHelper.maxRepsOfExercise(exercise);
            int sets = exerciseViewHelper.setCountOfExercise(exercise);
            setProgressBar.setOnTouchListener(
                    new SwipeBarOnTouchListener(getActivity(), setProgressBar, reps) {
                        @Override
                        public void onFlingEvent(int moved) {
                            try {
                                changeReps(moved);
                            } catch (ResourceException | IOException e) {
                                Log.d("Can't change reps", e.getMessage(), e);
                            }
                        }
                    });

            exerciseProgressBar.setOnTouchListener(
                    new SwipeBarOnTouchListener(getActivity(), exerciseProgressBar, sets) {
                        @Override
                        public void onFlingEvent(int moved) {
                            try {
                                moveToSet(moved);
                            } catch (ResourceException | IOException e) {
                                Log.d("Can't change set", e.getMessage(), e);
                            }
                        }
                    });

            view.findViewById(R.id.pager_exercise)
                    .setOnTouchListener(new PagerOnTouchListener(getActivity()) {
                        @Override
                        protected void onSwipeRight() {
                            if (workoutApplicationService.hasPreviousExercise(exerciseIndex)) {
                                exerciseIndex--;
                                changeExercise();
                            }
                        }

                        @Override
                        protected void onSwipeLeft() {
                            if (workoutApplicationService.hasNextExercise(exerciseIndex)) {
                                exerciseIndex++;
                                changeExercise();
                            } else {
                                finishWorkout();
                            }
                        }
                    });

            updateExerciseProgress();
            updateSetProgress();
            updatePage();
        } catch (ResourceException e) {
            Log.d("Can't create fragment", e.getMessage(), e);
        }
    }

    void finishWorkout() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setMessage(R.string.message_finish_workout)
                .setPositiveButton(android.R.string.yes, (dialog, which) -> {
                    try {
                        workoutApplicationService.finishCurrentWorkout();
                        ((MainActivity) getActivity()).updateBottomNavigation();
                        ((MainActivity) getActivity()).showSummary();
                    } catch (ResourceException e) {
                        Log.d("Can't finish workout", e.getMessage(), e);
                    }
                })
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    }

    void changeExercise() {
        updatePage();
        try {
            updateSetProgress();
            updateExerciseProgress();
        } catch (ResourceException e) {
            Log.d("Can't change exercise", e.getMessage(), e);
        }
    }

    private void changeReps(int moved) throws ResourceException, IOException {
        workoutApplicationService.addRepsToSet(exerciseIndex, moved);
        updateWorkoutProgress();
        updateExerciseProgress();
        updateSetProgress();
    }

{'fraction_non_alphanumeric': 0.08064634057176281, 'fraction_numerical': 0.0003655772464721796, 'mean_word_length': 2.0341614906832297, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3875089', 'n_tokens_mistral': 3375, 'n_tokens_neox': 3188, 'n_words': 540}