diff --git a/app/src/main/java/be/lepl1509group13/workoutwarrior/ListExercicesAdapter.java b/app/src/main/java/be/lepl1509group13/workoutwarrior/ListExercicesAdapter.java index d1d1f9c70b794c3edbdb528ad1d729321cf26bde..624d6a99e1e4be6379e1518949a19db3ab9ad62b 100644 --- a/app/src/main/java/be/lepl1509group13/workoutwarrior/ListExercicesAdapter.java +++ b/app/src/main/java/be/lepl1509group13/workoutwarrior/ListExercicesAdapter.java @@ -1,25 +1,51 @@ package be.lepl1509group13.workoutwarrior; +import be.lepl1509group13.workoutwarrior.ProgramCreationActivity; +import android.app.Dialog; import android.content.Context; import android.graphics.drawable.Drawable; +import android.text.InputType; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; +import androidx.constraintlayout.widget.ConstraintLayout; + import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import be.lepl1509group13.workoutwarrior.ProgramCreationActivity; + public class ListExercicesAdapter extends ArrayAdapter<String> { private final Context context; private ArrayList<String> values; + public Exercise actual_exo ; + public String img_actual_exo; + HashMap<Exercise, ArrayList<String>> program = new HashMap<>(); + + String name_exo; + String description_exo; + Long timer_exo; + Long break_exo; + ArrayList<String> days_exo = new ArrayList<String>(); + CheckBox cb_lundi, cb_mardi, cb_mercredi, cb_jeudi, cb_vendredi, cb_samedi, cb_dimanche; + private ArrayList<CheckBox> cb_list = new ArrayList<>(); + private ArrayList<String> current_days_checked = new ArrayList<>(); + Integer nbre_day = 0; + public ListExercicesAdapter(Context context, ArrayList<String> values) { super(context, R.layout.exercises_list_row, values); this.context = context; @@ -65,8 +91,160 @@ public class ListExercicesAdapter extends ArrayAdapter<String> { // Ecouteur de clics pour chaque élément de la ListView edit_button.setOnClickListener(view -> { - System.out.println("details : " + Arrays.toString(activity.getClass().getDeclaredFields())); - System.out.println("details : " + activity.break_time); + days_exo = new ArrayList<>(); + for (Map.Entry<Exercise, ArrayList<String>> set : activity.program.entrySet()) { + if (set.getKey().name == exercise) { + for (String day : set.getValue()) { + if(!days_exo.contains(day)){ + days_exo.add(day); + } + } + name_exo = set.getKey().name; + description_exo = set.getKey().description; + timer_exo = Long.valueOf(set.getKey().timer); + break_exo = Long.valueOf(set.getKey().breakVal); + img_actual_exo = set.getKey().image_url; + } + } + Dialog dialog = new Dialog(this.context); + dialog.setContentView(R.layout.modify_exercice_popup); + + ImageView closeButton = dialog.findViewById(R.id.close_btn); + closeButton.setOnClickListener(w -> dialog.dismiss()); + cb_lundi = dialog.findViewById(R.id.checkbox_monday); + cb_mardi = dialog.findViewById(R.id.checkbox_tuesday); + cb_mercredi = dialog.findViewById(R.id.checkbox_wednesday); + cb_jeudi = dialog.findViewById(R.id.checkbox_thursday); + cb_vendredi = dialog.findViewById(R.id.checkbox_friday); + cb_samedi = dialog.findViewById(R.id.checkbox_saturday); + cb_dimanche = dialog.findViewById(R.id.checkbox_sunday); + + cb_list.add(cb_lundi); + cb_list.add(cb_mardi); + cb_list.add(cb_mercredi); + cb_list.add(cb_jeudi); + cb_list.add(cb_vendredi); + cb_list.add(cb_samedi); + cb_list.add(cb_dimanche); + + for(int i = 0; i < days_exo.size(); i++){ + if(days_exo.get(i).equals("lundi")){ + cb_lundi.setChecked(true); + current_days_checked.add(days_exo.get(i)); + nbre_day += 1; + } + if(days_exo.get(i).equals("mardi")){ + cb_mardi.setChecked(true); + current_days_checked.add(days_exo.get(i)); + nbre_day += 1; + } + if(days_exo.get(i).equals("mercredi")){ + cb_mercredi.setChecked(true); + current_days_checked.add(days_exo.get(i)); + nbre_day += 1; + } + if(days_exo.get(i).equals("jeudi")){ + cb_jeudi.setChecked(true); + current_days_checked.add(days_exo.get(i)); + nbre_day += 1; + } + if(days_exo.get(i).equals("vendredi")){ + cb_vendredi.setChecked(true); + current_days_checked.add(days_exo.get(i)); + nbre_day += 1; + } + if(days_exo.get(i).equals("samedi")){ + cb_samedi.setChecked(true); + current_days_checked.add(days_exo.get(i)); + nbre_day += 1; + } + if(days_exo.get(i).equals("dimanche")){ + cb_dimanche.setChecked(true); + current_days_checked.add(days_exo.get(i)); + nbre_day += 1; + } + } + + for (CheckBox cb : cb_list) cb.setOnCheckedChangeListener(myCheckBoxListener); + ConstraintLayout form_layout = dialog.findViewById(R.id.constraintLayout2); + + EditText description_text = dialog.findViewById(R.id.exercise_description); + EditText timer_edit = dialog.findViewById(R.id.exercise_timer); + EditText break_edit = dialog.findViewById(R.id.exercice_break); + TextView timer_text = dialog.findViewById(R.id.timer_text); + if(description_exo != null){ + description_text.setText(description_exo); + } + if(timer_exo == 0){ + timer_edit.setVisibility(View.INVISIBLE); + timer_edit.setEnabled(false); + timer_text.setText(""); + }else{ + timer_edit.setText(timer_exo.toString()); + } + if(break_exo != null){ + break_edit.setText(break_exo.toString()); + } + + dialog.show(); + Button save_btn = dialog.findViewById(R.id.btn_save_modif); + save_btn.setOnClickListener(w -> { + if(nbre_day > 0){ + String description_str = description_text.getText().toString(); + String string_break = break_edit.getText().toString(); + Integer break_time = Integer.parseInt(string_break); + String string_timer = timer_edit.getText().toString(); + if(string_timer.equals("")){ + actual_exo = new Exercise(name_exo, description_str, break_time, "@drawable/developpe_couche_barre", 0); + }else{ + Integer timer_int = Integer.parseInt(string_timer); + actual_exo = new Exercise(name_exo, description_str, break_time, "@drawable/developpe_couche_barre", timer_int); + } + Iterator<Map.Entry<Exercise, ArrayList<String>>> iterator = activity.program.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry<Exercise, ArrayList<String>> entry = iterator.next(); + Exercise key = entry.getKey(); + if (key.name.equals(exercise)) { + key.description = description_str; + key.breakVal = break_time; + key.timer = Math.toIntExact(timer_exo); + } + } + dialog.dismiss(); + } + else{ + Toast.makeText(this.context, "Veuillez sélectionner au moins un jour !", + Toast.LENGTH_SHORT).show(); + } + }); + + form_layout.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (description_text.hasFocus()) { + description_text.clearFocus(); + } + } + }); + + description_text.setOnFocusChangeListener(new View.OnFocusChangeListener() { + @Override + public void onFocusChange(View v, boolean hasFocus) { + if (!hasFocus) { + description_text.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); + } else { + description_text.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); + } + } + }); + TextView name_exercice = dialog.findViewById(R.id.exercice_to_modify_name); + name_exercice.setText(name_exo); + + int resId = context.getResources().getIdentifier(img_actual_exo, "drawable", context.getPackageName()); + System.out.println(img_actual_exo); + System.out.println(resId); + ImageView image = dialog.findViewById(R.id.exercice_to_modify_image); + image.setImageResource(resId); }); // Ajouter un OnClickListener à l'image @@ -102,10 +280,6 @@ public class ListExercicesAdapter extends ArrayAdapter<String> { notifyDataSetChanged(); } - private void save_workout_details() { - - } - String getShortDayName(String day) { if (day == "lundi") return "LU"; if (day == "mardi") return "MA"; @@ -117,7 +291,70 @@ public class ListExercicesAdapter extends ArrayAdapter<String> { else throw new IllegalArgumentException(); } - + public CompoundButton.OnCheckedChangeListener myCheckBoxListener = + new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (buttonView == cb_lundi) { + if(current_days_checked.contains("lundi")){ + current_days_checked.remove("lundi"); + nbre_day -= 1; + }else{ + current_days_checked.add("lundi"); + nbre_day += 1; + } + } else if (buttonView == cb_mardi) { + if(current_days_checked.contains("mardi")){ + current_days_checked.remove("mardi"); + nbre_day -= 1; + }else{ + current_days_checked.add("mardi"); + nbre_day += 1; + } + } else if (buttonView == cb_mercredi) { + if(current_days_checked.contains("mercredi")){ + current_days_checked.remove("mercredi"); + nbre_day -= 1; + }else{ + current_days_checked.add("mercredi"); + nbre_day += 1; + } + } else if (buttonView == cb_jeudi) { + if(current_days_checked.contains("jeudi")){ + current_days_checked.remove("jeudi"); + nbre_day -= 1; + }else{ + current_days_checked.add("jeudi"); + nbre_day += 1; + } + } else if (buttonView == cb_vendredi) { + if(current_days_checked.contains("vendredi")){ + current_days_checked.remove("vendredi"); + nbre_day -= 1; + }else{ + current_days_checked.add("vendredi"); + nbre_day += 1; + } + } else if (buttonView == cb_samedi) { + if(current_days_checked.contains("samedi")){ + current_days_checked.remove("samedi"); + nbre_day -= 1; + }else{ + current_days_checked.add("samedi"); + nbre_day += 1; + } + } else if (buttonView == cb_dimanche) { + if(current_days_checked.contains("dimanche")){ + current_days_checked.remove("dimanche"); + nbre_day -= 1; + }else{ + current_days_checked.add("dimanche"); + nbre_day += 1; + } + //for (CheckBox cb : cb_list) if (!Objects.equals(cb, cb_dimanche)) cb.setChecked(false); + } + } + }; } diff --git a/app/src/main/java/be/lepl1509group13/workoutwarrior/ProgramCreationActivity.java b/app/src/main/java/be/lepl1509group13/workoutwarrior/ProgramCreationActivity.java index 8a03ba01d0606e287a1aed30fd3696b28063b52c..6a3668f5d000d989d574ae8e92b6abd04a95ee57 100644 --- a/app/src/main/java/be/lepl1509group13/workoutwarrior/ProgramCreationActivity.java +++ b/app/src/main/java/be/lepl1509group13/workoutwarrior/ProgramCreationActivity.java @@ -279,17 +279,17 @@ public class ProgramCreationActivity extends AppCompatActivity { @Override public void onCancelled(@NonNull DatabaseError databaseError) { System.out.println(databaseError); - } - }); + } + }); - ImageView closeButton = dialog.findViewById(R.id.close_btn); - closeButton.setOnClickListener(w -> dialog.dismiss()); + ImageView closeButton = dialog.findViewById(R.id.close_btn); + closeButton.setOnClickListener(w -> dialog.dismiss()); - TextView name_exercice = dialog.findViewById(R.id.exercice_to_modify_name); - name_exercice.setText(exercise_searched); + TextView name_exercice = dialog.findViewById(R.id.exercice_to_modify_name); + name_exercice.setText(exercise_searched); - Button save_btn = dialog.findViewById(R.id.btn_save_modif); - save_btn.setOnClickListener(w -> { + Button save_btn = dialog.findViewById(R.id.btn_save_modif); + save_btn.setOnClickListener(w -> { if(nbre_day > 0){ EditText description_text = dialog.findViewById(R.id.exercise_description); String description_str = description_text.getText().toString(); @@ -300,15 +300,15 @@ public class ProgramCreationActivity extends AppCompatActivity { EditText timer_edit = dialog.findViewById(R.id.exercise_timer); String string_timer = timer_edit.getText().toString(); + String img = "@drawable/"+img_actual_exo; if(string_timer.equals("")){ - actual_exo = new Exercise(exercise_searched, description_str, break_time, "@drawable/developpe_couche_barre", 0); - System.out.println(actual_exo); + actual_exo = new Exercise(exercise_searched, description_str, break_time, img, 0); }else{ Integer timer_int = Integer.parseInt(string_timer); - actual_exo = new Exercise(exercise_searched, description_str, break_time, "@drawable/developpe_couche_barre", timer_int); - actual_exo = new Exercise(exercise_searched, description_str, break_time, "@drawable/developpe_couche_barre", 0); + actual_exo = new Exercise(exercise_searched, description_str, break_time, img, timer_int); + actual_exo = new Exercise(exercise_searched, description_str, break_time, img, 0); } - addToProgram(actual_exo, new ArrayList<>(current_days_checked), description_text.getText().toString(), break_time); + addToProgram(actual_exo, new ArrayList<>(current_days_checked)); dialog.dismiss(); // Effacer le champ de texte AutoCompleteTextView input_exo.setText(""); @@ -412,7 +412,7 @@ public class ProgramCreationActivity extends AppCompatActivity { }); } - public void addToProgram(Exercise exercise, ArrayList<String> trainingDays, String description_text, Integer break_time) { + public void addToProgram(Exercise exercise, ArrayList<String> trainingDays) { /** * Add the new exercise to the program */