diff --git a/app/src/main/java/edu/usta/cs3443/habitquest/model/Goal.java b/app/src/main/java/edu/usta/cs3443/habitquest/model/Goal.java index c88624b..6d4f2e3 100644 --- a/app/src/main/java/edu/usta/cs3443/habitquest/model/Goal.java +++ b/app/src/main/java/edu/usta/cs3443/habitquest/model/Goal.java @@ -83,12 +83,10 @@ public Goal(String goalName, String goalType, String goalDescription, String goa public void setGoalEnd(String goalEnd) { this.goalEnd = goalEnd; } public void setGoalCompleted(Boolean goalCompleted) { this.goalCompleted = goalCompleted; } - public String toCSVString() { return goalName + "," + goalType + "," + goalDescription + "," + goalStart + "," + goalEnd + "," + goalCompleted; } - // Check if the goal is expired public boolean isExpired(Date today) { SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); @@ -189,6 +187,7 @@ public static String readFile(String filename, Context context) throws IOExcepti return content.toString(); } + public static void deleteGoalFromCSV(Goal goalToDelete, Context context) throws IOException { String filename = "goals.csv"; File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), filename); @@ -228,8 +227,52 @@ public static void deleteGoalFromCSV(Goal goalToDelete, Context context) throws } } } - //change goal to completed, this would rewrite the goals.csv file with chage in goalCompleted + + public static void updateGoalInCSV(Goal updatedGoal, Context context) throws IOException { + // New method to update a goal in the CSV file + String filename = "goals.csv"; + File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), filename); + + if (!file.exists()) { + return; + } + + List lines = new ArrayList<>(); + try (Scanner scanner = new Scanner(file)) { + while (scanner.hasNextLine()) { + lines.add(scanner.nextLine()); + } + } + + try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { + for (String line : lines) { + String[] columns = line.split(","); + if (columns.length == 6) { + String goalName = columns[0].trim(); + String goalType = columns[1].trim(); + String goalDescription = columns[2].trim(); + String goalStart = columns[3].trim(); + String goalEnd = columns[4].trim(); + Boolean goalCompleted = Boolean.parseBoolean(columns[5].trim()); + + if (updatedGoal.getGoalName().equals(goalName) && + updatedGoal.getGoalType().equals(goalType) && + updatedGoal.getGoalDescription().equals(goalDescription) && + updatedGoal.getGoalStart().equals(goalStart) && + updatedGoal.getGoalEnd().equals(goalEnd)) { + writer.write(updatedGoal.toCSVString()); + } else { + writer.write(line); + } + writer.newLine(); + } + } + } + } + + // Change goal to completed, this would rewrite the goals.csv file with change in goalCompleted public void markGoalCompleted(Goal goalToComplete, Context context) throws IOException { + // Method to mark a goal as completed and update the CSV file List goals = loadGoalsFromCSV(context); // Find the goal to update @@ -248,8 +291,6 @@ private void writeGoalsToCSV(List goals, Context context) throws IOExcepti String filename = "goals.csv"; File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), filename); - - try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { // Write CSV header writer.write("goalName,goalType,goalDescription,goalStart,goalEnd,goalCompleted\n"); @@ -265,8 +306,4 @@ private void writeGoalsToCSV(List goals, Context context) throws IOExcepti } } } - - - - -} \ No newline at end of file +} diff --git a/app/src/main/java/edu/usta/cs3443/habitquest/model/GoalAdapter.java b/app/src/main/java/edu/usta/cs3443/habitquest/model/GoalAdapter.java index 494ff51..398cf31 100644 --- a/app/src/main/java/edu/usta/cs3443/habitquest/model/GoalAdapter.java +++ b/app/src/main/java/edu/usta/cs3443/habitquest/model/GoalAdapter.java @@ -18,9 +18,11 @@ public class GoalAdapter extends RecyclerView.Adapter { private List goals; + private Context context; - public GoalAdapter(List goals) { + public GoalAdapter(List goals, Context context) { this.goals = goals; + this.context = context; // Added context to the constructor to be able to save the state to CSV. } @NonNull @@ -47,14 +49,12 @@ public void onBindViewHolder(@NonNull GoalViewHolder holder, int position) { // Set a listener to handle changes in the CheckBox holder.goalCompleted.setOnCheckedChangeListener((buttonView, isChecked) -> { goal.setGoalCompleted(isChecked); - // Notify the model or database of the change - // You might need to update the data source or notify the database here - //this code is malformed and needs to be fixed, it duplicates the all the goals in the list - /*try { - goal.markGoalCompleted(goal, holder.itemView.getContext()); + // Save the state to the CSV file + try { + Goal.updateGoalInCSV(goal, context); // Added call to update CSV when checkbox is changed } catch (IOException e) { - throw new RuntimeException(e); - }*/ + e.printStackTrace(); + } }); holder.deleteGoal.setOnClickListener(v -> { @@ -79,8 +79,6 @@ private void deleteGoal(Goal goal, Context context, int position) { } } - - @Override public int getItemCount() { return goals.size(); @@ -106,4 +104,4 @@ public GoalViewHolder(@NonNull View itemView) { deleteGoal = itemView.findViewById(R.id.deleteGoal); } } -} \ No newline at end of file +} diff --git a/app/src/main/java/edu/usta/cs3443/habitquest/set_Goal_Activity.java b/app/src/main/java/edu/usta/cs3443/habitquest/set_Goal_Activity.java index 54994a2..adefdfd 100644 --- a/app/src/main/java/edu/usta/cs3443/habitquest/set_Goal_Activity.java +++ b/app/src/main/java/edu/usta/cs3443/habitquest/set_Goal_Activity.java @@ -67,13 +67,13 @@ protected void onCreate(Bundle savedInstanceState) { label7.setVisibility(View.GONE); // Find all CheckBox views by their IDs - CheckBox checkbox1 = findViewById(R.id.checkbox1); - CheckBox checkbox2 = findViewById(R.id.checkbox2); - CheckBox checkbox3 = findViewById(R.id.checkbox3); - CheckBox checkbox4 = findViewById(R.id.checkbox4); - CheckBox checkbox5 = findViewById(R.id.checkbox5); - CheckBox checkbox6 = findViewById(R.id.checkbox6); - CheckBox checkbox7 = findViewById(R.id.checkbox7); + checkbox1 = findViewById(R.id.checkbox1); + checkbox2 = findViewById(R.id.checkbox2); + checkbox3 = findViewById(R.id.checkbox3); + checkbox4 = findViewById(R.id.checkbox4); + checkbox5 = findViewById(R.id.checkbox5); + checkbox6 = findViewById(R.id.checkbox6); + checkbox7 = findViewById(R.id.checkbox7); // Set visibility to GONE or INVISIBLE checkbox1.setVisibility(View.GONE); @@ -84,14 +84,6 @@ protected void onCreate(Bundle savedInstanceState) { checkbox6.setVisibility(View.GONE); checkbox7.setVisibility(View.GONE); - checkbox1 = findViewById(R.id.checkbox1); - checkbox2 = findViewById(R.id.checkbox2); - checkbox3 = findViewById(R.id.checkbox3); - checkbox4 = findViewById(R.id.checkbox4); - checkbox5 = findViewById(R.id.checkbox5); - checkbox6 = findViewById(R.id.checkbox6); - checkbox7 = findViewById(R.id.checkbox7); - ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.goal_type_array, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);