From 2c5b78ad4d2aa3291a42d8df0856135143983440 Mon Sep 17 00:00:00 2001 From: "Richard K." Date: Thu, 25 Jul 2024 17:48:40 -0400 Subject: [PATCH] Fixed checkstyle --- .../healthtrack/model/SortingContext.java | 2 - .../healthtrack/view/WorkoutPlanAdapter.java | 3 - .../healthtrack/view/WorkoutPlans.java | 309 +++++++++--------- 3 files changed, 152 insertions(+), 162 deletions(-) diff --git a/HealthTrack/app/src/main/java/com/example/healthtrack/model/SortingContext.java b/HealthTrack/app/src/main/java/com/example/healthtrack/model/SortingContext.java index 0a31cb5..9e799ce 100644 --- a/HealthTrack/app/src/main/java/com/example/healthtrack/model/SortingContext.java +++ b/HealthTrack/app/src/main/java/com/example/healthtrack/model/SortingContext.java @@ -1,7 +1,5 @@ package com.example.healthtrack.model; -import com.example.healthtrack.model.SortingStrategy; - import java.util.ArrayList; public class SortingContext { diff --git a/HealthTrack/app/src/main/java/com/example/healthtrack/view/WorkoutPlanAdapter.java b/HealthTrack/app/src/main/java/com/example/healthtrack/view/WorkoutPlanAdapter.java index 398862b..e69db30 100644 --- a/HealthTrack/app/src/main/java/com/example/healthtrack/view/WorkoutPlanAdapter.java +++ b/HealthTrack/app/src/main/java/com/example/healthtrack/view/WorkoutPlanAdapter.java @@ -11,7 +11,6 @@ import android.widget.Toast; import androidx.annotation.NonNull; -import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.RecyclerView; import com.example.healthtrack.R; @@ -21,8 +20,6 @@ import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; -import org.w3c.dom.Text; - import java.util.ArrayList; public class WorkoutPlanAdapter extends RecyclerView.Adapter { diff --git a/HealthTrack/app/src/main/java/com/example/healthtrack/view/WorkoutPlans.java b/HealthTrack/app/src/main/java/com/example/healthtrack/view/WorkoutPlans.java index 0314d78..cea960f 100644 --- a/HealthTrack/app/src/main/java/com/example/healthtrack/view/WorkoutPlans.java +++ b/HealthTrack/app/src/main/java/com/example/healthtrack/view/WorkoutPlans.java @@ -3,14 +3,11 @@ import android.app.Dialog; import android.content.Intent; import android.os.Bundle; -import android.os.Handler; -import android.os.HandlerThread; import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; -import android.widget.ImageButton; import android.widget.Toast; import com.example.healthtrack.model.NameSortStrategy; @@ -29,11 +26,7 @@ import androidx.recyclerview.widget.RecyclerView; import com.example.healthtrack.R; -import com.example.healthtrack.model.Workout; -import com.example.healthtrack.model.WorkoutPlan; import com.example.healthtrack.viewModel.WorkoutPlanViewModel; -import com.example.healthtrack.viewModel.WorkoutViewModel; -import com.google.firebase.Firebase; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; import com.google.firebase.database.ChildEventListener; @@ -41,11 +34,8 @@ import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; -import com.google.firebase.database.ValueEventListener; import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; import java.util.HashMap; public class WorkoutPlans extends AppCompatActivity { @@ -57,33 +47,46 @@ public class WorkoutPlans extends AppCompatActivity { private ArrayList planList; private ArrayList unfilteredList; private ArrayList workoutNameArrayList; - private WorkoutPlanViewModel workoutPlanViewModel; private SearchView searchBar; private SortingStrategy search = new NameSortStrategy(); private String lastQuery = ""; private FirebaseAuth mAuth; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_workout_plans); + initializeFields(); + setupRecyclerView(); + setupSearchBar(); + setupChildEventListeners(); + setupDialog(); + setupButtons(); + } - + private void initializeFields() { mAuth = FirebaseAuth.getInstance(); - recyclerView = findViewById(R.id.planList); database = FirebaseDatabase.getInstance().getReference("workout plans"); FirebaseUser currentUser = mAuth.getCurrentUser(); String userId = currentUser.getUid(); workoutDatabase = new WorkoutDatabaseRepository().getWorkoutsReference(userId); workoutPlanViewModel = new ViewModelProvider(this).get(WorkoutPlanViewModel.class); - recyclerView.setHasFixedSize(true); - recyclerView.setLayoutManager(new LinearLayoutManager(this)); planList = new ArrayList<>(); unfilteredList = new ArrayList<>(); workoutNameArrayList = new ArrayList<>(); + } + + private void setupRecyclerView() { + recyclerView = findViewById(R.id.planList); + recyclerView.setHasFixedSize(true); + recyclerView.setLayoutManager(new LinearLayoutManager(this)); WorkoutPlanAdapter workoutPlanAdapter = new WorkoutPlanAdapter(this, planList, workoutNameArrayList); recyclerView.setAdapter(workoutPlanAdapter); + } + + private void setupSearchBar() { searchBar = findViewById(R.id.searchView); searchBar.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override @@ -93,27 +96,35 @@ public boolean onQueryTextSubmit(String query) { @Override public boolean onQueryTextChange(String newText) { - if (!newText.isEmpty()) { - lastQuery = newText; - NameSortStrategy newSearch = new NameSortStrategy(); - newSearch.setName(newText); - search = newSearch; - planList = searchList(unfilteredList); - recyclerView.setAdapter(new WorkoutPlanAdapter(WorkoutPlans.this, planList, workoutNameArrayList)); - return true; - } else { - planList = refreshList(unfilteredList); - recyclerView.setAdapter(new WorkoutPlanAdapter(WorkoutPlans.this, planList, workoutNameArrayList)); - return true; - } + handleSearchQueryChange(newText); + return true; } }); + } + + private void handleSearchQueryChange(String newText) { + if (!newText.isEmpty()) { + lastQuery = newText; + NameSortStrategy newSearch = new NameSortStrategy(); + newSearch.setName(newText); + search = newSearch; + planList = searchList(unfilteredList); + } else { + planList = refreshList(unfilteredList); + } + recyclerView.setAdapter(new WorkoutPlanAdapter(WorkoutPlans.this, planList, workoutNameArrayList)); + } + + private void setupChildEventListeners() { + setupWorkoutDatabaseListener(); + setupDatabaseListener(); + } + private void setupWorkoutDatabaseListener() { workoutDatabase.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { - HashMap workoutData = - (HashMap) snapshot.getValue(); + HashMap workoutData = (HashMap) snapshot.getValue(); String name = (String) workoutData.get("name"); workoutNameArrayList.add(name); } @@ -138,57 +149,18 @@ public void onCancelled(@NonNull DatabaseError error) { } }); + } + private void setupDatabaseListener() { database.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { - HashMap> data = - (HashMap>) snapshot.getValue(); - ArrayList> dataList = new ArrayList(data.values()); - for(int i = 0; i < dataList.size(); i++) { - HashMap dataMap = dataList.get(i); - String userId = (String) dataMap.get("userId"); - String name = (String) dataMap.get("name"); - Integer calsPerSet = - Integer.parseInt(dataMap.get("caloriesPerSet").toString()); - Integer repsPerSet = Integer.parseInt(dataMap.get("repsPerSet").toString()); - Integer sets = Integer.parseInt(dataMap.get("sets").toString()); - String notes = (String) dataMap.get("notes"); - Integer time = Integer.parseInt(dataMap.get("time").toString()); - - WorkoutPlan plan = new WorkoutPlan(userId, name, calsPerSet, sets, - repsPerSet, time, notes); - planList.add(plan); - System.out.println(plan); - unfilteredList.add(plan); - recyclerView.setAdapter(new WorkoutPlanAdapter(WorkoutPlans.this, planList, workoutNameArrayList)); - } + handleDatabaseChildEvent(snapshot); } @Override public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { - HashMap> data = - (HashMap>) snapshot.getValue(); - ArrayList> dataList = new ArrayList(data.values()); - for(int i = 0; i < dataList.size(); i++) { - HashMap dataMap = dataList.get(i); - String userId = (String) dataMap.get("userId"); - String name = (String) dataMap.get("name"); - Integer calsPerSet = - Integer.parseInt(dataMap.get("caloriesPerSet").toString()); - Integer repsPerSet = Integer.parseInt(dataMap.get("repsPerSet").toString()); - Integer sets = Integer.parseInt(dataMap.get("sets").toString()); - String notes = (String) dataMap.get("notes"); - Integer time = Integer.parseInt(dataMap.get("time").toString()); - - WorkoutPlan plan = new WorkoutPlan(userId, name, calsPerSet, sets, - repsPerSet, time, notes); - if (!planList.contains(plan)) { - planList.add(plan); - unfilteredList.add(plan); - } - recyclerView.setAdapter(new WorkoutPlanAdapter(WorkoutPlans.this, planList, workoutNameArrayList)); - } + handleDatabaseChildEvent(snapshot); } @Override @@ -206,7 +178,34 @@ public void onCancelled(@NonNull DatabaseError error) { recyclerView.setAdapter(new WorkoutPlanAdapter(WorkoutPlans.this, planList, workoutNameArrayList)); } }); + } + + private void handleDatabaseChildEvent(@NonNull DataSnapshot snapshot) { + HashMap> data = (HashMap>) snapshot.getValue(); + ArrayList> dataList = new ArrayList<>(data.values()); + for (HashMap dataMap : dataList) { + WorkoutPlan plan = createWorkoutPlanFromDataMap(dataMap); + if (!planList.contains(plan)) { + planList.add(plan); + unfilteredList.add(plan); + } + recyclerView.setAdapter(new WorkoutPlanAdapter(WorkoutPlans.this, planList, workoutNameArrayList)); + } + } + + private WorkoutPlan createWorkoutPlanFromDataMap(HashMap dataMap) { + String userId = (String) dataMap.get("userId"); + String name = (String) dataMap.get("name"); + int calsPerSet = Integer.parseInt(dataMap.get("caloriesPerSet").toString()); + int repsPerSet = Integer.parseInt(dataMap.get("repsPerSet").toString()); + int sets = Integer.parseInt(dataMap.get("sets").toString()); + String notes = (String) dataMap.get("notes"); + int time = Integer.parseInt(dataMap.get("time").toString()); + return new WorkoutPlan(userId, name, calsPerSet, sets, repsPerSet, time, notes); + } + + private void setupDialog() { dialog = new Dialog(WorkoutPlans.this); dialog.setContentView(R.layout.workout_plan_popout); dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); @@ -215,117 +214,115 @@ public void onCancelled(@NonNull DatabaseError error) { dialog.setCanceledOnTouchOutside(true); btnDialogAdd = dialog.findViewById(R.id.publishPlan); - btnDialogAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - EditText workoutNameInput = dialog.findViewById(R.id.editWorkoutName); - EditText notesInput = dialog.findViewById(R.id.notes); - EditText setsInput = dialog.findViewById(R.id.sets); - EditText repsInput = dialog.findViewById(R.id.reps); - EditText timeInput = dialog.findViewById(R.id.time); - EditText caloriesInput = dialog.findViewById(R.id.calories); - - String workoutName = workoutNameInput.getText().toString(); - String notes = notesInput.getText().toString(); - String sets = setsInput.getText().toString(); - String reps = repsInput.getText().toString(); - String time = timeInput.getText().toString(); - String calories = caloriesInput.getText().toString(); - - if (TextUtils.isEmpty(workoutName) || TextUtils.isEmpty(sets) || TextUtils.isEmpty(reps) - || TextUtils.isEmpty(calories) || TextUtils.isEmpty(time)) { - Toast.makeText(WorkoutPlans.this, - "Please fill in all necessary fields", Toast.LENGTH_SHORT).show(); - return; - } - - int setsInt = Integer.parseInt(sets); - int repsInt = Integer.parseInt(reps); - int calsInt = Integer.parseInt(calories); - int timeInt = Integer.parseInt(time); - - FirebaseUser currentUser = mAuth.getCurrentUser(); - - String userId = currentUser.getUid(); - - WorkoutPlan workoutPlan = new WorkoutPlan(userId, workoutName, calsInt, setsInt, repsInt, timeInt, notes); - - if (planList.contains(workoutPlan)) { - Toast.makeText(WorkoutPlans.this, - "Duplicate plans not allowed", Toast.LENGTH_LONG).show(); - return; - } - - // Save data to Firebase - workoutPlanViewModel.addWorkoutPlan(userId, workoutPlan); - - workoutNameInput.setText(""); - notesInput.setText(""); - setsInput.setText(""); - repsInput.setText(""); - timeInput.setText(""); - caloriesInput.setText(""); - - dialog.dismiss(); + handleDialogAddButtonClick(); } }); + } + + private void handleDialogAddButtonClick() { + EditText workoutNameInput = dialog.findViewById(R.id.editWorkoutName); + EditText notesInput = dialog.findViewById(R.id.notes); + EditText setsInput = dialog.findViewById(R.id.sets); + EditText repsInput = dialog.findViewById(R.id.reps); + EditText timeInput = dialog.findViewById(R.id.time); + EditText caloriesInput = dialog.findViewById(R.id.calories); + + String workoutName = workoutNameInput.getText().toString(); + String notes = notesInput.getText().toString(); + String sets = setsInput.getText().toString(); + String reps = repsInput.getText().toString(); + String time = timeInput.getText().toString(); + String calories = caloriesInput.getText().toString(); + + if (areInputsValid(workoutName, sets, reps, calories, time)) { + createAndSaveWorkoutPlan(workoutName, notes, sets, reps, time, calories); + clearDialogInputs(workoutNameInput, notesInput, setsInput, repsInput, timeInput, caloriesInput); + dialog.dismiss(); + } else { + Toast.makeText(WorkoutPlans.this, "Please fill in all necessary fields", Toast.LENGTH_SHORT).show(); + } + } + + private boolean areInputsValid(String workoutName, String sets, String reps, String calories, String time) { + return !TextUtils.isEmpty(workoutName) && !TextUtils.isEmpty(sets) && !TextUtils.isEmpty(reps) + && !TextUtils.isEmpty(calories) && !TextUtils.isEmpty(time); + } + + private void createAndSaveWorkoutPlan(String workoutName, String notes, String sets, String reps, String time, String calories) { + int setsInt = Integer.parseInt(sets); + int repsInt = Integer.parseInt(reps); + int calsInt = Integer.parseInt(calories); + int timeInt = Integer.parseInt(time); + FirebaseUser currentUser = mAuth.getCurrentUser(); + String userId = currentUser.getUid(); + WorkoutPlan workoutPlan = new WorkoutPlan(userId, workoutName, calsInt, setsInt, repsInt, timeInt, notes); + + if (planList.contains(workoutPlan)) { + Toast.makeText(WorkoutPlans.this, "Duplicate plans not allowed", Toast.LENGTH_LONG).show(); + } else { + workoutPlanViewModel.addWorkoutPlan(userId, workoutPlan); + } + } - Button backButton = findViewById(R.id.btn_plans_back); - backButton.setOnClickListener(new View.OnClickListener() { + private void clearDialogInputs(EditText... inputs) { + for (EditText input : inputs) { + input.setText(""); + } + } + + private void setupButtons() { + findViewById(R.id.btn_plans_back).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Intent intent = new Intent(WorkoutPlans.this, HomeActivity.class); - startActivity(intent); + startActivity(new Intent(WorkoutPlans.this, HomeActivity.class)); } }); + setupNavigationButtons(); + setupCreatePlanButton(); + setupRefreshButton(); + } - // Calorie Button - Button openCalorieButton = findViewById(R.id.caloriesButton); - openCalorieButton.setOnClickListener(new View.OnClickListener() { + private void setupNavigationButtons() { + findViewById(R.id.caloriesButton).setOnClickListener(new View.OnClickListener() { + @Override public void onClick(View v) { - Intent intent = new Intent(WorkoutPlans.this, CalorieTracking.class); - startActivity(intent); + startActivity(new Intent(WorkoutPlans.this, CalorieTracking.class)); } }); - - // Tracker Button - Button openTrackerButton = findViewById(R.id.trackerButton); - openTrackerButton.setOnClickListener(new View.OnClickListener() { + findViewById(R.id.trackerButton).setOnClickListener(new View.OnClickListener() { + @Override public void onClick(View v) { - Intent intent = new Intent(WorkoutPlans.this, WorkoutTracker.class); - startActivity(intent); + startActivity(new Intent(WorkoutPlans.this, WorkoutTracker.class)); } }); - - // Workouts Button - Button openWorkoutsButton = findViewById(R.id.workoutsButton); - openWorkoutsButton.setOnClickListener(new View.OnClickListener() { + findViewById(R.id.workoutsButton).setOnClickListener(new View.OnClickListener() { + @Override public void onClick(View v) { - Intent intent = new Intent(WorkoutPlans.this, WorkoutPlans.class); - startActivity(intent); + startActivity(new Intent(WorkoutPlans.this, WorkoutPlans.class)); } }); - - // Community Button - Button openCommunityButton = findViewById(R.id.communityButton); - openCommunityButton.setOnClickListener(new View.OnClickListener() { + findViewById(R.id.communityButton).setOnClickListener(new View.OnClickListener() { + @Override public void onClick(View v) { - Intent intent = new Intent(WorkoutPlans.this, CommunityScreen.class); - startActivity(intent); + startActivity(new Intent(WorkoutPlans.this, CommunityScreen.class)); } }); + } - Button createPlan = findViewById(R.id.plan_btn); - createPlan.setOnClickListener(new View.OnClickListener() { + private void setupCreatePlanButton() { + findViewById(R.id.plan_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.show(); } }); + } - ImageButton refreshButton = findViewById(R.id.buttonRefresh); - refreshButton.setOnClickListener(new View.OnClickListener() { + private void setupRefreshButton() { + findViewById(R.id.buttonRefresh).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { planList = refreshList(unfilteredList); @@ -334,8 +331,6 @@ public void onClick(View v) { }); } - - // sort methods to call public ArrayList refreshList(ArrayList list) { SortingStrategy refresh = new RefreshSortStrategy(); return workoutPlanViewModel.filter(refresh, list); @@ -344,4 +339,4 @@ public ArrayList refreshList(ArrayList list) { public ArrayList searchList(ArrayList list) { return workoutPlanViewModel.filter(search, list); } -} \ No newline at end of file +}