Skip to content

Commit

Permalink
refactoring guided by PMD violations
Browse files Browse the repository at this point in the history
  • Loading branch information
avalax committed Jan 11, 2018
1 parent da51251 commit 3e118d3
Show file tree
Hide file tree
Showing 27 changed files with 309 additions and 285 deletions.
19 changes: 19 additions & 0 deletions pmd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,21 @@
<rule ref="category/java/errorprone.xml">
<exclude name="BeanMembersShouldSerialize" />
<exclude name="MissingSerialVersionUID" />
<exclude name="DataflowAnomalyAnalysis" />
</rule>
<rule ref="category/java/multithreading.xml"></rule>
<rule ref="category/java/performance.xml">
<exclude name="AvoidInstantiatingObjectsInLoops" />
<exclude name="AvoidFileStream" />
</rule>


<rule ref="category/java/codestyle.xml/ShortClassName">
<properties>
<property name="minimum">
<value>2</value>
</property>
</properties>
</rule>

<rule ref="category/java/codestyle.xml/ShortVariable">
Expand Down Expand Up @@ -62,4 +73,12 @@
</property>
</properties>
</rule>

<rule ref="category/java/errorprone.xml/AvoidDuplicateLiterals">
<properties>
<property name="maxDuplicateLiterals">
<value>6</value>
</property>
</properties>
</rule>
</ruleset>
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import javax.inject.Inject;

import de.avalax.fitbuddy.application.edit.workout.EditWorkoutApplicationService;
import de.avalax.fitbuddy.application.summary.FinishedWorkoutApplicationService;
import de.avalax.fitbuddy.application.workout.WorkoutApplicationService;
import de.avalax.fitbuddy.application.edit.workout.EditWorkoutService;
import de.avalax.fitbuddy.application.summary.FinishedWorkoutService;
import de.avalax.fitbuddy.application.workout.WorkoutService;
import de.avalax.fitbuddy.presentation.MainActivity;
import de.avalax.fitbuddy.presentation.ad_mob.AdMobProvider;

Expand All @@ -15,11 +15,11 @@

public class FitbuddyActivityTestRule extends ActivityTestRule<MainActivity> {
@Inject
EditWorkoutApplicationService editWorkoutApplicationService;
EditWorkoutService editWorkoutService;
@Inject
WorkoutApplicationService workoutApplicationService;
WorkoutService workoutService;
@Inject
FinishedWorkoutApplicationService finishedWorkoutApplicationService;
FinishedWorkoutService finishedWorkoutService;
@Inject
AdMobProvider adMobProvider;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,30 @@ public PersistenceRunner(FitbuddyActivityTestRule activityRule) {

public Workout addWorkout(BasicWorkoutBuilder builder) throws WorkoutException {
Workout workout = builder.build();
activityRule.editWorkoutApplicationService.saveWorkout(workout);
activityRule.editWorkoutService.saveWorkout(workout);
return workout;
}

public void finishWorkout(Workout workout) throws ResourceException {
activityRule.workoutApplicationService.switchWorkout(workout);
activityRule.workoutApplicationService.finishCurrentWorkout();
activityRule.workoutService.switchWorkout(workout);
activityRule.workoutService.finishCurrentWorkout();
}

public void deleteWorkouts() throws ResourceException {
List<Workout> workouts = activityRule.editWorkoutApplicationService.loadAllWorkouts();
List<Workout> workouts = activityRule.editWorkoutService.loadAllWorkouts();
for (Workout workout : workouts) {
activityRule.editWorkoutApplicationService.deleteWorkout(workout);
activityRule.editWorkoutService.deleteWorkout(workout);
}

if (activityRule.workoutApplicationService.hasActiveWorkout()) {
activityRule.workoutApplicationService.finishCurrentWorkout();
if (activityRule.workoutService.hasActiveWorkout()) {
activityRule.workoutService.finishCurrentWorkout();
}
}

public void deleteFinishedWorkouts() {
List<FinishedWorkout> workouts = activityRule.finishedWorkoutApplicationService.loadAllFinishedWorkouts();
List<FinishedWorkout> workouts = activityRule.finishedWorkoutService.loadAllFinishedWorkouts();
for (FinishedWorkout finishedWorkout : workouts) {
activityRule.finishedWorkoutApplicationService.delete(finishedWorkout);
activityRule.finishedWorkoutService.delete(finishedWorkout);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import de.avalax.fitbuddy.domain.model.workout.WorkoutException;
import de.avalax.fitbuddy.domain.model.workout.WorkoutRepository;

public class EditWorkoutApplicationService {
public class EditWorkoutService {

private WorkoutRepository workoutRepository;

public EditWorkoutApplicationService(
public EditWorkoutService(
WorkoutRepository workoutRepository) {
this.workoutRepository = workoutRepository;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import de.avalax.fitbuddy.domain.model.finished_workout.FinishedWorkoutId;
import de.avalax.fitbuddy.domain.model.finished_workout.FinishedWorkoutRepository;

public class FinishedWorkoutApplicationService {
public class FinishedWorkoutService {
private FinishedWorkoutRepository finishedWorkoutRepository;

public FinishedWorkoutApplicationService(FinishedWorkoutRepository finishedWorkoutRepository) {
public FinishedWorkoutService(FinishedWorkoutRepository finishedWorkoutRepository) {
this.finishedWorkoutRepository = finishedWorkoutRepository;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,17 @@
import de.avalax.fitbuddy.domain.model.workout.Workout;
import de.avalax.fitbuddy.domain.model.workout.WorkoutException;

public class WorkoutApplicationService {
public class WorkoutService {
private WorkoutSession workoutSession;
private FinishedWorkoutRepository finishedWorkoutRepository;

public WorkoutApplicationService(
public WorkoutService(
WorkoutSession workoutSession,
FinishedWorkoutRepository finishedWorkoutRepository) {
this.workoutSession = workoutSession;
this.finishedWorkoutRepository = finishedWorkoutRepository;
}

@Deprecated
public int countOfExercises() throws ResourceException {
return getWorkout().getExercises().size();
}

@Deprecated
public Exercise requestExercise(int position) throws ResourceException {
return getWorkout().getExercises().get(position);
Expand Down Expand Up @@ -52,14 +47,6 @@ public void setCurrentExercise(int index) throws ResourceException {
workoutSession.saveCurrentWorkout();
}

public void updateWeightOfCurrentSet(int index, double weight) throws ResourceException {
Exercise exercise = requestExercise(index);
int indexOfCurrentSet = exercise.getSets().indexOfCurrentSet();
exercise.getSets().get(indexOfCurrentSet).setWeight(weight);
//TODO only saveWorkout by android lifecycle
workoutSession.saveCurrentWorkout();
}

@Deprecated
public int indexOfCurrentExercise() throws ResourceException {
return getWorkout().getExercises().indexOfCurrentExercise();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public class WorkoutSession {

public WorkoutSession(Context context) {
this.context = context;
this.workout = readCurrentWorkoutFromFile();
try {
this.workout = readCurrentWorkoutFromFile();
} catch (IOException | ClassNotFoundException ignored) {
}
}

public void switchWorkout(Workout workout) throws WorkoutException {
Expand All @@ -42,26 +45,22 @@ public Workout getWorkout() {
return workout;
}

private Workout readCurrentWorkoutFromFile() {
Workout workout = null;
private Workout readCurrentWorkoutFromFile() throws IOException, ClassNotFoundException {
File file = new File(context.getDir("data", Context.MODE_PRIVATE), "currentWorkout");
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
workout = (Workout) ois.readObject();
ois.close();
fis.close();
} catch (IOException | ClassNotFoundException ignored) {
try (FileInputStream fis = new FileInputStream(file)) {
try (ObjectInputStream ois = new ObjectInputStream(fis)) {
return (Workout) ois.readObject();
}
}
return workout;
}

protected void writeCurrentWorkoutToFile() throws IOException {
File file = new File(context.getDir("data", Context.MODE_PRIVATE), "currentWorkout");
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
outputStream.writeObject(workout);
outputStream.flush();
outputStream.close();
try (FileOutputStream fis = new FileOutputStream(file)) {
try (ObjectOutputStream outputStream = new ObjectOutputStream(fis)) {
outputStream.writeObject(workout);
}
}
}

public boolean hasWorkout() {
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/de/avalax/fitbuddy/presentation/FitbuddyModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import dagger.Module;
import dagger.Provides;
import de.avalax.fitbuddy.R;
import de.avalax.fitbuddy.application.edit.workout.EditWorkoutApplicationService;
import de.avalax.fitbuddy.application.summary.FinishedWorkoutApplicationService;
import de.avalax.fitbuddy.application.workout.WorkoutApplicationService;
import de.avalax.fitbuddy.application.edit.workout.EditWorkoutService;
import de.avalax.fitbuddy.application.summary.FinishedWorkoutService;
import de.avalax.fitbuddy.application.workout.WorkoutService;
import de.avalax.fitbuddy.application.workout.WorkoutSession;
import de.avalax.fitbuddy.domain.model.exercise.ExerciseRepository;
import de.avalax.fitbuddy.domain.model.finished_exercise.FinishedExerciseRepository;
Expand Down Expand Up @@ -59,16 +59,16 @@ public FitbuddyModule(Context context) {

@Provides
@Singleton
WorkoutApplicationService provideWorkoutApplicationService() {
return new WorkoutApplicationService(
WorkoutService provideWorkoutService() {
return new WorkoutService(
workoutSession,
finishedWorkoutRepository);
}

@Provides
@Singleton
FinishedWorkoutApplicationService provideFinishedWorkoutApplicationService() {
return new FinishedWorkoutApplicationService(finishedWorkoutRepository);
FinishedWorkoutService provideFinishedWorkoutService() {
return new FinishedWorkoutService(finishedWorkoutRepository);
}

@Provides
Expand All @@ -80,8 +80,8 @@ ExerciseViewHelper provideExerciseViewHelper() {

@Provides
@Singleton
EditWorkoutApplicationService provideManageWorkout() {
return new EditWorkoutApplicationService(
EditWorkoutService provideManageWorkout() {
return new EditWorkoutService(
workoutRepository);
}

Expand All @@ -105,7 +105,7 @@ FinishedExerciseViewHelper provideFinishedExerciseViewHelper() {

@Provides
@Singleton
EditExerciseViewHelper provideEditExerciseApplicationService() {
EditExerciseViewHelper provideEditExerciseViewHelper() {
return new EditExerciseViewHelper(context);
}

Expand Down
22 changes: 11 additions & 11 deletions src/main/java/de/avalax/fitbuddy/presentation/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import javax.inject.Inject;

import de.avalax.fitbuddy.R;
import de.avalax.fitbuddy.application.edit.workout.EditWorkoutApplicationService;
import de.avalax.fitbuddy.application.summary.FinishedWorkoutApplicationService;
import de.avalax.fitbuddy.application.workout.WorkoutApplicationService;
import de.avalax.fitbuddy.application.edit.workout.EditWorkoutService;
import de.avalax.fitbuddy.application.summary.FinishedWorkoutService;
import de.avalax.fitbuddy.application.workout.WorkoutService;
import de.avalax.fitbuddy.domain.model.finished_workout.FinishedWorkout;
import de.avalax.fitbuddy.domain.model.workout.Workout;
import de.avalax.fitbuddy.domain.model.workout.WorkoutException;
Expand All @@ -35,12 +35,12 @@ public class MainActivity extends AppCompatActivity
private Menu menu;
private BottomNavigationView bottomNavigation;
@Inject
EditWorkoutApplicationService editWorkoutApplicationService;
EditWorkoutService editWorkoutService;
@Inject
FinishedWorkoutApplicationService finishedWorkoutApplicationService;
FinishedWorkoutService finishedWorkoutService;

@Inject
WorkoutApplicationService workoutApplicationService;
WorkoutService workoutService;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -64,8 +64,8 @@ protected void onCreate(Bundle savedInstanceState) {
public void updateBottomNavigation() {
MenuItem itemWorkout = bottomNavigation.getMenu().getItem(1);
MenuItem itemFinishedWorkout = bottomNavigation.getMenu().getItem(2);
itemWorkout.setEnabled(workoutApplicationService.hasActiveWorkout());
itemFinishedWorkout.setEnabled(finishedWorkoutApplicationService.hasFinishedWorkouts());
itemWorkout.setEnabled(workoutService.hasActiveWorkout());
itemFinishedWorkout.setEnabled(finishedWorkoutService.hasFinishedWorkouts());
}

@Override
Expand All @@ -87,15 +87,15 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
if (item.getItemId() == R.id.toolbar_delete_workout) {
Workout workout = (Workout) item.getIntent().getSerializableExtra("workout");
editWorkoutApplicationService.deleteWorkout(workout);
editWorkoutService.deleteWorkout(workout);
removeWorkoutFromList(workout);
mainToolbar();
return true;
}
if (item.getItemId() == R.id.toolbar_delete_finished_workout) {
FinishedWorkout workout = (FinishedWorkout) item.getIntent()
.getSerializableExtra("finishedWorkout");
finishedWorkoutApplicationService.delete(workout);
finishedWorkoutService.delete(workout);
removeFinishedWorkoutFromList(workout);
mainToolbar();
return true;
Expand Down Expand Up @@ -160,7 +160,7 @@ public void mainToolbar() {

public void selectWorkout(Workout workout) {
try {
workoutApplicationService.switchWorkout(workout);
workoutService.switchWorkout(workout);
updateBottomNavigation();
} catch (WorkoutException e) {
Log.e("WorkoutException", e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,17 @@

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import de.avalax.fitbuddy.R;

public class SelectableViewHolder extends RecyclerView.ViewHolder {
private final TextView titleTextView;
private final TextView subtitleTextView;
private final int backgroundColor;
private final int highlightColor;
private boolean selected;

public SelectableViewHolder(View view, int backgroundColor, int highlightColor) {
super(view);
titleTextView = view.findViewById(R.id.item_title);
subtitleTextView = view.findViewById(R.id.item_subtitle);
this.backgroundColor = backgroundColor;
this.highlightColor = highlightColor;
}

public TextView getTitleTextView() {
return titleTextView;
}

public TextView getSubtitleTextView() {
return subtitleTextView;
this.selected = false;
}

public View getView() {
Expand Down
Loading

0 comments on commit 3e118d3

Please sign in to comment.